Crates.io | alloy-contract |
lib.rs | alloy-contract |
version | |
source | src |
created_at | 2024-01-29 12:34:27.18589 |
updated_at | 2024-12-10 11:22:00.326721 |
description | Interact with on-chain contracts |
homepage | https://github.com/alloy-rs/alloy |
repository | https://github.com/alloy-rs/alloy |
max_upload_size | |
id | 1118828 |
Cargo.toml error: | TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Interact with on-chain contracts.
The main type is CallBuilder
, which is a builder for constructing calls to on-chain contracts.
It provides a way to encode and decode data for on-chain calls, and to send those calls to the chain.
See its documentation for more details.
Combined with the sol!
macro's #[sol(rpc)]
attribute, CallBuilder
can be used to interact with
on-chain contracts. The #[sol(rpc)]
attribute generates a method for each function in a contract
that returns a CallBuilder
for that function. See its documentation for more details.
# async fn test() -> Result<(), Box<dyn std::error::Error>> {
use alloy_contract::SolCallBuilder;
use alloy_network::Ethereum;
use alloy_primitives::{Address, U256};
use alloy_provider::ProviderBuilder;
use alloy_sol_types::sol;
sol! {
#[sol(rpc)] // <-- Important! Generates the necessary `MyContract` struct and function methods.
#[sol(bytecode = "0x1234")] // <-- Generates the `BYTECODE` static and the `deploy` method.
contract MyContract {
constructor(address) {} // The `deploy` method will also include any constructor arguments.
#[derive(Debug)]
function doStuff(uint a, bool b) public payable returns(address c, bytes32 d);
}
}
// Build a provider.
let provider = ProviderBuilder::new().with_recommended_fillers().on_builtin("http://localhost:8545").await?;
// If `#[sol(bytecode = "0x...")]` is provided, the contract can be deployed with `MyContract::deploy`,
// and a new instance will be created.
let constructor_arg = Address::ZERO;
let contract = MyContract::deploy(&provider, constructor_arg).await?;
// Otherwise, or if already deployed, a new contract instance can be created with `MyContract::new`.
let address = Address::ZERO;
let contract = MyContract::new(address, &provider);
// Build a call to the `doStuff` function and configure it.
let a = U256::from(123);
let b = true;
let call_builder = contract.doStuff(a, b).value(U256::from(50e18 as u64));
// Send the call. Note that this is not broadcasted as a transaction.
let call_return = call_builder.call().await?;
println!("{call_return:?}"); // doStuffReturn { c: 0x..., d: 0x... }
// Use `send` to broadcast the call as a transaction.
let _pending_tx = call_builder.send().await?;
# Ok(())
# }