Skip to content

Commit cef812a

Browse files
committed
trying to clean up testing framework
1 parent fcd55c4 commit cef812a

File tree

3 files changed

+159
-204
lines changed

3 files changed

+159
-204
lines changed

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

Lines changed: 108 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mod test {
104104
) {
105105
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
106106

107-
let update_data = good_update1();
107+
let update_data = ban_usd_update();
108108
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
109109

110110
alice.fund(update_fee);
@@ -116,9 +116,9 @@ mod test {
116116

117117
let price_result = pyth_contract
118118
.sender(alice)
119-
.get_price_unsafe(good_update1_feed_id());
119+
.get_price_unsafe(ban_usd_feed_id());
120120
assert!(price_result.is_ok());
121-
assert_eq!(price_result.unwrap(), good_update1_results_get_price());
121+
assert_eq!(price_result.unwrap(), ban_usd_results_get_price());
122122
}
123123

124124
#[motsu::test]
@@ -131,7 +131,7 @@ mod test {
131131

132132
alice.fund(U256::from(200));
133133

134-
let update_data = good_update1();
134+
let update_data = ban_usd_update();
135135
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
136136
let small_update_fee = update_fee / U256::from(2);
137137

@@ -150,10 +150,10 @@ mod test {
150150
) {
151151
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
152152

153-
let update_data1 = good_update1();
153+
let update_data1 = ban_usd_update();
154154
let update_fee1 = mock_get_update_fee(update_data1.clone()).unwrap();
155155

156-
let update_data2 = good_update2();
156+
let update_data2 = btc_usd_update();
157157
let update_fee2 = mock_get_update_fee(update_data2.clone()).unwrap();
158158

159159
alice.fund(update_fee1 + update_fee2);
@@ -170,9 +170,9 @@ mod test {
170170

171171
let price_result = pyth_contract
172172
.sender(alice)
173-
.get_price_unsafe(good_update1_feed_id());
173+
.get_price_unsafe(ban_usd_feed_id());
174174
assert!(price_result.is_ok());
175-
assert_eq!(price_result.unwrap(), good_update2_results_get_price());
175+
assert_eq!(price_result.unwrap(), ban_usd_results_get_price());
176176
}
177177

178178
#[motsu::test]
@@ -185,7 +185,7 @@ mod test {
185185

186186
let price_result = pyth_contract
187187
.sender(alice)
188-
.get_price_unsafe(good_update1_feed_id());
188+
.get_price_unsafe(ban_usd_feed_id());
189189
assert!(price_result.is_err());
190190
assert_eq!(
191191
price_result.unwrap_err(),
@@ -227,7 +227,7 @@ mod test {
227227
MockClock::set_time(Duration::from_secs(1761573860));
228228
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
229229

230-
let update_data = good_update2();
230+
let update_data = btc_usd_update();
231231
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
232232

233233
alice.fund(update_fee);
@@ -239,9 +239,9 @@ mod test {
239239

240240
let price_result = pyth_contract
241241
.sender(alice)
242-
.get_price_no_older_than(good_update1_feed_id(), u64::MAX);
242+
.get_price_no_older_than(btc_usd_feed_id(), u64::MAX);
243243
assert!(price_result.is_ok());
244-
assert_eq!(price_result.unwrap(), good_update2_results_get_price());
244+
assert_eq!(price_result.unwrap(), btc_usd_results_get_price());
245245
}
246246

247247
#[motsu::test]
@@ -253,7 +253,7 @@ mod test {
253253
MockClock::set_time(Duration::from_secs(1761573860));
254254
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
255255

256-
let update_data = good_update2();
256+
let update_data = btc_usd_update();
257257
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
258258

259259
alice.fund(update_fee);
@@ -265,7 +265,8 @@ mod test {
265265

266266
let price_result = pyth_contract
267267
.sender(alice)
268-
.get_price_no_older_than(good_update1_feed_id(), 1);
268+
.get_price_no_older_than(btc_usd_feed_id(), 1);
269+
println!("Price result: {:?}", price_result);
269270
assert!(price_result.is_err());
270271
assert_eq!(
271272
price_result.unwrap_err(),
@@ -274,14 +275,14 @@ mod test {
274275
}
275276

276277
#[motsu::test]
277-
fn test_multiple_updates_in_same_vaa_different_ids_updates_both(
278+
fn test_multiple_updates_different_ids_updates_both(
278279
pyth_contract: Contract<PythReceiver>,
279280
wormhole_contract: Contract<WormholeContract>,
280281
alice: Address,
281282
) {
282283
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
283284

284-
let update_data = multiple_updates_same_vaa();
285+
let update_data = multiple_updates_diff_vaa();
285286
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
286287

287288
alice.fund(update_fee);
@@ -291,41 +292,38 @@ mod test {
291292
.update_price_feeds(update_data);
292293
assert!(result.is_ok());
293294

294-
let first_id: [u8; 32] = [
295-
0xe6, 0x2d, 0xf6, 0xc8, 0xb4, 0xa8, 0x5f, 0xe1, 0xa6, 0x7d, 0xb4, 0x4d, 0xc1, 0x2d,
296-
0xe5, 0xdb, 0x33, 0x0f, 0x7a, 0xc6, 0x6b, 0x72, 0xdc, 0x65, 0x8a, 0xfe, 0xdf, 0x0f,
297-
0x4a, 0x41, 0x5b, 0x43,
298-
];
299-
let second_id: [u8; 32] = [
300-
0xff, 0x61, 0x49, 0x1a, 0x93, 0x11, 0x12, 0xdd, 0xf1, 0xbd, 0x81, 0x47, 0xcd, 0x1b,
301-
0x64, 0x13, 0x75, 0xf7, 0x9f, 0x58, 0x25, 0x12, 0x6d, 0x66, 0x54, 0x80, 0x87, 0x46,
302-
0x34, 0xfd, 0x0a, 0xce,
303-
];
304-
305-
let first_price_result = pyth_contract.sender(alice).get_price_unsafe(first_id);
295+
let first_price_result = pyth_contract
296+
.sender(alice)
297+
.get_price_unsafe(ban_usd_feed_id());
306298
assert!(first_price_result.is_ok());
307299
assert_eq!(
308300
first_price_result.unwrap(),
309-
multiple_updates_results_get_price()[0]
301+
multiple_updates_diff_vaa_results_get_price()[0]
310302
);
311303

312-
let second_price_result = pyth_contract.sender(alice).get_price_unsafe(second_id);
304+
let second_price_result = pyth_contract
305+
.sender(alice)
306+
.get_price_unsafe(btc_usd_feed_id());
313307
assert!(second_price_result.is_ok());
314308
assert_eq!(
315309
second_price_result.unwrap(),
316-
multiple_updates_results_get_price()[1]
310+
multiple_updates_diff_vaa_results_get_price()[1]
317311
);
318312
}
319313

320314
#[motsu::test]
321-
fn test_multiple_updates_different_ids_updates_both(
315+
fn test_price_feed_exists(
322316
pyth_contract: Contract<PythReceiver>,
323317
wormhole_contract: Contract<WormholeContract>,
324318
alice: Address,
325319
) {
326320
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
327321

328-
let update_data = multiple_updates_diff_vaa();
322+
assert!(!pyth_contract
323+
.sender(alice)
324+
.price_feed_exists(ban_usd_feed_id()));
325+
326+
let update_data = ban_usd_update();
329327
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
330328

331329
alice.fund(update_fee);
@@ -335,35 +333,94 @@ mod test {
335333
.update_price_feeds(update_data);
336334
assert!(result.is_ok());
337335

338-
let first_id: [u8; 32] = [
339-
0x3f, 0xa4, 0x25, 0x28, 0x48, 0xf9, 0xf0, 0xa1, 0x48, 0x0b, 0xe6, 0x27, 0x45, 0xa4,
340-
0x62, 0x9d, 0x9e, 0xb1, 0x32, 0x2a, 0xeb, 0xab, 0x8a, 0x79, 0x1e, 0x34, 0x4b, 0x3b,
341-
0x9c, 0x1a, 0xdc, 0xf5,
342-
];
343-
let second_id: [u8; 32] = TEST_PRICE_ID;
336+
assert!(pyth_contract
337+
.sender(alice)
338+
.price_feed_exists(ban_usd_feed_id()));
339+
}
344340

345-
let first_price_result = pyth_contract.sender(alice).get_price_unsafe(first_id);
346-
assert!(first_price_result.is_ok());
347-
assert_eq!(
348-
first_price_result.unwrap(),
349-
multiple_updates_diff_vaa_results_get_price()[0]
350-
);
341+
#[motsu::test]
342+
fn test_query_price_feed_doesnt_exist(
343+
pyth_contract: Contract<PythReceiver>,
344+
wormhole_contract: Contract<WormholeContract>,
345+
alice: Address,
346+
) {
347+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
351348

352-
let second_price_result = pyth_contract.sender(alice).get_price_unsafe(second_id);
353-
assert!(second_price_result.is_ok());
349+
let price_result = pyth_contract
350+
.sender(alice)
351+
.get_price_unsafe(ban_usd_feed_id());
352+
353+
assert!(price_result.is_err());
354354
assert_eq!(
355-
second_price_result.unwrap(),
356-
multiple_updates_diff_vaa_results_get_price()[1]
355+
price_result.unwrap_err(),
356+
PythReceiverError::PriceFeedNotFound
357357
);
358358
}
359359

360360
#[motsu::test]
361-
fn test_multiple_updates_same_id_updates_latest(
361+
fn test_query_price_feed_after_one_feed_update(
362362
pyth_contract: Contract<PythReceiver>,
363363
wormhole_contract: Contract<WormholeContract>,
364364
alice: Address,
365365
) {
366366
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
367-
alice.fund(U256::from(200));
367+
368+
let update_data = ban_usd_update();
369+
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
370+
371+
alice.fund(update_fee);
372+
373+
let result = pyth_contract
374+
.sender_and_value(alice, update_fee)
375+
.update_price_feeds(update_data);
376+
377+
assert!(result.is_ok());
378+
379+
let price_result = pyth_contract
380+
.sender(alice)
381+
.query_price_feed(ban_usd_feed_id());
382+
383+
assert!(price_result.is_ok());
384+
assert_eq!(price_result.unwrap(), ban_usd_results_full());
385+
}
386+
387+
#[motsu::test]
388+
fn test_query_price_feed_after_multiple_updates(
389+
pyth_contract: Contract<PythReceiver>,
390+
wormhole_contract: Contract<WormholeContract>,
391+
alice: Address,
392+
) {
393+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
394+
395+
let update_data = multiple_updates_diff_vaa();
396+
let update_fee = mock_get_update_fee(update_data.clone()).unwrap();
397+
398+
alice.fund(update_fee);
399+
400+
let result = pyth_contract
401+
.sender_and_value(alice, update_fee)
402+
.update_price_feeds(update_data);
403+
404+
assert!(result.is_ok());
405+
406+
let price_result1 = pyth_contract
407+
.sender(alice)
408+
.query_price_feed(ban_usd_feed_id());
409+
410+
assert!(price_result1.is_ok());
411+
assert_eq!(
412+
price_result1.unwrap(),
413+
multiple_updates_diff_vaa_results_full()[0]
414+
);
415+
416+
let price_result2 = pyth_contract
417+
.sender(alice)
418+
.query_price_feed(btc_usd_feed_id());
419+
420+
assert!(price_result2.is_ok());
421+
assert_eq!(
422+
price_result2.unwrap(),
423+
multiple_updates_diff_vaa_results_full()[1]
424+
);
368425
}
369426
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl PythReceiver {
123123
let price_info = self.latest_price_info.get(id_fb);
124124

125125
if price_info.publish_time.get() == U64::ZERO {
126-
return Err(PythReceiverError::PriceUnavailable);
126+
return Err(PythReceiverError::PriceFeedNotFound);
127127
}
128128

129129
Ok((
@@ -160,7 +160,7 @@ impl PythReceiver {
160160
age: u64,
161161
) -> Result<PriceReturn, PythReceiverError> {
162162
let price_info = self.get_price_unsafe(id)?;
163-
if !self.is_no_older_than(price_info.1, age) {
163+
if !self.is_no_older_than(price_info.3, age) {
164164
return Err(PythReceiverError::NewPriceUnavailable);
165165
}
166166
Ok(price_info)
@@ -188,7 +188,7 @@ impl PythReceiver {
188188
age: u64,
189189
) -> Result<PriceReturn, PythReceiverError> {
190190
let price_info = self.get_ema_price_unsafe(id)?;
191-
if !self.is_no_older_than(price_info.1, age) {
191+
if !self.is_no_older_than(price_info.3, age) {
192192
return Err(PythReceiverError::NewPriceUnavailable);
193193
}
194194
Ok(price_info)

0 commit comments

Comments
 (0)