February 4, 2026

Build a Decentralized Lending App With Compound – ICOVO

Build a Decentralized Lending App With Compound – ICOVO

Click on the Write button and send a transaction.

You should be able to see that 1Sai is subtracted and some cSai is added from/to your account.

There are two ways to withdraw your Sai. You can specify the number of Sai or the number of cSai to exchange with Sai.

The redeemUnderlying function takes the amount of Sai as an argument. The redeem function takes the amount of cSai. We will use the redeemUnderlying function here for simplicity:

Click on the Write button and send a transaction.

You can see that you returned cSai and received Sai instead.

You should also be able to see that there is still a tiny amount of cSai in your account. That’s the interest you accumulated.

All right, let’s get started with our application. Again, we will build a simple JavaScript application that lends and withdraws Sai. You can see this repository for the finished code.

Obtain Your Private Key and Address
First, obtain your private key and address in vanity-eth. Click on the “Generate” button at the bottom. Keep them somewhere for now. Please do NOT use them other than testing purposes. It’s not safe.

Get Some Eth and Sai
Send some Eth and Sai to the address you just obtained. 0.01 Eth should be enough to pay for the gas. I will supply only 1 Sai in this post.

*We will use mainnet cSai as, unfortunately, it is difficult to get testnet Sai tokens.

Get the Infura Endpoint
We will use Infura as a provider to communicate with the Ethereum network. Make an Infura account and get the endpoint at Mainnet by making a new project. It should look something like this:

https://mainnet.infura.io/v3/[PROJECT_ID]

Start a New npm Project

mkdir eht-compound-demo
cd eht-compound-demo/
npm init --yes

*If you don’t have npm and nodejs, install them from here.

Initialize Git

git init

Install the web3 Package

npm i — save web3@1.2.1

web3 is the Ethereum JavaScript API. It is an interface to Ethereum networks. It communicates with an Ethereum node or transacts with smart contracts deployed on the blockchain from your JavaScript application.

Install the ethereumjs-tx Package

npm i --save ethereumjs-tx@1.3.7

We use ethereumjs-tx to create and sign transactions.

Add Scripts to package.json

“scripts”: ,

mjs is a file extension that allows you to write with ES6 syntax.

Make saiTokenContract.mjs file and cSaiContract.mjs file
You can just copy the files from here and here.

In these files, I am simply declaring and exporting the abi and the address of sai token contract and the cSai contract.

Now we will start writing code. Let’s start from a lending feature.

We will make two separate files:

1. approveSaiMain.mjs

Make a New File
Make a new file approveSaiMain.mjs.

Import Packages

import Web3 from “web3”;
import EthTx from “ethereumjs-tx”;

Import cSaiContract and saiTokenContract

import cSaiContract from ‘./cSaiContract’;
import saiTokenContract from ‘./saiTokenContract’;

Set Up Web3
Set up Web3 to use Infura as your web3 provider:

const web3 = new Web3(
new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/[YOUR_PROJECT_ID]"
)
);

Declare Your Address and Private Key Variables

const addressFrom = “[YOUR_ADDRESS]”;
const privKey = “[YOUR_PRIVATE_KEY]”;

Instantiate the Sai Token Contract
You use the Contract method of web3 with the address and abi to instantiate the Sai token contract.

const saiTokenContractInstance = new web3.eth.Contract(
JSON.parse(saiTokenContract.saiTokenContractAbi),
saiTokenContract.saiTokenContractAddress
);

Now we are ready to call the sai token contract methods.

Declare Const Variables to Pass to the approve Function

const ADDRESS_SPENDER = cSaiContract.cSaiContractAddress;
const TOKENS = web3.utils.toHex(-1);

You can pass 1 * 10 ** 18 to the second argument of the approve function, but in practice, it’s a common convention to pass -1 so that your users never have to approve again. If you want to understand how this works, read this.

Encode the Abi for the approve Function

const approveEncodedABI = saiTokenContractInstance.methods
.approve(ADDRESS_SPENDER, TOKENS)
.encodeABI();

Declare the sendSignedTx Function
Make a function to sign a transaction object and then propagate that to an Ethereum network:

function sendSignedTx(transactionObject, cb) `, cb);
}

Construct a Transaction Object and Then Execute sendSignedTx

web3.eth.getTransactionCount(addressFrom).then(transactionNonce => ;sendSignedTx(transactionObject, function(error, result))
}
);

Set the gasLimit to 100000. You can change gasPrice if you want to pay less. Check out the current optimal gasPrice in EthGasStation. You can convert its gwei value to wei in Ethereum unit converter.

Run the File

npm run approve-sai-main

Once you get a transaction hash like this 0xa18d71767ba95b95831fbf3a441494165002bc5f4104afdb0406511ebce7cbe3, you can go to Etherscan to see the detail of your transaction. Below is my transaction as an example. If your transaction doesn’t go through, increase your gasPrice and send another transaction.

You can see that I called the approve function in “Input Data”.

2. supplySaiMain.mjs

I make another file, supplySaiMain.mjs, and call the mint function this time. I am going to skip the detail as most of the code is going to be the same. You can see the finished code in the repo.

Encode the abi of the mint function with the amount of Sai you want to mint.

const MINT_AMOUNT = web3.utils.toHex(1 * 10 ** 18);const mintEncodedABI = cSaiContractInstance.methods
.mint(MINT_AMOUNT)
.encodeABI();

Pass this ABI to a data property in a transaction object. Also, set the gasLimit to 200000 this time:

web3.eth.getTransactionCount(addressFrom).then(transactionNonce => ;sendSignedTx(transactionObject, function(error, result))
}
);

Run the file:

npm run supply-sai-main

You can see that 1Sai is transferred from your account. You can also see that you get some cSai in exchange.

Lastly, we are going to build a feature to withdraw Sai. Again, I am going to skip the detail as most of the code is going to be the same. You can see the finished code here. I make another file, withdrawSaiMain.mjs, and call the redeemUnderlying function this time.

Encode the abi of the redeemUnderlying function with the amount of Sai you want to withdraw.

const REDEEM_AMOUNT = web3.utils.toHex(1 * 10 ** 18);const redeemUnderlyingEncodedABI = cSaiContractInstance.methods
.redeemUnderlying(REDEEM_AMOUNT)
.encodeABI();

Pass this ABI to a data property in a transaction object. Also, set the gasLimit to 400000 this time:

web3.eth.getTransactionCount(addressFrom).then(transactionNonce => ;sendSignedTx(transactionObject, function(error, result))
}
);

Run the file:

npm run withdraw-sai-main

You can see that you get your 1Sai back and sent some cSai back to the cSai contract.

When you see your cSai balance, you still have a tiny amount. That’s the interest you accumulated.

We learned that the Compound is a decentralized lending protocol on the Ethereum blockchain that allows everybody in the world to lend or borrow crypto assets with no permission. Since it eliminates the coordination cost and the negotiation cost, it’s much easier and faster to use in comparison to other p2p crypto lending systems that have existed.

We also learned that lending and withdrawing in Compound is as simple as calling three methods in smart contracts. You can integrate this simple app into your dapp or wallet to accelerate the open finance revolution.

I hope now we share the same excitement of the crypto sharing economy.

Published at Sun, 01 Dec 2019 13:48:55 +0000

{flickr|100|campaign}

Previous Article

US Lawmakers Want to Brand Libra a Security, Association Disagrees

Next Article

US Lawmakers Want to Brand Libra a Security, Association Disagrees

You might be interested in …