Pyth EVM JS
Pyth provides real-time pricing data in a variety of asset classes, including cryptocurrency,
equities, FX and commodities. This library allows you to use these real-time prices on EVM-based networks.
Installation
npm
$ npm install --save @pythnetwork/pyth-evm-js
Yarn
$ yarn add @pythnetwork/pyth-evm-js
Quickstart
Pyth stores prices off-chain to minimize gas fees, which allows us to offer a wider selection of products and faster
update times. See On-Demand Updates for more
information about this approach. In order to use Pyth prices on chain, they must be fetched from an off-chain Hermes
instance. The EvmPriceServiceConnection
class can be used to interact with these services, providing a way to fetch
these prices directly in your code. The following example wraps an existing RPC provider and shows how to obtain Pyth
prices and submit them to the network:
const connection = new EvmPriceServiceConnection("https://hermes.pyth.network");
const priceIds = [
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace",
];
const priceUpdateData = await connection.getPriceFeedsUpdateData(priceIds);
const updateFee = await pythContract.methods
.getUpdateFee(priceUpdateData)
.call();
await someContract.methods
.doSomething(someArg, otherArg, priceUpdateData)
.send({ value: updateFee });
SomeContract
looks like so:
pragma solidity ^0.8.0;
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
contract SomeContract {
IPyth pyth;
constructor(address pythContract) {
pyth = IPyth(pythContract);
}
function doSomething(
uint someArg,
string memory otherArg,
bytes[] calldata priceUpdateData
) public payable {
// Update the prices to be set to the latest values
uint fee = pyth.getUpdateFee(priceUpdateData);
pyth.updatePriceFeeds{ value: fee }(priceUpdateData);
// Doing other things that uses prices
bytes32 priceId = 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b;
// Get the price if it is not older than 10 seconds from the current time.
PythStructs.Price price = pyth.getPriceNoOlderThan(priceId, 10);
}
}
We strongly recommend reading our guide which explains how to work with Pyth price feeds.
Off-chain prices
Many applications additionally need to display Pyth prices off-chain, for example, in their frontend application.
The EvmPriceServiceConnection
provides two different ways to fetch the current Pyth price.
The code blocks below assume that the connection
and priceIds
objects have been initialized as shown above.
The first method is a single-shot query:
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
console.log(priceFeeds[0].getPriceNoOlderThan(60));
console.log(priceFeeds[1].getEmaPriceNoOlderThan(60));
The object also supports a streaming websocket connection that allows you to subscribe to every new price update for a given feed.
This method is useful if you want to show continuously updating real-time prices in your frontend:
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
console.log(
`Received update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(60)}`
);
});
setTimeout(() => {
connection.closeWebSocket();
}, 60000);
Examples
There are two examples in examples.
EvmPriceServiceClient
This example fetches PriceFeed
updates using both a HTTP-request API and a streaming websocket API. You can run it with npm run example-client
. A full command that prints BTC and ETH price feeds, in the testnet network, looks like so:
npm run example-client -- \
--endpoint https://hermes.pyth.network \
--price-ids \
0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43 \
0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace
EvmRelay
This example shows how to update prices on an EVM network. It does the following:
- Gets update data to update given price feeds.
- Calls the pyth contract with the update data.
- Submits it to the network and prints the txhash if successful.
You can run this example with npm run example-relay
. A full command that updates BTC and ETH prices on the BNB Chain
testnet network looks like so:
npm run example-relay -- \
--network "https://data-seed-prebsc-1-s1.binance.org:8545" \
--pyth-contract "0x5744Cbf430D99456a0A8771208b674F27f8EF0Fb"\
--mnemonic "my good mnemonic" \
--endpoint https://hermes.pyth.network \
--price-ids \
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43" \
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"
Hermes endpoints
Pyth offers a free public endpoint at https://hermes.pyth.network. However, it is
recommended to obtain a private endpoint from one of the Hermes RPC providers for more reliability. You can find more
information about Hermes RPC providers
here.