Skip to content

Commit 0681af5

Browse files
feat: implement mock_instant integration for get_current_timestamp
- Modified get_current_timestamp to use MockClock::time().as_secs() in test mode - Added MockClock::set_time(Duration::from_secs(1761573860)) to three specific tests: - test_get_price_no_older_than_with_random_id_reverts_with_price_unavailable - test_get_price_no_older_than_where_update_younger_than_max_age_returns_price - test_get_price_no_older_than_reverts_too_old - Contract functionality unchanged (still uses block_timestamp in production) - All specified tests pass with mocked timestamp Co-Authored-By: ayush.suresh@dourolabs.xyz <byteSlayer31037@gmail.com>
1 parent a0310a2 commit 0681af5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod test {
77
use motsu::prelude::*;
88
use pythnet_sdk::wire::v1::{AccumulatorUpdateData, Proof};
99
use wormhole_contract::WormholeContract;
10+
use mock_instant::global::MockClock;
11+
use std::time::Duration;
1012
const TEST_PRICE_ID: [u8; 32] = [
1113
0xe6, 0x2d, 0xf6, 0xc8, 0xb4, 0xa8, 0x5f, 0xe1, 0xa6, 0x7d, 0xb4, 0x4d, 0xc1, 0x2d, 0xe5,
1214
0xdb, 0x33, 0x0f, 0x7a, 0xc6, 0x6b, 0x72, 0xdc, 0x65, 0x8a, 0xfe, 0xdf, 0x0f, 0x4a, 0x41,
@@ -191,6 +193,7 @@ mod test {
191193
wormhole_contract: Contract<WormholeContract>,
192194
alice: Address,
193195
) {
196+
MockClock::set_time(Duration::from_secs(1761573860));
194197
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
195198

196199
let random_id: [u8; 32] = [
@@ -215,6 +218,7 @@ mod test {
215218
wormhole_contract: Contract<WormholeContract>,
216219
alice: Address,
217220
) {
221+
MockClock::set_time(Duration::from_secs(1761573860));
218222
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
219223

220224
let update_data = good_update2();
@@ -240,6 +244,7 @@ mod test {
240244
wormhole_contract: Contract<WormholeContract>,
241245
alice: Address,
242246
) {
247+
MockClock::set_time(Duration::from_secs(1761573860));
243248
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
244249

245250
let update_data = good_update2();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod structs;
1212
#[cfg(test)]
1313
mod test_data;
1414

15+
#[cfg(test)]
16+
use mock_instant::global::MockClock;
17+
1518
use alloc::vec::Vec;
1619
use stylus_sdk::{
1720
alloy_primitives::{Address, FixedBytes, I32, I64, U16, U256, U32, U64},
@@ -377,7 +380,7 @@ impl PythReceiver {
377380
fn get_current_timestamp(&self) -> u64 {
378381
#[cfg(test)]
379382
{
380-
1761573860u64
383+
MockClock::time().as_secs()
381384
}
382385
#[cfg(not(test))]
383386
{

0 commit comments

Comments
 (0)