2
2
description : Consume Pyth Network prices in Solana applications
3
3
---
4
4
5
+ import { Callout } from " nextra/components" ;
6
+
5
7
# How to Use Real-Time Data in Solana Programs
6
8
7
9
This guide explains how to use real-time Pyth data in Solana applications.
8
10
9
11
## Install Pyth SDKs
10
12
11
- We provide two SDKs for Solana applications to cover the on- and off-chain portions of the integration:
13
+ Pyth provides two SDKs for Solana applications to cover the on- and off-chain portions of the integration:
12
14
13
15
### Rust SDK
14
16
@@ -31,11 +33,11 @@ npm install --save @pythnetwork/price-service-client @pythnetwork/pyth-solana-re
31
33
32
34
## Write Contract Code
33
35
34
- Add code to your Solana program to read the Pyth price .
36
+ Add the following code to your Solana program to read Pyth prices .
35
37
Pyth prices are posted to price update accounts that can be passed to any instruction that needs price data.
36
- If you are using Anchor, you can simply add an ` Account<'info, PriceUpdateV2> ` field to your ` Context ` struct:
38
+ For developers using Anchor, simply add an ` Account<'info, PriceUpdateV2> ` field to the ` Context ` struct:
37
39
38
- ``` rust copy
40
+ ``` rust {9} copy
39
41
use pyth_solana_receiver_sdk :: price_update :: {PriceUpdateV2 };
40
42
41
43
#[derive(Accounts )]
@@ -48,9 +50,11 @@ pub struct Sample<'info> {
48
50
}
49
51
```
50
52
51
- Warning: users must ensure that the account passed to their instruction is owned by the Pyth pull oracle program.
53
+ <Callout type = " info" emoji = " ℹ️" >
54
+ Users must ensure that the account passed to their instruction is owned by the Pyth Pull Oracle program.
52
55
Using Anchor with the ` Account<'info, PriceUpdateV2> ` type will automatically perform this check.
53
- However, if you are not using Anchor, it is your responsibility to perform this check.
56
+ However, it is the developer's responsibility to perform this check if they are not using Anchor.
57
+ </Callout >
54
58
55
59
Next, update the instruction logic to read the price from the price update account:
56
60
@@ -60,7 +64,7 @@ pub fn sample(ctx: Context<Sample>) -> Result<()> {
60
64
// get_price_no_older_than will fail if the price update is more than 30 seconds old
61
65
let maximum_age : u64 = 30 ;
62
66
// get_price_no_older_than will fail if the price update is for a different price feed.
63
- // This string is the id of the BTC/USD feed. See https://pyth.network/developers/price-feed-ids for all available ids .
67
+ // This string is the id of the BTC/USD feed. See https://pyth.network/developers/price-feed-ids for all available IDs .
64
68
let feed_id : [u8 ; 32 ] = get_feed_id_from_hex (" 0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43" )? ;
65
69
let price = price_update . get_price_no_older_than (& Clock :: get ()? , maximum_age , & feed_id )? ;
66
70
// Sample output:
@@ -71,17 +75,24 @@ pub fn sample(ctx: Context<Sample>) -> Result<()> {
71
75
}
72
76
```
73
77
74
- Warning: it is your responsibility to validate that the provided price update is for the appropriate price feed and timestamp.
75
- ` PriceUpdateV2 ` guarantees that the account contains a verified price for _ some_ price feed at _ some_ point in time.
76
- There are various methods on this struct (such as ` get_price_no_older_than ` ) that you can use to implement the necessary checks.
77
- Note: if you choose the price feed account integration (see below), you can use an account address check to validate the price feed id.
78
+ <Callout type = " warning" emoji = " ⚠️" >
79
+ Users must validate the price update for the appropriate ** price
80
+ feed** and ** timestamp** . ` PriceUpdateV2 ` guarantees that the account contains
81
+ a verified price for _ some_ price feed at _ some_ point in time. There are
82
+ various methods on this struct (such as ` get_price_no_older_than ` ) that users
83
+ can use to implement the necessary checks.
84
+
85
+ If you choose the price feed account integration (see below), you
86
+ can use an account address check to validate the price feed ID.
87
+
88
+ </Callout >
78
89
79
90
## Write Frontend Code
80
91
81
92
There are two different paths to the frontend integration of Pyth prices on Solana.
82
93
Developers can choose to use two different types of accounts:
83
94
84
- - ** Price feed accounts** hold a sequence of prices for a specific price feed id that always moves forward in time.
95
+ - ** Price feed accounts** hold a sequence of prices for a specific price feed ID that always moves forward in time.
85
96
These accounts have a fixed address that your program can depend on.
86
97
The Pyth Data Association maintains a set of price feed accounts that are continuously updated.
87
98
Such accounts are a good fit for applications that always want to consume the most recent price.
@@ -94,7 +105,7 @@ Both options are explained in the sections below, and developers should pick the
94
105
95
106
### Price Feed Accounts
96
107
97
- If you are using price feed accounts, your frontend code simply needs to pass the relevant price feed account address to the transaction.
108
+ For developers using price feed accounts, the frontend code needs to pass the relevant price feed ** account address** to the transaction.
98
109
Price feed accounts are program-derived addresses and thus the account ID for any price feed can be derived automatically.
99
110
The ` PythSolanaReceiver ` class provides a method for deriving this information:
100
111
@@ -115,16 +126,24 @@ const solUsdPriceFeedAccount = pythSolanaReceiver
115
126
.toBase58 ();
116
127
```
117
128
118
- The price feed account integration assumes that an off-chain process is continuously updating each price feed.
119
- The Pyth Data Association sponsors price updates for a subset of commonly-used price feeds on shard 0.
120
- Please see [ Sponsored Feeds] ( /price-feeds/sponsored-feeds ) for a list of sponsored feeds and their account addresses.
121
- However, updating a price feed is a permissionless operation, and anyone can run this process.
122
- Please see [ Using Scheduler] ( /price-feeds/schedule-price-updates/using-scheduler ) for more information.
123
- Running the scheduler can help with reliability and to update feed/shard pairs that are not part of the default schedule.
129
+ <Callout type = " info" emoji = " ℹ️" >
130
+ The Price Feed Accounts integration assumes that an off-chain process is
131
+ continuously updating each price feed. The Pyth Data Association sponsors
132
+ price updates for a subset of commonly used price feeds on shard 0. Please see
133
+ [ Sponsored Feeds] ( /price-feeds/sponsored-feeds ) for a list of sponsored feeds
134
+ and their account addresses.
135
+
136
+ Additionally, updating a price feed is a
137
+ permissionless operation, and anyone can run this process. Please see [ Using
138
+ Scheduler] ( /price-feeds/schedule-price-updates/using-scheduler ) for more
139
+ information. Running the scheduler can help with reliability and update
140
+ feed/shard pairs that are not part of the default schedule.
141
+
142
+ </Callout >
124
143
125
144
### Price Update Accounts
126
145
127
- If you are using price update accounts, your frontend code needs to perform two different tasks:
146
+ To use price update accounts, the frontend code needs to perform two different tasks:
128
147
129
148
1 . Fetch price updates from Hermes
130
149
2 . Post the price updates to Solana and invoke your application logic
@@ -137,7 +156,7 @@ Use `PriceServiceConnection` from `@pythnetwork/price-service-client` to fetch P
137
156
import { PriceServiceConnection } from " @pythnetwork/price-service-client" ;
138
157
139
158
// The URL below is a public Hermes instance operated by the Pyth Data Association.
140
- // Hermes is also available from several third party providers listed here:
159
+ // Hermes is also available from several third- party providers listed here:
141
160
// https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes
142
161
const priceServiceConnection = new PriceServiceConnection (
143
162
" https://hermes.pyth.network/" ,
@@ -155,6 +174,8 @@ const priceUpdateData: string[] = await priceServiceConnection.getLatestVaas([
155
174
console .log (priceUpdateData );
156
175
```
157
176
177
+ Consult [ Fetch Price Updates] ( /price-feeds/fetch-price-updates ) for more information on fetching price updates from Hermes.
178
+
158
179
#### Post price updates
159
180
160
181
Finally, post the price update to the Pyth program on Solana.
0 commit comments