Skip to content

Commit

Permalink
provider: Add receipt deserialize tests for AnyNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
moricho committed Jun 11, 2024
1 parent 7578618 commit 72c728e
Showing 1 changed file with 123 additions and 1 deletion.
124 changes: 123 additions & 1 deletion crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,42 @@ impl<T: Transport + Clone, N: Network> Provider<T, N> for RootProvider<T, N> {
mod tests {
use super::*;
use crate::{ProviderBuilder, WalletProvider};
use alloy_network::AnyNetwork;
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, b256, bytes};
use alloy_rpc_types_eth::request::TransactionRequest;
use alloy_rpc_types_eth::{request::TransactionRequest, WithOtherFields};
use serde::Deserialize;

fn init_tracing() {
let _ = tracing_subscriber::fmt::try_init();
}

// OtherFields for Optimism
#[derive(Debug, Deserialize)]
struct OptOtherFields {
#[serde(rename = "l1BaseFeeScalar")]
l1_base_fee_scalar: String,
#[serde(rename = "l1BlobBaseFee")]
l1_blob_base_fee: String,
#[serde(rename = "l1BlobBaseFeeScalar")]
l1_blob_base_fee_scalar: String,
#[serde(rename = "l1Fee")]
l1_fee: String,
#[serde(rename = "l1GasPrice")]
l1_gas_price: String,
#[serde(rename = "l1GasUsed")]
l1_gas_used: String,
}

// OtherFields for Arbitrum
#[derive(Debug, Deserialize)]
struct ArbOtherFields {
#[serde(rename = "gasUsedForL1")]
gas_used_for_l1: String,
#[serde(rename = "l1BlockNumber")]
l1_block_number: String,
}

#[cfg(feature = "reqwest")]
#[tokio::test]
async fn object_safety() {
Expand Down Expand Up @@ -1269,6 +1297,58 @@ mod tests {
);
}

#[tokio::test]
#[ignore]
async fn gets_tx_receipt_opt() {
init_tracing();
let url = "https://opt-mainnet.g.alchemy.com/v2/demo";
let provider = ProviderBuilder::new().network::<AnyNetwork>().on_http(url.parse().unwrap());
let receipt = provider
.get_transaction_receipt(b256!(
"2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2"
))
.await
.unwrap();
assert!(receipt.is_some());
let receipt = receipt.unwrap();
assert_eq!(
receipt.transaction_hash,
b256!("2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2")
);

let other: OptOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.l1_base_fee_scalar, "0x558");
assert_eq!(other.l1_blob_base_fee, "0x1");
assert_eq!(other.l1_blob_base_fee_scalar, "0xc5fc5");
assert_eq!(other.l1_fee, "0x105d4b2024");
assert_eq!(other.l1_gas_price, "0x5d749a07e");
assert_eq!(other.l1_gas_used, "0x800");
}

#[tokio::test]
#[ignore]
async fn gets_tx_receipt_arb() {
init_tracing();
let url = "https://arb-mainnet.g.alchemy.com/v2/demo";
let provider = ProviderBuilder::new().network::<AnyNetwork>().on_http(url.parse().unwrap());
let receipt = provider
.get_transaction_receipt(b256!(
"5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3"
))
.await
.unwrap();
assert!(receipt.is_some());
let receipt = receipt.unwrap();
assert_eq!(
receipt.transaction_hash,
b256!("5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3")
);

let other: ArbOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.gas_used_for_l1, "0x2c906");
assert_eq!(other.l1_block_number, "0x1323b96");
}

#[tokio::test]
async fn gets_max_priority_fee_per_gas() {
init_tracing();
Expand Down Expand Up @@ -1368,6 +1448,48 @@ mod tests {
assert_eq!(String::abi_decode(&result, true).unwrap(), "Wrapped Ether");
}

#[tokio::test]
#[cfg(any(
feature = "reqwest-default-tls",
feature = "reqwest-rustls-tls",
feature = "reqwest-native-tls",
))]
async fn call_opt_mainnet() {
use alloy_network::TransactionBuilder;
use alloy_sol_types::SolValue;

init_tracing();
let url = "https://opt-mainnet.g.alchemy.com/v2/demo";
let provider = ProviderBuilder::new().network::<AnyNetwork>().on_http(url.parse().unwrap());
let req = TransactionRequest::default()
.with_to(address!("4200000000000000000000000000000000000006")) // WETH
.with_input(bytes!("06fdde03")); // `name()`
let req = WithOtherFields::new(req);
let result = provider.call(&req).await.unwrap();
assert_eq!(String::abi_decode(&result, true).unwrap(), "Wrapped Ether");
}

#[tokio::test]
#[cfg(any(
feature = "reqwest-default-tls",
feature = "reqwest-rustls-tls",
feature = "reqwest-native-tls",
))]
async fn call_arb_mainnet() {
use alloy_network::TransactionBuilder;
use alloy_sol_types::SolValue;

init_tracing();
let url = "https://arb-mainnet.g.alchemy.com/v2/demo";
let provider = ProviderBuilder::new().network::<AnyNetwork>().on_http(url.parse().unwrap());
let req = TransactionRequest::default()
.with_to(address!("82aF49447D8a07e3bd95BD0d56f35241523fBab1")) // WETH
.with_input(bytes!("06fdde03")); // `name()`
let req = WithOtherFields::new(req);
let result = provider.call(&req).await.unwrap();
assert_eq!(String::abi_decode(&result, true).unwrap(), "Wrapped Ether");
}

#[tokio::test]
async fn test_empty_transactions() {
init_tracing();
Expand Down

0 comments on commit 72c728e

Please sign in to comment.