WusdnToEthOracleMiddlewareWithPyth
Inherits: OracleMiddlewareWithPyth
This contract is used to get the "inverse" price in number of ETH per WUSDN, so that it can be used for a shorting version of the USDN protocol with WUSDN as the underlying asset.
This version uses Pyth for low-latency prices used during validation actions and liquidations.
State Variables
ONE_DOLLAR
One dollar with the middleware decimals.
uint256 internal constant ONE_DOLLAR = 10 ** MIDDLEWARE_DECIMALS;
USDN_MAX_DIVISOR
Max divisor from the USDN token (as constant for gas optimisation).
uint256 internal constant USDN_MAX_DIVISOR = 1e18;
PRICE_NUMERATOR
Numerator for the price calculation in parseAndValidatePrice.
uint256 internal constant PRICE_NUMERATOR = 10 ** MIDDLEWARE_DECIMALS * ONE_DOLLAR * USDN_MAX_DIVISOR;
USDN
The USDN token address.
IUsdn internal immutable USDN;
Functions
constructor
constructor(
address pythContract,
bytes32 pythPriceID,
address chainlinkPriceFeed,
address usdnToken,
uint256 chainlinkTimeElapsedLimit
) OracleMiddlewareWithPyth(pythContract, pythPriceID, chainlinkPriceFeed, chainlinkTimeElapsedLimit);
Parameters
Name | Type | Description |
---|---|---|
pythContract | address | The address of the Pyth contract. |
pythPriceID | bytes32 | The ID of the ETH Pyth price feed. |
chainlinkPriceFeed | address | The address of the ETH Chainlink price feed. |
usdnToken | address | The address of the USDN token. |
chainlinkTimeElapsedLimit | uint256 | The duration after which a Chainlink price is considered stale. |
parseAndValidatePrice
Parse and validate data
and returns the corresponding price data.
*This function returns an approximation of the price, so how much ETH each WUSDN token is worth. The exact formula would be to divide the $/WUSDN price by the $/ETH price, which would look like this (as a decimal number): p = pWUSDN / pETH = (pUSDN * MAX_DIVISOR / divisor) / pETH = (pUSDN * MAX_DIVISOR) / (pETH * divisor) = ((usdnVaultBalance * pWstETH / usdnTotalSupply) * MAX_DIVISOR) / (pETH * divisor) = (usdnVaultBalance * pETH * stETHRatio * MAX_DIVISOR) / (pETH * divisor * usdnTotalSupply) = (usdnVaultBalance * stETHRatio * MAX_DIVISOR) / (usdnTotalSupply * divisor) Because we don't have historical access to the vault balance, the stETH ratio, the USDN total supply and the USDN divisor, we must approximate some parameters. The following approximations are made:
- The USDN price is $1
- The USDN divisor's current value is valid (constant) for the period where we need to provide prices. This greatly simplifies the formula (with $1 and pETH having 18 decimals): p = ($1 * MAX_DIVISOR) / (pETH * divisor) = 1e18 * MAX_DIVISOR / (pETH * divisor) Since we want to represent this price as an integer with a fixed precision of 18 decimals, the number needs to be multiplied by 1e18. p_wei = 1e54 / (pETH * divisor) Because we re-use the logic of the {OracleMiddleware}, we need to invert the adjustment direction. So if an action in the original protocol requires that we add the confidence interval to the neutral price (e.g. to open a new long position), then this oracle middleware needs to subtract the same confidence interval from the neutral price to achieve the same effect, i.e. penalizing the user. This is because the ETH price is in the denominator of the formula.*
function parseAndValidatePrice(
bytes32 actionId,
uint128 targetTimestamp,
Types.ProtocolAction action,
bytes calldata data
) public payable virtual override(CommonOracleMiddleware, IBaseOracleMiddleware) returns (PriceInfo memory);
Parameters
Name | Type | Description |
---|---|---|
actionId | bytes32 | A unique identifier for the current action. This identifier can be used to link an Initiate call with the corresponding Validate call. |
targetTimestamp | uint128 | The target timestamp for validating the price data. For validation actions, this is the timestamp of the initiation. |
action | Types.ProtocolAction | Type of action for which the price is requested. The middleware may use this to alter the validation of the price or the returned price. |
data | bytes | The data to be used to communicate with oracles, the format varies from middleware to middleware and can be different depending on the action. |
Returns
Name | Type | Description |
---|---|---|
<none> | PriceInfo | result_ The price and timestamp as PriceInfo. |