{"id":18487220,"url":"https://github.com/compound-developers/compound-supply-examples","last_synced_at":"2025-04-08T20:30:51.453Z","repository":{"id":40671985,"uuid":"237886029","full_name":"compound-developers/compound-supply-examples","owner":"compound-developers","description":"Code examples for supplying Ethereum assets to the Compound Protocol.","archived":false,"fork":false,"pushed_at":"2023-03-05T14:42:00.000Z","size":1709,"stargazers_count":138,"open_issues_count":11,"forks_count":38,"subscribers_count":5,"default_branch":"master","last_synced_at":"2023-11-07T19:02:40.730Z","etag":null,"topics":["blockchain","compound-finance","compound-protocol","decentralized-finance","defi","ethereum"],"latest_commit_sha":null,"homepage":"https://compound.finance/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/compound-developers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-03T04:39:09.000Z","updated_at":"2023-11-05T15:37:05.000Z","dependencies_parsed_at":"2023-01-31T17:15:31.108Z","dependency_job_id":null,"html_url":"https://github.com/compound-developers/compound-supply-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compound-developers%2Fcompound-supply-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compound-developers%2Fcompound-supply-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compound-developers%2Fcompound-supply-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compound-developers%2Fcompound-supply-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/compound-developers","download_url":"https://codeload.github.com/compound-developers/compound-supply-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223343662,"owners_count":17129953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","compound-finance","compound-protocol","decentralized-finance","defi","ethereum"],"created_at":"2024-11-06T12:50:20.169Z","updated_at":"2024-11-06T12:51:46.795Z","avatar_url":"https://github.com/compound-developers.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Quick Start: Supplying Assets to the Compound Protocol\n\nExamples for supplying Ethereum assets to the [Compound Protocol](https://compound.finance/?ref=github\u0026user=ajb413\u0026repo=compound-supply-examples).\n\n**[Full Quick Start Tutorial on the Compound Medium Blog](https://medium.com/compound-finance/supplying-assets-to-the-compound-protocol-ec2cf5df5aa)** \n\nIf 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**.\n\nJSON RPC can be utilized in the **web browser, with Node.js, or any other programming language with a web3 library**.\n\nIf you want to supply assets to the protocol from your Ethereum smart contract, see the `examples-solidity` folder.\n\n## What is the Compound Protocol?\nCompound 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:\n\n\u003ca href=\"https://compound.finance/?ref=github\u0026user=ajb413\u0026repo=compound-supply-examples\"\u003e\n    \u003cimg alt=\"Compound Finance\" src=\"https://raw.githubusercontent.com/ajb413/compound-interest-alerts/master/compound-finance-logo.png\" width=260 height=60/\u003e\n\u003c/a\u003e\n\n## Setup\nThe 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](https://hardhat.org/getting-started/).\n\nIf you haven't already, install [Node.js](https://nodejs.org/) LTS. Clone this repository, `cd` to the root directory of the project, and run:\n```bash\ngit clone git@github.com:compound-developers/compound-supply-examples.git\ncd compound-supply-examples/\nnpm install\n```\n\nWe'll need `npx` for this project. If you don't have `npx`, install it using this command:\n```bash\nnpm install -g npx\n```\n\nIf 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.\n\n### Running a Local Ethereum Testnet with Hardhat\nTo 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.\n\n**If you are not running your own Ethereum node, make an [Infura](https://infura.io/) account at [https://infura.io/](https://infura.io/) or at [Alchemy.com](https://alchemy.com).** Accounts are free. Get a **project ID** and supplant it into your [environment variable settings](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html), like below.\n\n```bash\ncd compound-supply-examples/\n\n## Set environment variables for the script to use\nexport MAINNET_PROVIDER_URL=\"https://mainnet.infura.io/v3/\u003cYOUR INFURA API KEY HERE\u003e\"\nexport DEV_ETH_MNEMONIC=\"clutch captain shoe salt awake harvest setup primary inmate ugly among become\"\n\n## Runs the Hardhat node locally\n## Also seeds your first mnemonic account with test Ether and ERC20s\nnode ./scripts/run-localhost-fork.js\n```\n\n## Supplying Assets Directly via JSON RPC\nThese 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.\n\nRunning 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.\n\nIf you want to test locally with a different supported asset (besides DAI), see the **Minting Localhost Test ERC20s** section below.\n\n### Localhost Testnet\n- Run your local testnet in a second command line window **using the command above**.\n- `node ./examples-js/web3-js/supply-eth.js` To supply ETH.\n- `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](https://compound.finance/markets?ref=github\u0026user=ajb413\u0026repo=compound-supply-examples).\n- Check out the other examples for Ethers.js and Compound.js in the `examples-js` folder; They all do the same thing.\n\n### Public Testnet or Mainnet\n- Make sure you have a wallet with ETH for the Ethereum network you plan to interface with (Main, Ropsten, Kovan, etc.).\n- 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.\n- 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`.\n- 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](https://compound.finance/docs#networks).\n\n## Supplying Assets With a Solidity Smart Contract\nThe 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.\n\n### Localhost Testnet\n- Run your local testnet in a second command line window **using the command above**.\n- Compile the smart contract in `./contracts/` by running `npx hardhat compile`\n- Next, deploy the smart contract to the localhost blockchain. `npx hardhat run ./scripts/deploy.js --network localhost`\n- 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.\n- Now you can run any of the following examples to supply via smart contract.\n- `node ./examples-solidity/web3-js/supply-eth.js` To supply ETH.\n- `node ./examples-solidity/web3-js/supply-erc20.js` To supply some DAI.\n\n### Public Testnet or Mainnet\nSee the Hardhat docs for more information on deploying to public Ethereum networks. https://hardhat.org/guides/deploying.html\n\n## Script Output Examples\n\n**Supply ETH via Web3.js JSON RPC**\n\u003cdetails\u003e\u003csummary\u003eOutput Example\u003c/summary\u003e\n\u003cp\u003e\n\n```\n$ node ./examples-js/web3-js/supply-eth.js\nMy wallet's ETH balance: 10000\n\nSupplying ETH to the Compound Protocol...\n\ncETH \"Mint\" operation successful.\n\nETH supplied to the Compound Protocol: 0.999999999922754\n\nMy wallet's cETH Token Balance: 49.86379983\n\nCurrent exchange rate from cETH to ETH: 0.020054628875698224\n\nRedeeming the cETH for ETH...\n\nExchanging all cETH based on cToken amount...\n\nMy wallet's cETH Token Balance: 0\nMy wallet's ETH balance: 9999.993847840284\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n**Supply ERC20 via Web3.js JSON RPC**\n\u003cdetails\u003e\u003csummary\u003eOutput Example\u003c/summary\u003e\n\u003cp\u003e\n\n```\n$ node ./examples-js/web3-js/supply-erc20.js\nDAI contract \"Approve\" operation successful.\nSupplying DAI to the Compound Protocol...\n\ncDAI \"Mint\" operation successful.\n\nDAI supplied to the Compound Protocol: 9.999999999941178\n\nMy wallet's cDAI Token Balance: 482.50451639\nMy wallet's DAI Token Balance: 41\n\nCurrent exchange rate from cDAI to DAI: 0.02072519460493164\n\nRedeeming the cDAI for DAI...\nExchanging all cDAI based on cToken amount...\n\nMy wallet's cDAI Token Balance: 0\nMy wallet's DAI Token Balance: 51.0000001323868\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n**Supply ETH via Solidity**\n\u003cdetails\u003e\u003csummary\u003eOutput Example\u003c/summary\u003e\n\u003cp\u003e\n\n```\n$ node ./examples-solidity/web3-js/supply-eth.js\nSupplied ETH to Compound via MyContract\nETH supplied to the Compound Protocol: 0.999999999985654072\nMyContract's cETH Token Balance: 49.93244867\nRedeeming the cETH for ETH...\nMyContract's cETH Token Balance: 0\nMyContract's ETH Balance: 1.000000000648673\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n**Supply ERC20 Token via Solidity**\n\u003cdetails\u003e\u003csummary\u003eOutput Example\u003c/summary\u003e\n\u003cp\u003e\n\n```\n$ node ./examples-solidity/web3-js/supply-erc20.js\nNow transferring DAI from my wallet to MyContract...\nMyContract now has DAI to supply to the Compound Protocol.\nMyContract is now minting cDAI...\nSupplied DAI to Compound via MyContract\nDAI supplied to the Compound Protocol: 9.999999999942267983\nMyContract's cDAI Token Balance: 482.50440136\nRedeeming the cDAI for DAI...\nMyContract's cDAI Token Balance: 0\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n## Minting Localhost Test ERC20s\n\nAll 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.\n\n## Ethers.js \u0026 Compound.js Examples\n\nThere are several other code examples for [ethers.js](https://ethers.org/) and [Compound.js](https://github.com/compound-finance/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.\n\n```bash\n## Ethers.js Examples\nnode ./examples-solidity/ethers-js/supply-erc20.js\nnode ./examples-solidity/ethers-js/supply-eth.js\nnode ./examples-js/ethers-js/supply-erc20.js\nnode ./examples-js/ethers-js/supply-eth.js\n\n## Compound.js Examples\nnode ./examples-solidity/compound-js/supply-erc20.js\nnode ./examples-solidity/compound-js/supply-eth.js\nnode ./examples-js/compound-js/supply-erc20.js\nnode ./examples-js/compound-js/supply-eth.js\n\n## Web3.js\nnode ./examples-solidity/web3-js/supply-erc20.js\nnode ./examples-solidity/web3-js/supply-eth.js\nnode ./examples-js/web3-js/supply-erc20.js\nnode ./examples-js/web3-js/supply-eth.js\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompound-developers%2Fcompound-supply-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompound-developers%2Fcompound-supply-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompound-developers%2Fcompound-supply-examples/lists"}