Skip to main content

Hifi Pool

Dedicated AMM for market-making hTokens, based on the Yield Space design.

Constant Functions

getQuoteForBuyingHToken

function getQuoteForBuyingHToken(
uint256 hTokenOut
) external returns (uint256 underlyingIn)

Quotes how much underlying would be required to buy hTokenOut hToken.

Requirements:

  • Cannot be called after maturity.

Parameters

NameTypeDescription
hTokenOutuint256The hypothetical amount of hTokens to sell.

Return Values

NameTypeDescription
underlyingInuint256The hypothetical amount of underlying required.

getQuoteForBuyingUnderlying

function getQuoteForBuyingUnderlying(
uint256 underlyingOut
) external returns (uint256 hTokenIn)

Quotes how many hTokens would be required to buy underlyingOut underlying.

Requirements:

  • Cannot be called after maturity.

Parameters

NameTypeDescription
underlyingOutuint256The hypothetical amount of underlying desired.

Return Values

NameTypeDescription
hTokenInuint256The hypothetical amount of hTokens required.

getMintInputs

function getMintInputs(
uint256 underlyingOffered
) external returns (uint256 hTokenRequired, uint256 poolTokensMinted)

Calculates how many hTokens would be required and how many LP tokens would be issued for a given amount of underlying invested.

Parameters

NameTypeDescription
underlyingOffereduint256The amount of underlying tokens invested.

Return Values

NameTypeDescription
hTokenRequireduint256The hypothetical amount of hTokens required to mint new LP tokens.
poolTokensMinteduint256The amount of LP tokens to mint.

getBurnOutputs

function getBurnOutputs(
uint256 poolTokensBurned
) external returns (uint256 underlyingReturned, uint256 hTokenReturned)

Calculates how much underlying and hToken would be returned for a given amount of LP tokens.

Parameters

NameTypeDescription
poolTokensBurneduint256The amount of LP tokens to burn.

Return Values

NameTypeDescription
underlyingReturneduint256The amount of reserve underlying retrieved.
hTokenReturneduint256The amount of reserve hToken retrieved.

getQuoteForSellingHToken

function getQuoteForSellingHToken(
uint256 hTokenIn
) external returns (uint256 underlyingOut)

Quotes how much underlying would be obtained by selling hTokenIn hToken.

Requirements:

  • Cannot be called after maturity.

Parameters

NameTypeDescription
hTokenInuint256The hypothetical amount of hTokens to sell.

Return Values

NameTypeDescription
underlyingOutuint256The hypothetical amount of underlying that would be obtained.

getQuoteForSellingUnderlying

function getQuoteForSellingUnderlying(
uint256 underlyingIn
) external returns (uint256 hTokenOut)

Quotes how many hTokens would be obtained by selling underlyingIn underlying.

Requirements:

  • Cannot be called after maturity.

Parameters

NameTypeDescription
underlyingInuint256The hypothetical amount of underlying to sell.

Return Values

NameTypeDescription
hTokenOutuint256The hypothetical amount of hTokens that would be obtained.

getNormalizedUnderlyingReserves

function getNormalizedUnderlyingReserves() external returns (uint256 normalizedUnderlyingReserves)

Returns the normalized underlying reserves, i.e. the Erc20 balance scaled to have 18 decimals.

getVirtualHTokenReserves

function getVirtualHTokenReserves() external returns (uint256 virtualHTokenReserves)

Returns the virtual hToken reserves, as explained in the whitepaper.

Adds the Erc20 hToken balance to the total supply of LP tokens.

maturity

function maturity() external returns (uint256)

The unix timestamp at which the hToken expires.

hToken

function hToken() external returns (contract IHToken)

The hToken traded in this pool.

underlying

function underlying() external returns (contract IErc20)

The underlying token traded in this pool.

underlyingPrecisionScalar

function underlyingPrecisionScalar() external returns (uint256)

The ratio between our native precision (18) and the underlying precision.

Non-Constant Functions

burn

function burn(
uint256 poolTokensBurned
) external returns (uint256 underlyingReturned, uint256 hTokenReturned)

Burns LP tokens in exchange for underlying tokens and hTokens.

Emits a {RemoveLiquidity} event.

Requirements:

  • The amount to burn cannot be zero.

Parameters

NameTypeDescription
poolTokensBurneduint256The amount of LP tokens to burn.

Return Values

NameTypeDescription
underlyingReturneduint256The amount of reserve underlying retrieved.
hTokenReturneduint256The amount of reserve hToken retrieved.

buyHToken

function buyHToken(
address to,
uint256 hTokenOut
) external returns (uint256 underlyingIn)

Buys hToken with underlying.

Emits a {Trade} event.

Requirements:

  • All from "getQuoteForBuyingHToken".
  • The caller must have allowed this contract to spend underlyingIn tokens.
  • The caller must have at least underlyingIn in their account.

Parameters

NameTypeDescription
toaddressThe account that receives the hToken being bought.
hTokenOutuint256The amount of hTokens being bought that will be transferred to the to account.

Return Values

NameTypeDescription
underlyingInuint256The amount of underlying that will be taken from the caller's account.

buyUnderlying

function buyUnderlying(
address to,
uint256 underlyingOut
) external returns (uint256 hTokenIn)

Buys underlying with hToken.

Requirements:

  • All from "getQuoteForBuyingUnderlying".
  • The caller must have allowed this contract to spend hTokenIn tokens.
  • The caller must have at least hTokenIn in their account.

Parameters

NameTypeDescription
toaddressThe account that receives the underlying being bought.
underlyingOutuint256The amount of underlying being bought that will be transferred to the to account.

Return Values

NameTypeDescription
hTokenInuint256The amount of hTokens that will be taken from the caller's account.

mint

function mint(
uint256 underlyingOffered
) external returns (uint256 poolTokensMinted)

Mints LP tokens in exchange for adding underlying tokens and hTokens. An appropriate amount of hTokens gets calculated and taken from the caller to be investigated alongside underlying tokens.

Emits an {AddLiquidity} event.

Requirements:

  • The caller must have allowed this contract to spend underlyingOffered and hTokenRequired tokens.

Parameters

NameTypeDescription
underlyingOffereduint256The amount of underlying tokens invested.

Return Values

NameTypeDescription
poolTokensMinteduint256The amount of LP tokens to mint.

sellHToken

function sellHToken(
address to,
uint256 hTokenIn
) external returns (uint256 underlyingOut)

Sells hToken for underlying.

Emits a {Trade} event.

Requirements:

  • All from "getQuoteForSellingHToken".
  • The caller must have allowed this contract to spend hTokenIn tokens.
  • The caller must have at least hTokenIn in their account.

Parameters

NameTypeDescription
toaddressThe account that receives the underlying being bought.
hTokenInuint256The amount of underlying being sold that is taken from the caller's account.

Return Values

NameTypeDescription
underlyingOutuint256The amount of underlying that will be transferred to the to account.

sellUnderlying

function sellUnderlying(
address to,
uint256 underlyingIn
) external returns (uint256 hTokenOut)

Sells underlying for hToken.

Emits a {Trade} event.

Requirements:

  • All from "getQuoteForSellingUnderlying".
  • The caller must have allowed this contract to spend underlyingIn tokens.
  • The caller must have at least underlyingIn in their account.

Parameters

NameTypeDescription
toaddressThe account that receives the hToken being bought.
underlyingInuint256The amount of underlying being sold that is taken from the caller's account.

Return Values

NameTypeDescription
hTokenOutuint256The amount of hTokenOut that will be transferred to the to account.

Events

AddLiquidity

event AddLiquidity(
uint256 maturity,
address provider,
uint256 underlyingAmount,
uint256 hTokenAmount,
uint256 poolTokenAmount
)

Emitted when liquidity is added to the AMM.

Parameters

NameTypeDescription
maturityuint256The maturity of the hToken.
provideraddressThe address of the liquidity provider.
underlyingAmountuint256The amount of underlying provided.
hTokenAmountuint256The amount of hTokens provided.
poolTokenAmountuint256The amount of pool tokens minted.

RemoveLiquidity

event RemoveLiquidity(
uint256 maturity,
address provider,
uint256 underlyingAmount,
uint256 hTokenAmount,
uint256 poolTokenAmount
)

Emitted when liquidity is removed from the AMM.

Parameters

NameTypeDescription
maturityuint256The maturity of the hToken.
provideraddressThe address of the liquidity withdrawn.
underlyingAmountuint256The amount of underlying withdrawn.
hTokenAmountuint256The amount of hTokens provided.
poolTokenAmountuint256The amount of pool tokens burned.

Trade

event Trade(
uint256 maturity,
address from,
address to,
int256 underlyingAmount,
int256 hTokenAmount
)

Emitted when a trade is made in the AMM.

Parameters

NameTypeDescription
maturityuint256The maturity of the hToken.
fromaddressThe account sending the tokens to the AMM.
toaddressThe account receiving the tokens from the AMM.
underlyingAmountint256The amount of underlying bought or sold.
hTokenAmountint256The amount of hTokens bought or sold.

Custom Errors

HifiPool__BondMatured

error HifiPool__BondMatured()

Emitted when the bond matured.

HifiPool__BuyHTokenZero

error HifiPool__BuyHTokenZero()

Emitted when attempting to buy a zero amount of hTokens.

HifiPool__BuyHTokenUnderlyingZero

error HifiPool__BuyHTokenUnderlyingZero()

Emitted when attempting to buy hTokens with a zero amount of underlying.

HifiPool__BuyUnderlyingZero

error HifiPool__BuyUnderlyingZero()

Emitted when attempting to buy a zero amount of underlying.

HifiPool__BurnZero

error HifiPool__BurnZero()

Emitted when offering zero underlying to mint LP tokens.

HifiPool__MintZero

error HifiPool__MintZero()

Emitted when offering zero underlying to mint LP tokens.

HifiPool__NegativeInterestRate

error HifiPool__NegativeInterestRate(uint256 virtualHTokenReserves, uint256 hTokenOut, uint256 normalizedUnderlyingReserves, uint256 normalizedUnderlyingIn)

Emitted when buying hTokens or selling underlying and the resultant hToken reserves would become smaller than the underlying reserves.

HifiPool__SellHTokenZero

error HifiPool__SellHTokenZero()

Emitted when attempting to sell a zero amount of hToken.

HifiPool__SellHTokenUnderlyingZero

error HifiPool__SellHTokenUnderlyingZero()

Emitted when attempting to sell hTokens in exchange for a zero amount of underlying.

HifiPool__SellUnderlyingZero

error HifiPool__SellUnderlyingZero()

Emitted when attempting to sell a zero amount of underlying.

HifiPool__ToInt256CastOverflow

error HifiPool__ToInt256CastOverflow(uint256 number)

Emitted when trying to convert a uint256 number that doesn't fit within int256.

HifiPool__VirtualHTokenReservesOverflow

error HifiPool__VirtualHTokenReservesOverflow(uint256 hTokenBalance, uint256 totalSupply)

Emitted when the hToken balance added to the total supply of LP tokens overflows uint256.