FluidDexT1 ​
Inherits:CoreHelpers
Implements core logics for Fluid Dex protocol. Note Token transfers happen directly from user to Liquidity contract and vice-versa.
Functions ​
constructor ​
constructor(ConstantViews memory constantViews_) CoreHelpers(constantViews_);
_swapIn ​
This function allows users to swap a specific amount of input tokens for output tokens
function _swapIn(bool swap0to1_, uint256 amountIn_, SwapInExtras memory extras_)
internal
returns (uint256 amountOut_);
Parameters
Name | Type | Description |
---|---|---|
swap0to1_ | bool | Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0 |
amountIn_ | uint256 | The exact amount of input tokens to swap |
extras_ | SwapInExtras | Additional parameters for the swap: - to: Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountOut_ - amountOutMin: The minimum amount of output tokens the user expects to receive - isCallback: If true, indicates that the input tokens should be transferred via a callback |
Returns
Name | Type | Description |
---|---|---|
amountOut_ | uint256 | The amount of output tokens received from the swap |
swapIn ​
Swap tokens with perfect amount in
function swapIn(bool swap0to1_, uint256 amountIn_, uint256 amountOutMin_, address to_)
public
payable
returns (uint256 amountOut_);
Parameters
Name | Type | Description |
---|---|---|
swap0to1_ | bool | Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0 |
amountIn_ | uint256 | The exact amount of tokens to swap in |
amountOutMin_ | uint256 | The minimum amount of tokens to receive after swap |
to_ | address | Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountOut_ |
Returns
Name | Type | Description |
---|---|---|
amountOut_ | uint256 | The amount of output tokens received from the swap |
swapInWithCallback ​
Swap tokens with perfect amount in and callback functionality
function swapInWithCallback(bool swap0to1_, uint256 amountIn_, uint256 amountOutMin_, address to_)
public
payable
returns (uint256 amountOut_);
Parameters
Name | Type | Description |
---|---|---|
swap0to1_ | bool | Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0 |
amountIn_ | uint256 | The exact amount of tokens to swap in |
amountOutMin_ | uint256 | The minimum amount of tokens to receive after swap |
to_ | address | Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountOut_ |
Returns
Name | Type | Description |
---|---|---|
amountOut_ | uint256 | The amount of output tokens received from the swap |
swapOut ​
Swap tokens with perfect amount out
function swapOut(bool swap0to1_, uint256 amountOut_, uint256 amountInMax_, address to_)
public
payable
returns (uint256 amountIn_);
Parameters
Name | Type | Description |
---|---|---|
swap0to1_ | bool | Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0 |
amountOut_ | uint256 | The exact amount of tokens to receive after swap |
amountInMax_ | uint256 | Maximum amount of tokens to swap in |
to_ | address | Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountIn_ |
Returns
Name | Type | Description |
---|---|---|
amountIn_ | uint256 | The amount of input tokens used for the swap |
swapOutWithCallback ​
Swap tokens with perfect amount out and callback functionality
function swapOutWithCallback(bool swap0to1_, uint256 amountOut_, uint256 amountInMax_, address to_)
public
payable
returns (uint256 amountIn_);
Parameters
Name | Type | Description |
---|---|---|
swap0to1_ | bool | Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0 |
amountOut_ | uint256 | The exact amount of tokens to receive after swap |
amountInMax_ | uint256 | Maximum amount of tokens to swap in |
to_ | address | Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountIn_ |
Returns
Name | Type | Description |
---|---|---|
amountIn_ | uint256 | The amount of input tokens used for the swap |
depositPerfect ​
Deposit tokens in equal proportion to the current pool ratio
function depositPerfect(uint256 shares_, uint256 maxToken0Deposit_, uint256 maxToken1Deposit_, bool estimate_)
public
payable
returns (uint256 token0Amt_, uint256 token1Amt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to mint |
maxToken0Deposit_ | uint256 | Maximum amount of token0 to deposit |
maxToken1Deposit_ | uint256 | Maximum amount of token1 to deposit |
estimate_ | bool | If true, function will revert with estimated deposit amounts without executing the deposit |
Returns
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | Amount of token0 deposited |
token1Amt_ | uint256 | Amount of token1 deposited |
withdrawPerfect ​
This function allows users to withdraw a perfect amount of collateral liquidity
function withdrawPerfect(uint256 shares_, uint256 minToken0Withdraw_, uint256 minToken1Withdraw_, address to_)
public
returns (uint256 token0Amt_, uint256 token1Amt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to withdraw |
minToken0Withdraw_ | uint256 | The minimum amount of token0 the user is willing to accept |
minToken1Withdraw_ | uint256 | The minimum amount of token1 the user is willing to accept |
to_ | address | Recipient of withdrawn tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with token0Amt_ & token1Amt_ |
Returns
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | The amount of token0 withdrawn |
token1Amt_ | uint256 | The amount of token1 withdrawn |
borrowPerfect ​
This function allows users to borrow tokens in equal proportion to the current debt pool ratio
function borrowPerfect(uint256 shares_, uint256 minToken0Borrow_, uint256 minToken1Borrow_, address to_)
public
returns (uint256 token0Amt_, uint256 token1Amt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to borrow |
minToken0Borrow_ | uint256 | Minimum amount of token0 to borrow |
minToken1Borrow_ | uint256 | Minimum amount of token1 to borrow |
to_ | address | Recipient of borrowed tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with token0Amt_ & token1Amt_ |
Returns
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | Amount of token0 borrowed |
token1Amt_ | uint256 | Amount of token1 borrowed |
paybackPerfect ​
This function allows users to pay back borrowed tokens in equal proportion to the current debt pool ratio
function paybackPerfect(uint256 shares_, uint256 maxToken0Payback_, uint256 maxToken1Payback_, bool estimate_)
public
payable
returns (uint256 token0Amt_, uint256 token1Amt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to pay back |
maxToken0Payback_ | uint256 | Maximum amount of token0 to pay back |
maxToken1Payback_ | uint256 | Maximum amount of token1 to pay back |
estimate_ | bool | If true, function will revert with estimated payback amounts without executing the payback |
Returns
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | Amount of token0 paid back |
token1Amt_ | uint256 | Amount of token1 paid back |
deposit ​
This function allows users to deposit tokens in any proportion into the col pool
function deposit(uint256 token0Amt_, uint256 token1Amt_, uint256 minSharesAmt_, bool estimate_)
public
payable
returns (uint256 shares_);
Parameters
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | The amount of token0 to deposit |
token1Amt_ | uint256 | The amount of token1 to deposit |
minSharesAmt_ | uint256 | The minimum amount of shares the user expects to receive |
estimate_ | bool | If true, function will revert with estimated shares without executing the deposit |
Returns
Name | Type | Description |
---|---|---|
shares_ | uint256 | The amount of shares minted for the deposit |
withdraw ​
This function allows users to withdraw tokens in any proportion from the col pool
function withdraw(uint256 token0Amt_, uint256 token1Amt_, uint256 maxSharesAmt_, address to_)
public
returns (uint256 shares_);
Parameters
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | The amount of token0 to withdraw |
token1Amt_ | uint256 | The amount of token1 to withdraw |
maxSharesAmt_ | uint256 | The maximum number of shares the user is willing to burn |
to_ | address | Recipient of withdrawn tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with shares_ |
Returns
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares burned for the withdrawal |
borrow ​
This function allows users to borrow tokens in any proportion from the debt pool
function borrow(uint256 token0Amt_, uint256 token1Amt_, uint256 maxSharesAmt_, address to_)
public
returns (uint256 shares_);
Parameters
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | The amount of token0 to borrow |
token1Amt_ | uint256 | The amount of token1 to borrow |
maxSharesAmt_ | uint256 | The maximum amount of shares the user is willing to receive |
to_ | address | Recipient of borrowed tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with shares_ |
Returns
Name | Type | Description |
---|---|---|
shares_ | uint256 | The amount of borrow shares minted to represent the borrowed amount |
payback ​
This function allows users to payback tokens in any proportion to the debt pool
function payback(uint256 token0Amt_, uint256 token1Amt_, uint256 minSharesAmt_, bool estimate_)
public
payable
returns (uint256 shares_);
Parameters
Name | Type | Description |
---|---|---|
token0Amt_ | uint256 | The amount of token0 to payback |
token1Amt_ | uint256 | The amount of token1 to payback |
minSharesAmt_ | uint256 | The minimum amount of shares the user expects to burn |
estimate_ | bool | If true, function will revert with estimated shares without executing the payback |
Returns
Name | Type | Description |
---|---|---|
shares_ | uint256 | The amount of borrow shares burned for the payback |
withdrawPerfectInOneToken ​
This function allows users to withdraw their collateral with perfect shares in one token
function withdrawPerfectInOneToken(uint256 shares_, uint256 minToken0_, uint256 minToken1_, address to_)
public
returns (uint256 withdrawAmt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to burn for withdrawal |
minToken0_ | uint256 | The minimum amount of token0 the user expects to receive (set to 0 if withdrawing in token1) |
minToken1_ | uint256 | The minimum amount of token1 the user expects to receive (set to 0 if withdrawing in token0) |
to_ | address | Recipient of withdrawn tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with withdrawAmt_ |
Returns
Name | Type | Description |
---|---|---|
withdrawAmt_ | uint256 | The amount of tokens withdrawn in the chosen token |
paybackPerfectInOneToken ​
This function allows users to payback their debt with perfect shares in one token
function paybackPerfectInOneToken(uint256 shares_, uint256 maxToken0_, uint256 maxToken1_, bool estimate_)
public
payable
returns (uint256 paybackAmt_);
Parameters
Name | Type | Description |
---|---|---|
shares_ | uint256 | The number of shares to burn for payback |
maxToken0_ | uint256 | The maximum amount of token0 the user is willing to pay (set to 0 if paying back in token1) |
maxToken1_ | uint256 | The maximum amount of token1 the user is willing to pay (set to 0 if paying back in token0) |
estimate_ | bool | If true, the function will revert with the estimated payback amount without executing the payback |
Returns
Name | Type | Description |
---|---|---|
paybackAmt_ | uint256 | The amount of tokens paid back in the chosen token |
liquidityCallback ​
liquidity callback for cheaper token transfers in case of deposit or payback. only callable by Liquidity during an operation.
function liquidityCallback(address token_, uint256 amount_, bytes calldata data_) external;
oraclePrice ​
the oracle assumes last set price of pool till the next swap happens. There's a possibility that during that time some interest is generated hence the last stored price is not the 100% correct price for the whole duration but the difference due to interest will be super low so this difference is ignored For example 2 swaps happened 10min (600 seconds) apart and 1 token has 10% higher interest than other. then that token will accrue about 10% * 600 / secondsInAYear = ~0.0002%
function oraclePrice(uint256[] memory secondsAgos_)
external
view
returns (Oracle[] memory twaps_, uint256 currentPrice_);
Parameters
Name | Type | Description |
---|---|---|
secondsAgos_ | uint256[] | array of seconds ago for which TWAP is needed. If user sends [10, 30, 60] then twaps_ will return [10-0, 30-10, 60-30] |
Returns
Name | Type | Description |
---|---|---|
twaps_ | Oracle[] | twap price, lowest price (aka minima) & highest price (aka maxima) between secondsAgo checkpoints |
currentPrice_ | uint256 | price of pool after the most recent swap |
getPricesAndExchangePrices ​
function getPricesAndExchangePrices() public;
_fallback ​
This function is called when a transaction is sent to the contract without matching any other function
It checks if the caller is authorized, enables re-entrancy protection, delegates the call to the admin implementation, and then disables re-entrancy protection
Only authorized callers (global or dex auth) can trigger this function
This function uses assembly to perform a delegatecall to the admin implementation to update configs related to DEX
Internal fallback function to handle calls to non-existent functions
function _fallback() private;
fallback ​
fallback() external payable;
receive ​
receive() external payable;
constantsView ​
returns all Vault constants
function constantsView() external view returns (ConstantViews memory constantsView_);
constantsView2 ​
returns all Vault constants
function constantsView2() external view returns (ConstantViews2 memory constantsView2_);
getCollateralReserves ​
Calculates the real and imaginary reserves for collateral tokens
This function retrieves the supply of both tokens from the liquidity layer, adjusts them based on exchange prices, and calculates imaginary reserves based on the geometric mean and price range
function getCollateralReserves(
uint256 geometricMean_,
uint256 upperRange_,
uint256 lowerRange_,
uint256 token0SupplyExchangePrice_,
uint256 token1SupplyExchangePrice_
) public view returns (CollateralReserves memory c_);
Parameters
Name | Type | Description |
---|---|---|
geometricMean_ | uint256 | The geometric mean of the token prices |
upperRange_ | uint256 | The upper price range |
lowerRange_ | uint256 | The lower price range |
token0SupplyExchangePrice_ | uint256 | The exchange price for token0 from liquidity layer |
token1SupplyExchangePrice_ | uint256 | The exchange price for token1 from liquidity layer |
Returns
Name | Type | Description |
---|---|---|
c_ | CollateralReserves | A struct containing the calculated real and imaginary reserves for both tokens: - token0RealReserves: The real reserves of token0 - token1RealReserves: The real reserves of token1 - token0ImaginaryReserves: The imaginary reserves of token0 - token1ImaginaryReserves: The imaginary reserves of token1 |
getDebtReserves ​
Calculates the debt reserves for both tokens
function getDebtReserves(
uint256 geometricMean_,
uint256 upperRange_,
uint256 lowerRange_,
uint256 token0BorrowExchangePrice_,
uint256 token1BorrowExchangePrice_
) public view returns (DebtReserves memory d_);
Parameters
Name | Type | Description |
---|---|---|
geometricMean_ | uint256 | The geometric mean of the upper and lower price ranges |
upperRange_ | uint256 | The upper price range |
lowerRange_ | uint256 | The lower price range |
token0BorrowExchangePrice_ | uint256 | The exchange price of token0 from liquidity layer |
token1BorrowExchangePrice_ | uint256 | The exchange price of token1 from liquidity layer |
Returns
Name | Type | Description |
---|---|---|
d_ | DebtReserves | The calculated debt reserves for both tokens, containing: - token0Debt: The debt amount of token0 - token1Debt: The debt amount of token1 - token0RealReserves: The real reserves of token0 derived from token1 debt - token1RealReserves: The real reserves of token1 derived from token0 debt - token0ImaginaryReserves: The imaginary debt reserves of token0 - token1ImaginaryReserves: The imaginary debt reserves of token1 |
Structs ​
SwapInExtras ​
struct SwapInExtras {
address to;
uint256 amountOutMin;
bool isCallback;
}