You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/price-feeds/use-real-time-data/solana.mdx
+130Lines changed: 130 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -241,6 +241,136 @@ The [SDK documentation](https://github.com/pyth-network/pyth-crosschain/tree/mai
241
241
partially verified price updates.
242
242
</Callout>
243
243
244
+
## Time-Weighted Average Price (TWAP)
245
+
246
+
Pyth also provides Time-Weighted Average Price (TWAP) for Solana applications. TWAP represents the average price over a specified time window, which can be useful for reducing the impact of short-term price volatility. The TWAP window is currently limited to a maximum of 10 minutes (600 seconds).
247
+
248
+
### Using TWAP in Solana Programs
249
+
250
+
To use TWAP in your Solana program, import the `TwapUpdate` struct from the Pyth Solana receiver SDK. The process for fetching and posting TWAP updates is similar to regular price updates from Hermes.
["0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"], // BTC/USD feed ID
312
+
twapWindowSeconds,
313
+
{ encoding: "base64" }
314
+
);
315
+
316
+
// TWAP updates are strings of base64-encoded binary data
317
+
console.log(twapUpdateData.binary.data);
318
+
```
319
+
320
+
For a complete example of fetching TWAP updates from Hermes, see the [HermesClient example script](https://github.com/pyth-network/pyth-crosschain/blob/main/apps/hermes/client/js/src/examples/HermesClient.ts) in the Pyth crosschain repository.
321
+
322
+
#### Post TWAP updates to Solana
323
+
324
+
Use `PythSolanaReceiver` to post the TWAP updates and consume them in your application:
For a complete example of posting TWAP updates to Solana, see the [post_twap_update.ts example script](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_twap_update.ts) in the Pyth crosschain repository.
363
+
364
+
### Example Application
365
+
366
+
See an end-to-end example of using Price Update Accounts for spot prices or TWAP Accounts for time-averaged prices in the [SendUSD Solana Demo App](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/solana/send_usd). It demonstrates how to fetch data from Hermes, post it to Solana, and consume it from a smart contract. The example includes:
367
+
368
+
- A React frontend for interacting with the contract
369
+
- A Solana program that consumes TWAP updates
370
+
- Complete transaction building for posting and consuming TWAP data
371
+
372
+
The example allows users to send a USD-denominated amount of SOL using either spot prices or TWAP prices, demonstrating how TWAP can be used to reduce the impact of price volatility.
373
+
244
374
## Additional Resources
245
375
246
376
You may find these additional resources helpful for developing your Solana application.
0 commit comments