Introduction
The BalanceSheet is the global debt registry of Hifi. All users who borrow fyTokens have to first open a vault bespoke to that fyToken contract. All vaults are recorded in and managed via the BalanceSheet.
Gas Costs
Gas usage is not deterministic due to requisite calls to third-party Erc20 tokens. We provide the table below for guidance only:
<110K if partial, <40K otherwise
Storage Properties
Fintroller
The unique Fintroller associated with this contract.
Copy FintrollerInterface public fintroller;
Constant Functions
Get Clutchable Collateral
Determines the amount of collateral that can be clutched when liquidating a borrow according to the formula:
c l u t c h e d C o l l a t e r a l = r e p a y A m o u n t ∗ l i q u i d a t i o n I n c e n t i v e ∗ u n d e r l y i n g P r i c e U s d c o l l a t e r a l P r i c e U s d clutchedCollateral = \frac {repayAmount * liquidationIncentive * underlyingPriceUsd} {collateralPriceUsd} c l u t c h e d C o ll a t er a l = co ll a t er a lP r i ce U s d re p a y A m o u n t ∗ l i q u i d a t i o n I n ce n t i v e ∗ u n d er l y in g P r i ce U s d
Copy function getClutchableCollateral(FyTokenInterface fyToken, uint256 repayAmount) external view returns (uint256)
fyToken
: The fyToken to make the query against.
repayAmount
: The amount of fyTokens to repay (must be non-zero).
RETURN
: The amount of clutchable collateral as uint256, specified in the collateral's decimal system.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
uint256 repayAmount = 10000000000000000000;
uint256 clutchableCollateral = balanceSheet.getClutchableCollateral(fyToken, repayAmount);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const repayAmount = "10000000000000000000";
const clutchableCollateral = await balanceSheet.getClutchableCollateral(fyToken, repayAmount);
Get Current Collateralization Ratio
Determines the current collateralization ratio for the given borrower account.
Copy function getCurrentCollateralizationRatio(FyTokenInterface fyToken, address borrower) public view returns (uint256)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: A quotient if locked collateral is non-zero, otherwise zero.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
uint256 currentCollateralizationRatio = balanceSheet.getCurrentCollateralizationRatio(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const currentCollateralizationRatio = await balanceSheet.getCurrentCollateralizationRatio(fyToken, borrower);
Get Hypothetical Collateralization Ratio
Determines the hypothetical collateralization ratio for the given locked collateral and debt, at the current prices provided by the oracle according to the formula:
c o l l a t e r a l i z a t i o n R a t i o = l o c k e d C o l l a t e r a l V a l u e U s d d e b t V a l u e U s d collateralizationRatio = \frac {lockedCollateralValueUsd} {debtValueUsd} co ll a t er a l i z a t i o n R a t i o = d e b t Va l u e U s d l oc k e d C o ll a t er a l Va l u e U s d
Copy function getHypotheticalCollateralizationRatio(FyTokenInterface fyToken, address borrower, uint256 lockedCollateral, uint256 debt) public view returns (uint256)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
lockedCollateral
: The hypothetical locked collateral.
debt
: The hypothetical debt.
RETURN
: The hypothetical collateralization ratio as a percentage mantissa if locked collateral is non-zero, otherwise zero.
Requirements
The oracle prices must be non-zero.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
address lockedCollateral = 10000000000000000000;
address debt = 5000000000000000000;
uint256 hypotheticalCollateralizationRatio = balanceSheet.getHypotheticalCollateralizationRatio(fyToken, borrower, lockedCollateral, debt);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const lockedCollateral = "10000000000000000000";
const debt = "5000000000000000000";
const hypotheticalCollateralizationRatio = await balanceSheet.getHypotheticalCollateralizationRatio(fyToken, borrower, lockedCollateral, debt);
Get Vault
Reads all the properties of a vault.
Copy function getVault(FyTokenInterface fyToken, address borrower) external view returns (uint256, uint256, uint256, bool)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: The vault with all its properties.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
(uint256 debt, uint256 freeCollateral, uint256 lockedCollateral, bool isOpen) = balanceSheet.getVault(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const vault = await balanceSheet.getVault(fyToken, borrower);
Get Vault Debt
Reads the debt held by the given account.
Copy function getVaultDebt(FyTokenInterface fyToken, address borrower) external view returns (uint256)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: The debt held by the borrower, as an uint256.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
uint256 vaultDebt = balanceSheet.getVaultDebt(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const vaultDebt = await balanceSheet.getVaultDebt(fyToken, borrower);
Get Vault Locked Collateral
Reads the amount of collateral that the given borrower account locked in the vault.
Copy function getVaultLockedCollateral(FyTokenInterface fyToken, address borrower) external view returns (uint256)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: The collateral locked in the vault by the borrower, as an uint256.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
uint256 vaultLockedCollateral = balanceSheet.getVaultLockedCollateral(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const vaultLockedCollateral = await balanceSheet.getVaultLockedCollateral(fyToken, borrower);
Is Account Underwater
Checks whether the borrower account can be liquidated or not.
Copy function isAccountUnderwater(FyTokenInterface fyToken, address borrower) external view returns (bool)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: true = is underwater, otherwise not.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
uint256 isAccountUnderwater = balanceSheet.isAccountUnderwater(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const isAccountUnderwater = await balanceSheet.isAccountUnderwater(fyToken, borrower);
Is Vault Open
Checks whether the borrower account has a vault opened for a particular fyToken.
Copy function isVaultOpen(FyTokenInterface fyToken, address borrower) external view returns (bool)
fyToken
: The fyToken to make the query against.
borrower
: The borrower account to make the query against.
RETURN
: true = is open, otherwise not.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
address borrower = 0x1234;
uint256 isVaultOpen = balanceSheet.isVaultOpen(fyToken, borrower);
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const borrower = 0x1234;
const isVaultOpen = await balanceSheet.isVaultOpen(fyToken, borrower);
Non-Constant Functions
Clutch Collateral
Transfers the collateral from the borrower's vault to the liquidator account. Emits a "ClutchCollateral" event.
Copy function clutchCollateral(FyTokenInterface fyToken, address liquidator, address borrower, uint256 collateralAmount ) external returns (bool)
fyToken
: The address of the fyToken contract.
liquidator
: The account who repays the borrower's debt and receives the collateral.
borrower
: The account who fell underwater and is liquidated.
collateralAmount
: The amount of collateral to clutch, specified in the collateral's decimal system.
RETURN
: true = success, otherwise it reverts
Requirements:
Can only be called by the fyToken contract.
There must be enough collateral in the borrower's vault.
Deposit Collateral
Deposits collateral into the account's vault. Emits a "DepositCollateral" event.
Copy function depositCollateral(FyTokenInterface fyToken, uint256 collateralAmount) external returns (bool)
fyToken
: The address of the fyToken contract.
collateralAmount
: The amount of collateral to deposit.
RETURN
: true = success, otherwise it reverts.
Requirements:
The amount to deposit cannot be zero.
The Fintroller must allow this action to be performed.
The caller must have allowed this contract to spend "collateralAmount" tokens.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheetInterface(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
uint256 collateralAmount = 10000000000000000000;
bool success = balanceSheet.depositCollateral(fyToken, collateralAmount);
require(success, "something went wrong");
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const collateralAmount = "10000000000000000000";
const depositCollateralTx = await balanceSheet.depositCollateral(fyToken, collateralAmount);
await depositCollateralTx.wait();
Free Collateral
Frees a portion or all of the locked collateral. Emits a "FreeCollateral" event.
Copy function freeCollateral(FyTokenInterface fyToken, uint256 collateralAmount) external returns (bool)
fyToken
: The address of the fyToken contract.
collateralAmount
: The amount of locked collateral to free.
RETURN
: true = success, otherwise it reverts.
Requirements:
The amount to free cannot be zero.
There must be enough locked collateral.
The borrower account cannot fall below the collateralization ratio.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheetInterface(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
uint256 collateralAmount = 10000000000000000000;
bool success = balanceSheet.freeCollateral(fyToken, collateralAmount);
require(success, "something went wrong");
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const collateralAmount = "10000000000000000000";
const freeCollateralTx = await balanceSheet.freeCollateral(fyToken, collateralAmount);
await freeCollateralTx.wait();
Lock Collateral
Locks a portion or all of the free collateral to make it eligible for borrowing. Emits a "LockCollateral" event.
Copy function lockCollateral(FyTokenInterface fyToken, uint256 collateralAmount) external returns (bool)
fyToken
: The address of the fyToken contract.
collateralAmount
: The amount of free collateral to lock.
RETURN
: true = success, otherwise it reverts.
Requirements:
The amount to lock cannot be zero.
There must be enough free collateral.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheetInterface(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
uint256 collateralAmount = 10000000000000000000;
bool success = balanceSheet.lockCollateral(fyToken, collateralAmount);
require(success, "something went wrong");
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const collateralAmount = "10000000000000000000";
const lockCollateralTx = await balanceSheet.lockCollateral(fyToken, collateralAmount);
await lockCollateralTx.wait();
Open Vault
Opens a Vault for the caller. Emits an "OpenVault" event.
Copy function openVault(FyTokenInterface fyToken) external returns (bool)
fyToken
: The address of the fyToken contract for which to open the vault.
RETURN
: true = success, otherwise it reverts.
Requirements:
The vault cannot be already open.
The fyToken must pass the inspection.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheetInterface(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
bool success = balanceSheet.openVault(fyToken);
require(success, "something went wrong");
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const openVaultTx = await balanceSheet.openVault(fyToken);
await openVaultTx.wait();
Set Vault Debt
Updates the debt accrued by a particular borrower account. Emits a "SetVaultDebt" event.
Copy function setVaultDebt(FyTokenInterface fyToken, address borrower, uint256 newVaultDebt) external returns (bool)
fyToken
: The address of the fyToken contract.
borrower
: The borrower account for which to update the debt.
newVaultDebt
: The new debt to assign to the borrower account.
RETURN
: true = success, otherwise it reverts.
Requirements:
Can only be called by the fyToken.
Withdraw Collateral
Withdraws a portion or all of the free collateral. Emits a "WithdrawCollateral" event.
Copy function withdrawCollateral(FyTokenInterface fyToken, uint256 collateralAmount) external returns (bool)
fyToken
: The address of the fyToken contract.
collateralAmount
: The amount of collateral to withdraw.
RETURN
: true = success, otherwise it reverts.
Requirements
The amount to withdraw cannot be zero.
There must be enough free collateral in the vault.
Solidity
Copy BalanceSheet balanceSheet = BalanceSheet(0xabcd...);
FyTokenInterface fyToken = FyTokenInterface(0xbcde...);
uint256 collateralAmount = 10000000000000000000;
bool success = balanceSheet.withdrawCollateral(fyToken, collateralAmount);
require(success, "something went wrong");
Ethers.js
Copy const balanceSheet = new ethers.Contract(0xabcd..., balanceSheetABI, signer);
const fyToken = new ethers.Contract(0xbcde..., fyTokenInterfaceABI, signer);
const collateralAmount = "10000000000000000000";
const withdrawCollateralTx = await balanceSheet.withdrawCollateral(fyToken, collateralAmount);
await withdrawCollateralTx.wait();