|
| 1 | +# Pyth Aptos JS |
| 2 | + |
| 3 | +[Pyth](https://pyth.network/) 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 Aptos networks. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +### npm |
| 8 | + |
| 9 | +``` |
| 10 | +$ npm install --save @pythnetwork/pyth-aptos-js |
| 11 | +``` |
| 12 | + |
| 13 | +### Yarn |
| 14 | + |
| 15 | +``` |
| 16 | +$ yarn add @pythnetwork/pyth-aptos-js |
| 17 | +``` |
| 18 | + |
| 19 | +## Quickstart |
| 20 | + |
| 21 | +Pyth stores prices off-chain to minimize gas fees, which allows us to offer a wider selection of products and faster update times. |
| 22 | +To use Pyth prices on chain, |
| 23 | +they must be fetched from an off-chain price service. The `AptosPriceServiceConnection` class can be used to interact with these services, |
| 24 | +providing a way to fetch these prices directly in your code. The following example wraps an existing RPC provider and shows how to obtain |
| 25 | +Pyth prices and submit them to the network: |
| 26 | + |
| 27 | +```typescript |
| 28 | +const connection = new AptosPriceServiceConnection( |
| 29 | + "https://xc-testnet.pyth.network" |
| 30 | +); // See Price Service endpoints section below for other endpoints |
| 31 | + |
| 32 | +const priceIds = [ |
| 33 | + // You can find the ids of prices at https://pyth.network/developers/price-feed-ids/#pyth-cross-chain-testnet |
| 34 | + "0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", // BTC/USD price id in testnet |
| 35 | + "0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", // ETH/USD price id in testnet |
| 36 | +]; |
| 37 | + |
| 38 | +// `getPythLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has |
| 39 | +// utility functions to get the current and exponentially-weighted moving average price, and other functionality. |
| 40 | +const priceFeeds = connection.getPythLatestPriceFeeds(priceIds); |
| 41 | +console.log(priceFeeds[0].getCurrentPrice()); // Price { conf: '1234', expo: -8, price: '12345678' } |
| 42 | +console.log(priceFeeds[1].getEmaPrice()); // Exponentially-weighted moving average price |
| 43 | + |
| 44 | +// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target |
| 45 | +// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should |
| 46 | +// call the Pyth Contract with this data. |
| 47 | +const priceUpdateData = await connection.getPriceUpdateData(priceIds); |
| 48 | + |
| 49 | +// Create a transaction and submit to your contract using the price update data |
| 50 | +const client = new AptosClient(endpoint); |
| 51 | +let result = await client.generateSignSubmitWaitForTransaction( |
| 52 | + sender, |
| 53 | + new TxnBuilderTypes.TransactionPayloadEntryFunction( |
| 54 | + TxnBuilderTypes.EntryFunction.natural( |
| 55 | + "0x..::your_module", |
| 56 | + "do_something", |
| 57 | + [], |
| 58 | + [priceUpdateData] |
| 59 | + ) |
| 60 | + ) |
| 61 | +); |
| 62 | +``` |
| 63 | + |
| 64 | +`your_module::do_something` should then call `pyth::update_price_feeds` before querying the data using `pyth::get_price`: |
| 65 | + |
| 66 | +```move |
| 67 | +module example::your_module { |
| 68 | + use pyth::pyth; |
| 69 | + use pyth::price_identifier; |
| 70 | + use aptos_framework::coin; |
| 71 | +
|
| 72 | + public fun do_something(user: &signer, pyth_update_data: vector<vector<u8>>) { |
| 73 | +
|
| 74 | + // First update the Pyth price feeds |
| 75 | + let coins = coin::withdraw(user, pyth::get_update_fee()); |
| 76 | + pyth::update_price_feeds(pyth_update_data, coins); |
| 77 | +
|
| 78 | + // Now we can use the prices which we have just updated |
| 79 | + let btc_usd_price_id = price_identifier::from_byte_vec( |
| 80 | + x"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"); |
| 81 | + let price = pyth::get_price(btc_usd_price_id); |
| 82 | +
|
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +We strongly recommend reading our guide which explains [how to work with Pyth price feeds](https://docs.pyth.network/consume-data/best-practices). |
| 88 | + |
| 89 | +### Example |
| 90 | + |
| 91 | +[This example](./src/examples/AptosRelay.ts) shows how to update prices on an EVM network. It does the following: |
| 92 | + |
| 93 | +1. Fetches update data from the Price Service for the given price feeds. |
| 94 | +2. Calls the Pyth Aptos contract with the update data. |
| 95 | + |
| 96 | +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: |
| 97 | + |
| 98 | +```bash |
| 99 | +export APTOS_KEY = "0x..."; |
| 100 | +npm run example-relay -- --endpoint https://xc-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6 --full-node https://fullnode.testnet.aptoslabs.com/v1 --pyth-contract 0xc655df014ec65d43b478e8e580a2e9493db954818efb01f1cb4a24654294a709 |
| 101 | +``` |
| 102 | + |
| 103 | +## How Pyth Works on Aptos |
| 104 | + |
| 105 | +Pyth prices are published on Pythnet, and relayed to Aptos using the [Wormhole Network](https://wormholenetwork.com/) as a cross-chain message passing bridge. The Wormhole Network observes when Pyth prices on Pythnet have changed and publishes an off-chain signed message attesting to this fact. This is explained in more detail [here](https://docs.wormholenetwork.com/wormhole/). |
| 106 | + |
| 107 | +This signed message can then be submitted to the Pyth contract on the Aptos networks, which will verify the Wormhole message and update the Pyth contract with the new price. |
| 108 | + |
| 109 | +### On-demand price updates |
| 110 | + |
| 111 | +Price updates are not submitted on the EVM networks automatically: rather, when a consumer needs to use the value of a price they should first submit the latest Wormhole update for that price to the Pyth contract on the EVM network they are working on. This will make the most recent price update available on-chain for EVM contracts to use. |
| 112 | + |
| 113 | +## Price Service endpoints |
| 114 | + |
| 115 | +We provide a public endpoint for the Price Service for Testnet, and will provide one for Mainnet on launch day. |
| 116 | + |
| 117 | +| Aptos Network | Price Service URL | |
| 118 | +| ------------- | ------------------------------- | |
| 119 | +| Testnet | https://xc-testnet.pyth.network | |
0 commit comments