7
7
} ,
8
8
env,
9
9
is_promise_success,
10
+ log,
10
11
near_bindgen,
11
12
AccountId ,
12
13
Gas ,
13
14
PanicOnDefault ,
14
15
Promise ,
15
- PromiseError ,
16
16
} ,
17
- pyth:: state:: PriceIdentifier ,
17
+ pyth:: state:: {
18
+ Price ,
19
+ PriceIdentifier ,
20
+ } ,
18
21
} ;
19
22
23
+ /// Our contract simply processes prices, so for now the only state we
24
+ /// need is the Pyth contract ID from which we will be fetching prices.
20
25
#[ near_bindgen]
21
26
#[ derive( BorshDeserialize , BorshSerialize , PanicOnDefault ) ]
22
27
pub struct PythExample {
@@ -31,35 +36,64 @@ impl PythExample {
31
36
Self { pyth }
32
37
}
33
38
34
- /// Get a Pyth Price Feed Result .
39
+ /// Example of submitting an update + request for multiple Price Feeds .
35
40
#[ payable]
36
- pub fn example_price_usage ( & mut self , identifier : PriceIdentifier , data : String ) -> Promise {
41
+ pub fn example_price_usage (
42
+ & mut self ,
43
+ identifiers : Vec < PriceIdentifier > ,
44
+ data : String ,
45
+ ) -> Promise {
37
46
pyth:: ext:: ext_pyth:: ext ( self . pyth . clone ( ) )
38
47
. with_static_gas ( Gas ( 30_000_000_000_000 ) )
39
48
. with_attached_deposit ( env:: attached_deposit ( ) )
40
49
. update_price_feeds ( data)
41
50
. then (
42
- pyth:: ext:: ext_pyth:: ext ( self . pyth . clone ( ) )
43
- . get_price ( identifier)
44
- . then (
45
- Self :: ext ( env:: current_account_id ( ) )
46
- . with_static_gas ( Gas ( 10_000_000_000 ) )
47
- . handle_example_price_usage ( ) ,
48
- ) ,
51
+ Self :: ext ( env:: current_account_id ( ) )
52
+ . with_static_gas ( Gas ( 10_000_000_000 ) )
53
+ . handle_update_callback ( identifiers) ,
49
54
)
50
55
}
51
56
57
+ /// Handle the case where prices successfully updated, we can start reads at this point.
52
58
#[ payable]
53
59
#[ private]
60
+ pub fn handle_update_callback ( & mut self , mut identifiers : Vec < PriceIdentifier > ) -> Promise {
61
+ if !is_promise_success ( ) {
62
+ panic ! ( "Failed to Update Prices" ) ;
63
+ }
64
+
65
+ // Fetch a few prices to use.
66
+ let price_1 = identifiers. pop ( ) . unwrap ( ) ;
67
+ let price_2 = identifiers. pop ( ) . unwrap ( ) ;
68
+ let price_1 = pyth:: ext:: ext_pyth:: ext ( self . pyth . clone ( ) ) . get_price ( price_1) ;
69
+ let price_2 = pyth:: ext:: ext_pyth:: ext ( self . pyth . clone ( ) ) . get_price ( price_2) ;
70
+
71
+ // Start parallel reads.
72
+ price_1. and ( price_2) . then (
73
+ Self :: ext ( env:: current_account_id ( ) )
74
+ . with_static_gas ( Gas ( 10_000_000_000 ) )
75
+ . handle_results_callback ( ) ,
76
+ )
77
+ }
78
+
79
+ /// Handle results of reading multiple prices, the prices can be accessed using
80
+ /// NEAR's env::promise_* functions.
81
+ #[ private]
54
82
#[ handle_result]
55
- pub fn handle_example_price_usage (
56
- & mut self ,
57
- #[ callback_result] _r : Result < Option < pyth:: state:: Price > , PromiseError > ,
83
+ pub fn handle_results_callback (
84
+ & self ,
85
+ #[ callback_result] price_1 : Result < Price , near_sdk:: PromiseError > ,
86
+ #[ callback_result] price_2 : Result < Price , near_sdk:: PromiseError > ,
58
87
) {
59
88
if !is_promise_success ( ) {
60
89
return ;
61
90
}
62
91
63
- // Do things with Price Feed Result.
92
+ let price_1 = price_1. unwrap ( ) ;
93
+ let price_2 = price_2. unwrap ( ) ;
94
+
95
+ // Do something with the prices.
96
+ log ! ( "{:?}" , price_1) ;
97
+ log ! ( "{:?}" , price_2) ;
64
98
}
65
99
}
0 commit comments