@@ -5,6 +5,7 @@ mod test {
5
5
use crate :: PythReceiver ;
6
6
use alloy_primitives:: { address, Address , I32 , I64 , U256 , U64 } ;
7
7
use motsu:: prelude:: * ;
8
+ use pythnet_sdk:: wire:: v1:: { AccumulatorUpdateData , Proof } ;
8
9
use wormhole_contract:: WormholeContract ;
9
10
const TEST_PRICE_ID : [ u8 ; 32 ] = [
10
11
0xe6 , 0x2d , 0xf6 , 0xc8 , 0xb4 , 0xa8 , 0x5f , 0xe1 , 0xa6 , 0x7d , 0xb4 , 0x4d , 0xc1 , 0x2d , 0xe5 ,
@@ -29,6 +30,8 @@ mod test {
29
30
const GOVERNANCE_CHAIN_ID : u16 = 1 ;
30
31
const GOVERNANCE_CONTRACT : U256 = U256 :: from_limbs ( [ 4 , 0 , 0 , 0 ] ) ;
31
32
33
+ const SINGLE_UPDATE_FEE_IN_WEI : U256 = U256 :: from_limbs ( [ 100 , 0 , 0 , 0 ] ) ;
34
+
32
35
#[ cfg( test) ]
33
36
fn current_guardians ( ) -> Vec < Address > {
34
37
vec ! [
@@ -54,6 +57,21 @@ mod test {
54
57
]
55
58
}
56
59
60
+ #[ cfg( test) ]
61
+ fn get_update_fee ( update_data : Vec < u8 > ) -> Result < U256 , PythReceiverError > {
62
+ let update_data_array: & [ u8 ] = & update_data;
63
+ let accumulator_update = AccumulatorUpdateData :: try_from_slice ( & update_data_array)
64
+ . map_err ( |_| PythReceiverError :: InvalidAccumulatorMessage ) ?;
65
+ match accumulator_update. proof {
66
+ Proof :: WormholeMerkle { vaa : _, updates } => {
67
+ let num_updates =
68
+ u8:: try_from ( updates. len ( ) ) . map_err ( |_| PythReceiverError :: TooManyUpdates ) ?;
69
+ Ok ( U256 :: from ( num_updates) . saturating_mul ( SINGLE_UPDATE_FEE_IN_WEI ) )
70
+ }
71
+ }
72
+ }
73
+
74
+ #[ cfg( test) ]
57
75
fn pyth_wormhole_init (
58
76
pyth_contract : & Contract < PythReceiver > ,
59
77
wormhole_contract : & Contract < WormholeContract > ,
@@ -73,7 +91,7 @@ mod test {
73
91
)
74
92
. unwrap ( ) ;
75
93
76
- let single_update_fee = U256 :: from ( 100u64 ) ;
94
+ let single_update_fee = SINGLE_UPDATE_FEE_IN_WEI ;
77
95
let valid_time_period = U256 :: from ( 3600u64 ) ;
78
96
79
97
let data_source_chain_ids = vec ! [ PYTHNET_CHAIN_ID ] ;
@@ -108,9 +126,10 @@ mod test {
108
126
alice. fund ( U256 :: from ( 200 ) ) ;
109
127
110
128
let update_data = test_data:: good_update1 ( ) ;
129
+ let update_fee = get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
111
130
112
131
let result = pyth_contract
113
- . sender_and_value ( alice, U256 :: from ( 100 ) )
132
+ . sender_and_value ( alice, update_fee )
114
133
. update_price_feeds ( update_data) ;
115
134
assert ! ( result. is_ok( ) ) ;
116
135
@@ -137,12 +156,14 @@ mod test {
137
156
) {
138
157
pyth_wormhole_init ( & pyth_contract, & wormhole_contract, & alice) ;
139
158
140
- alice. fund ( U256 :: from ( 50 ) ) ;
159
+ alice. fund ( U256 :: from ( 200 ) ) ;
141
160
142
161
let update_data = test_data:: good_update1 ( ) ;
162
+ let update_fee = get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
163
+ let small_update_fee = update_fee / U256 :: from ( 2 ) ;
143
164
144
165
let result = pyth_contract
145
- . sender_and_value ( alice, U256 :: from ( 50 ) )
166
+ . sender_and_value ( alice, small_update_fee )
146
167
. update_price_feeds ( update_data) ;
147
168
assert ! ( result. is_err( ) ) ;
148
169
assert_eq ! ( result. unwrap_err( ) , PythReceiverError :: InsufficientFee ) ;
@@ -159,14 +180,17 @@ mod test {
159
180
alice. fund ( U256 :: from ( 200 ) ) ;
160
181
161
182
let update_data1 = test_data:: good_update1 ( ) ;
183
+ let update_fee1 = get_update_fee ( update_data1. clone ( ) ) . unwrap ( ) ;
162
184
let result1 = pyth_contract
163
- . sender_and_value ( alice, U256 :: from ( 100 ) )
185
+ . sender_and_value ( alice, update_fee1 )
164
186
. update_price_feeds ( update_data1) ;
165
187
assert ! ( result1. is_ok( ) ) ;
166
188
167
189
let update_data2 = test_data:: good_update2 ( ) ;
190
+ let update_fee2 = get_update_fee ( update_data2. clone ( ) ) . unwrap ( ) ;
191
+
168
192
let result2 = pyth_contract
169
- . sender_and_value ( alice, U256 :: from ( 100 ) )
193
+ . sender_and_value ( alice, update_fee2 )
170
194
. update_price_feeds ( update_data2) ;
171
195
assert ! ( result2. is_ok( ) ) ;
172
196
@@ -236,8 +260,10 @@ mod test {
236
260
alice. fund ( U256 :: from ( 200 ) ) ;
237
261
238
262
let update_data = test_data:: good_update2 ( ) ;
263
+ let update_fee = get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
264
+
239
265
let result = pyth_contract
240
- . sender_and_value ( alice, U256 :: from ( 100 ) )
266
+ . sender_and_value ( alice, update_fee )
241
267
. update_price_feeds ( update_data) ;
242
268
assert ! ( result. is_ok( ) ) ;
243
269
@@ -269,8 +295,10 @@ mod test {
269
295
alice. fund ( U256 :: from ( 200 ) ) ;
270
296
271
297
let update_data = test_data:: good_update2 ( ) ;
298
+ let update_fee = get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
299
+
272
300
let result = pyth_contract
273
- . sender_and_value ( alice, U256 :: from ( 100 ) )
301
+ . sender_and_value ( alice, update_fee )
274
302
. update_price_feeds ( update_data) ;
275
303
assert ! ( result. is_ok( ) ) ;
276
304
@@ -295,8 +323,10 @@ mod test {
295
323
alice. fund ( U256 :: from ( 200 ) ) ;
296
324
297
325
let update_data = test_data:: multiple_updates ( ) ;
326
+ let update_fee = get_update_fee ( update_data. clone ( ) ) . unwrap ( ) ;
327
+
298
328
let result = pyth_contract
299
- . sender_and_value ( alice, U256 :: from ( 200 ) )
329
+ . sender_and_value ( alice, update_fee )
300
330
. update_price_feeds ( update_data) ;
301
331
assert ! ( result. is_ok( ) ) ;
302
332
0 commit comments