Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions public/samples/Solana/PriceFeeds/on-chain-read-anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

use anchor_lang::prelude::*;
use chainlink_solana as chainlink;
use chainlink_solana::v2::read_feed_v2;

//Program ID required by Anchor. Replace with your unique program ID once you build your project
declare_id!("HPuUpM1bKbaqx7yY2EJ4hGBaA3QsfP5cofHHK99daz85");
Expand Down Expand Up @@ -41,24 +41,32 @@ impl std::fmt::Display for Decimal {
#[program]
pub mod chainlink_solana_demo {
use super::*;
pub fn execute(ctx: Context<Execute>) -> Result<()> {
let round = chainlink::latest_round_data(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
pub fn execute(ctx: Context<Execute>) -> Result<()> {
let feed = &ctx.accounts.chainlink_feed;

// Read the feed data directly from the account (v2 SDK)
let result = read_feed_v2(
feed.try_borrow_data()?,
feed.owner.to_bytes(),
)
.map_err(|_| DemoError::ReadError)?;

let description = chainlink::description(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
// Get the latest round data
let round = result
.latest_round_data()
.ok_or(DemoError::RoundDataMissing)?;

let description = result.description();
let decimals = result.decimals();

// Convert description bytes to string
let description_str = std::str::from_utf8(&description)
.unwrap_or("Unknown")
.trim_end_matches('\0');

let decimals = chainlink::decimals(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;
// write the latest price to the program output
let decimal_print = Decimal::new(round.answer, u32::from(decimals));
msg!("{} price is {}", description, decimal_print);
msg!("{} price is {}", description_str, decimal_print);
Ok(())
}
}
Expand All @@ -67,6 +75,12 @@ pub mod chainlink_solana_demo {
pub struct Execute<'info> {
/// CHECK: We're reading data from this chainlink feed account
pub chainlink_feed: AccountInfo<'info>,
/// CHECK: This is the Chainlink program library
pub chainlink_program: AccountInfo<'info>
}

#[error_code]
pub enum DemoError {
#[msg("read error")]
ReadError,
#[msg("no round data")]
RoundDataMissing,
}
35 changes: 19 additions & 16 deletions public/samples/Solana/PriceFeeds/on-chain-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* DO NOT USE THIS CODE IN PRODUCTION.
*/

use chainlink_solana as chainlink;
use chainlink_solana::v2::read_feed_v2;

use solana_program::{
account_info::{next_account_info, AccountInfo},
Expand Down Expand Up @@ -56,26 +56,29 @@ pub fn process_instruction(

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

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

let description = chainlink::description(
chainlink_program.clone(),
feed_account.clone(),
)?;
// Get the latest round data
let round = result
.latest_round_data()
.ok_or(solana_program::program_error::ProgramError::InvalidAccountData)?;

let decimals = chainlink::decimals(
chainlink_program.clone(),
feed_account.clone(),
)?;
let description = result.description();
let decimals = result.decimals();

// Convert description bytes to string
let description_str = std::str::from_utf8(&description)
.unwrap_or("Unknown")
.trim_end_matches('\0');

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

Ok(())
}
Loading
Loading