-
Notifications
You must be signed in to change notification settings - Fork 309
feat(solana-receiver-js-sdk): verify & post TWAPs #2186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tejasbadadare
merged 20 commits into
main
from
tb/solana-receiver-js-sdk/post-twap-updates
Dec 19, 2024
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4451b45
feat: update IDL
tejasbadadare 8458dc4
feat: add postTwapUpdates function
tejasbadadare e61f976
Merge branch 'main' of github.com:pyth-network/pyth-crosschain into t…
tejasbadadare 9474df5
refactor: clean up
tejasbadadare 5fbf06c
Merge branch 'main' of github.com:pyth-network/pyth-crosschain into t…
tejasbadadare e9445f5
feat: add priceFeedIdToTwapUpdateAccount and update docs
tejasbadadare 1970a2a
doc: update readme
tejasbadadare 920aa9c
Apply suggestions from code review
tejasbadadare faf0407
refactor: address pr comments
tejasbadadare 82dbaba
Merge branch 'tb/solana-receiver-js-sdk/post-twap-updates' of github.…
tejasbadadare 78aee7b
refactor: extract shared VAA instruction building into `generateVaaIn…
tejasbadadare 2cb0b4b
refactor: keep buildCloseEncodedVaaInstruction in PythSolanaReceiver …
tejasbadadare e2df274
fix: update compute budget for postTwapUpdate based on devnet runs
tejasbadadare e4d4d1b
fix: fix comment
tejasbadadare 2628133
fix: imports, compute budget
tejasbadadare 7daa553
Merge branch 'main' of github.com:pyth-network/pyth-crosschain into t…
tejasbadadare 36c21a2
fix: add reclaimTwapRent to recv contract
tejasbadadare 4699780
fix(cli): increase compute budget to avoid serde issues, add print st…
tejasbadadare 404c21a
Apply suggestions from code review
tejasbadadare 200747f
doc: update docstring
tejasbadadare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_twap_update.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { Connection, Keypair, PublicKey } from "@solana/web3.js"; | ||
| import { InstructionWithEphemeralSigners, PythSolanaReceiver } from "../"; | ||
| import { Wallet } from "@coral-xyz/anchor"; | ||
| import fs from "fs"; | ||
| import os from "os"; | ||
tejasbadadare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { HermesClient } from "@pythnetwork/hermes-client"; | ||
| import { sendTransactions } from "@pythnetwork/solana-utils"; | ||
|
|
||
| // Get price feed ids from https://pyth.network/developers/price-feed-ids#pyth-evm-stable | ||
| const SOL_PRICE_FEED_ID = | ||
| "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"; | ||
| const ETH_PRICE_FEED_ID = | ||
| "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; | ||
|
|
||
| let keypairFile = ""; | ||
| if (process.env["SOLANA_KEYPAIR"]) { | ||
| keypairFile = process.env["SOLANA_KEYPAIR"]; | ||
| } else { | ||
| keypairFile = `${os.homedir()}/.config/solana/id.json`; | ||
| } | ||
|
|
||
| async function main() { | ||
| const connection = new Connection("https://api.devnet.solana.com"); | ||
| const keypair = await loadKeypairFromFile(keypairFile); | ||
| console.log( | ||
| `Sending transactions from account: ${keypair.publicKey.toBase58()}` | ||
| ); | ||
| const wallet = new Wallet(keypair); | ||
| const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet }); | ||
|
|
||
| // Get the TWAP update from hermes | ||
| const twapUpdateData = await getTwapUpdateData(); | ||
| console.log(`Posting TWAP update: ${twapUpdateData}`); | ||
|
|
||
| // Similar to price updates, we'll keep closeUpdateAccounts = false for easy exploration | ||
| const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({ | ||
| closeUpdateAccounts: false, | ||
| }); | ||
|
|
||
| // Post the TWAP updates to ephemeral accounts, one per price feed | ||
| await transactionBuilder.addPostTwapUpdates(twapUpdateData); | ||
| console.log( | ||
| "The SOL/USD TWAP update will get posted to:", | ||
| transactionBuilder.getTwapUpdateAccount(SOL_PRICE_FEED_ID).toBase58() | ||
| ); | ||
|
|
||
| await transactionBuilder.addTwapConsumerInstructions( | ||
| async ( | ||
| getTwapUpdateAccount: (priceFeedId: string) => PublicKey | ||
| ): Promise<InstructionWithEphemeralSigners[]> => { | ||
| // You can generate instructions here that use the TWAP updates posted above. | ||
| // getTwapUpdateAccount(<price feed id>) will give you the account you need. | ||
| return []; | ||
| } | ||
| ); | ||
|
|
||
| // Send the instructions in the builder in 1 or more transactions | ||
| sendTransactions( | ||
| await transactionBuilder.buildVersionedTransactions({ | ||
| computeUnitPriceMicroLamports: 100000, | ||
| tightComputeBudget: true, | ||
| }), | ||
| pythSolanaReceiver.connection, | ||
| pythSolanaReceiver.wallet | ||
| ); | ||
| } | ||
|
|
||
| // Fetch TWAP update data from Hermes | ||
| async function getTwapUpdateData() { | ||
| const hermesConnection = new HermesClient("https://hermes.pyth.network/", {}); | ||
|
|
||
| // Request TWAP updates for the last hour (3600 seconds) | ||
| const response = await hermesConnection.getLatestTwapUpdates( | ||
| [SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID], | ||
| 3600, | ||
| { encoding: "base64" } | ||
| ); | ||
|
|
||
| return response.binary.data; | ||
| } | ||
|
|
||
| // Load a solana keypair from an id.json file | ||
| async function loadKeypairFromFile(filePath: string): Promise<Keypair> { | ||
| try { | ||
| const keypairData = JSON.parse( | ||
| await fs.promises.readFile(filePath, "utf8") | ||
| ); | ||
| return Keypair.fromSecretKey(Uint8Array.from(keypairData)); | ||
| } catch (error) { | ||
| throw new Error(`Error loading keypair from file: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.