Skip to content

Commit 5fb8216

Browse files
Extracted common functionality for example tests
1 parent a9c6957 commit 5fb8216

File tree

6 files changed

+142
-175
lines changed

6 files changed

+142
-175
lines changed

examples/abi_decode/tests/abi_decode_integration_test.rs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod integration_test {
88
sol,
99
};
1010
use eyre::Result;
11-
use stylus_tools::devnet::Node;
11+
use stylus_tools::utils::testing::ExampleContractTester;
1212

1313
sol! {
1414
#[sol(rpc)]
@@ -18,53 +18,20 @@ mod integration_test {
1818
}
1919
}
2020

21-
const EXPECTED_ABI: &str = "\
21+
struct AbiDecodeIntegrationTester {}
22+
23+
impl ExampleContractTester for AbiDecodeIntegrationTester {
24+
const EXPECTED_ABI: &'static str = "\
2225
interface IDecoder {
2326
function encodeAndDecode(address _address, uint256 amount) external view returns (bool);
2427
2528
error DecodedFailed();
2629
}";
27-
const EXPECTED_CONSTRUCTOR: &str = "";
30+
}
2831

2932
#[tokio::test]
3033
async fn abi_decode() -> Result<()> {
31-
let exporter = stylus_tools::Exporter::builder().build();
32-
assert_eq!(exporter.export_abi()?, EXPECTED_ABI);
33-
assert_eq!(exporter.export_constructor()?, EXPECTED_CONSTRUCTOR);
34-
35-
let devnode = Node::new().await?;
36-
let rpc = devnode.rpc();
37-
38-
println!("Checking contract on Nitro ({rpc})...");
39-
stylus_tools::Checker::builder().rpc(rpc).build().check()?;
40-
println!("Checked contract");
41-
42-
let deployer = stylus_tools::Deployer::builder().rpc(rpc).build();
43-
println!("Estimating gas...");
44-
let gas_estimate = deployer.estimate_gas()?;
45-
println!("Estimated deployment gas: {gas_estimate} ETH");
46-
47-
println!("Deploying contract to Nitro ({rpc})...");
48-
let (address, tx_hash, gas_used) = deployer.deploy()?;
49-
println!("Deployed contract to {address}");
50-
51-
// Approximate equality is usually expected, but given the test conditions, the gas estimate equals the gas used
52-
assert_eq!(gas_used, gas_estimate);
53-
54-
println!("Activating contract at {address} on Nitro ({rpc})...");
55-
stylus_tools::Activator::builder()
56-
.rpc(rpc)
57-
.contract_address(address.to_string())
58-
.build()
59-
.activate()?;
60-
println!("Activated contract at {address}");
61-
62-
let verify = stylus_tools::Verifier::builder()
63-
.rpc(rpc)
64-
.deployment_tx_hash(tx_hash.to_string())
65-
.build()
66-
.verify();
67-
assert!(verify.is_ok(), "Failed to verify contract");
34+
let (devnode, address) = AbiDecodeIntegrationTester::init().await?;
6835
let provider = devnode.create_provider().await?;
6936

7037
// Instantiate contract

examples/abi_encode/tests/abi_encode_integration_test.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod integration_test {
88
sol,
99
};
1010
use eyre::Result;
11-
use stylus_tools::devnet::Node;
11+
use stylus_tools::utils::testing::ExampleContractTester;
1212

1313
sol! {
1414
#[sol(rpc)]
@@ -20,7 +20,9 @@ mod integration_test {
2020
}
2121
}
2222

23-
const EXPECTED_ABI: &str = "\
23+
struct AbiEncodeIntegrationTester {}
24+
impl ExampleContractTester for AbiEncodeIntegrationTester {
25+
const EXPECTED_ABI: &'static str = "\
2426
interface IEncoder {
2527
function encode(address target, uint256 value, string calldata func, bytes calldata data, uint256 timestamp) external view returns (uint8[] memory);
2628
@@ -30,48 +32,11 @@ interface IEncoder {
3032
3133
function encodeWithSignature(string calldata func, address _address, uint256 amount) external view returns (uint8[] memory);
3234
}";
33-
34-
const EXPECTED_CONSTRUCTOR: &str = "";
35+
}
3536

3637
#[tokio::test]
3738
async fn abi_encode() -> Result<()> {
38-
let exporter = stylus_tools::Exporter::builder().build();
39-
assert_eq!(exporter.export_abi()?, EXPECTED_ABI);
40-
assert_eq!(exporter.export_constructor()?, EXPECTED_CONSTRUCTOR);
41-
42-
let devnode = Node::new().await?;
43-
let rpc = devnode.rpc();
44-
45-
println!("Checking contract on Nitro ({rpc})...");
46-
stylus_tools::Checker::builder().rpc(rpc).build().check()?;
47-
println!("Checked contract");
48-
49-
let deployer = stylus_tools::Deployer::builder().rpc(rpc).build();
50-
println!("Estimating gas...");
51-
let gas_estimate = deployer.estimate_gas()?;
52-
println!("Estimated deployment gas: {gas_estimate} ETH");
53-
54-
println!("Deploying contract to Nitro ({rpc})...");
55-
let (address, tx_hash, gas_used) = deployer.deploy()?;
56-
println!("Deployed contract to {address}");
57-
58-
// Approximate equality is usually expected, but given the test conditions, the gas estimate equals the gas used
59-
assert_eq!(gas_used, gas_estimate);
60-
61-
println!("Activating contract at {address} on Nitro ({rpc})...");
62-
stylus_tools::Activator::builder()
63-
.rpc(rpc)
64-
.contract_address(address.to_string())
65-
.build()
66-
.activate()?;
67-
println!("Activated contract at {address}");
68-
69-
let verify = stylus_tools::Verifier::builder()
70-
.rpc(rpc)
71-
.deployment_tx_hash(tx_hash.to_string())
72-
.build()
73-
.verify();
74-
assert!(verify.is_ok(), "Failed to verify contract");
39+
let (devnode, address) = AbiEncodeIntegrationTester::init().await?;
7540
let provider = devnode.create_provider().await?;
7641

7742
// Instantiate contract

examples/arrays/tests/arrays_integration_test.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
mod integration_test {
66
use alloy::{primitives::U256, sol};
77
use eyre::Result;
8-
use stylus_tools::devnet::{addresses::OWNER, Node};
8+
use stylus_tools::devnet::addresses::OWNER;
9+
use stylus_tools::utils::testing::ExampleContractTester;
910

1011
sol! {
1112
#[sol(rpc)]
@@ -24,7 +25,10 @@ mod integration_test {
2425
}
2526
}
2627

27-
const EXPECTED_ABI: &str = "\
28+
struct ArraysIntegrationTester {}
29+
30+
impl ExampleContractTester for ArraysIntegrationTester {
31+
const EXPECTED_ABI: &'static str = "\
2832
interface IArrays {
2933
function push(uint256 i) external;
3034
@@ -48,47 +52,11 @@ interface IArrays {
4852
4953
function findArr3FirstExpectedValue(uint256 expected_value) external view returns (uint256);
5054
}";
51-
const EXPECTED_CONSTRUCTOR: &str = "";
55+
}
5256

5357
#[tokio::test]
5458
async fn arrays() -> Result<()> {
55-
let exporter = stylus_tools::Exporter::builder().build();
56-
assert_eq!(exporter.export_abi()?, EXPECTED_ABI);
57-
assert_eq!(exporter.export_constructor()?, EXPECTED_CONSTRUCTOR);
58-
59-
let devnode = Node::new().await?;
60-
let rpc = devnode.rpc();
61-
62-
println!("Checking contract on Nitro ({rpc})...");
63-
stylus_tools::Checker::builder().rpc(rpc).build().check()?;
64-
println!("Checked contract");
65-
66-
let deployer = stylus_tools::Deployer::builder().rpc(rpc).build();
67-
println!("Estimating gas...");
68-
let gas_estimate = deployer.estimate_gas()?;
69-
println!("Estimated deployment gas: {gas_estimate} ETH");
70-
71-
println!("Deploying contract to Nitro ({rpc})...");
72-
let (address, tx_hash, gas_used) = deployer.deploy()?;
73-
println!("Deployed contract to {address}");
74-
75-
// Approximate equality is usually expected, but given the test conditions, the gas estimate equals the gas used
76-
assert_eq!(gas_used, gas_estimate);
77-
78-
println!("Activating contract at {address} on Nitro ({rpc})...");
79-
stylus_tools::Activator::builder()
80-
.rpc(rpc)
81-
.contract_address(address.to_string())
82-
.build()
83-
.activate()?;
84-
println!("Activated contract at {address}");
85-
86-
let verify = stylus_tools::Verifier::builder()
87-
.rpc(rpc)
88-
.deployment_tx_hash(tx_hash.to_string())
89-
.build()
90-
.verify();
91-
assert!(verify.is_ok(), "Failed to verify contract");
59+
let (devnode, address) = ArraysIntegrationTester::init().await?;
9260
let provider = devnode.create_provider().await?;
9361

9462
// Instantiate contract

examples/constructor/tests/constructor_integration_test.rs

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ mod integration_test {
88
providers::Provider,
99
sol,
1010
};
11+
use alloy_primitives::TxHash;
1112
use eyre::Result;
12-
use stylus_tools::devnet::{addresses::OWNER, Node};
13+
use stylus_tools::devnet::addresses::OWNER;
14+
use stylus_tools::utils::testing::ExampleContractTester;
15+
use stylus_tools::{Deployer, Verifier};
1316

1417
sol! {
1518
#[sol(rpc)]
@@ -21,7 +24,10 @@ mod integration_test {
2124
}
2225
}
2326

24-
const EXPECTED_ABI: &str = "\
27+
struct ConstructorIntegrationTester {}
28+
29+
impl ExampleContractTester for ConstructorIntegrationTester {
30+
const EXPECTED_ABI: &str = "\
2531
interface IContract {
2632
function setNumber(uint256 number) external;
2733
@@ -31,61 +37,38 @@ interface IContract {
3137
3238
error Unauthorized();
3339
}";
34-
const EXPECTED_CONSTRUCTOR: &str = "constructor(uint256 initial_number) payable";
40+
const EXPECTED_CONSTRUCTOR: &'static str = "constructor(uint256 initial_number) payable";
41+
42+
fn deployer(rpc: &str) -> Deployer {
43+
Deployer::builder()
44+
.rpc(rpc.to_owned())
45+
.constructor_args(vec!["0xbeef".to_owned()])
46+
.constructor_value("12.34".to_owned())
47+
.build()
48+
}
49+
50+
fn test_verify(rpc: &str, tx_hash: TxHash) -> Result<()> {
51+
let verify = Verifier::builder()
52+
.rpc(rpc)
53+
.deployment_tx_hash(tx_hash.to_string())
54+
.build()
55+
.verify();
56+
assert!(verify.is_ok(), "Failed to verify contract");
57+
let verify = Verifier::builder()
58+
.rpc(rpc)
59+
.dir("../callee".to_owned())
60+
.deployment_tx_hash(tx_hash.to_string())
61+
.build()
62+
.verify();
63+
assert!(verify.is_err(), "Should fail verifying wrong contract");
64+
println!("Verified contract with tx hash {tx_hash}");
65+
Ok(())
66+
}
67+
}
3568

3669
#[tokio::test]
3770
async fn constructor() -> Result<()> {
38-
let exporter = stylus_tools::Exporter::builder().build();
39-
assert_eq!(exporter.export_abi()?, EXPECTED_ABI);
40-
assert_eq!(exporter.export_constructor()?, EXPECTED_CONSTRUCTOR);
41-
42-
let devnode = Node::new().await?;
43-
let rpc = devnode.rpc();
44-
45-
println!("Checking contract on Nitro ({rpc})...");
46-
stylus_tools::Checker::builder().rpc(rpc).build().check()?;
47-
println!("Checked contract");
48-
49-
let deployer = stylus_tools::Deployer::builder()
50-
.rpc(rpc.to_owned())
51-
.constructor_args(vec!["0xbeef".to_owned()])
52-
.constructor_value("12.34".to_owned())
53-
.build();
54-
println!("Estimating gas...");
55-
let gas_estimate = deployer.estimate_gas()?;
56-
println!("Estimated deployment gas: {gas_estimate} ETH");
57-
58-
println!("Deploying contract to Nitro ({rpc})...");
59-
let (address, tx_hash, gas_used) = deployer.deploy()?;
60-
println!("Deployed contract to {address}");
61-
62-
// Approximate equality is usually expected, but given the test conditions, the gas estimate equals the gas used
63-
assert_eq!(gas_used, gas_estimate);
64-
65-
println!("Activating contract at {address} on Nitro ({rpc})...");
66-
stylus_tools::Activator::builder()
67-
.rpc(rpc)
68-
.contract_address(address.to_string())
69-
.build()
70-
.activate()?;
71-
println!("Activated contract at {address}");
72-
73-
let verify = stylus_tools::Verifier::builder()
74-
.rpc(rpc)
75-
.deployment_tx_hash(tx_hash.to_string())
76-
.build()
77-
.verify();
78-
assert!(verify.is_ok(), "Failed to verify contract");
79-
80-
let verify = stylus_tools::Verifier::builder()
81-
.rpc(rpc)
82-
.dir("../callee".to_owned())
83-
.deployment_tx_hash(tx_hash.to_string())
84-
.build()
85-
.verify();
86-
assert!(verify.is_err(), "Should fail verifying wrong contract");
87-
println!("Verified contract with tx hash {tx_hash}");
88-
71+
let (devnode, address) = ConstructorIntegrationTester::init().await?;
8972
let provider = devnode.create_provider().await?;
9073

9174
// Check balance sent in constructor

stylus-tools/src/utils/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub(crate) mod stylus_sdk;
2727
pub(crate) mod toolchain;
2828
pub(crate) mod wasm;
2929

30+
#[cfg(feature = "integration-tests")]
31+
pub mod testing;
32+
3033
/// Pretty-prints a data fee.
3134
pub fn format_data_fee(fee: U256) -> String {
3235
// TODO: alternative to magic numbers

0 commit comments

Comments
 (0)