-
Notifications
You must be signed in to change notification settings - Fork 298
docs: code docs for solana sdk #1330
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
Merged
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
2ebde4b
Do it
guibescos c086dcf
Remove some duplicate code
guibescos d32169f
Cleanup
guibescos b49e084
Cleanup
guibescos fb0d06f
Cleanup import
guibescos d08e92a
Correct description
guibescos d73e808
Fix path
guibescos 3113026
Cleanup deps
guibescos 4b645cc
Unique
guibescos 191fa01
Works
guibescos 9172fad
Continue
guibescos 063de56
Lint
guibescos b905266
Lint config
guibescos 81a3307
Fix ci
guibescos 6a2bdbc
Checkpoint
guibescos dc687d1
Checkpoint
guibescos 0045908
Gitignore
guibescos d8757d5
Cleanup
guibescos b854868
Cleanup
guibescos 97f8bd0
Continue building the sdk
guibescos a5f0cae
build function
guibescos 8328496
Remove files
guibescos a0f95ea
Remove files
guibescos 131d8ce
Rename
guibescos f3fc9ef
Refactor : make transaction builder
guibescos db73a1f
Make commitment
guibescos 520e5fb
Move
guibescos eedd806
Progress
guibescos 42c7cd0
Checkpoint
guibescos 07756b1
Ephemeral signers 2
guibescos 16c1f86
Checkpoint
guibescos ef052fa
Checkpoint
guibescos 160c011
Fix bug
guibescos 2f7697c
Cleanup idls
guibescos 89eb8e8
Compute units
guibescos 0e362c5
Make program addresses configurable
guibescos 99b76b6
Handle arrays
guibescos 117a166
Handle arrays
guibescos 2e044b0
Move PythSolanaReceiver
guibescos 66bdbc7
Cleanup constants
guibescos 0068d5f
Contants
guibescos dcdd39f
Refactor constants
guibescos 9a13bb6
Gitignore refactor
guibescos 3a2562c
package lock
guibescos 6b2d09b
Cleanup idl
guibescos ecd2c4d
Add useful static
guibescos 4b0094a
Add useful static
guibescos 5926166
Add useful static
guibescos 6290cab
Lint
guibescos b92e8ce
Add lint config
guibescos 00554e9
Docs
guibescos a5c0fc4
Comments
guibescos afcfcf3
Docs
guibescos 0825092
Merged
guibescos 8e7d416
Don't touch this
guibescos c05b409
Readme
guibescos 2e047f8
Readme
guibescos 40adb68
Cleanup
guibescos 0e68936
Readme
guibescos 7ae5c93
Fix
guibescos 09e4bc3
Merge branch 'main' into solana/js-sdk
guibescos 8bf4dbd
address readme comments
guibescos 64f2daf
from pyth, not pythnet
guibescos d0b6323
Add a couple more comments
guibescos 3d7abc1
Rename cleanup to close
guibescos 9165bbb
Go go go
guibescos 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
83 changes: 83 additions & 0 deletions
83
target_chains/solana/sdk/js/pyth_solana_receiver/README.md
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,83 @@ | ||
# Pyth Solana Receiver JS SDK | ||
|
||
This is a Javascript SDK to interact with the Pyth Solana Receiver contract whose code lives [here](/target_chains/solana). | ||
|
||
## Pull model | ||
|
||
The Pyth Solana Receiver allows users to consume Pyth price updates on a pull basis. This means that the user is responsible for submitting the price data on-chain whenever they want to interact with an app that requires a price update. | ||
|
||
Price updates get posted into price update accounts, owned by the Receiver contract. Once an update has been posted to a price update account, it can be used by anyone by simply passing the price update account as one of the accounts in a Solana instruction. | ||
Price update accounts can be closed by whoever wrote them to recover the rent. | ||
|
||
## Example use | ||
|
||
```ts | ||
import { Connection, PublicKey } from '@solana/web3.js'; | ||
import { PriceServiceConnection } from '@pythnetwork/price-service-client'; | ||
import { PythSolanaReceiver } from '@pythnetwork/pyth-solana-receiver'; | ||
import { MyFirstPythApp, IDL } from './idl/my_first_pyth_app'; | ||
|
||
|
||
const SOL_PRICE_FEED_ID = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d" | ||
const ETH_PRICE_FEED_ID = "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace" | ||
|
||
const priceServiceConnection = new PriceServiceConnection("https://hermes.pyth.network/", { priceFeedRequestConfig: { binary: true } }); | ||
const priceUpdateData = await priceServiceConnection.getLatestVaas([SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID]); // Fetch off-chain price update data | ||
|
||
|
||
const myFirstPythApp = new Program<MyFirstPythApp>(IDL as MyFirstPythApp, , PublicKey.unique(), {}) | ||
const getInstructions = async (priceFeedIdToPriceUpdateAccount: Record<string, PublicKey>) => { return [{ instruction: await myFirstApp.methods.consume().accounts({ solPriceUpdate: priceFeedIdToPriceUpdateAccount[SOL_PRICE_FEED_ID], ethPriceUpdate: priceFeedIdToPriceUpdateAccount[ETH_PRICE_FEED_ID] }).instruction(), signers: [] }] }; | ||
|
||
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet }); | ||
const transactions = await pythSolanaReceiver.withPriceUpdate(priceUpdateData, getInstructions, {}) | ||
await pythSolanaReceiver.provider.sendAll(transactions); | ||
``` | ||
|
||
Or, alternatively: | ||
|
||
```ts | ||
import { PublicKey } from "@solana/web3.js"; | ||
import { PriceServiceConnection } from "@pythnetwork/price-service-client"; | ||
import { PythSolanaReceiver } from "@pythnetwork/pyth-solana-receiver"; | ||
import { MyFirstPythApp, IDL } from "./idl/my_first_pyth_app"; | ||
|
||
const SOL_PRICE_FEED_ID = | ||
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"; | ||
const ETH_PRICE_FEED_ID = | ||
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; | ||
|
||
const priceServiceConnection = new PriceServiceConnection( | ||
"https://hermes.pyth.network/", | ||
{ priceFeedRequestConfig: { binary: true } } | ||
); | ||
const priceUpdateData = await priceServiceConnection.getLatestVaas([ | ||
SOL_PRICE_FEED_ID, | ||
ETH_PRICE_FEED_ID, | ||
]); // Fetch off-chain price update data | ||
|
||
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet }); | ||
const { postInstructions, closeInstructions, priceFeedIdToPriceUpdateAccount } = | ||
await pythSolanaReceiver.buildPostPriceUpdateInstructions(priceUpdateData); // Get instructions to post the price update data and to close the accounts later | ||
|
||
const myFirstPythApp = new Program<MyFirstPythApp>( | ||
IDL as MyFirstPythApp, | ||
PublicKey.unique(), | ||
{} | ||
); | ||
const consumerInstruction: InstructionWithEphemeralSigners = { | ||
instruction: await myFirstPythApp.methods | ||
.consume() | ||
.accounts({ | ||
solPriceUpdate: priceFeedIdToPriceUpdateAccount[SOL_PRICE_FEED_ID], | ||
ethPriceUpdate: priceFeedIdToPriceUpdateAccount[ETH_PRICE_FEED_ID], | ||
}) | ||
.instruction(), | ||
signers: [], | ||
}; | ||
|
||
const transactions = pythSolanaReceiver.batchIntoVersionedTransactions( | ||
[...postInstructions, consumerInstruction, ...closeInstructions], | ||
{} | ||
); // Put all the instructions together | ||
await pythSolanaReceiver.provider.sendAll(transactions); | ||
``` |
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.