Skip to content

Commit 7620574

Browse files
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
1 parent 93efd61 commit 7620574

File tree

6 files changed

+148
-10
lines changed

6 files changed

+148
-10
lines changed

target_chains/solana/Anchor.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[workspace]
2+
members = [
3+
"programs/pyth-push-oracle",
4+
"programs/pyth-solana-receiver",
5+
"examples/send_usd",
6+
]
7+
18
[features]
29
seeds = false
310
skip-lint = false

target_chains/solana/Cargo.lock

Lines changed: 49 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/solana/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ members = [
44
"cli/",
55
"program_simulator/",
66
"pyth_solana_receiver_sdk/",
7-
"common_test_utils"
7+
"common_test_utils",
8+
"examples/send_usd"
89
]
910

1011
[profile.release]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "send-usd"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "send_usd"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
18+
[dependencies]
19+
anchor-lang = "0.28.0"
20+
solana-program = "1.16.20"
21+
pyth-solana-receiver-sdk = "0.1.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use {
2+
anchor_lang::{
3+
prelude::*,
4+
solana_program::{
5+
native_token::LAMPORTS_PER_SOL,
6+
system_instruction,
7+
},
8+
},
9+
pyth_solana_receiver_sdk::price_update::{
10+
get_feed_id_from_hex,
11+
PriceUpdateV2,
12+
},
13+
};
14+
15+
declare_id!("2e5gZD3suxgJgkCg4pkoogxDKszy1SAwokz8mNeZUj4M");
16+
17+
pub const MAXIMUM_AGE: u64 = 3600; // 1 hour
18+
pub const FEED_ID: &str = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
19+
20+
#[program]
21+
pub mod send_usd {
22+
use super::*;
23+
24+
pub fn send(ctx: Context<Send>, amount_in_usd: u64) -> Result<()> {
25+
let price_update = &mut ctx.accounts.price_update;
26+
let price = price_update.get_price_no_older_than(
27+
&Clock::get()?,
28+
MAXIMUM_AGE,
29+
&get_feed_id_from_hex(FEED_ID)?,
30+
)?;
31+
32+
let amount_in_lamports = LAMPORTS_PER_SOL
33+
.checked_mul(10_u64.pow(price.exponent.abs().try_into().unwrap()))
34+
.unwrap()
35+
.checked_mul(amount_in_usd)
36+
.unwrap()
37+
.checked_div(price.price.try_into().unwrap())
38+
.unwrap();
39+
40+
let transfer_instruction = system_instruction::transfer(
41+
ctx.accounts.payer.key,
42+
ctx.accounts.destination.key,
43+
amount_in_lamports,
44+
);
45+
anchor_lang::solana_program::program::invoke(
46+
&transfer_instruction,
47+
&[
48+
ctx.accounts.payer.to_account_info(),
49+
ctx.accounts.destination.to_account_info(),
50+
],
51+
)?;
52+
53+
Ok(())
54+
}
55+
}
56+
57+
#[derive(Accounts)]
58+
#[instruction(amount_in_usd : u64)]
59+
pub struct Send<'info> {
60+
#[account(mut)]
61+
pub payer: Signer<'info>,
62+
#[account(mut)]
63+
/// CHECK : Just a destination
64+
pub destination: AccountInfo<'info>,
65+
pub price_update: Account<'info, PriceUpdateV2>,
66+
pub system_program: Program<'info, System>,
67+
}

0 commit comments

Comments
 (0)