Supplying Assets to the Compound Protocol

Quick Start Guide

Adam Bavosa
Feb 12, 2020 · 14 min read
Developer Guide for Supplying Crypto Assets to the Compound Protocol
Developer Guide for Supplying Crypto Assets to the Compound Protocol

The Compound Protocol is a series of interest rate markets running on the Ethereum blockchain. When users and applications supply an asset to the Compound Protocol, they begin earning a variable interest rate instantly. Interest accrues every Ethereum block (currently ~13 seconds), and users can withdraw their principal plus interest anytime.

Under the hood, users are contributing their assets to a large pool of liquidity (a “market”) that is available for other users to borrow, and they share in the interest that borrowers pay back to the pool.

When users supply assets, they receive cTokens from Compound in exchange. cTokens are ERC20 tokens that can be redeemed for their underlying assets at any time. As interest accrues to the assets supplied, cTokens are redeemable at an exchange rate (relative to the underlying asset) that constantly increases over time, based on the rate of interest earned by the underlying asset.

Non-technical users can interact with the Compound Protocol using an interface like Argent, Coinbase Wallet, or; developers can create their own applications that interact with Compound’s smart contracts.

In this guide, we’re going to walk through supplying assets via Web3.js JSON RPC and via proxy smart contracts that live on the blockchain. These are two methods in which developers can write software to utilize the Compound Protocol.

There are examples in JavaScript and also Solidity.

Table of Contents for This Guide

If you are new to Ethereum, we suggest that you start by Setting up your Development Environment for Ethereum.

All of the code referenced in this guide can be found in this GitHub Repository: Quick Start: Supplying Assets to the Compound Protocol.

To copy the repository to your computer, run this on the command line after you’ve installed git:

git clone

Compound Markets

The smart contracts that power the protocol are deployed to the Ethereum blockchain. This means that at the time of this guide’s writing, the only types of assets that Compound can support are Ether and ERC-20 tokens.

The currently supported assets are listed here Based on the different implementation of Ether (ETH) and ERC-20 tokens, we have to utilize two similar processes:

  • The ETH supply method
  • The ERC20 token supply method

Like mentioned earlier, when someone supplies an asset to the protocol, they are given cTokens in exchange. The method for getting cETH is different from the method for getting cDAI, cUNI, or any other cToken for an ERC-20 asset. We’ll run through code examples and explanations for the two different asset supply methods.

When supplying Ether to the Compound protocol, an application can send ETH directly to the payable mint function in the cEther contract. Following that mint, cEther is minted for the wallet or contract that invoked the mint function. Remember that if you are calling this function from another smart contract, that contract needs a payable function in order to receive ETH when you redeem the cTokens later.

The operation is slightly different for cERC20 tokens. In order to mint cERC20 tokens, the invoking wallet or contract needs to first call the approve function on the underlying token’s contract. All ERC20 token contracts have an approve function.

The approval needs to indicate that the corresponding cToken contract is permitted to take up to the specified amount from the sender address. Subsequently, when the mint function is invoked, the cToken contract retrieves the indicated amount of underlying tokens from the sender address, based on the prior approve call.

Example code for each method (JS and Solidity) is available, open source, in the GitHub Repository linked above.

Connecting to the Ethereum Network

If you want to use a public test net (like Ropsten, Göerli, Kovan, or Rinkeby), make an Infura account at to get your API key. If you are using your own localhost test net, or the production mainnet, we will also use Infura.

If you are not hosting your own Ethereum node to access the blockchain, make an Infura account before continuing.

For more on connecting to a public Ethereum network, see the instructions in Setting up your Development Environment for Ethereum.

Supplying to the Compound Protocol on a Localhost Network

Let’s install and initialize Ganache CLI.

npm i -g ganache-cli## or for yarn fans: yarn global add ganache-cli

Run this command in a second command line window before you start running the code referenced later in this guide. The command spins up a test Ethereum blockchain on your localhost. Be sure to supplant your Infura project ID into the JSON RPC Provider URL.

ganache-cli \
-m "clutch captain shoe salt awake harvest setup primary inmate ugly among become" \
-i 1 \
-u 0x9759A6Ac90977b93B58547b4A71c78317f391A28

A quick explanation of each of the command line flags:

  • -f Forks the Main Ethereum network to your local machine for development and testing.
  • -m Runs Ganache with an Ethereum key set based on the mnemonic passed. The first 10 addresses have 100 test ETH in their balance on the local test net every time you boot Ganache. Do not use this mnemonic anywhere other than your localhost test net.
  • -i Sets an explicit network ID to avoid confusion and errors.
  • -u Unlocks an address so you can write to your localhost test blockchain without knowing that address’s private key. We are unlocking the above address so we can mint our own test DAI on our localhost test net (more on this later).

Once you have run Ganache CLI on your command line, it will log 10 wallet addresses, and 10 private keys. Each of the wallets will have 100 test ETH in them which can be used for executing smart contracts locally. Copy and save the first private key.

Image for post
Image for post

Supplying to Compound on a Public Network

You also should have collected some ETH for that network by purchasing/mining (Main), or a test net’s faucet (all the others). This is not necessary when using a localhost fork.

For example, here is Ropsten’s faucet You can send yourself 1 ETH every 24 hours from a single IP address. This is test ETH that is only applicable to the Ropsten test network.

Next, copy and safely store your wallet’s private key. Don’t do this if you are only testing on your localhost. The private key is used to sign transactions that are sent on the Ethereum network. The purpose of this is to certify that the transaction was created and submitted by a unique wallet.

If you are using MetaMask for your Ethereum wallet, open the menu, click the 3 dots on the right, Account Details, Export Private Key, and input your MetaMask password. This will reveal your private key. Keep it safe! Copy this value and save it for later.

Image for post
Image for post

It is a best practice to store a key like this as an environment variable on your local machine. When a key is stored as an environment variable, it can be referenced in code files by a variable name, instead of explicitly with a string. This promotes code cleanliness, and reduces the risk of exposing your secret.

Again, if you are only testing smart contracts on your localhost Ganache today, don’t get your MetaMask private key. We’ll rely on the private key from the Ganache command line log.

How to Supply ETH to Compound via Web3.js

Image for post
Image for post

Supplying Ether (ETH) to the Compound Protocol is as easy as calling the “mint” function in the Compound cEther smart contract. The “mint” function transfers ETH to the Compound contract address, and mints cETH tokens. The cETH tokes are transferred to the wallet of the supplier.

Remember that the amount of ETH that can be exchanged for cETH increases every Ethereum block, which is about every 13 seconds. There is no minimum or maximum amount of time that suppliers need to keep their asset in the protocol. See the varying exchange rate for each cToken by clicking on one at

For more information on cToken concepts see the cToken documentation.

In order to call the mint function, you need to first:

  • Have ETH in your Ethereum wallet.
  • Find your Ethereum wallet’s private key.
  • Connect to the network via Infura API key (see above section Connecting to the Ethereum Network)

There are several programming languages that have Ethereum Web3 libraries, but the most popular at the time of this guide’s writing is JavaScript.

We’ll be using Node.js JavaScript to call the mint function. The following code snippets are from this Node.js file in the supplying assets guide GitHub Repository. Web browser JavaScript is nearly identical to these code examples.

Let’s import Web3.js, and initialize the Web3 object. It’s pointing to our localhost’s Ganache, which has 100 test ETH in each of the test wallets. We get the same 10 test wallet addresses every time we run Ganache CLI with the mnemonic in the above command (from the Connecting to the Ethereum Network section).

If you are using a public network (Ropsten, Kovan, etc.), make sure your wallet has ETH, and that you have your wallet private key stored as an environment variable. Also, have your Infura API key ready if you are deploying to a public test net.

Replace the HTTP provider URL in the Web3 declaration line with the appropriate network’s provider if you are not using the Ganache test environment.

Next, we’ll add our wallet’s private key as a variable. It’s a best practice to access this as an environment variable.

If you are writing web browser JavaScript instead of Node.js, you can add the user’s private key to the Web3 object by using the ethereum.enable() command. Here is the alternative code snippet.

Next we’ll make some variables for the contract address and the contract ABI. The contract addresses are posted on this page: Remember to use the mainnet address if you are testing with Ganache CLI. The ABI is the same regardless of the Ethereum network that we are using.

The next section of code is where the magic happens. The first call in the main function supplies our ETH to the protocol by calling the mint function, which mints cETH. The cETH is transferred to our wallet address.

The three subsequent function calls are not necessary, but they are here for illustration. The first method calls a getter function in the Compound contract that shows how much underlying ETH our cToken balance entitles us to. The second function shows our wallet’s cToken balance. The third function gets the current exchange rate of cETH to ETH.

Our code sends 1 ETH to the contract, and gives our wallet cETH. The ratio of cETH to ETH should be in the ballpark of 50 to 1. Remember that the exchange rate of underlying to cToken increases over time.

Lastly, after the supply operation is complete, we’ll redeem our cTokens. This is what a user or application will do when they want to withdraw their crypto asset from the Compound protocol.

The first method, redeem, redeems based on the cToken amount passed to the function call.

The second method, redeemUnderlying, which is commented out, redeems ETH based on the amount passed to the function call.

Finally, we execute the main function and declare an error handler.

If you cloned the GitHub repository, be sure to run npm install in the root directory of the project before you try to run the script.

Here is the command for running the script from the root directory of the project:

node web3-js-examples/supply-eth-via-web3.js

Script example output:

How to Supply a Supported ERC20 Token to Compound via Solidity

Image for post
Image for post

The following will run through an example of adding an ERC20 token to the Compound protocol using Solidity smart contracts. The full Solidity file can be found in the project GitHub repository.

Here’s an overview of supplying a token to the Compound Protocol with Solidity:


  • Get some ERC20 token, in this case Dai. If you are working in the production environment, purchase some Dai for your Ethereum wallet. If you are working with a Ganache CLI test blockchain, follow the instructions in the section Minting Test Net Dai to get your test wallet some Dai.
  • Get the address of the ERC20 contract.
  • Get the address of the Compound cToken contract. See Dai on this page:

Order of Operations

  • You call your custom contract’s function for supplying to the Compound Protocol.
  • Your custom contract’s function calls the approve function from the original ERC20 token contract. This allows an amount of the token to be withdrawn by cToken from your custom contract’s token balance.
  • Your custom contract’s function calls the mint function in the Compound cToken contract.
  • Finally, we call your custom contract’s function for redeeming, to get the ERC20 token back.

Let’s get started. First we’ll walk through the code in our Solidity file, MyContracts.sol.

We added contract interfaces. The first is for our ERC20 token contract, and the second is for Compound’s corresponding cToken contract.

We’ll be able to call the production versions of the 3rd party contracts using these definitions. We need to initialize them with the production address of the deployed contracts, which we pass to each of the functions in MyContract.

The first function in MyContract allows the caller to supply an ERC20 token to the Compound Protocol. We will need to pass the underlying contract address, the cToken contract address, and the number of tokens we want to supply.

The function first creates references to the production instances of Dai and cDAI contracts using our interface definitions.

Then the function logs the exchange rate and the supply rate. These calls are not necessary for supplying. They are there for illustration. You can see the amounts in the “events” output later in JavaScript.

Next, our function approves the transfer of ERC20 token from our contract’s address to Compound’s cToken contract using the approve method.

Finally, our contract calls the cToken contract mint function. This supplies some Dai to the protocol, and gives our custom contract a balance of cDAI.

After we have supplied some Dai, we can redeem it at any time. The following function shows how we can accomplish that in Solidity.

The redeemCErc20Tokens function allows the caller to redeem based on the amount of underlying or the amount of cTokens. This is indicated by calling the function with a boolean for redeem type; True for cToken, and false for underlying amount.

If there is an error with redeeming, the error code is logged using MyLog. Error codes for cToken contracts are described in the documentation.

Now that we have our code written, let’s run it!


If you cloned the GitHub repository, be sure to run npm install in the root directory of the project before you try to run the compile script.

Compile your contract with the command below. The bytecode and ABI will be waiting in the .build/ folder.

node compile-smart-contracts.js


node deploy-smart-contracts.js
> Your contract was successfully deployed!
> The contract can be interfaced with at this address:
> 0x4a81cff73f1b8c6d94f50EDC08A4DEe7fbC109C6

Copy this and save it for later. We’ll need it to call the smart contract’s function to supply Dai to Compound.


First, the script makes a Web3 object and points it to the blockchain network that we want to use to supply to Compound.

Next, we make a reference to our Ethereum wallet private key. This should be a wallet that has some ETH (for gas) and also Dai (to supply to Compound). Our script’s main function first transfers Dai from our wallet to MyContract.

Next, we make some references to MyContract, the Dai contract, and also the Compound cDAI contract.

Remember, the cToken contract addresses and ABIs can be found here:, and MyContract’s address was logged when we deployed the contract.

See the Minting Test Net DAI section to find the newest Dai contract address.

Finally, we call our main function, which first transfers Dai from our wallet to MyContract.

Next we call the supplyErc20ToCompound function in MyContract, which sends 10 Dai to Compound in exchange for cDAI.

The next 2 function calls are not necessary for supplying. They illustrate how to get the balance of underlying ERC20 asset in the protocol and the amount of cTokens that MyContract now holds.

Lastly we call the redeemCErc20Tokens function in MyContract to redeem the cDAI for Dai. The example utilizes the redeem method by passing a cToken amount. Under that, there is a redeem underlying amount example, which is commented out.

Now we’re ready to run!

If you are running on localhost remember to mint test DAI before you run the JavaScript file!

node seed-account-with-erc20/dai.js

If you are running this on a public network, you’ll need to acquire Dai for that network.

To execute the script, navigate to the project root directory and run:

node solidity-examples/supply-erc20-via-solidity.js

If successful, the output of the script will show something like this:

Remember that this code will work with any of the ERC20 tokens that Compound supports. You will need to swap in the corresponding ERC20 token contract address and ABI into the JavaScript.

Thanks for reading! You are now able to supply assets to the Compound protocol using Solidity or JavaScript. We walked through supplying Ether, and also the supported ERC20 token assets. We can also redeem cTokens for those underlying assets later on. There are 4 different code examples available in the Supply Examples GitHub repository.

The next key concept of developing a DApp with the Compound protocol is borrowing assets. Be sure to check out the next developer quick start guide: Borrowing Assets from the Compound Protocol.

Be sure to subscribe to the Compound Newsletter. Feel free to comment on this post, or get in touch in the #development room on our very active Discord server.


Compound is an algorithmic, autonomous interest rate protocol built for developers.

Adam Bavosa

Written by

DevRel @



Compound is an algorithmic, autonomous interest rate protocol built for developers, to unlock a universe of open financial applications.

Adam Bavosa

Written by

DevRel @



Compound is an algorithmic, autonomous interest rate protocol built for developers, to unlock a universe of open financial applications.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store