Venus.js Examples [Alpha]

Code examples for client interaction with the Venus protocol via Venus.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!

 

Ethereum Read (JSON RPC eth_call)

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 Venus.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);
 

Ethereum Send Transaction (JSON RPC eth_sendTransaction)

Create and send an Ethereum transaction with JSON RPC. This button's transaction transfers your Ether to the Venus protocol using the trx method.


const oneEthInWei = '1000000000000000000';
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
const provider = window.ethereum;

(async function() {
  console.log('Supplying ETH to the Venus Protocol...');

  // Mint some cETH by supplying ETH to the Venus Protocol
  const trx = await Venus.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);
 

Supply

This button's transaction transfers your Ether to the Venus protocol using the supply method.


const venus = new Venus(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 Venus protocol...');
  const trx = await venus.supply(Venus.ETH, 1);
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);
 

Redeem

This button's transaction redeems your Ether from the Venus protocol using the redeem method.


const venus = new Venus(window.ethereum);

(async function() {

  console.log('Redeeming ETH...');
  const trx = await venus.redeem(Venus.ETH, 1); // also accepts cToken args
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);
 

Enter Markets

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 venus = new Venus(window.ethereum);

(async function() {

  console.log('Entering ETH market (use as collateral)...');
  const trx = await venus.enterMarkets(Venus.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 venus.exitMarket(Venus.ETH);

})().catch(console.error);
 

Borrow

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 venus = new Venus(window.ethereum);

(async function() {

  const daiScaledUp = '32000000000000000000';
  const trxOptions = { mantissa: true };

  console.log('Borrowing 32 Dai...');
  const trx = await venus.borrow(Venus.DAI, daiScaledUp, trxOptions);

  console.log('Ethers.js transaction object', trx);

})().catch(console.error);
 

Repay Borrow

This button's transaction repays 32 borrowed Dai using the repayBorrow method. This won't work unless you have an open borrow.


const venus = new Venus(window.ethereum);

(async function() {

  console.log('Repaying Dai borrow...');
  const address = null; // set this to any address to repayBorrowBehalf
  const trx = await venus.repayBorrow(Venus.DAI, 32, address);

  console.log('Ethers.js transaction object', trx);

})().catch(console.error);
 

Get Price

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 venus.getPrice(Venus.BAT);
  console.log('BAT in USDC', price);

})().catch(console.error);
 

Get Address

Fetches a relevant contract address using getAddress method.


// Accepts vTokens or underlyings. Second arg defaults to main net.
const vSXPAddressRopsten = Venus.util.getAddress(Venus.vSXP, 'ropsten');