avatarAlex Roan

Summary

The web content provides a technical guide on how to supply assets to DeFi protocols, specifically Compound Finance, using JavaScript.

Abstract

The article "How to Supply Assets to DeFi Protocols Using JavaScript" delves into the process of interacting with Compound Finance, a prominent DeFi platform, through JavaScript. It explains the concept of DeFi and its growing popularity within the Ethereum ecosystem, noting that over $1 billion is currently locked in DeFi protocols. The article highlights Compound Finance's ease of use for developers and outlines the steps to supply and redeem assets like ETH. It details the technical aspects of using cTokens, which are ERC20-compliant contracts, to mint and redeem assets, and how to calculate interest earned. The guide is aimed at developers with a basic understanding of JavaScript, Web3, and Ethereum smart contracts, providing code snippets and references to Compound Finance's documentation and ABIs for practical implementation.

Opinions

  • The author believes that the open-source nature of DeFi platforms like Compound Finance makes them understandable and accessible to developers.
  • There is an emphasis on the ease of use for developers engaging with DeFi protocols due to their open-source nature and available documentation.
  • The article suggests that the prospect of replacing traditional financial products with decentralized alternatives is popular and promising.
  • The author implies that learning to interact with DeFi protocols is a valuable skill for developers, indicating the growing importance and adoption of DeFi.

How to Supply Assets to DeFi Protocols Using JavaScript

Build your app to interact with Compound Finance

Photo by Irvan Smith on Unsplash.

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:

  1. Load the cETH contract.
  2. Supply ETH by calling mint, locking the ETH, and receiving cETH tokens.
  3. Read our cETH balance.
  4. Read our locked-up ETH balance.
  5. 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:

At this stage, our ETH will start to earn interest depending on the current interest rate (which can also be calculated using a mathematical formula).

Step 3: Read our cETH balance

Since the cToken contracts are ERC20-compliant, they provide a function called balanceOf. We can check the balance of our cETH by calling this and passing our account address to it:

let b = await cEthInstance.methods.balanceOf(account).call();

Step 4: Read our locked-up ETH balance

The cToken contracts also provide a function called balanceOfUnderlying that returns the underlying asset value of an account using their owned cTokens and the current exchange rate:

let u = await cEthInstance.methods.balanceOfUnderlying(account).call();

Step 5: Redeem our ETH from the protocol

Using the value u from Step 4 as redeemAmount in Figure 2, we can call redeemUnderlying to redeem our Ether:

Alternatively, we can use redeem instead of redeemUnderlying and pass in the value b from Step 3 as the redeemAmount.

Conclusion

With DeFi becoming easier to use and adopted by so many, learning how to interact with these protocols is a valuable tool in any developer’s toolbox.

If you’re interested in blockchain development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio. Check out some of these resources:

Programming
Blockchain
Ethereum
JavaScript
Defi
Recommended from ReadMedium