• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code examples for supplying Ethereum assets to the Compound Protocol.

Quick Start: Supplying Assets to the Compound Protocol

Examples for supplying Ethereum assets to the Compound Protocol.

Full Quick Start Tutorial on the Compound Medium Blog

If you want to supply assets directly to the protocol from your Ethereum wallet using JSON RPC, see the examples-js folder. There are examples for popular web3 libraries like Web3.js, Ethers.js, and Compound.js.

JSON RPC can be utilized in the web browser, with Node.js, or any other programming language with a web3 library.

If you want to supply assets to the protocol from your Ethereum smart contract, see the examples-solidity folder.

What is the Compound Protocol?

Compound is an open-source, decentralized autonomous protocol built for developers, to unlock a universe of new financial applications. Users can earn interest on their crypto assets and borrow crypto assets. Learn more on the website:

Compound Finance

Setup

The code in this repository can be used to supply assets to Compound on the Mainnet, any public test net, or your own localhost with Hardhat.

If you haven't already, install Node.js LTS. Clone this repository, cd to the root directory of the project, and run:

git clone [email protected]:compound-developers/compound-supply-examples.git
cd compound-supply-examples/
npm install

We'll need npx for this project. If you don't have npx, install it using this command:

npm install -g npx

If you want to use the JS examples in the web browser, you'll need to first import your web3 library of choice into your HTML (Web3.js, Ethers.js, or Compound.js). This step is not necessary if you are running the examples with only Node.js.

Running a Local Ethereum Testnet with Hardhat

To get the localhost testnet running, use the following commands in a second command line window. The command runs a local Hardhat node and forks Ethereum Mainnet to your machine.

If you are not running your own Ethereum node, make an Infura account at https://infura.io/ or at Alchemy.com. Accounts are free. Get a project ID and supplant it into your environment variable settings, like below.

cd compound-supply-examples/

## Set environment variables for the script to use
export MAINNET_PROVIDER_URL="https://mainnet.infura.io/v3/<YOUR INFURA API KEY HERE>"
export DEV_ETH_MNEMONIC="clutch captain shoe salt awake harvest setup primary inmate ugly among become"

## Runs the Hardhat node locally
## Also seeds your first mnemonic account with test Ether and ERC20s
node ./scripts/run-localhost-fork.js

Supplying Assets Directly via JSON RPC

These code examples can be run by a web browser or with Node.js. If you want to use a web browser, you'll need to import a library in your HTML or JS file.

Running these scripts will give your wallet cETH and cDAI. These are ERC20 Tokens that can be used to redeem an ever-increasing amount of the underlying asset. The cToken exchange rate increases every Ethereum block, they can be transferred, and can be used to redeem.

If you want to test locally with a different supported asset (besides DAI), see the Minting Localhost Test ERC20s section below.

Localhost Testnet

  • Run your local testnet in a second command line window using the command above.
  • node ./examples-js/web3-js/supply-eth.js To supply ETH.
  • node ./examples-js/web3-js/supply-erc20.js To supply some DAI. The same code can be used for any other ERC20 token that Compound supports.
  • Check out the other examples for Ethers.js and Compound.js in the examples-js folder; They all do the same thing.

Public Testnet or Mainnet

  • Make sure you have a wallet with ETH for the Ethereum network you plan to interface with (Main, Ropsten, Kovan, etc.).
  • Insert the private key of your wallet in the scripts where noted. It's a best practice to insert the private key using an environment variable instead of revealing it in the code with a string literal.
  • Replace the HTTP provider in the constructors in the scripts in examples-js/. Replace it using the string provided by the "Endpoint" selector in your Infura project dashboard. The local testnet provider is http://localhost:8545.
  • Next, replace the contract addresses in the scripts with the most recent ones. You can find Compound's cToken contract addresses for each network on this page: https://compound.finance/docs#networks.

Supplying Assets With a Solidity Smart Contract

The examples send ETH or DAI to a smart contract, which then mints cETH or cDAI. The contract can also redeem the cTokens for their corresponding underlying.

Localhost Testnet

  • Run your local testnet in a second command line window using the command above.
  • Compile the smart contract in ./contracts/ by running npx hardhat compile
  • Next, deploy the smart contract to the localhost blockchain. npx hardhat run ./scripts/deploy.js --network localhost
  • Now that the contract is deployed, copy the address that is logged by the deploy script and paste it into the example script, so it knows where to direct its transactions. All JS files in the examples-solidity directory have a variable called myContractAddress which is where the MyContract address should be supplanted.
  • Now you can run any of the following examples to supply via smart contract.
  • node ./examples-solidity/web3-js/supply-eth.js To supply ETH.
  • node ./examples-solidity/web3-js/supply-erc20.js To supply some DAI.

Public Testnet or Mainnet

See the Hardhat docs for more information on deploying to public Ethereum networks. https://hardhat.org/guides/deploying.html

Script Output Examples

Supply ETH via Web3.js JSON RPC

Output Example

$ node ./examples-js/web3-js/supply-eth.js
My wallet's ETH balance: 10000

Supplying ETH to the Compound Protocol...

cETH "Mint" operation successful.

ETH supplied to the Compound Protocol: 0.999999999922754

My wallet's cETH Token Balance: 49.86379983

Current exchange rate from cETH to ETH: 0.020054628875698224

Redeeming the cETH for ETH...

Exchanging all cETH based on cToken amount...

My wallet's cETH Token Balance: 0
My wallet's ETH balance: 9999.993847840284

Supply ERC20 via Web3.js JSON RPC

Output Example

$ node ./examples-js/web3-js/supply-erc20.js
DAI contract "Approve" operation successful.
Supplying DAI to the Compound Protocol...

cDAI "Mint" operation successful.

DAI supplied to the Compound Protocol: 9.999999999941178

My wallet's cDAI Token Balance: 482.50451639
My wallet's DAI Token Balance: 41

Current exchange rate from cDAI to DAI: 0.02072519460493164

Redeeming the cDAI for DAI...
Exchanging all cDAI based on cToken amount...

My wallet's cDAI Token Balance: 0
My wallet's DAI Token Balance: 51.0000001323868

Supply ETH via Solidity

Output Example

$ node ./examples-solidity/web3-js/supply-eth.js
Supplied ETH to Compound via MyContract
ETH supplied to the Compound Protocol: 0.999999999985654072
MyContract's cETH Token Balance: 49.93244867
Redeeming the cETH for ETH...
MyContract's cETH Token Balance: 0
MyContract's ETH Balance: 1.000000000648673

Supply ERC20 Token via Solidity

Output Example

$ node ./examples-solidity/web3-js/supply-erc20.js
Now transferring DAI from my wallet to MyContract...
MyContract now has DAI to supply to the Compound Protocol.
MyContract is now minting cDAI...
Supplied DAI to Compound via MyContract
DAI supplied to the Compound Protocol: 9.999999999942267983
MyContract's cDAI Token Balance: 482.50440136
Redeeming the cDAI for DAI...
MyContract's cDAI Token Balance: 0

Minting Localhost Test ERC20s

All assets supported by the Compound protocol can be seeded into the first account when doing localhost testing. See the amounts object at the top of the script ./scripts/run-localhost-fork.js. You can add assets and amounts to this object. When the localhost fork script is run, Hardhat will move tokens from a whale (cToken contract) to the first wallet of your selected mnemonic (in your environment variable). You can then use these assets freely on your localhost fork.

Ethers.js & Compound.js Examples

There are several other code examples for ethers.js and Compound.js. These SDKs can be used instead of Web3.js in each instance. Each version of the script does the same operations. To try the other code examples, run the scripts in the other folders.

## Ethers.js Examples
node ./examples-solidity/ethers-js/supply-erc20.js
node ./examples-solidity/ethers-js/supply-eth.js
node ./examples-js/ethers-js/supply-erc20.js
node ./examples-js/ethers-js/supply-eth.js

## Compound.js Examples
node ./examples-solidity/compound-js/supply-erc20.js
node ./examples-solidity/compound-js/supply-eth.js
node ./examples-js/compound-js/supply-erc20.js
node ./examples-js/compound-js/supply-eth.js

## Web3.js
node ./examples-solidity/web3-js/supply-erc20.js
node ./examples-solidity/web3-js/supply-eth.js
node ./examples-js/web3-js/supply-erc20.js
node ./examples-js/web3-js/supply-eth.js