Skip to content

Commit 58a64ba

Browse files
Merge pull request #361 from OffchainLabs/afilios/nit-3581-add-examples-that-test-cargo-stylus
Test verify, activate, and export in the integration tests
2 parents bb8eee6 + dade556 commit 58a64ba

File tree

40 files changed

+522
-176
lines changed

40 files changed

+522
-176
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-stylus/src/commands/verify.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub struct Args {
2222
#[arg(long)]
2323
deployment_tx: Vec<String>,
2424

25+
#[arg(long)]
26+
skip_clean: bool,
27+
2528
#[command(flatten)]
2629
project: ProjectArgs,
2730
#[command(flatten)]
@@ -40,7 +43,17 @@ pub async fn exec(args: Args) -> CargoStylusResult {
4043
return Err(eyre!("Invalid hash").into());
4144
}
4245
let hash = TxHash::from_slice(&hash);
43-
contract.verify(hash, &provider).await?;
46+
match contract.verify(hash, args.skip_clean, &provider).await? {
47+
stylus_tools::core::verification::VerificationStatus::Success => {
48+
println!("Verification successful");
49+
}
50+
stylus_tools::core::verification::VerificationStatus::Failure(failure) => {
51+
println!("Verification failed");
52+
println!("prelude mismatch: {:?}", failure.prelude_mismatch);
53+
println!("tx wasm length: {}", failure.tx_wasm_length);
54+
println!("build wasm length: {}", failure.build_wasm_length);
55+
}
56+
}
4457
} else {
4558
println!("Running in a Docker container for reproducibility, this may take a while",);
4659
let mut cli_args: Vec<String> =

examples/abi_decode/tests/abi_decode_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ mod integration_test {
2323
let devnode = Node::new().await?;
2424
let rpc = devnode.rpc();
2525
println!("Deploying contract to Nitro ({rpc})...");
26-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
26+
let (address, _, _) = stylus_tools::Deployer::builder()
27+
.rpc(rpc)
28+
.build()
29+
.deploy()?;
2730
println!("Deployed contract to {address}");
2831
let provider = devnode.create_provider().await?;
2932
let contract = IDecoder::IDecoderInstance::new(address, provider);

examples/abi_encode/tests/abi_encode_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ mod integration_test {
2525
let devnode = Node::new().await?;
2626
let rpc = devnode.rpc();
2727
println!("Deploying contract to Nitro ({rpc})...");
28-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
28+
let (address, _, _) = stylus_tools::Deployer::builder()
29+
.rpc(rpc)
30+
.build()
31+
.deploy()?;
2932
println!("Deployed contract to {address}");
3033
let provider = devnode.create_provider().await?;
3134
let contract = IEncoder::IEncoderInstance::new(address, provider);

examples/arrays/tests/arrays_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ mod integration_test {
2929
let devnode = Node::new().await?;
3030
let rpc = devnode.rpc();
3131
println!("Deploying contract to Nitro ({rpc})...");
32-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
32+
let (address, _, _) = stylus_tools::Deployer::builder()
33+
.rpc(rpc)
34+
.build()
35+
.deploy()?;
3336
println!("Deployed contract to {address}");
3437
let provider = devnode.create_provider().await?;
3538
let contract = IArrays::IArraysInstance::new(address, provider);

examples/call/tests/call_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ mod integration_test {
4141
let devnode = Node::new().await?;
4242
let rpc = devnode.rpc();
4343
println!("Deploying contract to Nitro ({rpc})...");
44-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
44+
let (address, _, _) = stylus_tools::Deployer::builder()
45+
.rpc(rpc)
46+
.build()
47+
.deploy()?;
4548
println!("Deployed contract to {address}");
4649
let provider = devnode.create_provider().await?;
4750
let contract = IExampleContract::IExampleContractInstance::new(address, provider);

examples/caller/tests/caller_callee_integration_test.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ mod integration_test {
4040
let provider = devnode.create_provider().await?;
4141

4242
println!("Deploying callee contract to Nitro ({rpc})...");
43-
let callee_address = stylus_tools::Deployer::new(rpc.to_owned())
44-
.with_contract_dir("../callee".into())
43+
let (callee_address, _, _) = stylus_tools::Deployer::builder()
44+
.rpc(rpc)
45+
.dir("../callee".to_owned())
46+
.build()
4547
.deploy()?;
4648
println!("Deployed callee contract to {callee_address}");
4749

4850
println!("Deploying caller contract to Nitro ({rpc})...");
49-
let caller_address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
51+
let (caller_address, _, _) = stylus_tools::Deployer::builder()
52+
.rpc(rpc)
53+
.build()
54+
.deploy()?;
5055
println!("Deployed caller contract to {caller_address}");
5156

5257
let caller = ICaller::ICallerInstance::new(caller_address, provider);

examples/constants/tests/constants_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ mod integration_test {
2020
let devnode = Node::new().await?;
2121
let rpc = devnode.rpc();
2222
println!("Deploying contract to Nitro ({rpc})...");
23-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
23+
let (address, _, _) = stylus_tools::Deployer::builder()
24+
.rpc(rpc)
25+
.build()
26+
.deploy()?;
2427
println!("Deployed contract to {address}");
2528
let provider = devnode.create_provider().await?;
2629
let contract = IContract::IContractInstance::new(address, provider);

examples/constructor/tests/constructor_integration_test.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,55 @@ mod integration_test {
2121
}
2222
}
2323

24+
const EXPECTED_ABI: &str = "\
25+
interface IContract {
26+
function setNumber(uint256 number) external;
27+
28+
function number() external view returns (uint256);
29+
30+
function owner() external view returns (address);
31+
32+
error Unauthorized();
33+
}";
34+
const EXPECTED_CONSTRUCTOR: &str = "constructor(uint256 initial_number) payable";
35+
2436
#[tokio::test]
2537
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+
2642
let devnode = Node::new().await?;
2743
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+
2858
println!("Deploying contract to Nitro ({rpc})...");
29-
let address = stylus_tools::Deployer::new(rpc.to_owned())
30-
.with_constructor_args(vec!["0xbeef".to_owned()])
31-
.with_constructor_value("12.34".to_owned())
32-
.deploy()?;
59+
let (address, _tx_hash, gas_used) = deployer.deploy()?;
3360
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+
3473
let provider = devnode.create_provider().await?;
3574

3675
// Check balance sent in constructor

examples/custom_storage_slots/tests/custom_storage_slots_integration_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ mod integration_test {
2020
let devnode = Node::new().await?;
2121
let rpc = devnode.rpc();
2222
println!("Deploying contract to Nitro ({rpc})...");
23-
let address = stylus_tools::Deployer::new(rpc.to_owned()).deploy()?;
23+
let (address, _, _) = stylus_tools::Deployer::builder()
24+
.rpc(rpc)
25+
.build()
26+
.deploy()?;
2427
println!("Deployed contract to {address}");
2528
let provider = devnode.create_provider().await?;
2629
let contract = IContract::IContractInstance::new(address, provider);

0 commit comments

Comments
 (0)