
2025-04-04
|
45 mins to read
Building a Pool-Based Lending Protocol on Agoric: A Guide to DeFi Application Development
What is Agoric?
- Hardened JavaScript: Enhanced security for smart contract development.
- Eventual Send with E(): Asynchronous message passing for robust applications.
- Far(), Remotable, and Marshaling: Secure object communication.
- Notifiers and Subscriptions: Reactive programming for real-time updates.
The Evolution of Lending and Borrowing
The Role of Compound Finance in Pool-Based Lending
How Does Compound Finance Work?
In Compound Finance, users can either supply liquidity to pools or borrow assets.
- Earning Interest: Interest accrues per block, increasing over time.
- COMP Governance Tokens: Users earn COMP tokens, which can be traded or used for governance voting.
Who are you?
What’s your income?
Have you repaid previous debts?
There are two main types of collateralisation:
- Under-collateralisation: The collateral value is less than the loan amount. For example, with a 30% collateralisation factor, you’d need to provide £30 to borrow £100. This is a more traditional approach.
- Over-collateralisation: The collateral must exceed the loan value. For instance, with £100 collateral and an 80% collateralisation factor, you can borrow £80. This ensures the lender is covered even if the borrower defaults.
Liquidation occurs when the collateral is sold to cover the debt if the borrower defaults. If the borrower’s debt exceeds the collateral’s value, liquidation is triggered to secure the lender’s funds.
- Automated Market Maker (AMM): Sell collateral via an automated market system.
- Manual Sale: Sell at a slightly lower price to attract buyers.
While Compound uses the second method for decentralisation, our protocol will use the first, as Agoric has its own native AMM.
Compound Finance adjusts rates dynamically based on market conditions to ensure liquidity and balance. Rates include:
- Utilisation Ratio
- Borrow Rate
- Exchange Rate
These rates help balance supply and demand, ensuring the market remains liquid.
You can take a look at the markets listed in the Compound protocol if you wish to see how these variables are used in real life. For more information, check the Compound Whitepaper. For the implementation, see the Compound Protocol Repository on GitHub.
Implementing a Pool-Based Lending Protocol on Agoric
With this context, let us explore our adaptation of a pool-based lending protocol on Agoric. Before proceeding, ensure familiarity with the following prerequisites:
- ERTP: Agoric's framework for digital assets or tokens.
- Zoe: A smart contract development framework that safeguards users' assets from bugs and malicious code.
- Agoric Offers: The basic logic used when interacting with contracts.
- Agoric Native AMM: An Automated Market Maker for trading various tokens, including those imported via IBC.
- PriceAuthority: An interface between Zoe and price oracles.
Overview of the Protocol
The complete code for this protocol is available in this repository. Users can perform five main operations within our protocol:
Deposit Money
Borrow Money
Redeem Deposited Money
Adjust Loan
Close Loan
Functionality for these operations is distributed among multiple components. An high level overview of protocol components can be found below.

Key Modules of the Protocol
- LendingPool: Entry point directing users to the correct pools after necessary checks. See the code.
- PoolManager: Manages pools for specific assets (e.g., VAN Pool for VAN token), storing underlying assets, minting protocol tokens (e.g., AgVAN), and handling deposits, borrowing, and redemptions. See the code.
- PriceManager: Central hub for managing multiple price authorities used by the protocol. See the code.
- ProtocolMath: Implements key financial formulas, including exchange rate, utilisation ratio, and borrow rate. See the code.
- DebtsPerCollateral: Groups loans with the same collateral and debt type, ensuring consistent responses to market changes, such as liquidation triggers. See the code.
- Loan: Core module for loan management, enabling balance adjustments, loan closures, and dapp notifications. See the code.
- LiquidationObserver: Monitors price changes and initiates liquidation if loans become undercollateralised. See the code.
A simple but powerful example of an interaction with our protocol can be the scenario where a user deposits money into a pool. The diagram below can help you understand what an end-to-end interaction would look like.

Setup And Deploy Your Environment
Ensure you have the following prerequisites to build and use the agoric-sdk:
Linux shell or equivalent
Node.js (LTS version >= 14.x.x)
Yarn
Git
1. Install agoric-sdk
git clone https://github.com/Agoric/agoric-sdk.git
cd agoric-sdk
git checkout beta
git pull
yarn install
yarn link-cli ~/bin/agoric
agoric --version
Our protocol is built on the agoric-sdk beta branch so the version should be 0.15.0
2. Startup The Dapp
git clone https://github.com/anilhelvaci/dapp-pool-lending-protocol.git
cd dapp-pool-lending-protocol
git checkout blog/bytepitch
git pull
In Agoric there are two types of local test chains; a sim-chain and a local-chain. A sim-chain, simulated chain, is where we can test our dapps and contracts but there's no transaction cost. So it's simulated, not real. On the other hand there's a local-chain which is based on the cosmos-sdk, therefore it's based on GoLang. Starting a local-chain requires building some go packages. If you plan to test with multiple clients interacting with the same chain, local-chain is the way to go. Check for further information.
cd dapp-pool-lending-protocol
agoric start --verbose --reset
For Agoric CLI reference, check .
cd dapp-pool-lending-protocol
agoric deploy contract/deploy/deploy.js api/deploy.js
Deployment Details:
The agoric deploy command accepts multiple scripts:
- contract/deploy/deploy.js: Sets up the environment (AMM, price authorities, etc.) and starts the LendingPool contract.
- api/deploy.js: Requests test tokens and adds liquidity to the pools, bootstrapping the protocol.
agoric open --repl
cd dapp-pool-lending-protocol/ui
yarn start
Interact With The Dapp: A Step-by-Step Guide
To interact with the dapp, approve it in your wallet and visit localhost:3000. You should be able to see something like this below.

- Markets: Displays general market data and enables you to Supply or Borrow money from a selected pool.
- Activity: Shows the user’s activity such as deposited assets and borrows. Also enables the user Redeem, Close or Adjust Loan.
- APY: Annual Percentage Yield represents the interest paid on debt and earned on supplied assets. In our protocol, the Supply and Borrow APYs are the same since we don’t use a ReserveFactor.
- MMR: Minimum Margin Requirement is the ratio between collateral and debt value. For example, with $150 collateral, you can borrow up to $100.

There, you’ll find a list of supplied and borrowed assets for each pool.
The protocol is pre-bootstrapped with liquidity, so the deposits section isn’t empty.
Depositing funds into a pool.
Borrowing from another pool using protocol tokens as collateral.
Adjusting the loan by repaying part of the debt.
Closing the loan by repaying in full and reclaiming collateral.
Redeeming the initial deposit.
1. Deposit Money
You might notice that the numbers you see in supply screenshots is inconsistent with rest of the screenshots, that's because I took them out of sync from the rest of the sequence.
Click on VAN market and below dialog should open:

Here, enter the amount you wish to supply and the protocol token you'll receive automatically be calculated, you can also specify slippage rate to protect your transaction from the sudden price changes of the market.
Then go to your wallet and approve the offer below:


2. Borrow Money





Notice that the exchange rate did not change because there's no interest accrued yet. So let's accrue some interest.
From the below screenshot, you should notice that total borrows for both market and user profile is increased and also the exchange rate for VAN market is also increased.


3. Adjust Loan

Again, go to your wallet and approve the below transaction:



4. Close Loan

Go to your wallet approve the transaction below:



5. Redeem Your Money

Now, go to your wallet and approve the transaction:


Conclusion
In this article, we explored key concepts and practical implementations related to Agoric and pool-based lending protocols. We covered:
What Agoric is, how it works, and how to develop with it.
How to build a smart contract using Agoric.
The fundamentals of a pool-based lending protocol and its most well-known implementation, Compound Finance.
Our approach to implementing a pool-based lending protocol.
A step-by-step user journey, from depositing funds to repaying loans.
Real-world applications in blockchain, DeFi, DApp development, Web3, Ethereum, smart contracts, and blockchain protocols.
See other
Articles

2025-04-16
|
05 mins to read
What Makes BytePitch’s Product Team Unique in a Fast-Moving Market
At BytePitch, our Product Team adapts to every challenge across industries and technologies. Learn how our flexible mindset, strong client relationships, and people-first values drive innovation and deliver real results in a fast-evolving digital market.





