How to Use Javascript to supply The DeFi protocol Compound Assets?
Originally written by Kyle& NBSP;
Introduction: This article is for developers with a basic understanding of JavaScript, Web3, and Ethereum smart contracts.
DeFi has become a very popular use case for the Ethereum ecosystem. At the time of this writing, the DeFi protocol is locked up in excess of $1 billion. So far, the prospect of replacing existing financial products with open, decentralized alternatives has been very popular.

One of the reasons these DeFi platforms are so good is because they are easy for developers to use. Because of their open source nature, developers can better understand them and anyone willing to learn how to use them can use them.
One of the biggest players in the DeFi space is Compound Finance, currently valued at about $115 million in ecologically locked value. Everyone can provide assets for their agreements and earn interest on them, but how?
& have spent
What about the Compound? & have spent
Compound currently supports 9 tokens: BAT, DAI, ETH, REP, SAI, USDC, USDT, WBTC, ZRX. These are digital assets that you can earn interest on.
Let's take DAI as an example. When you provide DAI to the protocol, the DAI is locked until you decide to take it back. When DAI is locked down, you need to agree to an agreement that these values can be lent out, earning interest on the entire liquidity pool. You can think of it as a savings account.
Once deposited, you can always see how many DAI are locked in the pool, what the current interest rate is, borrow other assets, and so on.
& have spent
Technology realization & NBSP;
What does this asset supply process look like from a technical perspective?
This protocol USES an ERC20 compatibility contract for each supported market. They're called Ctokens. Each of them has an external function: Mint, which is the sending destination of the value of the supply. This function is used to receive the value of the supply, calculate the equivalent number of Ctokens based on the exchange rate, and exchange them for the value of the supply.
For example, if the exchange rate is 1:1, one ETH is supplied and the sender receives a cETH.
The inverse function of MINT is the 'cursor' function. When the user wants to extract the locked value, the required number of cETH tokens is redeemed and the equivalent ETH is returned, based on the current exchange rate.
Note: There's also a redeemstable function that USES the base asset units instead of the cToken units as input.
& have spent
Write code & NBSP;
Prerequisites: This walkthrough assumes a basic understanding of JavaScript, Web3, and ethereum intelligent contracts.
We will provide and convert ETH to Compound Finance as follows:
Load the cETH contract.
Provide the ETH by calling Mint, locking it and receiving the cETH token.
Read our cETH balance.
Read the ETH balance that we locked.
Redeem our ETH from the protocol by providing cETH.
Step 1: Load the cETH contract
Each token contract has a different address on each network. The ABI for the cToken contract can be derived from & NBSP;https://compound.finance/docs/abi/
In our scenario, using cETH and Kovan to test the network, our URL is NBSP;https://compound.finance/docs/abi/kovan/cETH.
You'll find the address of each contract on each network in the Compound's official documentation.
Using Web3, we initialize the contract instance like this:
Const cEthInstance = new web3.eth.Contract(cEthABI, addr);& have spent
Step 2: Provide ETH
& have spent
Figure 1 shows how easy it is to create a cToken using Web3's send function:

Figure 1: Cast cToken
At this stage, our ETH will start earning interest based on the current interest rate (which can also be calculated using a mathematical formula).
& have spent
Step 3: Read our cETH balance
& have spent
Because the cToken contracts are compatible with ERC20, they provide a function called balanceOf. We can check our cETH balance by calling and passing it our account address:
Let b = await cEthInstance. The methods. BalanceOf (account). The call ();& have spent
Step 4: Read the locked ETH balance
& have spent
The cToken contract also provides a function called Balanceofstable, which refunds the underlying asset value of the account with the cToken it owns and the current exchange rate:
Let u = awaitCEthInstance. The methods. BalanceOfUnderlying (account). The call ();
Step 5: Redeem our ETH from the agreement
Using the value u from step 4 as redeemAmount in Figure 2, we can call redeemAmount to redeem our ETH:

Figure 2: Redemption process
Alternatively, we can use 'b' instead of redeemAmount and use the value in step 3 as redeemAmount.
& have spent
Conclusion & have spent
As DeFi becomes easier to use and adopted by many, learning how to interact with these protocols is a valuable tool in any developer's toolkit.
If you are interested in blockchain development, I will write tutorials, walkthroughs, tips and tricks on getting started and building a portfolio of products. Check out the following blockchain development resources:https://medium.com
Text link:https://www.8btc.com
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia