-
Notifications
You must be signed in to change notification settings - Fork 35
chore(pricefeed) Add docs for cross rate #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
# Combine Two Price Feeds / Derive a Cross Rate | ||
|
||
This guide shows how to combine two price feeds to derive a cross rate. These are also known as "synthetic" price feeds. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this called a synthetic feed? no need for new terminology here imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alot of folks asked us if we support synthetic feeds. It helps us with SEO and docs internal search as well. |
||
Cross rates or Synthetic Price feeds are useful for trading pairs that are not directly supported by Pyth. | ||
|
||
<Callout type="info" emoji="ℹ️"> | ||
This guide is for developers who are building applications on **EVM | ||
blockchains** using **solidity only**. | ||
</Callout> | ||
|
||
## Example | ||
|
||
For example, if you want to trade the price of **`ETH/EUR{:jsx}`**, which is not directly supported by Pyth, you can combine the price of **`ETH/USD{:jsx}`** and **`EUR/USD{:jsx}`** to derive the price of **`ETH/EUR{:jsx}`**. | ||
|
||
$$ | ||
\large{\text{ETH/EUR} = \text{ETH/USD} \div \text{EUR/USD}} | ||
$$ | ||
|
||
## Derive a cross rate | ||
|
||
Pyth provides [`deriveCrossRate`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/sdk/solidity/PythUtils.sol#L77) function to combine two price feeds. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pyth here is imprecise. this should say The Pyth Solidity SDK provides ... |
||
This method is available in [Pyth solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity). | ||
|
||
This method takes the following parameters: | ||
|
||
- `price1`: The first price feed value, representing a / b (e.g., ETH/USD). Must be a signed integer (int64). | ||
- `expo1`: The exponent for price1, indicating the number of decimal places. | ||
- `price2`: The second price feed value, representing c / b (e.g., EUR/USD). | ||
- `expo2`: The exponent for price2. | ||
- `targetExponent`: The desired exponent for the output cross rate (a / c). The result will be scaled to this exponent. | ||
|
||
Returns: | ||
|
||
- `crossRate`: The computed cross rate (a / c), scaled to targetExponent. | ||
|
||
### ⚠️ Things to Keep in Mind | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo this should go after the example |
||
|
||
- The function reverts if either price is **negative**, or if any exponent is **less than -255**. | ||
- The result is rounded down. If the result is smaller than 1 in the given `targetExponent{:jsx}`, it will return 0. | ||
- Confidence intervals are not derived in this function. If needed, you have to derive them manually. | ||
- Reverts with `PythErrors.ExponentOverflow{:jsx}` if `targetExponent + expo1 - expo2{:jsx}` is outside the range **[-58, 58]**. | ||
|
||
## Example | ||
|
||
```solidity copy | ||
pragma solidity ^0.8.0; | ||
|
||
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol"; | ||
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol"; | ||
import "@pythnetwork/pyth-sdk-solidity/PythUtils.sol"; | ||
|
||
contract ExampleCrossRate { | ||
IPyth public pyth; | ||
|
||
constructor(address _pythContract) { | ||
pyth = IPyth(_pythContract); | ||
} | ||
|
||
// priceUpdate should include both price feeds | ||
function getEthPerEur( | ||
bytes32 ethUsdId, | ||
bytes32 eurUsdId, | ||
bytes[] calldata priceUpdate | ||
) external payable returns (int64 price, int32 expo) { | ||
// Update both feeds | ||
uint fee = pyth.getUpdateFee(priceUpdate); | ||
pyth.updatePriceFeeds{ value: fee }(priceUpdate); | ||
|
||
// Fetch prices | ||
PythStructs.Price memory ethUsd = pyth.getPriceNoOlderThan(ethUsdId, 60); | ||
PythStructs.Price memory eurUsd = pyth.getPriceNoOlderThan(eurUsdId, 60); | ||
|
||
// Derive ETH/EUR = ETH/USD ÷ EUR/USD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
int32 targetExpo = -8; | ||
int64 ethPerEur = PythUtils.deriveCrossRate( | ||
ethUsd.price, | ||
ethUsd.expo, | ||
eurUsd.price, | ||
eurUsd.expo, | ||
targetExpo | ||
); | ||
|
||
return (ethPerEur, targetExpo); | ||
} | ||
} | ||
|
||
``` | ||
|
||
## Additional Resources | ||
|
||
You may find these additional resources helpful. | ||
|
||
### How to use real-time data in EVM contracts | ||
|
||
The [How to use real-time data in EVM contracts](./use-real-time-data/evm) guide provides a step-by-step guide on how to use real-time data in EVM contracts. | ||
|
||
### Price Feed IDs | ||
|
||
The [Price Feed IDs](https://www.pyth.network/developers/price-feed-ids) page lists the price feed IDs for each asset supported by Pyth. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just pick one of these titles -- this is too long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo "derive cross rate" is better than the other one