All Projects → compound-developers → compound-supply-examples

compound-developers / compound-supply-examples

Licence: MIT license
Code examples for supplying Ethereum assets to the Compound Protocol.

Programming Languages

javascript
184084 projects - #8 most used programming language
solidity
1140 projects

Projects that are alternatives of or similar to compound-supply-examples

compound-governance-examples
Examples for building on Compound Protocol Community Governance.
Stars: ✭ 52 (-56.3%)
Mutual labels:  defi, decentralized-finance, compound-finance, compound-protocol
compound-borrow-examples
Code examples for borrowing Ethereum assets from the Compound Protocol.
Stars: ✭ 76 (-36.13%)
Mutual labels:  defi, decentralized-finance, compound-finance, compound-protocol
visor-core
The DeFi protocol for Active Liquidity Management. Building on Uniswap v3.
Stars: ✭ 50 (-57.98%)
Mutual labels:  defi, decentralized-finance
go-compound
Golang client for compound.finace api and smart contracts
Stars: ✭ 23 (-80.67%)
Mutual labels:  defi, compound-finance
octofi-app-aquafarm
OctoFi - Track your DeFi portfolio, find new investment opportunities, buy and sell directly, and wrap your tentacles around a sea of gains. Hosted on Github Pages.
Stars: ✭ 36 (-69.75%)
Mutual labels:  defi, decentralized-finance
DeFi-Map
List of decentralized finance projects on Ethereum.
Stars: ✭ 49 (-58.82%)
Mutual labels:  defi, decentralized-finance
prb-math
Solidity library for advanced fixed-point math
Stars: ✭ 404 (+239.5%)
Mutual labels:  defi, decentralized-finance
botdexdamar
🤖 multichain trading bot with sniper, frontrun, backrun, sandwich
Stars: ✭ 124 (+4.2%)
Mutual labels:  defi
ape
The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
Stars: ✭ 339 (+184.87%)
Mutual labels:  defi
Nescience-Indexing-CLI
Nescience Software & Capital Rebalancing Tool
Stars: ✭ 26 (-78.15%)
Mutual labels:  defi
MultiDexArbBot
This is an arbitrage bot that uses existing price aggregators such as 1inch, Paraswap, dex.ag, matcha and more to get the best exchange rates across different decentralized exchanges on different blockchains and ecosystems.
Stars: ✭ 67 (-43.7%)
Mutual labels:  defi
cyclone-contracts
Cyclone is a multi-chain, non-custodial, privacy-preserving protocol. Cyclone applies zkSNARKs to enable transactional privacy by breaking the on-chain link between depositor and recipient addresses. It uses a smart contract that accepts coins/tokens deposits, which can be withdrawn by a different address. Whenever an asset is withdrawn from Cyc…
Stars: ✭ 49 (-58.82%)
Mutual labels:  defi
idle-contracts
idle.finance
Stars: ✭ 30 (-74.79%)
Mutual labels:  defi
mai-protocol
A Protocol for trading decentralized derivatives on Ethereum
Stars: ✭ 22 (-81.51%)
Mutual labels:  defi
polkaswap-web
Polkaswap is a non-custodial, cross-chain AMM DEX protocol for swapping tokens based on Polkadot and Kusama relay chains, Polkadot and Kusama parachains, and blockchains directly connected via bridges. Polkaswap removes trusted intermediaries and provides the opportunity for faster trading.
Stars: ✭ 43 (-63.87%)
Mutual labels:  defi
yam-www
Yam website.
Stars: ✭ 41 (-65.55%)
Mutual labels:  defi
Awesome NFTs
A curated collection about NFTs - by bt3gl
Stars: ✭ 42 (-64.71%)
Mutual labels:  defi
digital-copyright
Stamp your code with a trackable digital copyright
Stars: ✭ 17 (-85.71%)
Mutual labels:  defi
defi-app-old
DeFi Blockchain desktop app for Windows, Linux and Mac.
Stars: ✭ 12 (-89.92%)
Mutual labels:  defi
chainlink defi
Build a defi yield farmable dApp. Get started here.
Stars: ✭ 134 (+12.61%)
Mutual labels:  defi

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].