douren5898 2018-06-05 20:15 采纳率: 100%
浏览 227
已采纳

Golang Web3松露

I am using this Ethereum Go Client and trying to call and get the response of a Smart Contract function.

The function in the smart contract is very simple (for testing now):

  function getVotesForImgIds() external view returns(uint32){
    return 12345;
  }

I am using truffle to deploy the contract:

truffle compile
truffle migrate

My Go server is very basic too, here is the important part in the main func:

abi := getVotesContractJson()["abi"] //works fine

jsonAbi, err := json.Marshal(abi)
if err != nil {
    log.Fatal(err)
}

var connection = web3.NewWeb3(providers.NewHTTPProvider("127.0.0.1:8545", 10, false))
contract, err := connection.Eth.NewContract(string(jsonAbi))
if err != nil {
    log.Fatal(err)
}
//contract works

transaction := new(dto.TransactionParameters)
transaction.Gas = big.NewInt(4000000)

result, err := contract.Call(transaction, "getVotesForImgIds")
if result != nil && err == nil {
    fmt.Println("result: ", result)
    // -------------------->
    //this will print: result:  &{87 2.0 0x0 <nil> }
} else {
    log.Fatal("call error:", err)
}

Why does this result in &{87 2.0 0x0 <nil> } ? How can I get the real value returned by the smart contract? I tried all the result.ToInt() etc. already...

  • 写回答

2条回答 默认 最新

  • douhu3424 2018-06-06 05:47
    关注

    You are not setting the contract address in your go file: https://github.com/regcostajr/go-web3/blob/master/test/eth/eth-contract_test.go#L75

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图