Skip to main content

Helpers

Git Source

Author: Struct Finance

Collection of helper functions

Functions

getInvestedAndExcess

Given the total amount invested, we want to find out how many of this investor's deposits were actually used. Use findUpperBound on the prefixSum to find the point where total deposits were accepted. For example, if $2000 was deposited by all investors and $1000 was invested, then some position in the prefixSum splits the array into deposits that got in, and deposits that didn't get in. That same position maps to userSums. This is the user's deposits that got in. Since we are keeping track of the sums, we know at that position the total deposits for a user was $15, even if it was 15 $1 deposits. And we know the amount that didn't get in is the last value in userSum - the amount that got it.

function getInvestedAndExcess(DataTypes.Investor storage investor, uint256 invested)
external
view
returns (uint256 userInvested, uint256 excess);

Parameters

NameTypeDescription
investorDataTypes.InvestorA specific investor
investeduint256The total amount invested

_isWithinBound

This methods calculates the relative percentage difference.

function _isWithinBound(uint256 _rate1, uint256 _rate2) public pure returns (bool);

Parameters

NameTypeDescription
_rate1uint256Rate from the AMM
_rate2uint256Rate from the Chainlink price feed

Returns

NameTypeDescription
<none>boolA flag that states whether the given rates lies within the MAX_DEVIATION

calculateFees

Used to calculate fees to be sent to the receiver once the funds are withdrawn from LP

function calculateFees(
uint256 _tokensInvestableSr,
uint256 _tokensAtMaturitySr,
uint256 _tokensInvestableJr,
uint256 _tokensAtMaturityJr,
DataTypes.ProductConfig storage _productConfig
) external returns (uint256, uint256);

Parameters

NameTypeDescription
_tokensInvestableSruint256The total amount of Senior tranche tokens that were eligible for investment
_tokensAtMaturitySruint256The total amount of Senior tranche tokens withdrawn after maturity
_tokensInvestableJruint256The total amount of Junior tranche tokens that were eligible for investment
_tokensAtMaturityJruint256The total amount of Junior tranche tokens withdrawn after maturity
_productConfigDataTypes.ProductConfigThe configuration/specs of the product

Returns

NameTypeDescription
<none>uint256_srFeeTotal The total fee charged as senior tranche tokens
<none>uint256_jrFeeTotal The total fee charged as junior tranche tokens

swapAndSendFeeToReceiver

Performance Fee

Sends the specified % of the fee to the recipient

function swapAndSendFeeToReceiver(
IJoeRouter _joeRouter,
uint256 _feeTotalSr,
uint256 _feeTotalJr,
address[] calldata _seniorToNative,
address[] calldata _juniorToNative,
address _feeReceiver
) external;

Parameters

NameTypeDescription
_joeRouterIJoeRouterInterface for the joeRouter contract
_feeTotalSruint256Total fees accumulated from the senior tranche
_feeTotalJruint256Total fees accumulated from the junior tranche
_seniorToNativeaddress[]Swap path array for the senior to native token
_juniorToNativeaddress[]Swap path array for the junior to native token
_feeReceiveraddressAddress of the fee receiver (distribution manager)

_sendReceiverFee

Send the receiver fee.

function _sendReceiverFee(
IJoeRouter _joeRouter,
uint256 _feeTotal,
address[] calldata _path,
address _feeReceiver,
address _nativeToken
) private;

Parameters

NameTypeDescription
_joeRouterIJoeRouterInterface for the joeRouter contract
_feeTotaluint256Total fees accumulated by product
_pathaddress[]Swap path
_feeReceiveraddressAddress of the fee receiver (distribution manager)
_nativeTokenaddressAddress of the native token (WAVAX)

_wrapAVAXForDeposit

Converts AVAX to wAVAX for deposit

function _wrapAVAXForDeposit(uint256 _depositAmount, address payable wAVAX) external;

Parameters

NameTypeDescription
_depositAmountuint256The amount the user wishes to deposit in AVAX
wAVAXaddress payableThe address of the native tokens

getAssetPrice

Returns the price of the given asset

function getAssetPrice(IStructPriceOracle _structPriceOracle, address _asset) public view returns (uint256);

Parameters

NameTypeDescription
_structPriceOracleIStructPriceOracleThe oracle address of Struct price feed
_assetaddressThe address of the asset

getTrancheTokenRateV2

Validates and returns the exchange rate for the given assets from the chainlink oracle and AMM.

This is required to prevent oracle manipulation attacks.

function getTrancheTokenRateV2(
IStructPriceOracle _structPriceOracle,
address[] storage _path,
ILBQuoter _lbQuoter,
uint256 _amountOut
) external view returns (bool, uint256, uint256, uint256);

Parameters

NameTypeDescription
_structPriceOracleIStructPriceOracleThe oracle address of Struct price feed
_pathaddress[]The path to get the exchange rate from the AMM (LP)
_lbQuoterILBQuoterThe address of the TJ LB Quoter contract
_amountOutuint256The amount of tokens to be swapped out

tokenDecimalsToWei

Calculate the exchange rate using the prices from StructPriceOracle (Chainlink price feed) Calculate the exchange rate using the Router if amountOut < 1 token, set it to 10 ** decimals to avoid incorrect rate Check if the relative price diff % is within the MAX_DEVIATION if yes, return the exchange rate and chainlink price along with a flag if not, return the price and rate as 0 along with false flag

Converts the passed value from WEI to token decimals

function tokenDecimalsToWei(uint256 _decimals, uint256 _amount) public pure returns (uint256);

Parameters

NameTypeDescription
_decimalsuint256Number of decimals the target token has (Is dynamic)
_amountuint256Amount that has to be converted from the current token decimals to 18 decimals

weiToTokenDecimals

Converts the passed value from token decimals to WEI

function weiToTokenDecimals(uint256 _decimals, uint256 _amount) public pure returns (uint256);

Parameters

NameTypeDescription
_decimalsuint256Number of decimals the target token has (Is dynamic)
_amountuint256Amount that has to be converted from 18 decimals to the current token decimals

_getTokenBalance

function _getTokenBalance(IERC20Metadata _token, address _account) internal view returns (uint256 _balance);

Events

PerformanceFeeSent

Emits when the performance fee is sent to the feeReceiver

event PerformanceFeeSent(DataTypes.Tranche _tranche, uint256 _tokensSent);

ManagementFeeSent

Emits when the management fee is sent to the feeReceiver

event ManagementFeeSent(DataTypes.Tranche _tranche, uint256 _tokensSent);

FeeCharged

Emits the total fees charged for each tranche

event FeeCharged(uint256 feeTotalSr, uint256 feeTotalJr);