1- use std:: mem:: size_of;
21use anchor_lang:: prelude:: * ;
3- use solana_program:: account_info:: AccountInfo ;
42
53pub mod state;
6- use state:: PriceFeed ;
74use state:: AdminConfig ;
5+ use state:: PriceFeed ;
86
97mod error;
108use error:: ErrorCode ;
119
12- declare_id ! ( "BZh3CP454Ca1C9yBp2tpGAkXoKFti9x8ShJLSxNDpoxa" ) ;
13-
14- #[ derive( Accounts ) ]
15- pub struct InitRequest < ' info > {
16- #[ account( address = * program_id @ ErrorCode :: Unauthorized ) ]
17- pub program : Signer < ' info > ,
18- #[ account( mut ) ]
19- pub payer : Signer < ' info > ,
20- #[ account( init, payer = payer, space = 8 + size_of:: <AdminConfig >( ) ) ]
21- pub config : Account < ' info , AdminConfig > ,
22- pub system_program : Program < ' info , System > ,
23- }
10+ declare_id ! ( "GFPM2LncpbWiLkePLs3QjcLVPw31B2h23FwFfhig79fh" ) ;
2411
25- #[ derive( Accounts ) ]
26- pub struct QueryRequest < ' info > {
27- pub config : Account < ' info , AdminConfig > ,
28- #[ account( address = config. loan_price_feed_id @ ErrorCode :: InvalidArgument ) ]
29- pub pyth_loan_account : Account < ' info , PriceFeed > ,
30- #[ account( address = config. collateral_price_feed_id @ ErrorCode :: InvalidArgument ) ]
31- pub pyth_collateral_account : Account < ' info , PriceFeed > ,
32- }
12+ const BASE : f64 = 10.0 ;
3313
3414#[ program]
35- pub mod example_sol_anchor_contract {
15+ pub mod sol_anchor_contract {
3616 use super :: * ;
3717
3818 pub fn init ( ctx : Context < InitRequest > , config : AdminConfig ) -> Result < ( ) > {
3919 ctx. accounts . config . set_inner ( config) ;
4020 Ok ( ( ) )
4121 }
4222
43- pub fn loan_to_value ( ctx : Context < QueryRequest > , loan_qty : i64 , collateral_qty : i64 ) -> Result < ( ) > {
23+ pub fn loan_to_value (
24+ ctx : Context < QueryRequest > ,
25+ loan_qty : i64 ,
26+ collateral_qty : i64 ,
27+ ) -> Result < ( ) > {
4428 msg ! ( "Loan quantity is {}." , loan_qty) ;
4529 msg ! ( "Collateral quantity is {}." , collateral_qty) ;
4630
47-
4831 let loan_feed = & ctx. accounts . pyth_loan_account ;
4932 let collateral_feed = & ctx. accounts . pyth_collateral_account ;
5033 // With high confidence, the maximum value of the loan is
@@ -62,10 +45,21 @@ pub mod example_sol_anchor_contract {
6245 let mut loan_max_value = loan_max_price
6346 . checked_mul ( loan_qty)
6447 . ok_or ( ErrorCode :: Overflow ) ?;
48+
49+ // WARNING : f64 SHOULD NOT BE USED IN SMART CONTRACTS, IT IS USED HERE ONLY FOR LOGGING PURPOSES
50+ // lets get the maximum loan value based on computation
51+ // i.e {} * 10^({})
52+ // loan_max_value * 10^(loan_price.expo)
53+ let exponent: i32 = loan_price. expo ;
54+ let result = BASE . powi ( exponent. abs ( ) ) ;
55+ let result = if exponent < 0 { 1.0 / result } else { result } ;
56+ let result_loan_value = loan_max_value as f64 * result;
57+
6558 msg ! (
66- "The maximum loan value is {} * 10^({})." ,
59+ "The maximum loan value is {} * 10^({}) = {} ." ,
6760 loan_max_value,
68- loan_price. expo
61+ loan_price. expo,
62+ result_loan_value
6963 ) ;
7064
7165 // With high confidence, the minimum value of the collateral is
@@ -83,10 +77,21 @@ pub mod example_sol_anchor_contract {
8377 let mut collateral_min_value = collateral_min_price
8478 . checked_mul ( collateral_qty)
8579 . ok_or ( ErrorCode :: Overflow ) ?;
80+
81+ // WARNING : f64 SHOULD NOT BE USED IN SMART CONTRACTS, IT IS USED HERE ONLY FOR LOGGING PURPOSES
82+ // lets get the minimum collateral value based on computation
83+ // i.e {} * 10^({})
84+ // i.e collateral_min_value * 10^(collateral_price.expo)
85+ let exponent: i32 = collateral_price. expo ;
86+ let result = BASE . powi ( exponent. abs ( ) ) ;
87+ let result: f64 = if exponent < 0 { 1.0 / result } else { result } ;
88+ let result_collateral_value = collateral_min_value as f64 * result;
89+
8690 msg ! (
87- "The minimum collateral value is {} * 10^({})." ,
91+ "The minimum collateral value is {} * 10^({}) = {} ." ,
8892 collateral_min_value,
89- collateral_price. expo
93+ collateral_price. expo,
94+ result_collateral_value
9095 ) ;
9196
9297 // If the loan and collateral prices use different exponent,
@@ -116,3 +121,29 @@ pub mod example_sol_anchor_contract {
116121 }
117122 }
118123}
124+
125+ #[ derive( Accounts ) ]
126+ pub struct InitRequest < ' info > {
127+ #[ account( mut ) ]
128+ pub payer : Signer < ' info > ,
129+ #[ account(
130+ init,
131+ payer = payer,
132+ space = 8 + AdminConfig :: INIT_SPACE
133+ ) ]
134+ pub config : Account < ' info , AdminConfig > ,
135+ pub system_program : Program < ' info , System > ,
136+ }
137+
138+ #[ derive( Accounts ) ]
139+ pub struct QueryRequest < ' info > {
140+ pub config : Account < ' info , AdminConfig > ,
141+ #[ account(
142+ address = config. loan_price_feed_id @ ErrorCode :: InvalidArgument
143+ ) ]
144+ pub pyth_loan_account : Account < ' info , PriceFeed > ,
145+ #[ account(
146+ address = config. collateral_price_feed_id @ ErrorCode :: InvalidArgument
147+ ) ]
148+ pub pyth_collateral_account : Account < ' info , PriceFeed > ,
149+ }
0 commit comments