IonHandlerBase

Git Source

The base handler contract for simpler interactions with the IonPool core contract. It combines various individual interactions into one compound interaction to facilitate reaching user end-goals in atomic fashion.

To actually borrow from IonPool, a user must submit a "normalized" borrow amount. This contract is designed to be user-intuitive and, thus, allows a user to submit a standard desired borrow amount, which this contract will then convert into to the appropriate "normalized" borrow amount.

State Variables

BASE

IERC20 public immutable BASE;

WETH

IWETH9 public immutable WETH;

ILK_INDEX

uint8 public immutable ILK_INDEX;

POOL

IonPool public immutable POOL;

JOIN

GemJoin public immutable JOIN;

LST_TOKEN

IERC20 public immutable LST_TOKEN;

WHITELIST

Whitelist public immutable WHITELIST;

Functions

checkDeadline

Checks if the tx is being executed before the designated deadline for execution.

This is used to prevent txs that have sat in the mempool for too long from executing at unintended prices.

modifier checkDeadline(uint256 deadline);

onlyWhitelistedBorrowers

Checks if msg.sender is on the whitelist.

This contract will be on the protocolControlledWhitelist. As such, it will validate that users are on the whitelist itself and be able to bypass the whitelist check on IonPool.

modifier onlyWhitelistedBorrowers(bytes32[] calldata proof);

Parameters

constructor

Creates a new instance of IonHandlerBase

constructor(uint8 _ilkIndex, IonPool _ionPool, GemJoin _gemJoin, Whitelist _whitelist);

Parameters

depositAndBorrow

Combines gem-joining and depositing collateral and then borrowing into one compound action.

function depositAndBorrow(
    uint256 amountCollateral,
    uint256 amountToBorrow,
    bytes32[] calldata proof
)
    external
    onlyWhitelistedBorrowers(proof);

Parameters

_depositAndBorrow

Handles all logic to gem-join and deposit collateral, followed by a borrow. It is also possible to use this function simply to gem-join and deposit collateral atomically by setting amountToBorrow to 0.

function _depositAndBorrow(
    address vaultHolder,
    address receiver,
    uint256 amountCollateral,
    uint256 amountToBorrow,
    AmountToBorrow amountToBorrowType
)
    internal;

Parameters

repayFullAndWithdraw

Will repay all debt and withdraw desired collateral amount. This function can also simply be used for a full repayment (which may be difficult through a direct tx to the IonPool) by setting collateralToWithdraw to 0.

Will repay the debt belonging to msg.sender. This function is necessary because with rate updating every single block, it may be difficult to repay a full amount if a user uses the total debt from a previous block. If a user ends up repaying all but dust amounts of debt (due to a slight rate change), then they repayment will likely fail due to the dust parameter.

function repayFullAndWithdraw(uint256 collateralToWithdraw) external;

Parameters

_getFullRepayAmount

Helper function to get the repayment amount for all the debt of a user.

This simply emulates the rounding behaviour of the IonPool to arrive at an accurate value.

function _getFullRepayAmount(address user) internal view returns (uint256 repayAmount, uint256 normalizedDebt);

Parameters

Returns

repayAndWithdraw

Combines repaying debt and then withdrawing and gem-exitting collateral into one compound action. If repaying all is the intention, use repayFullAndWithdraw() instead to prevent tx revert from dust amounts of debt in vault.

function repayAndWithdraw(uint256 debtToRepay, uint256 collateralToWithdraw) external;

Parameters

_repayAndWithdraw

Handles all logic to repay debt, followed by a collateral withdrawal and gem-exit. This function can also be used to just withdraw and gem-exit in atomic fashion by setting the debtToRepay to 0.

function _repayAndWithdraw(
    address vaultHolder,
    address receiver,
    uint256 collateralToWithdraw,
    uint256 debtToRepay
)
    internal;

Parameters

receive

ETH cannot be directly sent to this contract.

To allow unwrapping of WETH into ETH.

receive() external payable;

Errors

CannotSendEthToContract

error CannotSendEthToContract();

FlashloanRepaymentTooExpensive

error FlashloanRepaymentTooExpensive(uint256 repaymentAmount, uint256 maxRepaymentAmount);

TransactionDeadlineReached

error TransactionDeadlineReached(uint256 deadline);

Enums

AmountToBorrow

During conversion from borrow amount -> "normalized" borrow amount," there is division required. In certain scenarios, it may be desirable to round up during division, in others, to round down. This enum allows a developer to indicate the rounding direction by describing the amountToBorrow. If it IS_MIN, then the final borrowed amount should be larger than amountToBorrow (round up), and vice versa for IS_MAX (round down).

enum AmountToBorrow {
    IS_MIN,
    IS_MAX
}