55 */
66
77use 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
1111declare_id ! ( "HPuUpM1bKbaqx7yY2EJ4hGBaA3QsfP5cofHHK99daz85" ) ;
@@ -41,24 +41,32 @@ impl std::fmt::Display for Decimal {
4141#[ program]
4242pub 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 {
6775pub 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}
0 commit comments