Go语言 将参数作为bytes32传递给Solidity智能合约

rt4zxlrg  于 2023-04-27  发布在  Go
关注(0)|答案(3)|浏览(131)

我正在使用this Ethereum Go Client并试图将字符串/bytes 32传递给Solidity。
智能合约中的功能非常简单(现在测试):

function vote(bytes32 id) {
    //id has the value 0x0000000000000000000000000000000000000000000000000000000000000000
  }

唿叫

hash, err := contract.Send(transaction, "vote", "myString")

将导致0x0000000000000000000000000000000000000000000000000000000000000000
对于bytes 32参数id...
我如何从Go向我的智能合约传递参数,以便solidity具有正确的值?
或者,我只需要为该字符串传递一个唯一的标识符,我可以很容易地在Golang中从该字符串创建...

osh3o9ms

osh3o9ms1#

我想你得把它编码

types.ComplexString("myString")
mnemlml8

mnemlml82#

要将一个字符串转换为bytes32,你所要做的就是在Go中创建一个固定长度的字节数组,并将字符串的值复制到其中。

value := [32]byte{}
copy(key[:], []byte("hello"))

然后,您可以将值传递给智能合约函数:

hash, err := contract.Send(transaction, "vote", value)
mspsb9vt

mspsb9vt3#

包的创建者告诉我,造成这种情况的原因是这个问题:
https://web.archive.org/web/20201226194418/https://github.com/regcostajr/go-web3/issues/31

**regcostajr**于2018年6月6日发表评论:

Hi**@ jeffpretes**,感谢您的问题,合约结构中的参数编码似乎完全错误,我们需要根据solidity文档重新实现:http://solidity.readthedocs.io/en/develop/abi-spec.html#use-of-dynamic-types
他正试图解决它。

相关问题