Skip to main content

Periphery

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

But modularization comes with a cost. In order to provide a good user experience in our user interface, we have to "compose" multiple contract calls. 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. For a detailed explanation, you can refer to this guide. 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 in the sections below.

Functions

addLiquidity

function addLiquidity(
contract IHifiPool hifiPool,
uint256 underlyingOffered,
uint256 maxHTokenRequired
) external

Adds liquidity to the AMM.

Requirements:

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
underlyingOffereduint256The amount of underlying to invest.
maxHTokenRequireduint256The maximum amount of hTokens that the user is willing to accept.

borrowHToken

function borrowHToken(
contract IBalanceSheetV1 balanceSheet,
contract IHToken hToken,
uint256 borrowAmount
) external

Borrows hTokens.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
hTokencontract IHTokenThe address of the HToken contract.
borrowAmountuint256The amount of hTokens to borrow.

borrowHTokenAndAddLiquidity

function borrowHTokenAndAddLiquidity(
contract IBalanceSheetV1 balanceSheet,
contract IHifiPool hifiPool,
uint256 maxBorrowAmount,
uint256 underlyingOffered
) external

Borrows hTokens and adds liquidity to the AMM.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The 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 invest.
underlyingOffereduint256The amount of underlying to invest.

borrowHTokenAndBuyUnderlying

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

Borrows hTokens and buys underlying.

Emits a {BorrowHTokenAndBuyUnderlying} event.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The 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.

borrowHTokenAndSellHToken

function borrowHTokenAndSellHToken(
contract IBalanceSheetV1 balanceSheet,
contract IHifiPool hifiPool,
uint256 borrowAmount,
uint256 minUnderlyingOut
) external

Borrows hTokens and sells them.

Emits a {BorrowHTokenAndSellHToken} event.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
borrowAmountuint256The exact amount of hTokens to borrow and sell.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.

buyHToken

function buyHToken(
contract IHifiPool hifiPool,
uint256 hTokenOut,
uint256 maxUnderlyingIn
) external

Buys hTokens with underlying.

Requirements:

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
hTokenOutuint256The exact amount of hTokens that the user wants to buy.
maxUnderlyingInuint256The maximum amount of underlying that the user is willing to pay.

buyHTokenAndAddLiquidity

function buyHTokenAndAddLiquidity(
contract IHifiPool hifiPool,
uint256 hTokenOut,
uint256 maxUnderlyingAmount
) external

Buys hTokens and adds liquidity to the AMM.

Requirements:

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
hTokenOutuint256The amount of hTokens to buy.
maxUnderlyingAmountuint256The maximum amount of underlying that the user is willing to sell and invest.

buyHTokenAndRepayBorrow

function buyHTokenAndRepayBorrow(
contract IHifiPool hifiPool,
contract IBalanceSheetV1 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 IBalanceSheetV1The 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.

buyUnderlying

function buyUnderlying(
contract IHifiPool hifiPool,
uint256 underlyingOut,
uint256 maxHTokenIn
) external

Buys underlying with hTokens.

Requirements:

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
underlyingOutuint256The exact amount of underlying that the user wants to buy.
maxHTokenInuint256The maximum amount of hTokens that the user is willing to pay.

buyUnderlyingAndAddLiquidity

function buyUnderlyingAndAddLiquidity(
contract IHifiPool hifiPool,
uint256 maxHTokenAmount,
uint256 underlyingOffered
) external

Buys underlying and adds liquidity to the AMM.

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
maxHTokenAmountuint256maxHTokenAmount The maximum amount of hTokens that the user is willing to invest.
underlyingOffereduint256The amount of underlying to invest.

depositCollateral

function depositCollateral(
contract IBalanceSheetV1 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 IBalanceSheetV1The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
depositAmountuint256The amount of collateral to deposit.

depositCollateralAndBorrowHToken

function depositCollateralAndBorrowHToken(
contract IBalanceSheetV1 balanceSheet,
contract IErc20 collateral,
contract IHToken hToken,
uint256 depositAmount,
uint256 borrowAmount
) external

Deposits collateral into the vault and borrows hTokens.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
hTokencontract IHTokenThe address of the HToken contract.
depositAmountuint256The amount of collateral to deposit.
borrowAmountuint256The amount of hTokens to borrow.

depositCollateralAndBorrowHTokenAndAddLiquidity

function depositCollateralAndBorrowHTokenAndAddLiquidity(
contract IBalanceSheetV1 balanceSheet,
contract IErc20 collateral,
contract IHifiPool hifiPool,
uint256 depositAmount,
uint256 maxBorrowAmount,
uint256 underlyingOffered
) external

Deposits collateral into the vault, borrows hTokens and adds liquidity to the AMM.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
depositAmountuint256The amount of collateral to deposit.
maxBorrowAmountuint256The amount of hTokens to borrow and the max amount that the user is willing to invest.
underlyingOffereduint256The amount of underlying to invest.

depositCollateralAndBorrowHTokenAndSellHToken

function depositCollateralAndBorrowHTokenAndSellHToken(
contract IBalanceSheetV1 balanceSheet,
contract IErc20 collateral,
contract IHifiPool hifiPool,
uint256 depositAmount,
uint256 borrowAmount,
uint256 minUnderlyingOut
) external

Deposits collateral into the vault, borrows hTokens and sells them.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
depositAmountuint256The amount of collateral to deposit.
borrowAmountuint256The exact amount of hTokens to borrow.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.

depositUnderlyingAsCollateralAndBorrowHTokenAndAddLiquidity

function depositUnderlyingAsCollateralAndBorrowHTokenAndAddLiquidity(
contract IBalanceSheetV1 balanceSheet,
contract IHifiPool hifiPool,
uint256 depositAmount,
uint256 underlyingOffered
) external

Deposits underlying as collateral into the vault, borrows hTokens and adds liquidity to the AMM.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
depositAmountuint256The amount of underlying to deposit as collateral.
underlyingOffereduint256The amount of underlying to invest.

redeemHToken

function redeemHToken(
contract IHToken hToken,
uint256 hTokenAmount
) external

Redeems hTokens for underlying.

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 redeem.

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.

removeLiquidityAndRedeemHToken

function removeLiquidityAndRedeemHToken(
contract IHifiPool hifiPool,
uint256 poolTokensBurned
) external

Removes liquidity from the AMM, and redeems all hTokens for underlying.

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.

removeLiquidityAndRepayBorrowAndWithdrawCollateral

function removeLiquidityAndRepayBorrowAndWithdrawCollateral(
contract IHifiPool hifiPool,
contract IBalanceSheetV1 balanceSheet,
contract IErc20 collateral,
uint256 poolTokensBurned,
uint256 repayAmount,
uint256 withdrawAmount
) external

Removes liquidity from the AMM, repays the borrow and withdraws collateral.

Requirements:

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

Parameters

NameTypeDescription
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
collateralcontract IErc20The address of the collateral contract.
poolTokensBurneduint256The amount of LP tokens to burn.
repayAmountuint256The amount of hTokens to repay.
withdrawAmountuint256The amount of collateral to withdraw.

removeLiquidityAndSellHToken

function removeLiquidityAndSellHToken(
contract IHifiPool hifiPool,
uint256 poolTokensBurned,
uint256 minUnderlyingOut
) external

Removes liquidity from the AMM, and sells all hTokens for underlying.

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.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.

repayBorrow

function repayBorrow(
contract IBalanceSheetV1 balanceSheet,
contract IHToken hToken,
uint256 repayAmount
) external

Repays the hToken borrow.

Requirements:

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

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
hTokencontract IHTokenThe address of the HToken contract.
repayAmountuint256The amount of hTokens to repay.

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.

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 IBalanceSheetV1 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 IBalanceSheetV1The 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.

supplyUnderlying

function supplyUnderlying(
contract IHToken hToken,
uint256 underlyingAmount
) external

Supplies the underlying to mint hTokens.

Requirements:

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

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
underlyingAmountuint256The amount of underlying to supply.

supplyUnderlyingAndRepayBorrow

function supplyUnderlyingAndRepayBorrow(
contract IHToken hToken,
contract IBalanceSheetV1 balanceSheet,
uint256 underlyingAmount
) external

Supplies underlying to mint hTokens and repay the hToken borrow.

Requirements:

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

Parameters

NameTypeDescription
hTokencontract IHTokenThe address of the HToken contract.
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
underlyingAmountuint256The amount of underlying to supply.

withdrawCollateral

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

Withdraws collateral from the vault.

Parameters

NameTypeDescription
balanceSheetcontract IBalanceSheetV1The 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 IBalanceSheetV1 balanceSheet
) external

Wraps ETH into WETH and make 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 IBalanceSheetV1The address of the BalanceSheet contract.

wrapEthAndDepositAndBorrowHTokenAndSellHToken

function wrapEthAndDepositAndBorrowHTokenAndSellHToken(
contract WethInterface weth,
contract IBalanceSheetV1 balanceSheet,
contract IHifiPool hifiPool,
uint256 borrowAmount,
uint256 minUnderlyingOut
) external

Wraps ETH into WETH, deposits collateral into the vault, borrows hTokens and sells them.

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

Parameters

NameTypeDescription
wethcontract WethInterfaceThe address of the WETH contract.
balanceSheetcontract IBalanceSheetV1The address of the BalanceSheet contract.
hifiPoolcontract IHifiPoolThe address of the HifiPool contract.
borrowAmountuint256The exact amount of hTokens to borrow and sell for underlying.
minUnderlyingOutuint256The minimum amount of underlying that the user is willing to accept.

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.

BorrowHTokenAndSellHToken

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

Emitted when hTokens are borrowed and sold for underlying.

Parameters:

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