Skip to main content

Periphery

The Hifi protocol is architected such that every function does one job and job only. In software engineering parlance, this practice is called separation of concerns, a design principle which makes it easier to reason about how the protocol behaves. Most importantly, it gives the protocol better security guarantees.

Modularization comes with a cost, though. It forces us to "compose" multiple contract calls, if we wish to provide a good user experience in our user interface. This is where DSProxy comes into play - a smart contract wallet designed to address this need.

For brevity, we won't expound on the technical properties of DSProxy here. You can refer to this guide for a detailed explanation. In short:

  1. DSProxy is a smart contract wallet.
  2. There is a so-called "target contract" that contains composite scripts, toward which the DSProxy delegate calls.

Is it the target contract that is documented herein.

Functions

borrowHTokenAndBuyUnderlying

function borrowHTokenAndBuyUnderlying(
contract IBalanceSheetV2 balanceSheet,
contract IHifiPool hifiPool,
uint256 maxBorrowAmount,
uint256 underlyingOut
) external

Borrows hTokens and buys underlying.

Emits a {BorrowHTokenAndBuyUnderlying} event.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
maxBorrowAmountuint256The amount of hTokens to borrow and the max amount that the user is willing to pay.
underlyingOutuint256The exact amount of underlying that the user wants to buy.

buyHTokenAndRepayBorrow

function buyHTokenAndRepayBorrow(
contract IHifiPool hifiPool,
contract IBalanceSheetV2 balanceSheet,
uint256 maxUnderlyingIn,
uint256 hTokenOut
) external

Buys hTokens with underlying and repays the borrow.

Requirements:

  • The caller must have allowed the DSProxy to spend maxUnderlyingIn tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
maxUnderlyingInuint256The maximum amount of underlying that the user is willing to pay.
hTokenOutuint256The exact amount of hTokens to buy and the amount to repay and the maximum amount to repay.

buyHTokenAndRepayBorrowWithSignature

function buyHTokenAndRepayBorrowWithSignature(
contract IHifiPool hifiPool,
contract IBalanceSheetV2 balanceSheet,
uint256 maxUnderlyingIn,
uint256 hTokenOut,
uint256 deadline,
bytes signatureUnderlying
) external

Buys hTokens with underlying and repays the borrow.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend maxUnderlyingIn tokens for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
maxUnderlyingInuint256The maximum amount of underlying that the user is willing to pay.
hTokenOutuint256The exact amount of hTokens to buy and the amount to repay and the maximum amount to repay.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureUnderlyingbytesThe packed signature for the underlying.

depositCollateral

function depositCollateral(
contract IBalanceSheetV2 balanceSheet,
contract IErc20 collateral,
uint256 depositAmount
) external

Deposits collateral into the vault.

Requirements:

  • The caller must have allowed the DSProxy to spend collateralAmount tokens.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
depositAmountuint256The amount of collateral to deposit.

depositCollateralWithSignature

function depositCollateralWithSignature(
contract IBalanceSheetV2 balanceSheet,
contract IErc20Permit collateral,
uint256 depositAmount,
uint256 deadline,
bytes signatureCollateral
) external

Deposits collateral into the vault using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend depositAmount tokens for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
collateralcontract IErc20PermitThe address of the collateral contract.
depositAmountuint256The amount of collateral to deposit.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureCollateralbytesThe packed signature for the collateral.

depositUnderlyingAndMintHTokenAndAddLiquidity

function depositUnderlyingAndMintHTokenAndAddLiquidity(
contract IHifiPool hifiPool,
uint256 depositAmount,
uint256 underlyingOffered
) external

Deposits underlying in the HToken contract to mint hTokens, and adds liquidity to the AMM.

Requirements:

  • The caller must have allowed the DSProxy to spend depositAmount + underlyingOffered tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
depositAmountuint256The amount of underlying to deposit to mint equivalent amount of hTokens.
underlyingOffereduint256The amount of underlying to invest.

depositUnderlyingAndMintHTokenAndAddLiquidityWithSignature

function depositUnderlyingAndMintHTokenAndAddLiquidityWithSignature(
contract IHifiPool hifiPool,
uint256 depositAmount,
uint256 underlyingOffered,
uint256 deadline,
bytes signatureUnderlying
) external

Deposits underlying in the HToken contract to mint hTokens, and adds liquidity to the AMM using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend depositAmount + underlyingOffered for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
depositAmountuint256The amount of underlying to deposit to mint equivalent amount of hTokens.
underlyingOffereduint256The amount of underlying to invest.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureUnderlyingbytesThe packed signature for the underlying.

depositUnderlyingAndRepayBorrow

function depositUnderlyingAndRepayBorrow(
contract IHToken hToken,
contract IBalanceSheetV2 balanceSheet,
uint256 underlyingAmount
) external

Deposits underlying in the HToken contract to mint hTokens, and repays the borrow.

Requirements:

  • The caller must have allowed the DSProxy to spend underlyingAmount tokens.

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
underlyingAmountuint256The amount of underlying to deposit.

depositUnderlyingAndRepayBorrowWithSignature

function depositUnderlyingAndRepayBorrowWithSignature(
contract IHToken hToken,
contract IBalanceSheetV2 balanceSheet,
uint256 underlyingAmount,
uint256 deadline,
bytes signatureUnderlying
) external

Supplies underlying to mint hTokens and repay the hToken borrow using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend underlyingAmount for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
underlyingAmountuint256The amount of underlying to supply.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureUnderlyingbytesThe packed signature for the underlying.

redeem

function redeem(
contract IHToken hToken,
uint256 hTokenAmount,
uint256 underlyingAmount
) external

Redeems the underlying in exchange for hTokens.

Requirements:

  • The caller must have allowed the DSProxy to spend hTokenAmount hTokens.

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
hTokenAmountuint256The amount of hTokens to provide.
underlyingAmountuint256The amount of underlying to redeem.

redeemWithSignature

function redeemWithSignature(
contract IHToken hToken,
uint256 hTokenAmount,
uint256 underlyingAmount,
uint256 deadline,
bytes signatureHToken
) external

Redeems hTokens for underlying using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend hTokenAmount for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
hTokenAmountuint256The amount of hTokens to redeem.
underlyingAmountuint256The amount of underlying to redeem.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureHTokenbytesThe packed signature for hToken.

removeLiquidity

function removeLiquidity(
contract IHifiPool hifiPool,
uint256 poolTokensBurned
) external

Removes liquidity from the AMM.

Requirements:

  • The caller must have allowed the DSProxy to spend poolTokensBurned tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.

removeLiquidityAndRedeem

function removeLiquidityAndRedeem(
contract IHifiPool hifiPool,
uint256 poolTokensBurned
) external

Removes liquidity from the AMM and redeems underlying in exchange for all hTokens retrieved from the AMM.

Requirements:

  • The caller must have allowed the DSProxy to spend poolTokensBurned tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.

removeLiquidityAndRedeemWithSignature

function removeLiquidityAndRedeemWithSignature(
contract IHifiPool hifiPool,
uint256 poolTokensBurned,
uint256 deadline,
bytes signatureLPToken
) external

Removes liquidity from the AMM, and redeems all hTokens for the underlying using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend poolTokensBurned for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureLPTokenbytesThe packed signature for LP tokens.

removeLiquidityAndWithdrawUnderlying

function removeLiquidityAndWithdrawUnderlying(
contract IHifiPool hifiPool,
uint256 poolTokensBurned,
uint256 withdrawAmount
) external

Removes liquidity from the AMM, and withdraws underlying in exchange for hTokens.

Requirements:

  • The caller must have allowed the DSProxy to spend poolTokensBurned tokens.
  • Can only be called before maturation.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.
withdrawAmountuint256The amount of underlying to withdraw in exchange for hTokens.

removeLiquidityAndWithdrawUnderlyingWithSignature

function removeLiquidityAndWithdrawUnderlyingWithSignature(
contract IHifiPool hifiPool,
uint256 poolTokensBurned,
uint256 withdrawAmount,
uint256 deadline,
bytes signatureLPToken
) external

Removes liquidity from the AMM, and withdraws underlying in exchange for hTokens using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend poolTokensBurned for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.
withdrawAmountuint256The amount of underlying to withdraw in exchange for hTokens.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureLPTokenbytesThe packed signature for LP tokens.

removeLiquidityWithSignature

function removeLiquidityWithSignature(
contract IHifiPool hifiPool,
uint256 poolTokensBurned,
uint256 deadline,
bytes signatureLPToken
) external

Removes liquidity from the AMM using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend poolTokensBurned for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
poolTokensBurneduint256The amount of LP tokens to burn.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureLPTokenbytesThe packed signature for LP tokens.

sellHToken

function sellHToken(
contract IHifiPool hifiPool,
uint256 hTokenIn,
uint256 minUnderlyingOut
) external

Sells hTokens for underlying.

Requirements:

  • The caller must have allowed DSProxy to spend hTokenIn tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
hTokenInuint256The exact amount of hTokens that the user wants to sell.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.

sellHTokenWithSignature

function sellHTokenWithSignature(
contract IHifiPool hifiPool,
uint256 hTokenIn,
uint256 minUnderlyingOut,
uint256 deadline,
bytes signatureHToken
) external

Sells hTokens for underlying using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend hTokenIn hTokens for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
hTokenInuint256The exact amount of hTokens that the user wants to sell.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureHTokenbytesThe packed signature for HTokens.

sellUnderlying

function sellUnderlying(
contract IHifiPool hifiPool,
uint256 underlyingIn,
uint256 minHTokenOut
) external

Sells underlying for hTokens.

Requirements:

  • The caller must have allowed DSProxy to spend underlyingIn tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
underlyingInuint256The exact amount of underlying that the user wants to sell.
minHTokenOutuint256The minimum amount of hTokens that the user is willing to accept.

sellUnderlyingAndRepayBorrow

function sellUnderlyingAndRepayBorrow(
contract IHifiPool hifiPool,
contract IBalanceSheetV2 balanceSheet,
uint256 underlyingIn,
uint256 minHTokenOut
) external

Sells underlying for hTokens, then uses them to repay the hToken borrow.

Requirements:

  • The caller must have allowed the DSProxy to spend underlyingIn tokens.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
underlyingInuint256The exact amount of underlying that the user wants to sell.
minHTokenOutuint256The minimum amount of hTokens that the user is willing to accept and the maximum
amount to repay.

sellUnderlyingAndRepayBorrowWithSignature

function sellUnderlyingAndRepayBorrowWithSignature(
contract IHifiPool hifiPool,
contract IBalanceSheetV2 balanceSheet,
uint256 underlyingIn,
uint256 minHTokenOut,
uint256 deadline,
bytes signatureUnderlying
) external

Sells underlying for hTokens, then uses them to repay the hToken borrow using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend underlyingIn for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
underlyingInuint256The exact amount of underlying that the user wants to sell.
minHTokenOutuint256The minimum amount of hTokens that the user is willing to accept and the maximum
amount to repay.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureUnderlyingbytesThe packed signature for the underlying.

sellUnderlyingWithSignature

function sellUnderlyingWithSignature(
contract IHifiPool hifiPool,
uint256 underlyingIn,
uint256 minHTokenOut,
uint256 deadline,
bytes signatureUnderlying
) external

Sells underlying for hTokens using EIP-2612 signatures.

Requirements:

  • The signature must be a valid signed approval given by the caller to the DSProxy to spend underlyingIn for the given deadline and the caller's current nonce.

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
underlyingInuint256The exact amount of underlying that the user wants to sell.
minHTokenOutuint256The minimum amount of hTokens that the user is willing to accept.
deadlineuint256The deadline beyond which the signature is not valid anymore.
signatureUnderlyingbytesThe packed signature for the underlying.

withdrawCollateral

function withdrawCollateral(
contract IBalanceSheetV2 balanceSheet,
contract IErc20 collateral,
uint256 withdrawAmount
) external

Withdraws collateral from the vault.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
withdrawAmountuint256The amount of collateral to withdraw.

wrapEthAndDepositCollateral

function wrapEthAndDepositCollateral(
contract WethInterface weth,
contract IBalanceSheetV2 balanceSheet
) external

Wraps ETH into WETH and makes a collateral deposit in the BalanceSheet contract.

This is a payable function so it can receive ETH transfers.

Parameters

NameTypeDescription
wethcontract WethInterfaceThe address of the WETH contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.

withdrawCollateralAndUnwrapWeth

function withdrawCollateralAndUnwrapWeth(
contract WethInterface weth,
contract IBalanceSheetV2 balanceSheet,
uint256 withdrawAmount
) external

Withdraws WETH from the vault and unwraps it into ETH.

Parameters

NameTypeDescription
wethcontract WethInterfaceThe address of the WETH contract.
balanceSheetcontract IBalanceSheetV2The address of the BalanceSheet contract.
withdrawAmountuint256The amount of WETH to withdraw.

Events

BorrowHTokenAndBuyUnderlying

event BorrowHTokenAndBuyUnderlying(
address borrower,
uint256 borrowAmount,
uint256 underlyingAmount
)

Emitted when hTokens are borrowed and used to buy underlying.

Parameters

NameTypeDescription
borroweraddressThe address of the borrower.
borrowAmountuint256The amount of hTokens borrowed and sold.
underlyingAmountuint256The amount of underlying bought.

Custom Errors

HifiProxyTarget__AddLiquidityHTokenSlippage

error HifiProxyTarget__AddLiquidityHTokenSlippage(uint256 expectedHTokenRequired, uint256 actualHTokenRequired)

Emitted when the hToken slippage is higher than what the user is willing to tolerate.

HifiProxyTarget__AddLiquidityUnderlyingSlippage

error HifiProxyTarget__AddLiquidityUnderlyingSlippage(uint256 expectedUnderlyingRequired, uint256 actualUnderlyingRequired)

Emitted when the underlying slippage is higher than what the user is willing to tolerate.

HifiProxyTarget__TradeSlippage

error HifiProxyTarget__TradeSlippage(uint256 expectedAmount, uint256 actualAmount)

Emitted when the slippage is higher than what the user is willing to tolerate.