Skip to content

Commit 146451c

Browse files
authored
Data feeds: Solana SDK v2 (#3097)
1 parent 8d4beba commit 146451c

File tree

3 files changed

+273
-62
lines changed

3 files changed

+273
-62
lines changed

public/samples/Solana/PriceFeeds/on-chain-read-anchor.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use anchor_lang::prelude::*;
8-
use chainlink_solana as chainlink;
8+
use chainlink_solana::v2::read_feed_v2;
99

1010
//Program ID required by Anchor. Replace with your unique program ID once you build your project
1111
declare_id!("HPuUpM1bKbaqx7yY2EJ4hGBaA3QsfP5cofHHK99daz85");
@@ -41,24 +41,32 @@ impl std::fmt::Display for Decimal {
4141
#[program]
4242
pub mod chainlink_solana_demo {
4343
use super::*;
44-
pub fn execute(ctx: Context<Execute>) -> Result<()> {
45-
let round = chainlink::latest_round_data(
46-
ctx.accounts.chainlink_program.to_account_info(),
47-
ctx.accounts.chainlink_feed.to_account_info(),
48-
)?;
44+
pub fn execute(ctx: Context<Execute>) -> Result<()> {
45+
let feed = &ctx.accounts.chainlink_feed;
46+
47+
// Read the feed data directly from the account (v2 SDK)
48+
let result = read_feed_v2(
49+
feed.try_borrow_data()?,
50+
feed.owner.to_bytes(),
51+
)
52+
.map_err(|_| DemoError::ReadError)?;
4953

50-
let description = chainlink::description(
51-
ctx.accounts.chainlink_program.to_account_info(),
52-
ctx.accounts.chainlink_feed.to_account_info(),
53-
)?;
54+
// Get the latest round data
55+
let round = result
56+
.latest_round_data()
57+
.ok_or(DemoError::RoundDataMissing)?;
58+
59+
let description = result.description();
60+
let decimals = result.decimals();
61+
62+
// Convert description bytes to string
63+
let description_str = std::str::from_utf8(&description)
64+
.unwrap_or("Unknown")
65+
.trim_end_matches('\0');
5466

55-
let decimals = chainlink::decimals(
56-
ctx.accounts.chainlink_program.to_account_info(),
57-
ctx.accounts.chainlink_feed.to_account_info(),
58-
)?;
5967
// write the latest price to the program output
6068
let decimal_print = Decimal::new(round.answer, u32::from(decimals));
61-
msg!("{} price is {}", description, decimal_print);
69+
msg!("{} price is {}", description_str, decimal_print);
6270
Ok(())
6371
}
6472
}
@@ -67,6 +75,12 @@ pub mod chainlink_solana_demo {
6775
pub struct Execute<'info> {
6876
/// CHECK: We're reading data from this chainlink feed account
6977
pub chainlink_feed: AccountInfo<'info>,
70-
/// CHECK: This is the Chainlink program library
71-
pub chainlink_program: AccountInfo<'info>
78+
}
79+
80+
#[error_code]
81+
pub enum DemoError {
82+
#[msg("read error")]
83+
ReadError,
84+
#[msg("no round data")]
85+
RoundDataMissing,
7286
}

public/samples/Solana/PriceFeeds/on-chain-read.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* DO NOT USE THIS CODE IN PRODUCTION.
55
*/
66

7-
use chainlink_solana as chainlink;
7+
use chainlink_solana::v2::read_feed_v2;
88

99
use solana_program::{
1010
account_info::{next_account_info, AccountInfo},
@@ -56,26 +56,29 @@ pub fn process_instruction(
5656

5757
// This is the account of the price feed data to read from
5858
let feed_account = next_account_info(accounts_iter)?;
59-
// This is the chainlink solana program ID
60-
let chainlink_program = next_account_info(accounts_iter)?;
6159

62-
let round = chainlink::latest_round_data(
63-
chainlink_program.clone(),
64-
feed_account.clone(),
65-
)?;
60+
// Read the feed data directly from the account (v2 SDK)
61+
let result = read_feed_v2(
62+
feed_account.try_borrow_data()?,
63+
feed_account.owner.to_bytes(),
64+
)
65+
.map_err(|_| solana_program::program_error::ProgramError::InvalidAccountData)?;
6666

67-
let description = chainlink::description(
68-
chainlink_program.clone(),
69-
feed_account.clone(),
70-
)?;
67+
// Get the latest round data
68+
let round = result
69+
.latest_round_data()
70+
.ok_or(solana_program::program_error::ProgramError::InvalidAccountData)?;
7171

72-
let decimals = chainlink::decimals(
73-
chainlink_program.clone(),
74-
feed_account.clone(),
75-
)?;
72+
let description = result.description();
73+
let decimals = result.decimals();
74+
75+
// Convert description bytes to string
76+
let description_str = std::str::from_utf8(&description)
77+
.unwrap_or("Unknown")
78+
.trim_end_matches('\0');
7679

7780
let decimal_print = Decimal::new(round.answer, u32::from(decimals));
78-
msg!("{} price is {}", description, decimal_print);
81+
msg!("{} price is {}", description_str, decimal_print);
7982

8083
Ok(())
8184
}

0 commit comments

Comments
 (0)