Understanding the Different Types of Smart Contracts in AAVE Protocol
As a decentralized finance (DeFi) developer, it’s important to understand the different types of smart contracts that make up the AAVE protocol. In this article, we’ll provide a detailed overview of the different types of smart contracts in AAVE and how they work. We’ll also provide examples of how to interact with these contracts using the aave-js library.
The AAVE protocol is built on Ethereum and consists of a series of smart contracts that enable lending and borrowing functionality. These smart contracts can be grouped into three main categories: the lending pool contracts, the price oracle contracts, and the market contracts.
Lending Pool Contracts
The lending pool contracts are the core of the AAVE protocol and are responsible for managing the lending and borrowing of assets. Each asset has its own lending pool contract, which tracks the supply and demand for that asset and determines the interest rates.
To interact with the lending pool contracts using the aave-js library, you can use the getLendingPool()
method. This method takes an asset symbol or contract address as an argument and returns an instance of the LendingPool contract for that asset. You can then use the methods of the LendingPool contract to interact with the lending pool.
For example, to get the total supply of an asset in the lending pool, you can use the totalSupply()
method:
const lendingPool = await aave.getLendingPool('DAI');
// get the DAI lending pool contract
const totalSupply = await lendingPool.totalSupply();
// get the total supply of DAI in the lending pool
To get the current interest rate for an asset in the lending pool, you can use the getCurrentVariableInterestRate()
method:
const lendingPool = await aave.getLendingPool('ETH');
// get the ETH lending pool contract
const interestRate = await lendingPool.getCurrentVariableInterestRate();
// get the current interest rate for ETH
Price Oracle Contracts
The price oracle contracts are responsible for providing the current prices of assets to the lending pool contracts. These prices are used to determine the value of collateral and the liquidation threshold for loans.
To interact with the price oracle contracts using the aave-js library, you can use the getPriceOracle()
method. This method takes an asset symbol or contract address as an argument and returns an instance of the PriceOracle contract for that asset. You can then use the methods of the PriceOracle contract to interact with the price oracle.
For example, to get the current price of an asset in the price oracle, you can use the getCurrentPrice()
method:
const priceOracle = await aave.getPriceOracle('DAI');
// get the DAI price oracle contract
const currentPrice = await priceOracle.getCurrentPrice();
// get the current price of DAI
To get the minimum collateralization ratio for an asset in the price oracle, you can use the getMinimumCollateralizationRatio()
method:
const priceOracle = await aave.getPriceOracle('ETH');
// get the ETH price oracle contract
const minCollateralizationRatio = await priceOracle.getMinimumCollateralizationRatio();
// get the minimum collateralization ratio for ETH
Market Contracts
The market contracts are responsible for managing the sale and purchase of assets in the AAVE protocol. These contracts allow users to buy and sell assets using the AAVE token as a medium of exchange.
To interact with the market contracts using the aave-js library, you can use the getMarket()
method. This method takes an asset symbol or contract address as an argument and returns an instance of the Market contract for that asset. You can then use the methods of the Market contract to interact with the market.
For example, to buy an asset in the market using the aave-js library, you can use the buy()
method. This method takes two arguments: the asset to buy (in the form of a symbol or contract address), and the amount of AAVE to spend (in wei). For example:
const market = await aave.getMarket('DAI');
// get the DAI market contract
await market.buy('DAI', '100000000');
// buy 100 DAI using AAVE
To sell an asset in the market using the aave-js library, you can use the sell()
method. This method takes the same arguments as the buy()
method. For example:
const market = await aave.getMarket('ETH');
// get the ETH market contract
await market.sell('ETH', '100000000');
// sell 100 ETH for AAVE
Conclusion
In this article, we’ve explored the different types of smart contracts in the AAVE protocol and provided examples of how to interact with them using the aave-js library. Understanding these smart contracts is key to building DeFi applications on top of the AAVE protocol.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
How do you get the total supply of an asset in the lending pool using the aave-js library?
To get the total supply of an asset in the lending pool using the aave-js library, use the getLendingPool()
method to get the LendingPool contract for the asset, and then use the totalSupply()
method of the LendingPool contract. For example:
const lendingPool = await aave.getLendingPool('DAI');
// get the DAI lending pool contract
const totalSupply = await lendingPool.totalSupply();
// get the total supply of DAI in the lending pool
How do you get the current interest rate for an asset in the lending pool using the aave-js library?
To get the current interest rate for an asset in the lending pool using the aave-js library, use the getLendingPool()
method to get the LendingPool contract for the asset, and then use the getCurrentVariableInterestRate()
method of the LendingPool contract. For example:
const lendingPool = await aave.getLendingPool('ETH');
// get the ETH lending pool contract
const interestRate = await lendingPool.getCurrentVariableInterestRate();
// get the current interest rate for ETH
How do you get the current price of an asset in the price oracle using the aave-js library?
To get the current price of an asset in the price oracle using the aave-js library, use the getPriceOracle()
method to get the PriceOracle contract for the asset, and then use the getCurrentPrice()
method of the PriceOracle contract. For example:
const priceOracle = await aave.getPriceOracle('DAI');
// get the DAI price oracle contract
const currentPrice = await priceOracle.getCurrentPrice();
// get the current price of DAI
How do you get the minimum collateralization ratio for an asset in the price oracle using the aave-js library?
To get the minimum collateralization ratio for an asset in the price oracle using the aave-js library, use the getPriceOracle()
method to get the PriceOracle contract for the asset, and then use the getMinimumCollateralizationRatio()
method of the PriceOracle contract. For example:
const priceOracle = await aave.getPriceOracle('ETH');
// get the ETH price oracle contract
const minCollateralizationRatio = await priceOracle.getMinimumCollateralizationRatio();
// get the minimum collateralization ratio for ETH
How do you sell an asset in the market using the aave-js library?
To sell an asset in the market using the aave-js library, use the getMarket()
method to get the Market contract for the asset, and then use the sell()
method of the Market contract. This method takes two arguments: the asset to sell (in the form of a symbol or contract address), and the amount of AAVE to receive (in wei). For example:
const market = await aave.getMarket('ETH');
// get the ETH market contract
await market.sell('ETH', '100000000');
// sell 100 ETH for AAVE
Note that you will need to have the asset you want to sell in your wallet and have approved the Market contract to transfer it. You can do this using the approve()
method of the ERC20 contract for the asset. For example:
const erc20 = new web3.eth.Contract(ERC20_ABI, '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE');
// get the ERC20 contract for the asset
await erc20.methods.approve('0xAAveMarketAddress', '100000000').send({ from: '0xMyWalletAddress' });
// approve the Market contract to transfer 100 of the asset