一旦您部署了您的合约,就像在前面的部分中看到的那样,您可能希望:
下面是一个示例。假设您的 Sway 合约有两个名为 initialize_counter(u64)
和 increment_counter(u64)
的 ABI 方法。一旦您部署了该合约,您可以像这样调用这些方法:
// This will generate your contract's methods onto `MyContract`.
// This means an instance of `MyContract` will have access to all
// your contract's methods that are running on-chain!
abigen!(Contract(
name = "MyContract",
abi = "e2e/sway/contracts/contract_test/out/release/contract_test-abi.json"
));
// This is an instance of your contract which you can use to make calls to your functions
let contract_instance = MyContract::new(contract_id_2, wallet);
let response = contract_instance
.methods()
.initialize_counter(42) // Build the ABI call
.call() // Perform the network call
.await?;
assert_eq!(42, response.value);
let response = contract_instance
.methods()
.increment_counter(10)
.call()
.await?;
assert_eq!(52, response.value);
上面的示例使用了所有默认配置,并执行了一个简单的合约调用。
此外,如果出于任何原因您需要将提交与值检索分开,您可以像下面这样操作:
let response = contract_instance
.methods()
.initialize_counter(42)
.submit()
.await?;
let value = response.response().await?.value;
接下来,我们将看到如何进一步配置合约调用中的许多不同参数。