Ethers Pass方法名称带有参数而不是Hexdata
我正在使用ethers
与以太坊上的合同进行交互。 这就是我的方法的样子:
const tx = await signer
.sendTransaction({
to: contractAddress,
value: ethers.utils.parseEther(price),
data: hex,
gasPrice: ethers.utils.parseUnits(gas, 9),
})
它可以正常工作。但是,每当我必须查看etherscan以查找事务十六进制并将其传递到data
参数。是否可以将方法名称从合同和参数传递给它,而不是传递此HEX
值?
也许ethers
中有任何方法可以用参数编码该方法中的十六进制值?
I am using ethers
to interact with contracts on Ethereum.
This is how my method looks like:
const tx = await signer
.sendTransaction({
to: contractAddress,
value: ethers.utils.parseEther(price),
data: hex,
gasPrice: ethers.utils.parseUnits(gas, 9),
})
And it works fine. But every time I have to look on Etherscan to find transaction hex and pass it to the data
parameter. Is it possible to just pass method name from contract and parameters to it instead of passing this hex
value?
Maybe there is any method in ethers
to encode that method with parameters into hex value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是合同ABI。通常,它以JSON对象的形式写成您可以在JS脚本中导入的对象,也可以像这样写入(人类可读性):
ABI包含合同的所有(或某些)方法以及如何对其进行编码/解码。
使用ABI,您可以创建这样的合同对象:
可以用来发送这样的交易:
What you need is the contract ABI. Generally it's written as a json object you can import in your js script, or it can be written like this (human-readable-ABI):
The ABI contains all (or some) methods of a contract and how to encode/decode them.
With the ABI you can create a contract object like this:
which can be used to send transactions like this: