1 个不稳定版本
0.1.0 | 2024 年 6 月 1 日 |
---|
#9 in #sol
403 每月下载量
用于 linera-alloy
695KB
13K SLoC
linera-alloy-contract
与链上合约交互。
主要类型是 CallBuilder
,它是一个用于构建对链上合约调用的构建器。它提供了一种对链上调用进行编码和解码数据的方法,并将这些调用发送到链上。请参阅其文档以获取更多信息。
用法
与 sol!
宏的 #[sol(rpc)]
属性结合使用,CallBuilder
可以用于与链上合约交互。该 #[sol(rpc)]
属性为合约中的每个函数生成一个返回 CallBuilder
的方法。请参阅其文档以获取更多信息。
# async fn test() -> Result<(), Box<dyn std::error::Error>> {
use linera_alloy_contract::SolCallBuilder;
use linera_alloy_network::Ethereum;
use linera_alloy_primitives::{Address, U256};
use linera_alloy_provider::ProviderBuilder;
use linera_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(())
# }
依赖项
~26–36MB
~725K SLoC