Code examples for client interaction with the Fortress protocol via Fortress.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 Fortress.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 Fortress protocol using the trx method.
const oneEthInWei = '1000000000000000000';
const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
const provider = window.ethereum;
(async function() {
console.log('Supplying ETH to the Fortress Protocol...');
// Mint some cETH by supplying ETH to the Fortress Protocol
const trx = await Fortress.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 Fortress protocol using the supply method.
const fortress = new Fortress(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 Fortress protocol...');
const trx = await fortress.supply(Fortress.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
This button's transaction redeems your Ether from the Fortress protocol using the redeem method.
const fortress = new Fortress(window.ethereum);
(async function() {
console.log('Redeeming ETH...');
const trx = await fortress.redeem(Fortress.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 fortress = new Fortress(window.ethereum);
(async function() {
console.log('Entering ETH market (use as collateral)...');
const trx = await fortress.enterMarkets(Fortress.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 fortress.exitMarket(Fortress.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 fortress = new Fortress(window.ethereum);
(async function() {
const daiScaledUp = '32000000000000000000';
const trxOptions = { mantissa: true };
console.log('Borrowing 32 Dai...');
const trx = await fortress.borrow(Fortress.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 fortress = new Fortress(window.ethereum);
(async function() {
console.log('Repaying Dai borrow...');
const address = null; // set this to any address to repayBorrowBehalf
const trx = await fortress.repayBorrow(Fortress.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 fortress.getPrice(Fortress.BAT);
console.log('BAT in USDC', price);
})().catch(console.error);
Fetches a relevant contract address using getAddress method.
// Accepts fTokens or underlyings. Second arg defaults to main net.
const fDAIAddressRopsten = Fortress.util.getAddress(Fortress.fDAI, 'ropsten');