Feed updates
Commodities.CLLX6/USDremovedCrypto.PRL/USDwent liveCommodities.LIZ6/CNYaddedCommodities.LIX6/CNYaddedCommodities.WTIZ7/USDaddedCommodities.WTIX7/USDaddedCommodities.WTIV7/USDaddedCommodities.WTIU7/USDaddedCommodities.WTIQ7/USDaddedCommodities.WTIN7/USDaddedCommodities.WTIM7/USDaddedCommodities.WTIK7/USDaddedCommodities.WTIJ7/USDaddedCommodities.WTIH7/USDadded
View all
Developer Hub

Derive Cross Rates

Learn how to derive synthetic cross rates from two price feeds

This guide shows how to combine two price feeds to derive a cross rate. These are also known as "synthetic" price feeds. Cross rates or Synthetic Price feeds are useful for trading pairs that are not directly supported by Pyth.

For example, if you want to trade the price of ETH/EUR, which is not directly supported by Pyth, you can combine the price of ETH/USD and EUR/USD to derive the price of ETH/EUR.

ETH/EUR=ETH/USD÷EUR/USD\large{\text{ETH/EUR} = \text{ETH/USD} \div \text{EUR/USD}}

Derive a Cross Rate

Each feed gives a price as an integer plus an exponent. Convert each to a decimal (price × 10exponent), then combine them:

  • Divide when both feeds share the quote currency: ETH/USD ÷ EUR/USD = ETH/EUR.
  • Multiply when the shared currency is the quote of one feed and the base of the other: ETH/USD × USD/JPY = ETH/JPY. Many FX feeds use USD as the base, such as USD/JPY, USD/CHF and USD/CAD.

Example: all three prices from one Pyth Pro response:

FeedPriceExponentDecimal
ETH/USD239431451523-82394.31451523
EUR/USD112392-51.12392
USD/JPY158002-3158.002

So ETH/EUR = 2394.31451523 ÷ 1.12392 ≈ 2130.3247, and ETH/JPY = 2394.31451523 × 158.002 ≈ 378,306.48.

A cross rate is only as fresh as its older feed. FX feeds pause outside FX market hours, so on a weekend ETH/EUR moves only with ETH/USD. Check feedUpdateTimestamp and marketSession for both feeds before using the result.

On-Chain

The following sections show how to calculate cross rates on-chain using Pyth Pro and Pyth Core contracts.

Pyth Pro Contracts

Subscribe or fetch one update carrying both feeds, in the evm format with the price and exponent properties (Subscribe to Prices). After verifying the update, look up each feed by its ID, then divide:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import { PythLazer } from "pyth-lazer-sdk/PythLazer.sol";
import { PythLazerLib } from "pyth-lazer-sdk/PythLazerLib.sol";
import { PythLazerStructs } from "pyth-lazer-sdk/PythLazerStructs.sol";

contract EthEurConsumer {
  using PythLazerLib for PythLazerStructs.Feed;

  uint32 constant ETH_USD = 2;
  uint32 constant EUR_USD = 327;
  int256 constant TARGET_EXPONENT = -8;

  PythLazer public pythLazer;

  constructor(address pythLazerAddress) {
    pythLazer = PythLazer(pythLazerAddress);
  }

  function ethEur(bytes calldata update) external payable returns (uint256) {
    (bytes memory payload, ) = pythLazer.verifyUpdate{
      value: pythLazer.verification_fee()
    }(update);
    PythLazerStructs.Update memory parsed = PythLazerLib.parseUpdateFromPayload(
      payload
    );

    int64 ethUsd;
    int16 ethUsdExponent;
    int64 eurUsd;
    int16 eurUsdExponent;
    for (uint256 i = 0; i < parsed.feeds.length; i++) {
      PythLazerStructs.Feed memory feed = parsed.feeds[i];
      if (feed.getFeedId() == ETH_USD) {
        ethUsd = feed.getPrice();
        ethUsdExponent = feed.getExponent();
      } else if (feed.getFeedId() == EUR_USD) {
        eurUsd = feed.getPrice();
        eurUsdExponent = feed.getExponent();
      }
    }
    require(ethUsd > 0 && eurUsd > 0, "missing price");

    int256 shift = int256(ethUsdExponent) - eurUsdExponent - TARGET_EXPONENT;
    uint256 base = uint256(int256(ethUsd));
    uint256 quote = uint256(int256(eurUsd));
    if (shift >= 0) {
      return (base * 10 ** uint256(shift)) / quote;
    }
    return base / (quote * 10 ** uint256(-shift));
  }
}

With the prices above, ethEur returns 213032468078, that is, 2130.32468078 at exponent -8. For a multiply cross such as ETH/JPY, multiply the two prices and add the second exponent instead of subtracting it.

The same arithmetic works on every chain Pyth Pro supports. The chain guides show how to verify an update on each chain.

Legacy Contracts

The legacy Pyth Core SDKs include helpers for calculating cross rates. Both only divide.

Derive a cross rate

The Pyth Solidity SDK provides deriveCrossRate function to combine two price feeds. This method is available in Pyth solidity SDK.

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.

Example

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 (uint256 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
    int32 targetExpo = -8;
    uint256 ethPerEur = PythUtils.deriveCrossRate(
      ethUsd.price,
      ethUsd.expo,
      eurUsd.price,
      eurUsd.expo,
      targetExpo
    );

    return (ethPerEur, targetExpo);
  }
}

⚠️ Things to Keep in Mind

  • 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, it will return 0.
  • Confidence intervals are not derived in this function. If needed, you have to derive them manually.
  • Reverts with PythErrors.ExponentOverflow if targetExponent + expo1 - expo2 is outside the range [-58, 58].

Additional Resources

You may find these additional resources helpful.

On this page