|
1 | 1 | ---
|
2 |
| -description: Consume Pyth Network prices in applications on Aptos |
| 2 | +description: Consume Pyth Network prices in Aptos applications |
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# Pyth on Aptos chains |
| 5 | +import { Callout } from "nextra/components"; |
6 | 6 |
|
7 |
| -Pyth is available on Aptos Mainnet, Aptos Testnet, and Movement devnet. |
| 7 | +# How to Use Real-Time Data in Aptos Contracts |
8 | 8 |
|
9 |
| -Aptos contracts can update and fetch the Pyth prices using the Pyth Aptos Contract, which has been deployed on Mainnet. The documented source code can be found [here](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/aptos/contracts/sources/pyth.move). |
| 9 | +This guide explains how to use real-time Pyth data in Aptos applications. |
10 | 10 |
|
11 |
| -## Updating Price Feeds |
| 11 | +## Configuring the `Move.toml` file |
12 | 12 |
|
13 |
| -The mechanism by which price feeds are updated on Aptos is explained [here](./pythnet-price-feeds.md). The [pyth-aptos-js](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/aptos/sdk/js) package can be used to fetch price feed update data which can be passed to the `pyth::update_price` on-chain function. |
| 13 | +Add the Pyth Contract to your project dependencies in the `Move.toml` file: |
14 | 14 |
|
15 |
| -## Example Applications |
| 15 | +```toml copy |
| 16 | +[dependencies] |
| 17 | +Pyth = { git = "https://github.com/pyth-network/pyth-crosschain.git", subdir = "target_chains/aptos/contracts", rev = "main" } |
| 18 | +``` |
16 | 19 |
|
17 |
| -- [Minimal on-chain contract](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/aptos/examples/fetch_btc_price/sources/example.move) which updates and returns the Pyth BTC/USD price. |
18 |
| -- [Full-stack React app and on-chain contract](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/aptos/examples/mint_nft) which uses the [pyth-aptos-js](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/aptos/sdk/js) package to update the price used by the contract. |
19 |
| -- [In-depth explanation](https://youtu.be/0b0RXi41pN0) of how Pyth works on Aptos and how to integrate Pyth data in your application from one of our contributor. |
| 20 | +The named addresses of `pyth`, `wormhole`, and `deployers` must be defined at compile time. These addresses are used to interact with the Pyth contract on Aptos. |
20 | 21 |
|
21 |
| -## Contract Addresses |
| 22 | +```toml copy |
| 23 | +[addresses] |
| 24 | +pyth = "0x7e783b349d3e89cf5931af376ebeadbfab855b3fa239b7ada8f5a92fbea6b387" |
| 25 | +deployer = "0xb31e712b26fd295357355f6845e77c888298636609e93bc9b05f0f604049f434" |
| 26 | +wormhole = "0x5bc11445584a763c1fa7ed39081f1b920954da14e04b32440cba863d03e19625" |
| 27 | +``` |
22 | 28 |
|
23 |
| -Developers will need the address of the Pyth price feed contract on their blockchain in order to use Pyth. |
24 |
| -Please consult [Aptos Contract Addresses](../contract-addresses/aptos) to find the address for your blockchain. |
| 29 | +Consult [Aptos Contract Addresses](../contract-addresses/aptos) for the complete list of contract addresses on different Aptos networks. |
25 | 30 |
|
26 |
| -## Price Feed IDs |
| 31 | +## Write Contract Code |
27 | 32 |
|
28 |
| -| Network | Available Price Feeds | |
29 |
| -| ------------- | ---------------------------------------------------------------------------------------------------------------------------- | |
30 |
| -| Aptos Testnet | [https://pyth.network/developers/price-feed-ids#aptos-testnet](https://pyth.network/developers/price-feed-ids#aptos-testnet) | |
31 |
| -| Aptos Mainnet | [https://pyth.network/developers/price-feed-ids#aptos-mainnet](https://pyth.network/developers/price-feed-ids#aptos-mainnet) | |
| 33 | +The code snippet below provides an example module fetching the BTC/USD price from Pyth price feeds: |
| 34 | + |
| 35 | +```rust {21} copy |
| 36 | +module example::example { |
| 37 | + use pyth::pyth; |
| 38 | + use pyth::price::Price; |
| 39 | + use pyth::price_identifier; |
| 40 | + use aptos_framework::coin; |
| 41 | + |
| 42 | + // Add the pyth_price_update argument to any method on your contract that needs to read the Pyth price. |
| 43 | + // See https://docs.pyth.network/price-feeds/fetch-price-updates for more information on how to fetch the pyth_price_update. |
| 44 | + public fun get_btc_usd_price(user: &signer, pyth_price_update: vector<vector<u8>>): Price { |
| 45 | + |
| 46 | + // First update the Pyth price feeds |
| 47 | + let coins = coin::withdraw(user, pyth::get_update_fee(&pyth_price_update)); |
| 48 | + pyth::update_price_feeds(pyth_price_update, coins); |
| 49 | + |
| 50 | + // Read the current price from a price feed. |
| 51 | + // Each price feed (e.g., BTC/USD) is identified by a price feed ID. |
| 52 | + // The complete list of feed IDs is available at https://pyth.network/developers/price-feed-ids |
| 53 | + // Note: Aptos uses the Pyth price feed ID without the `0x` prefix. |
| 54 | + let btc_price_identifier = x"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"; |
| 55 | + let btc_usd_price_id = price_identifier::from_byte_vec(btc_price_identifier); |
| 56 | + pyth::get_price(btc_usd_price_id) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +<Callout type="info" emoji="ℹ️"> |
| 63 | + The `pyth_price_update` argument contains verified prices from Pyth. Calling |
| 64 | + `pyth::update_price_feeds` with this value updates the on-chain Pyth price and |
| 65 | + ensures your application has recent price data. The pyth_price_update can be |
| 66 | + fetched from Hermes; Consult [Fetch Price Updates](../fetch-price-updates) for |
| 67 | + more information on how to fetch the `pyth_price_update`. |
| 68 | +</Callout> |
| 69 | + |
| 70 | +The code snippet above does the following things: |
| 71 | + |
| 72 | +1. Call `pyth::get_update_fee` to get the fee required to update the Pyth price feeds. |
| 73 | +1. Call `pyth::update_price_feeds` and pass `pyth_price_update` to update the Pyth price feeds. |
| 74 | +1. Call `pyth::get_price` to read the current price, providing the [price feed ID](https://pyth.network/developers/price-feed-ids) you wish to read. |
| 75 | + |
| 76 | +## Additional Resources |
| 77 | + |
| 78 | +You may find these additional resources helpful for developing your Aptos application. |
| 79 | + |
| 80 | +### API Reference |
| 81 | + |
| 82 | +The [Aptos API reference](../api-reference/aptos/) lets you interactively explore the complete API of the Pyth contract. |
| 83 | + |
| 84 | +### Example Applications |
| 85 | + |
| 86 | +- [Minimal on-chain contract](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/aptos/fetch_btc_price/sources/example.move), which updates and returns the BTC/USD price from Pyth price feeds. |
| 87 | +- [Mint NFT](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/aptos/mint_nft) that use Pyth price feeds to mint an NFT. |
0 commit comments