Skip to content

Commit c8380bb

Browse files
committed
stopped hardcoding update fees
1 parent 94bf971 commit c8380bb

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

target_chains/stylus/contracts/pyth-receiver/src/integration_tests.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod test {
55
use crate::PythReceiver;
66
use alloy_primitives::{address, Address, I32, I64, U256, U64};
77
use motsu::prelude::*;
8+
use pythnet_sdk::wire::v1::{AccumulatorUpdateData, Proof};
89
use wormhole_contract::WormholeContract;
910
const TEST_PRICE_ID: [u8; 32] = [
1011
0xe6, 0x2d, 0xf6, 0xc8, 0xb4, 0xa8, 0x5f, 0xe1, 0xa6, 0x7d, 0xb4, 0x4d, 0xc1, 0x2d, 0xe5,
@@ -29,6 +30,8 @@ mod test {
2930
const GOVERNANCE_CHAIN_ID: u16 = 1;
3031
const GOVERNANCE_CONTRACT: U256 = U256::from_limbs([4, 0, 0, 0]);
3132

33+
const SINGLE_UPDATE_FEE_IN_WEI: U256 = U256::from_limbs([100, 0, 0, 0]);
34+
3235
#[cfg(test)]
3336
fn current_guardians() -> Vec<Address> {
3437
vec![
@@ -54,6 +57,21 @@ mod test {
5457
]
5558
}
5659

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)]
5775
fn pyth_wormhole_init(
5876
pyth_contract: &Contract<PythReceiver>,
5977
wormhole_contract: &Contract<WormholeContract>,
@@ -73,7 +91,7 @@ mod test {
7391
)
7492
.unwrap();
7593

76-
let single_update_fee = U256::from(100u64);
94+
let single_update_fee = SINGLE_UPDATE_FEE_IN_WEI;
7795
let valid_time_period = U256::from(3600u64);
7896

7997
let data_source_chain_ids = vec![PYTHNET_CHAIN_ID];
@@ -108,9 +126,10 @@ mod test {
108126
alice.fund(U256::from(200));
109127

110128
let update_data = test_data::good_update1();
129+
let update_fee = get_update_fee(update_data.clone()).unwrap();
111130

112131
let result = pyth_contract
113-
.sender_and_value(alice, U256::from(100))
132+
.sender_and_value(alice, update_fee)
114133
.update_price_feeds(update_data);
115134
assert!(result.is_ok());
116135

@@ -137,12 +156,14 @@ mod test {
137156
) {
138157
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
139158

140-
alice.fund(U256::from(50));
159+
alice.fund(U256::from(200));
141160

142161
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);
143164

144165
let result = pyth_contract
145-
.sender_and_value(alice, U256::from(50))
166+
.sender_and_value(alice, small_update_fee)
146167
.update_price_feeds(update_data);
147168
assert!(result.is_err());
148169
assert_eq!(result.unwrap_err(), PythReceiverError::InsufficientFee);
@@ -159,14 +180,17 @@ mod test {
159180
alice.fund(U256::from(200));
160181

161182
let update_data1 = test_data::good_update1();
183+
let update_fee1 = get_update_fee(update_data1.clone()).unwrap();
162184
let result1 = pyth_contract
163-
.sender_and_value(alice, U256::from(100))
185+
.sender_and_value(alice, update_fee1)
164186
.update_price_feeds(update_data1);
165187
assert!(result1.is_ok());
166188

167189
let update_data2 = test_data::good_update2();
190+
let update_fee2 = get_update_fee(update_data2.clone()).unwrap();
191+
168192
let result2 = pyth_contract
169-
.sender_and_value(alice, U256::from(100))
193+
.sender_and_value(alice, update_fee2)
170194
.update_price_feeds(update_data2);
171195
assert!(result2.is_ok());
172196

@@ -236,8 +260,10 @@ mod test {
236260
alice.fund(U256::from(200));
237261

238262
let update_data = test_data::good_update2();
263+
let update_fee = get_update_fee(update_data.clone()).unwrap();
264+
239265
let result = pyth_contract
240-
.sender_and_value(alice, U256::from(100))
266+
.sender_and_value(alice, update_fee)
241267
.update_price_feeds(update_data);
242268
assert!(result.is_ok());
243269

@@ -269,8 +295,10 @@ mod test {
269295
alice.fund(U256::from(200));
270296

271297
let update_data = test_data::good_update2();
298+
let update_fee = get_update_fee(update_data.clone()).unwrap();
299+
272300
let result = pyth_contract
273-
.sender_and_value(alice, U256::from(100))
301+
.sender_and_value(alice, update_fee)
274302
.update_price_feeds(update_data);
275303
assert!(result.is_ok());
276304

@@ -295,8 +323,10 @@ mod test {
295323
alice.fund(U256::from(200));
296324

297325
let update_data = test_data::multiple_updates();
326+
let update_fee = get_update_fee(update_data.clone()).unwrap();
327+
298328
let result = pyth_contract
299-
.sender_and_value(alice, U256::from(200))
329+
.sender_and_value(alice, update_fee)
300330
.update_price_feeds(update_data);
301331
assert!(result.is_ok());
302332

0 commit comments

Comments
 (0)