forked from smartcontractkit/solana-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
78 lines (67 loc) · 2.41 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use anchor_lang::prelude::*;
use chainlink_solana as chainlink;
declare_id!("JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm");
#[account]
pub struct Decimal {
pub value: i128,
pub decimals: u32,
}
impl Decimal {
pub fn new(value: i128, decimals: u32) -> Self {
Decimal { value, decimals }
}
}
impl std::fmt::Display for Decimal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut scaled_val = self.value.to_string();
if scaled_val.len() <= self.decimals as usize {
scaled_val.insert_str(
0,
&vec!["0"; self.decimals as usize - scaled_val.len()].join(""),
);
scaled_val.insert_str(0, "0.");
} else {
scaled_val.insert(scaled_val.len() - self.decimals as usize, '.');
}
f.write_str(&scaled_val)
}
}
#[program]
pub mod chainlink_solana_demo {
use super::*;
pub fn execute(ctx: Context<Execute>) -> ProgramResult {
let round = chainlink::latest_round_data(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
let description = chainlink::description(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
let decimals = chainlink::decimals(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
// Set the account value
let decimal: &mut Account<Decimal> = &mut ctx.accounts.decimal;
decimal.value=round.answer;
decimal.decimals=u32::from(decimals);
// Also print the value to the program output
let decimal_print = Decimal::new(round.answer, u32::from(decimals));
msg!("{} price is {}", description, decimal_print);
Ok(())
}
}
#[derive(Accounts)]
pub struct Execute<'info> {
#[account(init, payer = user, space = 100)]
pub decimal: Account<'info, Decimal>,
#[account(mut)]
pub user: Signer<'info>,
/// CHECK: We're reading data from this specified chainlink feed
pub chainlink_feed: AccountInfo<'info>,
/// CHECK: This is the Chainlink program library on Devnet
pub chainlink_program: AccountInfo<'info>,
/// CHECK: This is the devnet system program
pub system_program: Program<'info, System>,
}