bToken Functions

Mint

The mint function transfers an asset into the protocol, which begins accumulating interest based on the current Supply Rate for the asset. The user receives an amount of bTokens equal to the underlying tokens supplied, divided by the current Exchange Rate.

BBrc20

function mint(uint mintAmount) returns (uint)
  • msg.sender: The account which shall supply the asset, and own the minted bTokens.

  • mintAmount: The amount of the asset to be supplied, in units of the underlying asset.

  • RETURN: 0 on success, otherwise an Error code Before supplying an asset, users must first approve the bToken to access their token balance.

BBtt

function mint() payable
  • msg.value: The amount of ether to be supplied, in wei.

  • msg.sender: The account which shall supply the ether, and own the minted bTokens.

  • RETURN: No return, reverts on error.

Solidity

Erc20 underlying = Erc20(0xToken...);     // get a handle for the underlying asset contract
BBrc20 bToken = BBrc20(0x3FDA...);        // get a handle for the corresponding bToken contract
underlying.approve(address(bToken), 100); // approve the transfer
assert(bToken.mint(100) == 0);            // mint the bTokens and assert there is no error

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
await bToken.methods.mint().send({from: myAccount, value: 50});

Redeem

The redeem function converts a specified quantity of bTokens into the underlying asset, and returns them to the user. The amount of underlying tokens received is equal to the quantity of bTokens redeemed, multiplied by the current Exchange Rate. The amount redeemed must be less than the user's Account Liquidity and the market's available liquidity.

BBrc20 / BBtt

function redeem(uint redeemTokens) returns (uint)
  • msg.sender: The account to which redeemed funds shall be transferred.

  • redeemTokens: The number of bTokens to be redeemed.

  • RETURN: 0 on success, otherwise an Error code

Solidity

BBtt bToken = BBtt(0x3FDB...);
require(bToken.redeem(7) == 0, "something went wrong");

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
bToken.methods.redeem(1).send({from: ...});

Redeem Underlying

The redeem underlying function converts bTokens into a specified quantity of the underlying asset, and returns them to the user. The amount of bTokens redeemed is equal to the quantity of underlying tokens received, divided by the current Exchange Rate. The amount redeemed must be less than the user's Account Liquidity and the market's available liquidity.

BBrc20 / BBtt

function redeemUnderlying(uint redeemAmount) returns (uint)
  • msg.sender: The account to which redeemed funds shall be transferred.

  • redeemAmount: The amount of underlying to be redeemed.

  • RETURN: 0 on success, otherwise an Error code

Solidity

BBtt bToken = BBtt(0x3FDB...);
require(bToken.redeemUnderlying(50) == 0, "something went wrong");

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
bToken.methods.redeemUnderlying(10).send({from: ...});

Borrow

The borrow function transfers an asset from the protocol to the user, and creates a borrow balance which begins accumulating interest based on the Borrow Rate for the asset. The amount borrowed must be less than the user's Account Liquidity and the market's available liquidity. To borrow Ether, the borrower must be 'payable' (solidity).

BBrc20 / BBtt

function borrow(uint borrowAmount) returns (uint)
  • msg.sender: The account to which borrowed funds shall be transferred.

  • borrowAmount : The amount of the underlying asset to be borrowed.

  • RETURN: 0 on success, otherwise an Error code

Solidity

BBrc20 bToken = BBrc20(0x3FDA...);
require(bToken.borrow(100) == 0, "got collateral?");

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
await bToken.methods.borrow(50).send({from: 0xMyAccount});

Repay Borrow

The repay function transfers an asset into the protocol, reducing the user's borrow balance.

BBrc20

function repayBorrow(uint repayAmount) returns (uint)
  • msg.sender: The account which borrowed the asset, and shall repay the borrow.

  • repayAmount: The amount of the underlying borrowed asset to be repaid. A value of -1 (i.e. 2^256 - 1) can be used to repay the full amount.

  • RETURN: 0 on success, otherwise an Error code Before repaying an asset, users must first approve the bToken to access their token balance.

BBtt

function repayBorrow() payable
  • msg.value: The amount of ether to be repaid, in wei.

  • msg.sender: The account which borrowed the asset, and shall repay the borrow.

  • RETURN: No return, reverts on error.

Solidity

BBtt bToken = BBtt(0x3FDB...);
require(bToken.repayBorrow.value(100)() == 0, "transfer approved?");

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
bToken.methods.repayBorrow(10000).send({from: ...});

Repay Borrow Behalf

The repay function transfers an asset into the protocol, reducing the target user's borrow balance.

BBrc20

function repayBorrowBehalf(address borrower, uint repayAmount) returns (uint)
  • msg.sender: The account which shall repay the borrow.

  • borrower: The account which borrowed the asset to be repaid.

  • repayAmount: The amount of the underlying borrowed asset to be repaid. A value of -1 (i.e. 2^256 - 1) can be used to repay the full amount.

  • RETURN: 0 on success, otherwise an Error code Before repaying an asset, users must first approve the bToken to access their token balance.

BBtt

function repayBorrowBehalf(address borrower) payable
  • msg.value: The amount of ether to be repaid, in wei.

  • msg.sender: The account which shall repay the borrow.

  • borrower: The account which borrowed the asset to be repaid.

  • RETURN: No return, reverts on error.

Solidity

BBtt bToken = BBtt(0x3FDB...);
require(bToken.repayBorrowBehalf.value(100)(0xBorrower) == 0, "transfer approved?");

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
await bToken.methods.repayBorrowBehalf(0xBorrower, 10000).send({from: 0xPayer});

Transfer

Transfer is an BRC-20 method that allows accounts to send tokens to other Ethereum addresses. A bToken transfer will fail if the account has entered that bToken market and the transfer would have put the account into a state of negative liquidity.

BBrc20 / BBtt

function transfer(address recipient, uint256 amount) returns (bool)
  • recipient: The transfer recipient address.

  • amount: The amount of bTokens to transfer.

  • RETURN: Returns a boolean value indicating whether or not the operation succeeded.

Solidity

BBtt bToken = BBtt(0x3FDB...);
bToken.transfer(0xABCD..., 100000000000);

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
await bToken.methods.transfer(0xABCD..., 100000000000).send({from: 0xSender});

Liquidate Borrow

A user who has negative account liquidity is subject to liquidation by other users of the protocol to return his/her account liquidity back to positive (i.e. above the collateral requirement). When a liquidation occurs, a liquidator may repay some or all of an outstanding borrow on behalf of a borrower and in return receive a discounted amount of collateral held by the borrower; this discount is defined as the liquidation incentive. A liquidator may close up to a certain fixed percentage (i.e. close factor) of any individual outstanding borrow of the underwater account. Unlike in v1, liquidators must interact with each bToken contract in which they wish to repay a borrow and seize another asset as collateral. When collateral is seized, the liquidator is transferred bTokens, which they may redeem the same as if they had supplied the asset themselves. Users must approve each bToken contract before calling liquidate (i.e. on the borrowed asset which they are repaying), as they are transferring funds into the contract.

BBrc20

function liquidateBorrow(address borrower, uint amount, address collateral) returns (uint)
  • msg.sender: The account which shall liquidate the borrower by repaying their debt and seizing their collateral.

  • borrower: The account with negative account liquidity that shall be liquidated.

  • repayAmount: The amount of the borrowed asset to be repaid and converted into collateral, specified in units of the underlying borrowed asset.

  • bTokenCollateral: The address of the bToken currently held as collateral by a borrower, that the liquidator shall seize.

  • RETURN: 0 on success, otherwise an Error code Before supplying an asset, users must first approve the bToken to access their token balance.

BBtt

function liquidateBorrow(address borrower, address bTokenCollateral) payable
  • msg.value: The amount of ether to be repaid and converted into collateral, in wei.

  • msg.sender: The account which shall liquidate the borrower by repaying their debt and seizing their collateral.

  • borrower: The account with negative account liquidity that shall be liquidated.

  • bTokenCollateral: The address of the bToken currently held as collateral by a borrower, that the liquidator shall seize.

  • RETURN: No return, reverts on error.

Solidity

BBtt bToken = BBtt(0x3FDB...);
BBrc20 bTokenCollateral = BBrc20(0x3FDA...);
require(bToken.liquidateBorrow.value(100)(0xBorrower, bTokenCollateral) == 0, "borrower underwater??");

Web3 1.0

const bToken = BBrc20.at(0x3FDA...);
const bTokenCollateral = BBtt.at(0x3FDB...);
await bToken.methods.liquidateBorrow(0xBorrower, 33, bTokenCollateral).send({from: 0xLiquidator});

Key Events

Error Codes

Failure Info

Exchange Rate

Each bToken is convertible into an ever increasing quantity of the underlying asset, as interest accrues in the market. The exchange rate between a bToken and the underlying asset is equal to:

exchangeRate = (getCash() + totalBorrows() - totalReserves()) / totalSupply()

BBrc20 / BBtt

function exchangeRateCurrent() returns (uint)
  • RETURN: The current exchange rate as an unsigned integer, scaled by 1 * 10^(18 - 8 + Underlying Token Decimals).

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint exchangeRateMantissa = bToken.exchangeRateCurrent();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const exchangeRate = (await bToken.methods.exchangeRateCurrent().call()) / 1e18;

Tip: note the use of call vs. send to invoke the function from off-chain without incurring gas costs.

Get Cash

Cash is the amount of underlying balance owned by this bToken contract. One may query the total amount of cash currently available to this market.

BBrc20 / BBtt

function getCash() returns (uint)
  • RETURN: The quantity of underlying asset owned by the contract.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint cash = bToken.getCash();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const cash = (await bToken.methods.getCash().call());

Total Borrows

Total Borrows is the amount of underlying currently loaned out by the market, and the amount upon which interest is accumulated to suppliers of the market.

BBrc20 / BBtt

function totalBorrowsCurrent() returns (uint)
  • RETURN: The total amount of borrowed underlying, with interest.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint borrows = bToken.totalBorrowsCurrent();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const borrows = (await bToken.methods.totalBorrowsCurrent().call());

Borrow Balance

A user who borrows assets from the protocol is subject to accumulated interest based on the current borrow rate. Interest is accumulated every block and integrations may use this function to obtain the current value of a user's borrow balance with interest.

BBrc20 / BBtt

function borrowBalanceCurrent(address account) returns (uint)
  • account: The account which borrowed the assets.

  • RETURN: The user's current borrow balance (with interest) in units of the underlying asset.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint borrows = bToken.borrowBalanceCurrent(msg.caller);

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const borrows = await bToken.methods.borrowBalanceCurrent(account).call();

Borrow Rate

At any point in time one may query the contract to get the current borrow rate per block.

BBrc20 / BBtt

function borrowRatePerBlock() returns (uint)
  • RETURN: The current borrow rate as an unsigned integer, scaled by 1e18.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint borrowRateMantissa = bToken.borrowRatePerBlock();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const borrowRate = (await bToken.methods.borrowRatePerBlock().call()) / 1e18;

Total Supply

Total Supply is the number of tokens currently in circulation in this bToken market. It is part of the EIP-20 interface of the bToken contract.

BBrc20 / BBtt

function totalSupply() returns (uint)
  • RETURN: The total number of tokens in circulation for the market.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint tokens = bToken.totalSupply();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const tokens = (await bToken.methods.totalSupply().call());

Underlying Balance

The user's underlying balance, representing their assets in the protocol, is equal to the user's bToken balance multiplied by the Exchange Rate.

BBrc20 / BBtt

function balanceOfUnderlying(address account) returns (uint)
  • account: The account to get the underlying balance of.

  • RETURN: The amount of underlying currently owned by the account.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint tokens = bToken.balanceOfUnderlying(msg.caller);

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const tokens = await bToken.methods.balanceOfUnderlying(account).call();

Supply Rate

At any point in time one may query the contract to get the current supply rate per block. The supply rate is derived from the borrow rate, reserve factor and the amount of total borrows.

BBrc20 / BBtt

function supplyRatePerBlock() returns (uint)
  • RETURN: The current supply rate as an unsigned integer, scaled by 1e18.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint supplyRateMantissa = bToken.supplyRatePerBlock();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const supplyRate = (await bToken.methods.supplyRatePerBlock().call()) / 1e18;

Total Reserves

Reserves are an accounting entry in each bToken contract that represents a portion of historical interest set aside as cash which can be withdrawn or transferred through the protocol's governance. A small portion of borrower interest accrues into the protocol, determined by the reserve factor.

BBrc20 / BBtt

function totalReserves() returns (uint)
  • RETURN: The total amount of reserves held in the market.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint reserves = bToken.totalReserves();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const reserves = (await bToken.methods.totalReserves().call());

Reserve Factor

The reserve factor defines the portion of borrower interest that is converted into reserves.

BBrc20 / BBtt

function reserveFactorMantissa() returns (uint)
  • RETURN: The current reserve factor as an unsigned integer, scaled by 1e18.

Solidity

BBrc20 bToken = BToken(0x3FDA...);
uint reserveFactorMantissa = bToken.reserveFactorMantissa();

Web3 1.0

const bToken = BBtt.at(0x3FDB...);
const reserveFactor = (await bToken.methods.reserveFactorMantissa().call()) / 1e18;

Last updated