Interacting with Compound.finance
Compound is one of the oldest protocols out there, it allows you to lend and borrow just like everybody else, but how can you do that with typescript, ethers.js and Angular ?
That is what I am going to show you today with some code examples.
in this occasion we are going to start up a new dapp using Angular to demonstrate how to do it.
First let's explain some of the fundamental concepts of Compound.
cTokens
Each asset supported by the Compound Protocol is integrated through a cToken contract, which is an EIP-20 compliant representation of balances supplied to the protocol. By minting cTokens, users (1) earn interest through the cToken's exchange rate, which increases in value relative to the underlying asset, and (2) gain the ability to use cTokens as collateral.
cTokens are the primary means of interacting with the Compound Protocol; when a user mints, redeems, borrows, repays a borrow, liquidates a borrow, or transfers cTokens, she will do so using the cToken contract.
Comptroller
The Comptroller is the risk management layer of the Compound protocol; it determines how much collateral a user is required to maintain, and whether (and by how much) a user can be liquidated. Each time a user interacts with a cToken, the Comptroller is asked to approve or deny the transaction.
So let's see some code.
We are going to connect our metamask wallet to our page so that we are able to see our cDai balance.
Then we are going to deposit DAI to the cDAI contract in the kovan network because we do not want to use real money.
andFinally we are going to listen to the Mint event that the cDAI contract raises when DAI is deposited.
First of all we have to request permission to the provider that Metamask injects in the global object.
Once we are connected we are going to retrieve the current cDAI balance:
In order to deposit we need to get a handler of a signer,
and finally here we can listen to the Mint event raised by the cDai contract.
Good! you have reached the end of the article, if you want, you can take a look at the whole code here:https://github.com/javieracrich/dapp