May 27, 2026

Capitalizations Index – B ∞/21M

Building Applications on Compound Finance – QuikNode –

Building Applications on Compound Finance – QuikNode –

Build open financial tools using Compound and QuikNode

Open Finance is on the rise. Protocols are allowing people to lend and borrow tokens trustless via smart-contracts. In the last article, we talked about MakerDao and Dai JS (also fueling open and decentralized finance [#DeFi]). In this article, we will deep-dive into Compound Protocol and see how we can build Web3 applications on top of it.

What is Compound?

Compound Protocol enables trust-less borrowing and lending on the Ethereum Protocol.

“Compound is a money market protocol on the Ethereum blockchain — allowing individuals, institutions, and applications to frictionlessly earn interest on or borrow cryptographic assets without having to negotiate with a counterparty or peer”. [1]

How Compound Protocol Works?

The Compound Protocol (like any other protocol built on top of Ethereum) is a collection of Smart Contracts. Any individual can lock their Ether (also other assets) into Compound Protocol’s smart contract, this is what we call supply, and borrowers can borrow this supply at some interest rate. Let’s look at an example.

Let’s say Ether (ETH) price is $200 and DAI is $1. Now you want to borrow 100 DAI, so you put $150 worth of ETH as collateral and receive 100 DAI. You need to pay an annual interest rate on the borrowed 100 DAI. When you pay back 100 DAI, you will get your collateral back.

Note- Compound team has launched V2 where this 150% collateral is now calculated on every asset (it means there will be different collateral for borrowing different assets).

Now if you don’t want to borrow and instead have some extra ETH (also other tokens supported by the protocol) lying around, you can earn interest by providing your asset as Supply to the Compound Protocol. You will earn an annual interest based on the supply you provide.

Where does the interest come from?

When one borrows from Compound, she needs to pay interest according to the interest rate (at the time of borrowing). Of this interest rate, Compound takes 5–10% (aka. sponsor equity) and the rest is spread out among suppliers.

If you have more questions about, quickly read this FAQs.

Now enough of economics, let’s talk some code!

We will talk about Compound v2 which is still not on the main net, instead working on a testnet.

cTokens

Let’s first understand the cToken: these are simply ERC20 tokens for real assets on the Compound protocol. So if you want to use ZRX, Compound will wrap them into cToken and make it cZRX. There will be a separate contract for every asset. So to create borrowing and lending, we need these cTokens.

Lets deep dive into the code. In this tutorial, we will borrow DAI and put ETH as collateral. Before that, a few things to note here:

  • Full code is here.
  • We are using Rinkeby testnet.
  • I added my private keys and address so it will be easy for you to run and test this. (DO NOT POST YOUR PRIVATE KEY PUBLICLY EVER).
  • You need a Web3 URL for accessing Rinkeby network (can spin up a QuikNode and get it here).
  • This Documentation, which we’ll reference often throughout the article.

Now let’s understand what we’re actually doing here. We need to have a cToken contract for ETH and DAI, which are cETH and cDAI. We will also need another contract called Comptroller, which manages the risk associated with the Compound Protocol v2. It is responsible for determining when accounts are underwater and can be liquidated. It determines how much collateral can be seized in exchange for liquidating an underwater account. It also provides a defense mechanism to protect the protocol against unforeseen attacks on the market.

let cETH ="0xbed6d9490a7cd81ff0f06f29189160a9641a358f";
let CEther = new web3.eth.Contract(abi, cETH);
let cDAI = "0x2acc448d73e8d53076731fea2ef3fc38214d0a7d"
let cDaiInstance = new web3.eth.Contract(daiAbi, cDAI);
let comptroller = "0x8d2a2836d44d6735a2f783e6418caedb86da58d8";
const troll = new web3.eth.Contract(trollAbi, comptroller);

We need to mint some cETH so we can use them borrow on Compound. The Compound Protocol only recognizes these minted tokens.

CEther.methods.mint().send()
.on('transactionHash', (hash) => )
.on('confirmation', (confirmationNumber, receipt) => )
.on('receipt', (receipt) => )
.on('error', console.error);

Here we are minting cETH for 2 Ether. The mint function transfers an asset into the money market, which begins accumulating interest based on the current Supply Rate for the asset. The number of tokens minted is the amount of underlying assets being provided divided by the current Exchange Rate.

Now we need to enable markets for both ETH and DAI tokens using the Comptroller Contract.

troll.methods.enterMarkets([cETH, cDAI]).send().on('transactionHash', (hash) => )
.on('confirmation', (confirmationNumber, receipt) => )
.on('receipt', (receipt) => )
.on('error', console.error);

Now let’s borrow 5 DAI.

cDaiInstance.methods.borrow(5).send().on('transactionHash', (hash) => )
.on('confirmation', (confirmationNumber, receipt) => )
.on('receipt', (receipt) => )
.on('error', console.error);

That’s it, we borrowed 5 DAI! To see the balance:

cDaiInstance.methods.borrowBalanceCurrent(address).call().then(result => )

Your homework is to follow the Compound V2 docs and repay my debt.

If you get stuck, start a discussion below — our community of peer Web3 devs is here to help!

About QuikNode

QuikNode is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum nodes. We’re working on something interesting from the past few months and will be launching soon, so subscribe our newsletter for more updates!! 😃

Published at Wed, 24 Apr 2019 10:14:16 +0000

Previous Article

Three Scientists Dismissed from MD Ander Cancer Center Amid “Academic Espionage” Allegations

Next Article

Bitfury, Swiss Investment Firm Launch Regulated Bitcoin Mining Fund

You might be interested in …

Goldunioncoin (guc) price alert, chart & news on bitscreener. Com

GoldUnionCoin (GUC) Price Alert, Chart & News on BitScreener.com

GoldUnionCoin (GUC) Price Alert, Chart & News on BitScreener.com More detail: https://bitscreener.com/coins/goldunioncoin PLUS GOLD UNION COIN (Symbol: GUC) is an ethereum smart coin used to send GUCP across ethereum system. GUCP is a distributed intelligent […]

The dark side of the money-1

The Dark Side of the Money-1

The Dark Side of the Money-1By zeevveez on 2011-08-28 20:11:42[wpr5_ebay kw=”bitcoin” num=”1″ ebcat=”” cid=”5338043562″ lang=”en-US” country=”0″ sort=”bestmatch”]

Re: 曝榆林官员被"二奶"抓拍嫖娼 官方:他正接受调查

Re: 曝榆林官员被"二奶"抓拍嫖娼 官方:他正接受调查 2月13日15:54,陕西榆林市纪委发布消息称:近日,网曝“榆林一官员现场被抓拍嫖娼”,榆林市纪委迅速组织人员进行初步核实。目前,市劳动人事仲裁院院长、党支部书记徐彦江正在接受组织调查。 陕西榆林纪委:市劳动人事仲裁院院长徐彦江正接受组织调查 图中(右一)为被举报的徐彦江 此前报道 陕西一官员当二奶面与小姐发生关系? 纪委:已知道此事 2月12日,微博网友@军事那点儿事 在网上爆料,称一名刘姓女子举报陕西榆林市人力资源和社会保障局就业促进科负责人徐某包养“二奶”,为断绝二奶“离婚娶自己”的想法,徐某竟当着“二奶”面与性工作者不可描述。 微博一并发布了5张图片,其中一张为女性,另两张不可描述,剩余两张内容为举报徐某“花80万买副局长职务”、“戴劳力士手表、LV皮带”的内容。 2月13日,榆林市纪委办公室工作人员向封面新闻-华西都市报记者表示,“纪委已知道此事”。 微博网友@军事那点儿事是在2月12日18:28在网上发布消息的,内容称,“网曝陕西榆林人社局就业科科长徐某,隐瞒婚姻包养小刘姑娘为二奶,二人还拍了婚纱照,接着小刘逼迫徐离婚娶自己。徐为气走小刘,便在酒店开房叫小姐,并当着她的面与小姐展示 老汉推车 等 实战 动作,小刘认为徐这是在侮辱自己的人格,于是便拍了艳照进行举报”。 陕西榆林纪委:市劳动人事仲裁院院长徐彦江正接受组织调查 该微博一并配发了5张图片。其中一张为女性,另两张不可描述,剩余两张为举报徐某“花80万买副局长职务”、“戴劳力士手表、LV皮带”的内容。 2月13日上午,封面新闻-华西都市报记者查询电榆林市人力资源和社会保障局官方网站发现,该局就业促进科负责人名字与被举报者名字一样。 陕西榆林纪委:市劳动人事仲裁院院长徐彦江正接受组织调查 记者致电该局就业促进科,欲与徐某本人联系了解此事真伪,但电话一直无人接听。 随后,记者数次致电榆林市人力资源和社会保障局纪检监察室,但接电话的工作人员表示,领导开会去了。记者向其反映徐某被举报的,欲了解此事真伪,但刚开口,工作人员便打断,“我不清楚”,随即挂断电话。 随后,记者又致电榆林市纪委监察局办公室,工作人员表示,“纪委已知道此事,详细情况请咨询纪委的宣传部门”。对于纪委对此事的调查进度,该工作人员并未回答。 记者拨打榆林市纪委宣传部门电话,但一直无人接听。此前,榆林市委宣传部工作人员曾向媒体介绍,当地纪委已在调查此事。 http://news.163.com/17/0213/16/CD5T26MV00018AOR.html (Why?) Published at Tue, 14 Feb 2017 03:05:36 […]