Last active
April 25, 2024 08:02
-
-
Save Freyskeyd/233382722c5c10a92d734cce56e5425c to your computer and use it in GitHub Desktop.
Ethers MockResponse order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// MockProvider seems to stack response in the wrong order | |
/// If I've a test that expect to: | |
/// - get_latest_block_number | |
/// - get_block | |
/// | |
/// I'm expecting to define the mock for `get_last_block_number` and then `get_block`. | |
/// It's because of how MockProvider is processing request: https://github.com/gakonst/ethers-rs/blob/master/ethers-providers/src/rpc/transports/mock.rs#L64 | |
/// When a request comes in, it pop_back instead of pop_front. | |
/// However, in the current setup I need to do the opposite, making the definition a bit weird in tests: | |
/// Test from: https://github.com/FuelLabs/fuel-canary-watchtower/blob/8d2939447a6495564b95f67d359b697695d11992/src/ethereum_watcher/ethereum_chain.rs#L130 | |
/// Executing: https://github.com/FuelLabs/fuel-canary-watchtower/blob/8d2939447a6495564b95f67d359b697695d11992/src/ethereum_watcher/ethereum_chain.rs#L52 | |
#[tokio::test] | |
async fn test_get_seconds_since_last_block() { | |
let (mock, chain) = setup_mock_provider().await; | |
let latest_block_number = U64::from(100); | |
let past_timestamp = U256::from(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() - 10); | |
mock.push_response(MockResponse::Value( | |
serde_json::to_value(&Block::<()> { | |
number: Some(latest_block_number), | |
timestamp: past_timestamp, | |
..Default::default() | |
}) | |
.unwrap(), | |
)); | |
mock.push_response(MockResponse::Value(serde_json::json!(latest_block_number))); | |
let seconds_since_last_block = chain.get_seconds_since_last_block().await; | |
assert!(seconds_since_last_block.is_ok()); | |
assert_eq!(seconds_since_last_block.unwrap(), 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment