DEV Community

Javier Acrich
Javier Acrich

Posted on

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.

  1. We are going to connect our metamask wallet to our page so that we are able to see our cDai balance.

  2. Then we are going to deposit DAI to the cDAI contract in the kovan network because we do not want to use real money.
    and

  3. Finally 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.

Image description

Once we are connected we are going to retrieve the current cDAI balance:

Image description

In order to deposit we need to get a handler of a signer,

Image description

and finally here we can listen to the Mint event raised by the cDai contract.

Image description

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

Top comments (0)