Skip to content

Commit

Permalink
doing nat spec
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDeadCe11 committed Jul 24, 2024
1 parent 51cb5bd commit ddd2e41
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 85 deletions.
2 changes: 2 additions & 0 deletions script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ address constant MAINNET_CHAINLINK_CBETH_ETH_FEED = 0xa668682974E3f121185a3cD94f
address constant MAINNET_CHAINLINK_LINK_USD_FEED = 0x86E53CF1B870786351Da77A57575e79CB55812CB;
address constant MAINNET_CHAINLINK_GRT_USD_FEED = 0x0F38D86FceF4955B705F35c9e41d1A16e0637c73;

address constant MAINNET_CHAINLINK_SEQUENCER_FEED = 0xFdB631F5EE196F0ed6FAa767959853A9F217697D;

////////// PENDLE //////////
uint32 constant MAINNET_PENDLE_TWAP_DURATION = 900;
address constant MAINNET_PENDLE_ORACLE = 0x9a9Fa8338dd5E5B2188006f1Cd2Ef26d921650C2;
Expand Down
15 changes: 10 additions & 5 deletions src/contracts/oracles/pendle/PendleLpToSyRelayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ contract PendleLpToSyRelayer {
uint32 public twapDuration;
string public symbol;

/**
* @dev at the end of the constructor we must call IPMarket(market).getOracleState(_market, _twapDuration) and check that
* increaseObservationsCardinalityRequired is false. If not we must wait for at least the twapDuration,
* to allow data population.
* @param _market the address of the pendle market we want to get the prices from
* @param _oracle the pendle oracle contract
* @param _twapDuration the desired TWAP duration in seconds (recommended 900s);
*/
constructor(address _market, address _oracle, uint32 _twapDuration) {
require(_market != address(0) && _oracle != address(0), 'Invalid address');
require(_twapDuration != 0, 'Invalid TWAP duration');
Expand All @@ -29,12 +37,9 @@ contract PendleLpToSyRelayer {
(SY,,) = market.readTokens();

symbol = string(abi.encodePacked('LP / ', SY.symbol()));
// test if oracle is ready

(bool increaseCardinalityRequired,, bool oldestObservationSatisfied) = oracle.getOracleState(_market, _twapDuration);
// It's required to call IPMarket(market).increaseObservationsCardinalityNext(cardinalityRequired) and wait
// for at least the twapDuration, to allow data population.
// also
// It's necessary to wait for at least the twapDuration, to allow data population.

require(!increaseCardinalityRequired && oldestObservationSatisfied, 'Oracle not ready');
}

Expand Down
15 changes: 10 additions & 5 deletions src/contracts/oracles/pendle/PendleYtToSyRelayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ contract PendleYtToSyRelayer {
uint32 public twapDuration;
string public symbol;

/**
* @dev at the end of the constructor we must call IPMarket(market).getOracleState(_market, _twapDuration) and check that
* increaseObservationsCardinalityRequired is false. If not we must wait for at least the twapDuration,
* to allow data population.
* @param _market the address of the pendle market we want to get the prices from
* @param _oracle the pendle oracle contract
* @param _twapDuration the desired TWAP duration in seconds (recommended 900s);
*/
constructor(address _market, address _oracle, uint32 _twapDuration) {
require(_market != address(0) && _oracle != address(0), 'Invalid address');
require(_twapDuration != 0, 'Invalid TWAP duration');
Expand All @@ -30,12 +38,9 @@ contract PendleYtToSyRelayer {
(SY,, YT) = market.readTokens();

symbol = string(abi.encodePacked(YT.symbol(), ' / ', SY.symbol()));
// test if oracle is ready

(bool increaseCardinalityRequired,, bool oldestObservationSatisfied) = oracle.getOracleState(_market, _twapDuration);
// It's required to call IPMarket(market).increaseObservationsCardinalityNext(cardinalityRequired) and wait
// for at least the twapDuration, to allow data population.
// also
// It's necessary to wait for at least the twapDuration, to allow data population.

require(!increaseCardinalityRequired && oldestObservationSatisfied, 'Oracle not ready');
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/oracles/IBaseOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IBaseOracle {

/**
* @dev Fetch the latest oracle result and whether it is valid or not
* @dev This method should never revert
* @dev This method should never revert
*/
function getResultWithValidity() external view returns (uint256 _result, bool _validity);

Expand Down
46 changes: 42 additions & 4 deletions src/interfaces/oracles/pendle/IStandardizedYield.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pragma solidity ^0.8.20;
import {IERC20Metadata} from '@interfaces/utils/IERC20Metadata.sol';

interface IStandardizedYield is IERC20Metadata {
/// @dev Emitted when any base tokens is deposited to mint shares
/**
* @dev Emitted when any base tokens is deposited to mint shares
*/
event Deposit(
address indexed caller,
address indexed receiver,
Expand All @@ -13,7 +15,9 @@ interface IStandardizedYield is IERC20Metadata {
uint256 amountSyOut
);

/// @dev Emitted when any shares are redeemed for base tokens
/**
* @dev Emitted when any shares are redeemed for base tokens
*/
event Redeem(
address indexed caller,
address indexed receiver,
Expand All @@ -22,13 +26,17 @@ interface IStandardizedYield is IERC20Metadata {
uint256 amountTokenOut
);

/// @dev check `assetInfo()` for more information
/**
* @dev check `assetInfo()` for more information
*/
enum AssetType {
TOKEN,
LIQUIDITY
}

/// @dev Emitted when (`user`) claims their rewards
/**
* @dev Emitted when (`user`) claims their rewards
*/
event ClaimRewards(address indexed user, address[] rewardTokens, uint256[] rewardAmounts);

/**
Expand Down Expand Up @@ -97,8 +105,16 @@ interface IStandardizedYield is IERC20Metadata {
*/
function accruedRewards(address user) external view returns (uint256[] memory rewardAmounts);

/**
* @notice returns the indexes of the Current rewards
* @return indexes an array of reward indices
*/
function rewardIndexesCurrent() external returns (uint256[] memory indexes);

/**
* @notice returns the indexes of stored rewards
* @return indexes an array of reward indices
*/
function rewardIndexesStored() external view returns (uint256[] memory indexes);

/**
Expand All @@ -121,15 +137,37 @@ interface IStandardizedYield is IERC20Metadata {
*/
function getTokensOut() external view returns (address[] memory res);

/**
* @notice Checks if an incoming token is valid
* @param token the token address
* @return true if this is a valid token
*/
function isValidTokenIn(address token) external view returns (bool);

/**
* @notice Checks if an outgoing token is valid
* @param token the token address
* @return true if this is a valid token
*/
function isValidTokenOut(address token) external view returns (bool);

/**
* @notice Calculates the a mount of shares to be given for a potential token deposit
* @param tokenIn address of the token being deposited
* @param amountTokenToDeposit the amount of the token to be deposited
* @return amountSharesOut the amount of shares you would get if this deposit was made
*/
function previewDeposit(
address tokenIn,
uint256 amountTokenToDeposit
) external view returns (uint256 amountSharesOut);

/**
* @notice Calculates the a mount of tokens to be given for a potential share redemption
* @param tokenIn address of the token being claimed
* @param amountTokenToDeposit the amount of the shares to be redeemed
* @return amountTokenOut the amount of token you would get if this deposit was made
*/
function previewRedeem(address tokenOut, uint256 amountSharesToRedeem) external view returns (uint256 amountTokenOut);

/**
Expand Down
46 changes: 27 additions & 19 deletions src/libraries/gmx/GmxDeposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ pragma solidity ^0.8.9;
*
*/
library GmxDeposit {
// @dev there is a limit on the number of fields a struct can have when being passed
// or returned as a memory variable which can cause "Stack too deep" errors
// use sub-structs to avoid this issue
// @param addresses address values
// @param numbers number values
// @param flags boolean values
/**
* @dev there is a limit on the number of fields a struct can have when being passed
* or returned as a memory variable which can cause "Stack too deep" errors
* use sub-structs to avoid this issue
* @param addresses address values
* @param numbers number values
* @param flags boolean values
*/
struct DepositProps {
Addresses addresses;
Numbers numbers;
Flags flags;
}

// @param account the account depositing liquidity
// @param receiver the address to send the liquidity tokens to
// @param callbackContract the callback contract
// @param uiFeeReceiver the ui fee receiver
// @param market the market to deposit to
/**
* @param account the account depositing liquidity
* @param receiver the address to send the liquidity tokens to
* @param callbackContract the callback contract
* @param uiFeeReceiver the ui fee receiver
* @param market the market to deposit to
*/
struct Addresses {
address account;
address receiver;
Expand All @@ -35,13 +39,15 @@ library GmxDeposit {
address[] shortTokenSwapPath;
}

// @param initialLongTokenAmount the amount of long tokens to deposit
// @param initialShortTokenAmount the amount of short tokens to deposit
// @param minMarketTokens the minimum acceptable number of liquidity tokens
// @param updatedAtBlock the block that the deposit was last updated at
// sending funds back to the user in case the deposit gets cancelled
// @param executionFee the execution fee for keepers
// @param callbackGasLimit the gas limit for the callbackContract
/**
* @param initialLongTokenAmount the amount of long tokens to deposit
* @param initialShortTokenAmount the amount of short tokens to deposit
* @param minMarketTokens the minimum acceptable number of liquidity tokens
* @param updatedAtBlock the block that the deposit was last updated at
* sending funds back to the user in case the deposit gets cancelled
* @param executionFee the execution fee for keepers
* @param callbackGasLimit the gas limit for the callbackContract
*/
struct Numbers {
uint256 initialLongTokenAmount;
uint256 initialShortTokenAmount;
Expand All @@ -52,7 +58,9 @@ library GmxDeposit {
uint256 callbackGasLimit;
}

// @param shouldUnwrapNativeToken whether to unwrap the native token when
/**
* @param shouldUnwrapNativeToken whether to unwrap the native token when
*/
struct Flags {
bool shouldUnwrapNativeToken;
}
Expand Down
11 changes: 11 additions & 0 deletions src/libraries/gmx/GmxMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ import {GmxPrice} from './GmxPrice.sol';
* @notice GMX Market Library
*/
library GmxMarket {
/**
* @param marketToken the address of the gm token contract
* @param indexToken the address of the contract of the token being traded in the perp market. address(0) for a swap market
* @param longToken the address of the token backing long positions. often the same as the index token
* @param shortToken the address of the token backing short positions. often a stable coin
*/
struct MarketProps {
address marketToken;
address indexToken;
address longToken;
address shortToken;
}

/**
* @param indexTokenPrice the price of the index token
* @param longToken price the price of the long token
* @param shortTokenPrice the price of the short token
*/
struct MarketPrices {
GmxPrice.PriceProps indexTokenPrice;
GmxPrice.PriceProps longTokenPrice;
Expand Down
26 changes: 14 additions & 12 deletions src/libraries/gmx/GmxMarketPoolValueInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ pragma solidity ^0.8.9;
* @notice GMX MarketPoolVaultInfo Library
*/
library GmxMarketPoolValueInfo {
// @dev struct to avoid stack too deep errors for the getPoolValue call
// @param value the pool value
// @param longTokenAmount the amount of long token in the pool
// @param shortTokenAmount the amount of short token in the pool
// @param longTokenUsd the USD value of the long tokens in the pool
// @param shortTokenUsd the USD value of the short tokens in the pool
// @param totalBorrowingFees the total pending borrowing fees for the market
// @param borrowingFeePoolFactor the pool factor for borrowing fees
// @param impactPoolAmount the amount of tokens in the impact pool
// @param longPnl the pending pnl of long positions
// @param shortPnl the pending pnl of short positions
// @param netPnl the net pnl of long and short positions
/**
* @dev struct to avoid stack too deep errors for the getPoolValue call
* @param value the pool value
* @param longTokenAmount the amount of long token in the pool
* @param shortTokenAmount the amount of short token in the pool
* @param longTokenUsd the USD value of the long tokens in the pool
* @param shortTokenUsd the USD value of the short tokens in the pool
* @param totalBorrowingFees the total pending borrowing fees for the market
* @param borrowingFeePoolFactor the pool factor for borrowing fees
* @param impactPoolAmount the amount of tokens in the impact pool
* @param longPnl the pending pnl of long positions
* @param shortPnl the pending pnl of short positions
* @param netPnl the net pnl of long and short positions
*/
struct PoolValueInfoProps {
int256 poolValue;
int256 longPnl;
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/gmx/GmxPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pragma solidity ^0.8.9;
* @notice GMX Price Library
*/
library GmxPrice {
/**
* @param min the minimum acceptable price
* @param max the maximum acceptable price
*/
struct PriceProps {
uint256 min;
uint256 max;
Expand Down
44 changes: 26 additions & 18 deletions src/libraries/gmx/GmxWithdrawal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ pragma solidity ^0.8.9;
*
*/
library GmxWithdrawal {
// @dev there is a limit on the number of fields a struct can have when being passed
// or returned as a memory variable which can cause "Stack too deep" errors
// use sub-structs to avoid this issue
// @param addresses address values
// @param numbers number values
// @param flags boolean values
/**
* @dev there is a limit on the number of fields a struct can have when being passed
* or returned as a memory variable which can cause "Stack too deep" errors
* use sub-structs to avoid this issue
* @param addresses address values
* @param numbers number values
* @param flags boolean values
*/
struct WithdrawalProps {
Addresses addresses;
Numbers numbers;
Flags flags;
}
/**
* @param account The account to withdraw for.
* @param receiver The address that will receive the withdrawn tokens.
* @param callbackContract The contract that will be called back.
* @param uiFeeReceiver The ui fee receiver.
* @param market The market on which the withdrawal will be executed.
*/

// @param account The account to withdraw for.
// @param receiver The address that will receive the withdrawn tokens.
// @param callbackContract The contract that will be called back.
// @param uiFeeReceiver The ui fee receiver.
// @param market The market on which the withdrawal will be executed.
struct Addresses {
address account;
address receiver;
Expand All @@ -32,13 +36,15 @@ library GmxWithdrawal {
address[] longTokenSwapPath;
address[] shortTokenSwapPath;
}
/**
* @param marketTokenAmount The amount of market tokens that will be withdrawn.
* @param minLongTokenAmount The minimum amount of long tokens that must be withdrawn.
* @param minShortTokenAmount The minimum amount of short tokens that must be withdrawn.
* @param updatedAtBlock The block at which the withdrawal was last updated.
* @param executionFee The execution fee for the withdrawal.
* @param callbackGasLimit The gas limit for calling the callback contract.
*/

// @param marketTokenAmount The amount of market tokens that will be withdrawn.
// @param minLongTokenAmount The minimum amount of long tokens that must be withdrawn.
// @param minShortTokenAmount The minimum amount of short tokens that must be withdrawn.
// @param updatedAtBlock The block at which the withdrawal was last updated.
// @param executionFee The execution fee for the withdrawal.
// @param callbackGasLimit The gas limit for calling the callback contract.
struct Numbers {
uint256 marketTokenAmount;
uint256 minLongTokenAmount;
Expand All @@ -48,8 +54,10 @@ library GmxWithdrawal {
uint256 executionFee;
uint256 callbackGasLimit;
}
/**
* @param shouldUnwrapNativeToken whether to unwrap the native token when
*/

// @param shouldUnwrapNativeToken whether to unwrap the native token when
struct Flags {
bool shouldUnwrapNativeToken;
}
Expand Down
Loading

0 comments on commit ddd2e41

Please sign in to comment.