【问题标题】:Pass parameter as bytes32 to Solidity Smart Contract将参数作为 bytes32 传递给 Solidity 智能合约
【发布时间】:2018-11-16 15:19:28
【问题描述】:

我正在使用 this Ethereum Go Client 并尝试将字符串 / bytes32 传递给 Solidity。

智能合约中的功能很简单(现在测试):

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

打电话

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

将导致0x0000000000000000000000000000000000000000000000000000000000000000

对于 bytes32 参数 id...

我必须如何将参数从 Go 传递给我的智能合约,以便solidity 具有正确的值?

或者,我只需要为该字符串传递一个唯一标识符,我可以在 Golang 中轻松地从字符串中创建该标识符...

【问题讨论】:

    标签: go ethereum solidity web3 go-ethereum


    【解决方案1】:

    我认为你必须对其进行编码

    types.ComplexString("myString")
    

    【讨论】:

      【解决方案2】:

      要将字符串转换为 bytes32 以保证可靠性,您只需在 Go 中创建一个固定长度的字节数组并将字符串的值复制到其中。

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

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

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

      【讨论】:

        【解决方案3】:

        包的创建者告诉我这是这个问题的原因:https://github.com/regcostajr/go-web3/issues/31

        他正在努力解决它。

        【讨论】:

          猜你喜欢
          • 2022-08-20
          • 2018-10-21
          • 2018-09-16
          • 2022-08-05
          • 1970-01-01
          • 2021-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多