如何使用Ethers.rs调用智能合约的函数?

huangapple go评论69阅读模式
英文:

How to call a function of a smart contract with Ethers.rs?

问题

我正在使用ethers.rs,想要调用部署的智能合约的一个函数。我不想使用智能合约的 ABI。

根据我迄今为止的研究,我找到了一种使用函数选择器的方法。这必须与函数参数一起编码到交易的data字段中。

如果我只知道合约地址和要调用的函数,我如何在 ethers.rs 中实现这一点?

英文:

I'm using ethers.rs & want to call a function of a deployed smart contract. I don't to use the ABI of smart contract for this.

Based on my research so far I've found a way of using the function selector. This has to be encoded along with the function arguments in the data field of the transaction.

How can I do so using ethers.rs if I just know the contract address & the function that I want to call?

答案1

得分: 0

首先,您需要使用abigen解析您的合约abi:

abigen!(ERC20Token, "./erc20.json",);

更多信息请参阅此处

接下来,您创建您的合约对象:

let contract = ERC20Token::new(address, client);

最后,您调用它:

contract.total_supply().call().await

contract.transfer(to, amount).call().await

您可以在这里查看完整示例。

英文:

First you need to parse your contract abi with abigen:

abigen!(ERC20Token, "./erc20.json",);

more information here

next you create your contract object:

let contract = ERC20Token::new(address, client);

and finally you call it:

contract.total_supply().call().await

contract.transfer(to, amount).call().await

you can check the full example here

huangapple
  • 本文由 发表于 2023年1月9日 11:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定