### Build and Run Off-chain Examples (Bash) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Provides commands to build and run the off-chain example programs included with the Pyth SDK for Solana. These examples demonstrate fetching and displaying Pyth price data. ```bash cargo build --examples cargo run --example eth_price ``` ```bash cargo run --example get_accounts ``` -------------------------------- ### Build, Deploy, and Invoke Solana Contract Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/examples/sol-contract/README.md Provides shell commands to set up, build, deploy, and invoke the example Solana contract that uses the Pyth SDK. Assumes necessary tools like cargo, solana, npm, and node are installed. ```shell # Enter the root directory of this example > cd examples/sol-contract # Build the example contract > scripts/build.sh # Config solana CLI and set the url as devnet > solana config set --url https://api.devnet.solana.com # Deploy the example contract > scripts/deploy.sh # Invoke the example contract > scripts/invoke.sh ``` -------------------------------- ### Anchor CLI Commands for Pyth SDK Example Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/examples/sol-anchor-contract/README.md This snippet outlines the necessary Anchor CLI commands to set up, build, deploy, and run the Pyth SDK example program on Solana. It includes generating program keys, configuring Anchor.toml, building the contract, deploying it, and executing client-side tests. ```shell > solana-keygen new -o program_address.json # Use the pubkey generated to replace the following two places # "example_sol_anchor_contract" in Anchor.toml # "declare_id!()" in programs/example-sol-anchor-contract/src/lib.rs # Enter the directory and build this example > cd examples/sol-contract-anchor > anchor build # Change the `wallet` field in Anchor.toml to your own wallet # And then deploy the example contract; An error may occur if # your wallet does not have enough funds > anchor deploy --program-keypair program_address.json --program-name example-sol-anchor-contract # Install the client dependencies and invoke this program > anchor run install > anchor run test ``` -------------------------------- ### Install Pyth Solana SDK Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Add the pyth-sdk-solana crate as a dependency to your project's Cargo.toml file to use the SDK. ```toml [dependencies] pyth-sdk-solana="" ``` -------------------------------- ### Pre-commit Hook Installation Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/README.md Instructions for installing and enabling pre-commit hooks in the Pyth SDK repository to automatically check and fix code formatting and other issues before each commit. ```Shell pre-commit install ``` -------------------------------- ### Build and Test Pyth SDK Solana Contract Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/test-contract/README.md Commands to build and test the Pyth SDK Solana contract using Cargo BPF. Requires Solana CLI tools installation. ```bash cargo build-bpf cargo test-bpf ``` -------------------------------- ### Schema Generation Example Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/README.md Demonstrates how to generate JSON Schema files for Pyth structures, ensuring consistency across different languages and repositories. This process is automated via a CI check. ```Rust cargo run --example schema ``` -------------------------------- ### Read Pyth Price Feed in Rust Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/examples/sol-contract/README.md Demonstrates how to load a Pyth price feed from a Solana account and retrieve the current price with a confidence interval. This is crucial for lending protocols to assess the value of loans and collateral. ```rust let feed1 = load_price_feed_from_account_info(pyth_loan_account)?; let current_timestamp1 = Clock::get()?.unix_timestamp; let result1 = feed1.get_price_no_older_than(current_timestamp1, 60).ok_or(ProgramError::Custom(3))?; ``` -------------------------------- ### Calculate Loan Value with Pyth Price in Rust Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/examples/sol-contract/README.md Calculates the maximum value of a loan based on the Pyth price and its confidence interval. This is a key step in lending protocols to determine loan eligibility and risk. ```rust let loan_max_price = result1 .price .checked_add(result1.conf as i64) .ok_or(ProgramError::Custom(4))?; let loan_max_value = loan_max_price .checked_mul(loan_qty) .ok_or(ProgramError::Custom(4))?; ``` -------------------------------- ### Get Current Price from PriceFeed (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk/README.md Retrieves the current price from a `PriceFeed` that is not older than a specified threshold. The price is returned with a confidence interval, both as fixed-point numbers (a * 10^e). Returns `None` if the price is unavailable. ```rust const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds let current_timestamp = ...; let current_price: Price = price_feed.get_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).ok_or(StdError::not_found("Current price is not available"))?; println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo); ``` -------------------------------- ### Get EMA Price from PriceFeed (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk/README.md Retrieves the exponentially-weighted moving average (EMA) price from a `PriceFeed` that is not older than a specified threshold. The price is returned with a confidence interval, both as fixed-point numbers (a * 10^e). Returns `None` if the EMA price is unavailable. ```rust const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds let current_timestamp = ...; let ema_price: Price = price_feed.get_ema_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).ok_or(StdError::not_found("EMA price is not available"))?; println!("price: ({} +- {}) x 10^{}", ema_price.price, ema_price.conf, ema_price.expo); ``` -------------------------------- ### Build and Test Pyth SDK (Bash) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Instructions for building and testing the Pyth SDK for Rust, covering both native compilation and BPF compilation for Solana programs. Requires Solana CLI tools for BPF builds. ```bash cargo build cargo test ``` ```bash cargo build-bpf cargo test-bpf ``` -------------------------------- ### Load Solana Pyth Accounts (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Demonstrates how to load Pyth account data into Rust structs using the `pyth_sdk_solana::state` module. This is useful for interacting with Pyth data on the Solana blockchain. ```rust use pyth_sdk_solana::state::*; // replace with account data, either passed to on-chain program or from RPC node let price_account_data: Vec = ...; let price_account: &PriceAccount = load_price_account( &price_account_data ).unwrap(); let product_account_data: Vec = ...; let product_account: &ProductAccount = load_product_account( &product_account_data ).unwrap(); let mapping_account_data: Vec = ...; let mapping_account: &MappingAccount = load_mapping_account( &mapping_account_data ).unwrap(); ``` -------------------------------- ### Load Price Feed Off-Chain (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Loads a Pyth price feed from an off-chain Solana Account. It uses the Solana RPC client to fetch account data and then constructs a PriceFeed struct. The current price is retrieved with a staleness check, and the details are printed. ```rust use pyth_sdk_solana::{load_price_feed_from_account, PriceFeed}; const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds let current_time = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs() as i64; let price_key: Pubkey = ...; let mut price_account: Account = clnt.get_account(&price_key).unwrap(); let price_feed: PriceFeed = load_price_feed_from_account( &price_key, &mut price_account ).unwrap(); let current_price: Price = price_feed.get_price_no_older_than(current_time, STALENESS_THRESHOLD).unwrap(); println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo); ``` -------------------------------- ### Release Process for Pyth SDK Crates Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/README.md Details the steps required to release a new version of a Pyth SDK crate. This involves updating the version in Cargo.toml, merging to main, and creating a GitHub release to publish to crates.io. ```APIDOC 1. Increment version in `Cargo.toml` (e.g., `0.0.1-beta.0`). 2. Merge changes to `main` on GitHub. 3. Create and publish a GitHub release (e.g., `pyth-sdk-solana:v1.3.2`) to trigger CI and publish to crates.io. ``` -------------------------------- ### Load Price Feed On-Chain (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/README.md Loads a Pyth price feed from an on-chain Solana AccountInfo. It then retrieves the current price, ensuring it's not older than a specified staleness threshold in seconds. The price and confidence interval are printed to the console. ```rust use pyth_sdk_solana::{load_price_feed_from_account_info, PriceFeed}; const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds let price_account_info: AccountInfo = ...; let price_feed: PriceFeed = load_price_feed_from_account_info( &price_account_info ).unwrap(); let current_timestamp = Clock::get()?.unix_timestamp; let current_price: Price = price_feed.get_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).unwrap(); msg!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo); ``` -------------------------------- ### Run Instruction Count Program Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/test-contract/README.md Command to run the instruction count program independently to log resource consumption of library functions. ```bash cargo test-bpf --test instruction_count ``` -------------------------------- ### Calculate Collateral Valuation Price - Rust Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk/README.md Calculates the valuation price for collateral by factoring in market impact and liquidity. This function interpolates a discount based on the total deposits relative to a defined endpoint, scaling the price accordingly without altering its confidence interval. ```rust let btc_usd: Price = ...; // deposits is the total number of tokens deposited in the protocol let deposits: u64 = 100; // deposits_endpoint represents the token deposits at which rate_discount_final kicks in let deposits_endpoint: u64 = 1000; // rate_discount_initial and _final are percentages, expressed in fixed point as x * 10^discount_exponent. // E.g. 50 with discount_exponent = -2 is 50%. let rate_discount_initial: u64 = 100; let rate_discount_final: u64 = 90; let discount_exponent: i32 = 2; let price_collateral: Price = btc_usd.get_collateral_valuation_price( deposits, deposits_endpoint, rate_discount_initial, rate_discount_final, discount_exponent).ok_or(StdError::not_found("Issue with querying collateral price"))?; println!("The valuation price for the collateral given {} tokens deposited is ({} +- {}) x 10^{} USD", deposits, price_collateral.price, price_collateral.conf, price_collateral.expo); ``` -------------------------------- ### Calculate Basket Value (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk/README.md Computes the total value of a basket of multiple assets. Each asset in the basket is represented by its `Price` and quantity (in fixed-point format a * 10^e). The function returns the total value as a `Price` struct with a specified exponent for the result. ```rust let btc_usd: Price = ...; let eth_usd: Price = ...; // Quantity of each asset in fixed-point a * 10^e. // This represents 0.1 BTC and .05 ETH. // -8 is desired exponent for result let basket_price: Price = Price::price_basket(&[ (btc_usd, 10, -2), (eth_usd, 5, -2) ], -8); println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD", basket_price.price, basket_price.conf, basket_price.expo); ``` -------------------------------- ### Calculate Non-USD Price (Rust) Source: https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk/README.md Calculates the price of an asset in a different quote currency by combining two USD prices. The result is a `Price` struct representing the price in the new quote currency, with a specified exponent for the result. ```rust let btc_usd: Price = ...; let eth_usd: Price = ...; // -8 is the desired exponent for the result let btc_eth: Price = btc_usd.get_price_in_quote(ð_usd, -8); println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.