Code examples for client interaction with the Rifi protocol via Rifi.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 rETH using the read method.
// mainnet
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
(async function() {
const srpb = await Rifi.eth.read(
cEthAddress,
'function supplyRatePerBlock() returns (uint256)',
// [], // [optional] parameters
// {} // [optional] call options, provider, network, plus Ethers.js "overrides"
);
console.log('rETH 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 Rifi protocol using the trx method.
const oneEthInWei = '1000000000000000000';
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
const provider = window.ethereum;
(async function() {
console.log('Supplying ETH to the Rifi Protocol...');
// Mint some rETH by supplying ETH to the Rifi Protocol
const trx = await Rifi.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 Rifi protocol using the supply method.
const rifi = new Rifi(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 Rifi protocol...');
const trx = await rifi.supply(Rifi.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction redeems your Ether from the Rifi protocol using the redeem method.
const rifi = new Rifi(window.ethereum);
(async function() {
console.log('Redeeming ETH...');
const trx = await rifi.redeem(Rifi.ETH, 1); // also accepts rToken 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 rifi = new Rifi(window.ethereum);
(async function() {
console.log('Entering ETH market (use as collateral)...');
const trx = await rifi.enterMarkets(Rifi.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 rifi.exitMarket(Rifi.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 rifi = new Rifi(window.ethereum);
(async function() {
const daiScaledUp = '32000000000000000000';
const trxOptions = { mantissa: true };
console.log('Borrowing 32 Dai...');
const trx = await rifi.borrow(Rifi.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 rifi = new Rifi(window.ethereum);
(async function() {
console.log('Repaying Dai borrow...');
const address = null; // set this to any address to repayBorrowBehalf
const trx = await rifi.repayBorrow(Rifi.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, rTokens or underlyings. Second arg defaults to USDC.
const price = await rifi.getPrice(Rifi.BAT);
console.log('BAT in USDC', price);
})().catch(console.error);
Fetches a relevant contract address using getAddress method.
// Accepts rTokens or underlyings. Second arg defaults to main net.
const cEthAddressRopsten = Rifi.util.getAddress(Rifi.rETH, 'ropsten');