1
1
#[ cfg( test) ]
2
2
mod test {
3
- use crate :: { error:: PythReceiverError } ;
4
- use crate :: test_data:: { self , good_update2_results, multiple_updates_results} ;
3
+ use crate :: error:: PythReceiverError ;
5
4
use crate :: test_data:: good_update1_results;
5
+ use crate :: test_data:: { self , good_update2_results, multiple_updates_results} ;
6
6
use crate :: PythReceiver ;
7
7
use alloy_primitives:: { Address , U256 } ;
8
8
use motsu:: prelude:: * ;
@@ -13,7 +13,7 @@ mod test {
13
13
0xdb , 0x33 , 0x0f , 0x7a , 0xc6 , 0x6b , 0x72 , 0xdc , 0x65 , 0x8a , 0xfe , 0xdf , 0x0f , 0x4a , 0x41 ,
14
14
0x5b , 0x43 ,
15
15
] ;
16
-
16
+
17
17
const PYTHNET_CHAIN_ID : u16 = 26 ;
18
18
const PYTHNET_EMITTER_ADDRESS : [ u8 ; 32 ] = [
19
19
0xe1 , 0x01 , 0xfa , 0xed , 0xac , 0x58 , 0x51 , 0xe3 , 0x2b , 0x9b , 0x23 , 0xb5 , 0xf9 , 0x41 , 0x1a ,
@@ -26,19 +26,29 @@ mod test {
26
26
const GOVERNANCE_CONTRACT : U256 = U256 :: from_limbs ( [ 4 , 0 , 0 , 0 ] ) ;
27
27
28
28
const SINGLE_UPDATE_FEE_IN_WEI : U256 = U256 :: from_limbs ( [ 100 , 0 , 0 , 0 ] ) ;
29
+ const TRANSACTION_FEE_IN_WEI : U256 = U256 :: from_limbs ( [ 32 , 0 , 0 , 0 ] ) ;
29
30
30
31
#[ cfg( test) ]
31
- fn mock_get_update_fee ( update_data : Vec < u8 > ) -> Result < U256 , PythReceiverError > {
32
- let update_data_array: & [ u8 ] = & update_data;
33
- let accumulator_update = AccumulatorUpdateData :: try_from_slice ( & update_data_array)
34
- . map_err ( |_| PythReceiverError :: InvalidAccumulatorMessage ) ?;
35
- match accumulator_update. proof {
36
- Proof :: WormholeMerkle { vaa : _, updates } => {
37
- let num_updates =
38
- u8:: try_from ( updates. len ( ) ) . map_err ( |_| PythReceiverError :: TooManyUpdates ) ?;
39
- Ok ( U256 :: from ( num_updates) . saturating_mul ( SINGLE_UPDATE_FEE_IN_WEI ) )
32
+ fn mock_get_update_fee ( update_data : Vec < Vec < u8 > > ) -> Result < U256 , PythReceiverError > {
33
+ let mut total_num_updates: u64 = 0 ;
34
+ for data in & update_data {
35
+ let update_data_array: & [ u8 ] = & data;
36
+ let accumulator_update = AccumulatorUpdateData :: try_from_slice ( & update_data_array)
37
+ . map_err ( |_| PythReceiverError :: InvalidAccumulatorMessage ) ?;
38
+ match accumulator_update. proof {
39
+ Proof :: WormholeMerkle { vaa : _, updates } => {
40
+ let num_updates = u64:: try_from ( updates. len ( ) )
41
+ . map_err ( |_| PythReceiverError :: TooManyUpdates ) ?;
42
+ total_num_updates += num_updates;
43
+ }
40
44
}
41
45
}
46
+ Ok ( get_total_fee ( total_num_updates) )
47
+ }
48
+
49
+ fn get_total_fee ( total_num_updates : u64 ) -> U256 {
50
+ U256 :: from ( total_num_updates) . saturating_mul ( SINGLE_UPDATE_FEE_IN_WEI )
51
+ + TRANSACTION_FEE_IN_WEI
42
52
}
43
53
44
54
#[ cfg( test) ]
@@ -93,22 +103,19 @@ mod test {
93
103
) {
94
104
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
95
105
96
- alice. fund ( U256 :: from ( 200 ) ) ;
97
-
98
106
let update_data = test_data:: good_update1 ( ) ;
99
107
let update_fee = mock_get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
100
108
109
+ alice. fund ( update_fee) ;
110
+
101
111
let result = pyth_contract
102
112
. sender_and_value ( alice, update_fee)
103
113
. update_price_feeds ( update_data) ;
104
114
assert ! ( result. is_ok( ) ) ;
105
115
106
116
let price_result = pyth_contract. sender ( alice) . get_price_unsafe ( TEST_PRICE_ID ) ;
107
117
assert ! ( price_result. is_ok( ) ) ;
108
- assert_eq ! (
109
- price_result. unwrap( ) ,
110
- good_update1_results( )
111
- ) ;
118
+ assert_eq ! ( price_result. unwrap( ) , good_update1_results( ) ) ;
112
119
}
113
120
114
121
#[ motsu:: test]
@@ -140,29 +147,27 @@ mod test {
140
147
) {
141
148
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
142
149
143
- alice. fund ( U256 :: from ( 200 ) ) ;
144
-
145
150
let update_data1 = test_data:: good_update1 ( ) ;
146
151
let update_fee1 = mock_get_update_fee ( update_data1. clone ( ) ) . unwrap ( ) ;
152
+
153
+ let update_data2 = test_data:: good_update2 ( ) ;
154
+ let update_fee2 = mock_get_update_fee ( update_data2. clone ( ) ) . unwrap ( ) ;
155
+
156
+ alice. fund ( update_fee1 + update_fee2) ;
157
+
147
158
let result1 = pyth_contract
148
159
. sender_and_value ( alice, update_fee1)
149
160
. update_price_feeds ( update_data1) ;
150
161
assert ! ( result1. is_ok( ) ) ;
151
162
152
- let update_data2 = test_data:: good_update2 ( ) ;
153
- let update_fee2 = mock_get_update_fee ( update_data2. clone ( ) ) . unwrap ( ) ;
154
-
155
163
let result2 = pyth_contract
156
164
. sender_and_value ( alice, update_fee2)
157
165
. update_price_feeds ( update_data2) ;
158
166
assert ! ( result2. is_ok( ) ) ;
159
167
160
168
let price_result = pyth_contract. sender ( alice) . get_price_unsafe ( TEST_PRICE_ID ) ;
161
169
assert ! ( price_result. is_ok( ) ) ;
162
- assert_eq ! (
163
- price_result. unwrap( ) ,
164
- good_update2_results( )
165
- ) ;
170
+ assert_eq ! ( price_result. unwrap( ) , good_update2_results( ) ) ;
166
171
}
167
172
168
173
#[ motsu:: test]
@@ -213,11 +218,11 @@ mod test {
213
218
) {
214
219
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
215
220
216
- alice. fund ( U256 :: from ( 200 ) ) ;
217
-
218
221
let update_data = test_data:: good_update2 ( ) ;
219
222
let update_fee = mock_get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
220
223
224
+ alice. fund ( update_fee) ;
225
+
221
226
let result = pyth_contract
222
227
. sender_and_value ( alice, update_fee)
223
228
. update_price_feeds ( update_data) ;
@@ -227,10 +232,7 @@ mod test {
227
232
. sender ( alice)
228
233
. get_price_no_older_than ( TEST_PRICE_ID , u64:: MAX ) ;
229
234
assert ! ( price_result. is_ok( ) ) ;
230
- assert_eq ! (
231
- price_result. unwrap( ) ,
232
- good_update2_results( )
233
- ) ;
235
+ assert_eq ! ( price_result. unwrap( ) , good_update2_results( ) ) ;
234
236
}
235
237
236
238
#[ motsu:: test]
@@ -241,11 +243,11 @@ mod test {
241
243
) {
242
244
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
243
245
244
- alice. fund ( U256 :: from ( 200 ) ) ;
245
-
246
246
let update_data = test_data:: good_update2 ( ) ;
247
247
let update_fee = mock_get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
248
248
249
+ alice. fund ( update_fee) ;
250
+
249
251
let result = pyth_contract
250
252
. sender_and_value ( alice, update_fee)
251
253
. update_price_feeds ( update_data) ;
@@ -269,11 +271,11 @@ mod test {
269
271
) {
270
272
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
271
273
272
- alice. fund ( U256 :: from ( 200 ) ) ;
273
-
274
274
let update_data = test_data:: multiple_updates ( ) ;
275
275
let update_fee = mock_get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
276
276
277
+ alice. fund ( update_fee) ;
278
+
277
279
let result = pyth_contract
278
280
. sender_and_value ( alice, update_fee)
279
281
. update_price_feeds ( update_data) ;
@@ -292,16 +294,10 @@ mod test {
292
294
293
295
let first_price_result = pyth_contract. sender ( alice) . get_price_unsafe ( first_id) ;
294
296
assert ! ( first_price_result. is_ok( ) ) ;
295
- assert_eq ! (
296
- first_price_result. unwrap( ) ,
297
- multiple_updates_results( ) [ 0 ]
298
- ) ;
297
+ assert_eq ! ( first_price_result. unwrap( ) , multiple_updates_results( ) [ 0 ] ) ;
299
298
300
299
let second_price_result = pyth_contract. sender ( alice) . get_price_unsafe ( second_id) ;
301
300
assert ! ( second_price_result. is_ok( ) ) ;
302
- assert_eq ! (
303
- second_price_result. unwrap( ) ,
304
- multiple_updates_results( ) [ 1 ]
305
- ) ;
301
+ assert_eq ! ( second_price_result. unwrap( ) , multiple_updates_results( ) [ 1 ] ) ;
306
302
}
307
303
}
0 commit comments