Skip to content

Commit 62ee2a7

Browse files
authored
(chore): Solana guide update (#298)
* solana guide update * resolved comments * requested comments * tiny update
1 parent 202d445 commit 62ee2a7

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

pages/price-feeds/use-real-time-data/solana.mdx

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
description: Consume Pyth Network prices in Solana applications
33
---
44

5+
import { Callout } from "nextra/components";
6+
57
# How to Use Real-Time Data in Solana Programs
68

79
This guide explains how to use real-time Pyth data in Solana applications.
810

911
## Install Pyth SDKs
1012

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:
1214

1315
### Rust SDK
1416

@@ -31,11 +33,11 @@ npm install --save @pythnetwork/price-service-client @pythnetwork/pyth-solana-re
3133

3234
## Write Contract Code
3335

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.
3537
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:
3739

38-
```rust copy
40+
```rust {9} copy
3941
use pyth_solana_receiver_sdk::price_update::{PriceUpdateV2};
4042

4143
#[derive(Accounts)]
@@ -48,9 +50,11 @@ pub struct Sample<'info> {
4850
}
4951
```
5052

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.
5255
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>
5458

5559
Next, update the instruction logic to read the price from the price update account:
5660

@@ -60,7 +64,7 @@ pub fn sample(ctx: Context<Sample>) -> Result<()> {
6064
// get_price_no_older_than will fail if the price update is more than 30 seconds old
6165
let maximum_age: u64 = 30;
6266
// 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.
6468
let feed_id: [u8; 32] = get_feed_id_from_hex("0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43")?;
6569
let price = price_update.get_price_no_older_than(&Clock::get()?, maximum_age, &feed_id)?;
6670
// Sample output:
@@ -71,17 +75,24 @@ pub fn sample(ctx: Context<Sample>) -> Result<()> {
7175
}
7276
```
7377

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>
7889

7990
## Write Frontend Code
8091

8192
There are two different paths to the frontend integration of Pyth prices on Solana.
8293
Developers can choose to use two different types of accounts:
8394

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.
8596
These accounts have a fixed address that your program can depend on.
8697
The Pyth Data Association maintains a set of price feed accounts that are continuously updated.
8798
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
94105

95106
### Price Feed Accounts
96107

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.
98109
Price feed accounts are program-derived addresses and thus the account ID for any price feed can be derived automatically.
99110
The `PythSolanaReceiver` class provides a method for deriving this information:
100111

@@ -115,16 +126,24 @@ const solUsdPriceFeedAccount = pythSolanaReceiver
115126
.toBase58();
116127
```
117128

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>
124143

125144
### Price Update Accounts
126145

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:
128147

129148
1. Fetch price updates from Hermes
130149
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
137156
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
138157

139158
// 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:
141160
// https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes
142161
const priceServiceConnection = new PriceServiceConnection(
143162
"https://hermes.pyth.network/",
@@ -155,6 +174,8 @@ const priceUpdateData: string[] = await priceServiceConnection.getLatestVaas([
155174
console.log(priceUpdateData);
156175
```
157176

177+
Consult [Fetch Price Updates](/price-feeds/fetch-price-updates) for more information on fetching price updates from Hermes.
178+
158179
#### Post price updates
159180

160181
Finally, post the price update to the Pyth program on Solana.

0 commit comments

Comments
 (0)