Skip to content

Instantly share code, notes, and snippets.

@ajb413
Created February 11, 2020 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajb413/d3260e23eb1a0848af5f0c67fe9d7fea to your computer and use it in GitHub Desktop.
Save ajb413/d3260e23eb1a0848af5f0c67fe9d7fea to your computer and use it in GitHub Desktop.
Supply ERC20 to Compound with Web3.js
// Tell DAI contract to allow 10 DAI to be taken by Compound's contract
daiContract.methods.approve(compoundCDaiContractAddress, web3.utils.toBN(10e18))
.send({
from: myWalletAddress,
gasLimit: web3.utils.toHex(150000), // posted at compound.finance/developers#gas-costs
gasPrice: web3.utils.toHex(20000000000) // use ethgasstation.info (mainnet only)
}).then((result) => {
console.log('DAI contract "Approve" operation successful.');
console.log('Sending DAI to the Compound Protocol...');
// Mint cDAI by supplying DAI to the Compound Protocol
const dai = 10e18; // 10 DAI
return compoundCDaiContract.methods.mint(web3.utils.toBN(dai)).send({
from: myWalletAddress,
gasLimit: web3.utils.toHex(600000),
gasPrice: web3.utils.toHex(20000000000),
});
}).then((result) => {
console.log('cDAI "Mint" operation successful.');
return compoundCDaiContract.methods
.balanceOfUnderlying(myWalletAddress).call();
}).then((balanceOfUnderlying) => {
balanceOfUnderlying = web3.utils.fromWei(balanceOfUnderlying).toString();
console.log("DAI supplied to the Compound Protocol:", balanceOfUnderlying);
return compoundCDaiContract.methods.balanceOf(myWalletAddress).call();
}).then((cTokenBalance) => {
cTokenBalance = (cTokenBalance / 1e8).toString();
console.log("My wallet's cDAI Token Balance:", cTokenBalance);
}).catch((error) => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment