How to Supply Assets to DeFi Protocols Using JavaScript
Build your app to interact with Compound Finance

DeFi has become an extremely popular use case of the Ethereum ecosystem. At the time of writing, there is over $1 billion locked in DeFi protocols. The prospect of replacing existing financial products with open, decentralised alternatives has so far proven very popular.
What makes most of these DeFi platforms great is their ease of use for developers. Due to their open-source nature, they are understandable and can be utilized by anyone willing to learn how.
One of the biggest is Compound Finance, with around $115 million in locked value. Anyone can supply assets to their protocol and start earning interest on it, but how does this process work?
How Does Compound Finance Work?
Compound currently functions in nine markets: BAT, DAI, ETH, REP, SAI, USDC, USDT, WBTC, ZRX. These are the digital assets that you can earn interest on.
Let’s use DAI as an example. Once you supply DAI to the protocol, that value is locked in until you decide to withdraw it. While it’s there, though, you’re agreeing that the protocol can lend that value out, thus earning the whole pot interest. Think of it as a savings account.
Once it’s in there, you can check how much DAI there is at any time, what the current interest rate is, borrow other assets, etc.
Let’s get technical
What does this supply process look like from a technical standpoint?
The protocol uses ERC20-compliant contracts for each of the supported markets. They are called cTokens. They each have an external function: mint, where the supplied value is sent. This function receives the supplied value, calculates the equivalent number of cTokens depending on the exchange rate, and exchanges them for the supplied value.
For example, if the exchange rate is 1:1, then supplying one ETH will result in the sender receiving one cETH.
The opposite of mint is a function called redeem. When a user wants to withdraw their locked-up value, calling redeem with the desired number of cETH tokens will return the equivalent value of ETH depending on the current exchange rate.
Note: There is also a redeemUnderlying function that uses the underlying asset units instead of the cToken units as input.
Writing the Code
Prerequisite: This walkthrough assumes a basic understanding of JavaScript, Web3, and Ethereum smart contracts.
We’re going to supply and redeem ETH to Compound Finance following these steps:
- Load the cETH contract.
- Supply ETH by calling
mint, locking the ETH, and receiving cETH tokens. - Read our cETH balance.
- Read our locked-up ETH balance.
- Redeem our ETH from the protocol by supplying cETH.
Step 1: Loading the cETH contract
Each token contract has a different address on each network. The ABIs for cToken contracts are publically available at https://compound.finance/docs/abi/
In our scenario, using cETH and the Kovan testnet, our URL is https://compound.finance/docs/abi/kovan/cETH.
Addresses for each contract on each network can be found in Compound’s official documentation.
Using web3, we initialize our contract instance like this:
const cEthInstance = new web3.eth.Contract(cEthABI, addr);Step 2: Supplying ETH
Figure 1 shows how easy it is to mint cTokens using web3’s send function:
