feat: add solana target chain example (#1446)
* feat: add send usd example for solana * fix: rust fmt * fix: remove unused dependency ahash * fix: imports * refactor: use get_price_no_older_than * fix: package name * fix: fix naming conventions * feat: add workspace members in Anchor.toml * fix: set maximum age to 1 hour * fix: use public crates for send usd example
This commit is contained in:
parent
93efd61ea4
commit
76205745c8
|
@ -1,3 +1,10 @@
|
|||
[workspace]
|
||||
members = [
|
||||
"programs/pyth-push-oracle",
|
||||
"programs/pyth-solana-receiver",
|
||||
"examples/send_usd",
|
||||
]
|
||||
|
||||
[features]
|
||||
seeds = false
|
||||
skip-lint = false
|
||||
|
|
|
@ -1030,8 +1030,8 @@ dependencies = [
|
|||
"pyth-sdk",
|
||||
"pyth-sdk-solana",
|
||||
"pyth-solana-receiver",
|
||||
"pyth-solana-receiver-sdk",
|
||||
"pythnet-sdk",
|
||||
"pyth-solana-receiver-sdk 0.1.0",
|
||||
"pythnet-sdk 2.0.0",
|
||||
"rand 0.8.5",
|
||||
"serde_wormhole",
|
||||
"solana-program",
|
||||
|
@ -3023,8 +3023,8 @@ dependencies = [
|
|||
"common-test-utils",
|
||||
"program-simulator",
|
||||
"pyth-solana-receiver",
|
||||
"pyth-solana-receiver-sdk",
|
||||
"pythnet-sdk",
|
||||
"pyth-solana-receiver-sdk 0.1.0",
|
||||
"pythnet-sdk 2.0.0",
|
||||
"serde_wormhole",
|
||||
"solana-program",
|
||||
"solana-sdk",
|
||||
|
@ -3071,8 +3071,8 @@ dependencies = [
|
|||
"byteorder",
|
||||
"common-test-utils",
|
||||
"program-simulator",
|
||||
"pyth-solana-receiver-sdk",
|
||||
"pythnet-sdk",
|
||||
"pyth-solana-receiver-sdk 0.1.0",
|
||||
"pythnet-sdk 2.0.0",
|
||||
"rand 0.8.5",
|
||||
"serde_wormhole",
|
||||
"solana-program",
|
||||
|
@ -3094,8 +3094,8 @@ dependencies = [
|
|||
"clap 3.2.23",
|
||||
"hex",
|
||||
"pyth-solana-receiver",
|
||||
"pyth-solana-receiver-sdk",
|
||||
"pythnet-sdk",
|
||||
"pyth-solana-receiver-sdk 0.1.0",
|
||||
"pythnet-sdk 2.0.0",
|
||||
"serde_wormhole",
|
||||
"shellexpand",
|
||||
"solana-client",
|
||||
|
@ -3111,7 +3111,19 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anchor-lang",
|
||||
"hex",
|
||||
"pythnet-sdk",
|
||||
"pythnet-sdk 2.0.0",
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyth-solana-receiver-sdk"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "937c8595148fb2a9a90439daf6a371a5b3c9fcd9b636f26d36ae31d6846d4339"
|
||||
dependencies = [
|
||||
"anchor-lang",
|
||||
"hex",
|
||||
"pythnet-sdk 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
|
@ -3137,6 +3149,25 @@ dependencies = [
|
|||
"wormhole-vaas-serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pythnet-sdk"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f246dc1ea2966f811d894590014f9163942b33255fc7560fe629726a721aad1"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"borsh 0.10.3",
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"fast-math",
|
||||
"hex",
|
||||
"rustc_version",
|
||||
"serde",
|
||||
"sha3 0.10.6",
|
||||
"slow_primes",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "qstring"
|
||||
version = "0.7.2"
|
||||
|
@ -3725,6 +3756,15 @@ version = "1.0.20"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
|
||||
|
||||
[[package]]
|
||||
name = "send-usd"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anchor-lang",
|
||||
"pyth-solana-receiver-sdk 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"solana-program",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.193"
|
||||
|
|
|
@ -4,7 +4,8 @@ members = [
|
|||
"cli/",
|
||||
"program_simulator/",
|
||||
"pyth_solana_receiver_sdk/",
|
||||
"common_test_utils"
|
||||
"common_test_utils",
|
||||
"examples/send_usd"
|
||||
]
|
||||
|
||||
[profile.release]
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
[package]
|
||||
name = "send-usd"
|
||||
version = "0.1.0"
|
||||
description = "Created with Anchor"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
name = "send_usd"
|
||||
|
||||
[features]
|
||||
no-entrypoint = []
|
||||
no-idl = []
|
||||
no-log-ix-name = []
|
||||
cpi = ["no-entrypoint"]
|
||||
default = []
|
||||
|
||||
[dependencies]
|
||||
anchor-lang = "0.28.0"
|
||||
solana-program = "1.16.20"
|
||||
pyth-solana-receiver-sdk = "0.1.0"
|
|
@ -0,0 +1,2 @@
|
|||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
|
@ -0,0 +1,67 @@
|
|||
use {
|
||||
anchor_lang::{
|
||||
prelude::*,
|
||||
solana_program::{
|
||||
native_token::LAMPORTS_PER_SOL,
|
||||
system_instruction,
|
||||
},
|
||||
},
|
||||
pyth_solana_receiver_sdk::price_update::{
|
||||
get_feed_id_from_hex,
|
||||
PriceUpdateV2,
|
||||
},
|
||||
};
|
||||
|
||||
declare_id!("2e5gZD3suxgJgkCg4pkoogxDKszy1SAwokz8mNeZUj4M");
|
||||
|
||||
pub const MAXIMUM_AGE: u64 = 3600; // 1 hour
|
||||
pub const FEED_ID: &str = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
|
||||
|
||||
#[program]
|
||||
pub mod send_usd {
|
||||
use super::*;
|
||||
|
||||
pub fn send(ctx: Context<Send>, amount_in_usd: u64) -> Result<()> {
|
||||
let price_update = &mut ctx.accounts.price_update;
|
||||
let price = price_update.get_price_no_older_than(
|
||||
&Clock::get()?,
|
||||
MAXIMUM_AGE,
|
||||
&get_feed_id_from_hex(FEED_ID)?,
|
||||
)?;
|
||||
|
||||
let amount_in_lamports = LAMPORTS_PER_SOL
|
||||
.checked_mul(10_u64.pow(price.exponent.abs().try_into().unwrap()))
|
||||
.unwrap()
|
||||
.checked_mul(amount_in_usd)
|
||||
.unwrap()
|
||||
.checked_div(price.price.try_into().unwrap())
|
||||
.unwrap();
|
||||
|
||||
let transfer_instruction = system_instruction::transfer(
|
||||
ctx.accounts.payer.key,
|
||||
ctx.accounts.destination.key,
|
||||
amount_in_lamports,
|
||||
);
|
||||
anchor_lang::solana_program::program::invoke(
|
||||
&transfer_instruction,
|
||||
&[
|
||||
ctx.accounts.payer.to_account_info(),
|
||||
ctx.accounts.destination.to_account_info(),
|
||||
],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Accounts)]
|
||||
#[instruction(amount_in_usd : u64)]
|
||||
pub struct Send<'info> {
|
||||
#[account(mut)]
|
||||
pub payer: Signer<'info>,
|
||||
#[account(mut)]
|
||||
/// CHECK : Just a destination
|
||||
pub destination: AccountInfo<'info>,
|
||||
pub price_update: Account<'info, PriceUpdateV2>,
|
||||
pub system_program: Program<'info, System>,
|
||||
}
|
Loading…
Reference in New Issue