Helpers
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
Name | Type | Description |
---|---|---|
investor | DataTypes.Investor | A specific investor |
invested | uint256 | The total amount invested |
_isWithinBound
This methods calculates the relative percentage difference.
function _isWithinBound(uint256 _rate1, uint256 _rate2) public pure returns (bool);
Parameters
Name | Type | Description |
---|---|---|
_rate1 | uint256 | Rate from the AMM |
_rate2 | uint256 | Rate from the Chainlink price feed |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | A 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
Name | Type | Description |
---|---|---|
_tokensInvestableSr | uint256 | The total amount of Senior tranche tokens that were eligible for investment |
_tokensAtMaturitySr | uint256 | The total amount of Senior tranche tokens withdrawn after maturity |
_tokensInvestableJr | uint256 | The total amount of Junior tranche tokens that were eligible for investment |
_tokensAtMaturityJr | uint256 | The total amount of Junior tranche tokens withdrawn after maturity |
_productConfig | DataTypes.ProductConfig | The configuration/specs of the product |
Returns
Name | Type | Description |
---|---|---|
<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
Name | Type | Description |
---|---|---|
_joeRouter | IJoeRouter | Interface for the joeRouter contract |
_feeTotalSr | uint256 | Total fees accumulated from the senior tranche |
_feeTotalJr | uint256 | Total fees accumulated from the junior tranche |
_seniorToNative | address[] | Swap path array for the senior to native token |
_juniorToNative | address[] | Swap path array for the junior to native token |
_feeReceiver | address | Address 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
Name | Type | Description |
---|---|---|
_joeRouter | IJoeRouter | Interface for the joeRouter contract |
_feeTotal | uint256 | Total fees accumulated by product |
_path | address[] | Swap path |
_feeReceiver | address | Address of the fee receiver (distribution manager) |
_nativeToken | address | Address of the native token (WAVAX) |
_wrapAVAXForDeposit
Converts AVAX to wAVAX for deposit
function _wrapAVAXForDeposit(uint256 _depositAmount, address payable wAVAX) external;
Parameters
Name | Type | Description |
---|---|---|
_depositAmount | uint256 | The amount the user wishes to deposit in AVAX |
wAVAX | address payable | The address of the native tokens |
getAssetPrice
Returns the price of the given asset
function getAssetPrice(IStructPriceOracle _structPriceOracle, address _asset) public view returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
_structPriceOracle | IStructPriceOracle | The oracle address of Struct price feed |
_asset | address | The 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
Name | Type | Description |
---|---|---|
_structPriceOracle | IStructPriceOracle | The oracle address of Struct price feed |
_path | address[] | The path to get the exchange rate from the AMM (LP) |
_lbQuoter | ILBQuoter | The address of the TJ LB Quoter contract |
_amountOut | uint256 | The 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
Name | Type | Description |
---|---|---|
_decimals | uint256 | Number of decimals the target token has (Is dynamic) |
_amount | uint256 | Amount 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
Name | Type | Description |
---|---|---|
_decimals | uint256 | Number of decimals the target token has (Is dynamic) |
_amount | uint256 | Amount 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);