Example: deploy_from_artifact
Example
To run this example:
- Clone the examples repository:
git clone git@github.com:alloy-rs/examples.git
- Run:
cargo run --example deploy_from_artifact
//! Example of deploying a contract from an artifact using the `sol!` macro to Anvil and interacting
//! with it.
use alloy::{primitives::U256, providers::ProviderBuilder, sol};
use eyre::Result;
// Codegen from artifact.
sol!(
#[allow(missing_docs)]
#[sol(rpc)]
Counter,
"examples/artifacts/Counter.json"
);
#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();
// Deploy the `Counter` contract.
let contract = Counter::deploy(&provider).await?;
println!("Deployed contract at address: {}", contract.address());
// Set the number to 42.
let builder = contract.setNumber(U256::from(42));
let tx_hash = builder.send().await?.watch().await?;
println!("Set number to 42: {tx_hash}");
// Increment the number to 43.
let builder = contract.increment();
let tx_hash = builder.send().await?.watch().await?;
println!("Incremented number: {tx_hash}");
// Retrieve the number, which should be 43.
let builder = contract.number();
// Note: because the artifact generated by `solc` does not include named return values it is
// not possible to derive the return value name `number` from the artifact. This means that the
// return value must be accessed by index - as if it is an unnamed value.
// If you prefer to use named return values, it is recommended to embed the Solidity code
// directly in the `sol!` macro as shown in `deploy_from_contract.rs`.
let number = builder.call().await?._0;
println!("Retrieved number: {number}");
Ok(())
}
Find the source code on Github here.