Code examples for client interaction with the Compound protocol via Compound.js.
Run Ganache with a fork of main net on your local machine and set MetaMask to Localhost 8545 to test writing transactions.
DO NOT use this web page with Ethereum main net or with your funded Ethereum keys / wallet!
Generic Ethereum blockchain read with JSON RPC. This example fetches the supply rate per block for cETH using the read method.
// mainnet
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
(async function() {
const srpb = await Compound.eth.read(
cEthAddress,
'function supplyRatePerBlock() returns (uint256)',
// [], // [optional] parameters
// {} // [optional] call options, provider, network, plus Ethers.js "overrides"
);
console.log('cETH market supply rate per block:', srpb.toString());
})().catch(console.error);
Create and send an Ethereum transaction with JSON RPC. This button's transaction transfers your Ether to the Compound protocol using the trx method.
const oneEthInWei = '1000000000000000000';
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
const provider = window.ethereum;
(async function() {
console.log('Supplying ETH to the Compound Protocol...');
// Mint some cETH by supplying ETH to the Compound Protocol
const trx = await Compound.eth.trx(
cEthAddress,
'function mint() payable',
[],
{
provider,
value: oneEthInWei
}
);
// const result = await trx.wait(1); // JSON object of trx info, once mined
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction transfers your Ether to the Compound protocol using the supply method.
const compound = new Compound(window.ethereum);
// Ethers.js overrides are an optional 3rd parameter for `supply`
// const trxOptions = { gasLimit: 250000, mantissa: false };
(async function() {
console.log('Supplying ETH to the Compound protocol...');
const trx = await compound.supply(Compound.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction redeems your Ether from the Compound protocol using the redeem method.
const compound = new Compound(window.ethereum);
(async function() {
console.log('Redeeming ETH...');
const trx = await compound.redeem(Compound.ETH, 1); // also accepts cToken args
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction enables supplied assets to be used as collateral using the enterMarkets method. Note that there is a corresponding exitMarket method for exiting a single market.
const compound = new Compound(window.ethereum);
(async function() {
console.log('Entering ETH market (use as collateral)...');
const trx = await compound.enterMarkets(Compound.ETH); // also accepts []
console.log('Ethers.js transaction object', trx);
// Exit a market (string argument of only 1 market at a time)
// const trx = await compound.exitMarket(Compound.ETH);
})().catch(console.error);
This button's transaction borrows 32 Dai, against collateral, using the borrow method. Remember to supply a supported asset as collateral and enter that market prior to borrowing.
const compound = new Compound(window.ethereum);
(async function() {
const daiScaledUp = '32000000000000000000';
const trxOptions = { mantissa: true };
console.log('Borrowing 32 Dai...');
const trx = await compound.borrow(Compound.DAI, daiScaledUp, trxOptions);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction repays 32 borrowed Dai using the repayBorrow method. This won't work unless you have an open borrow.
const compound = new Compound(window.ethereum);
(async function() {
console.log('Repaying Dai borrow...');
const address = null; // set this to any address to repayBorrowBehalf
const trx = await compound.repayBorrow(Compound.DAI, 32, address);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
Fetches the BAT price in USDC using the getPrice method.
(async function() {
// Accepts 2 args, cTokens or underlyings. Second arg defaults to USDC.
const price = await compound.getPrice(Compound.BAT);
console.log('BAT in USDC', price);
})().catch(console.error);
Fetches a relevant contract address using getAddress method.
// Accepts cTokens or underlyings. Second arg defaults to main net.
const cEthAddressRopsten = Compound.util.getAddress(Compound.cETH, 'ropsten');