1
- use std:: mem:: size_of;
2
1
use anchor_lang:: prelude:: * ;
3
- use solana_program:: account_info:: AccountInfo ;
4
2
5
3
pub mod state;
6
- use state:: PriceFeed ;
7
4
use state:: AdminConfig ;
5
+ use state:: PriceFeed ;
8
6
9
7
mod error;
10
8
use error:: ErrorCode ;
11
9
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" ) ;
24
11
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 ;
33
13
34
14
#[ program]
35
- pub mod example_sol_anchor_contract {
15
+ pub mod sol_anchor_contract {
36
16
use super :: * ;
37
17
38
18
pub fn init ( ctx : Context < InitRequest > , config : AdminConfig ) -> Result < ( ) > {
39
19
ctx. accounts . config . set_inner ( config) ;
40
20
Ok ( ( ) )
41
21
}
42
22
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 < ( ) > {
44
28
msg ! ( "Loan quantity is {}." , loan_qty) ;
45
29
msg ! ( "Collateral quantity is {}." , collateral_qty) ;
46
30
47
-
48
31
let loan_feed = & ctx. accounts . pyth_loan_account ;
49
32
let collateral_feed = & ctx. accounts . pyth_collateral_account ;
50
33
// With high confidence, the maximum value of the loan is
@@ -62,10 +45,21 @@ pub mod example_sol_anchor_contract {
62
45
let mut loan_max_value = loan_max_price
63
46
. checked_mul ( loan_qty)
64
47
. 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
+
65
58
msg ! (
66
- "The maximum loan value is {} * 10^({})." ,
59
+ "The maximum loan value is {} * 10^({}) = {} ." ,
67
60
loan_max_value,
68
- loan_price. expo
61
+ loan_price. expo,
62
+ result_loan_value
69
63
) ;
70
64
71
65
// With high confidence, the minimum value of the collateral is
@@ -83,10 +77,21 @@ pub mod example_sol_anchor_contract {
83
77
let mut collateral_min_value = collateral_min_price
84
78
. checked_mul ( collateral_qty)
85
79
. 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
+
86
90
msg ! (
87
- "The minimum collateral value is {} * 10^({})." ,
91
+ "The minimum collateral value is {} * 10^({}) = {} ." ,
88
92
collateral_min_value,
89
- collateral_price. expo
93
+ collateral_price. expo,
94
+ result_collateral_value
90
95
) ;
91
96
92
97
// If the loan and collateral prices use different exponent,
@@ -116,3 +121,29 @@ pub mod example_sol_anchor_contract {
116
121
}
117
122
}
118
123
}
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