umbracle / ethgo

Ethereum Golang API
https://www.ethgoproject.io
Mozilla Public License 2.0
484 stars 133 forks source link

decoding struct fails #266

Open rpuntaie opened 3 weeks ago

rpuntaie commented 3 weeks ago

This js works

#!/usr/bin/env node
const {AbiCoder} = require("ethers");
dataHex = "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000a16e02e87b7454126e5e10d957a927a7f5b5d2be0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000003e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137375626d69744b657967656e286279746573290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004104792730167230add71afb0459dd093980a5dbef6b8cfd2c9eef5f403d8b87a7a03da89bde572e8f564a39ad05452f854fe45328fa8ee7148fb8ee3131b78e6226000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041043770e37d91bbbb001e8c60de87d4fafd44626c8b85e08fbadf8f45778841a0462b0b88cea6cbb10ca931b0cb70d9d2aca23635100e0365bf1e6b07f929b45b3200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004104e397c219c024160ce8c5e35a23dd51ab6b9296cad9f3d6c03f7dbe6b294c4d61c529fd79bd30d1f2dda9a9f70d6f316de01ed9d100e0496cc30a4454215cb7260000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000410463b437e92335bf367ab5b3b5bda4ff218cf5e2ac6555b47c187e20ac274476fcf30d1b56ce6fc861c23b8ab147f00df140c53291257ecb58e89e4815803f0f470000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
var data = Buffer.from(dataHex,'hex');
const jobInfoABI = [
  "string functionSignature",
  "address executionContract",
  "uint256 minResultsNeeded",
  "bytes[] data",
  "bytes[] results",
  "address[] submitterOfResult",
  "int256 score",
];
const abiCoder = new AbiCoder();
const decodedData = abiCoder.decode([`tuple(${jobInfoABI.join(",")})`], data);
decodedData[0].data[0]
console.log(decodedData[0].data[0] == '0x04792730167230add71afb0459dd093980a5dbef6b8cfd2c9eef5f403d8b87a7a03da89bde572e8f564a39ad05452f854fe45328fa8ee7148fb8ee3131b78e6226')

So I would expect that the data can also be decoded with ethgo, but it cannot. Here is the go code.

/*
mkdir works.not
cd works.not
# place this code in works.not.go
go mod tidy
go run works.not.go
*/
package main

import (
    "encoding/hex"
    "fmt"
    "log"
    "math/big"

    "github.com/umbracle/ethgo"
    "github.com/umbracle/ethgo/abi"
)

var (
    jobInfo = "tuple(string functionSignature, address executionContract, uint256 minResultsNeeded, bytes[] data, bytes[] results, address[] submitterOfResult, int256 score)"
)

type JobInfo struct {
    functionSignature string
    executionContract ethgo.Address
    minResultsNeeded  *big.Int
    data              [][]byte
    results           [][]byte
    submitterOfResult []ethgo.Address
    score             *big.Int
}

func main() {
    typ := abi.MustNewType(jobInfo)
    dataHex := "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000a16e02e87b7454126e5e10d957a927a7f5b5d2be0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000003e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137375626d69744b657967656e286279746573290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004104792730167230add71afb0459dd093980a5dbef6b8cfd2c9eef5f403d8b87a7a03da89bde572e8f564a39ad05452f854fe45328fa8ee7148fb8ee3131b78e6226000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041043770e37d91bbbb001e8c60de87d4fafd44626c8b85e08fbadf8f45778841a0462b0b88cea6cbb10ca931b0cb70d9d2aca23635100e0365bf1e6b07f929b45b3200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004104e397c219c024160ce8c5e35a23dd51ab6b9296cad9f3d6c03f7dbe6b294c4d61c529fd79bd30d1f2dda9a9f70d6f316de01ed9d100e0496cc30a4454215cb7260000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000410463b437e92335bf367ab5b3b5bda4ff218cf5e2ac6555b47c187e20ac274476fcf30d1b56ce6fc861c23b8ab147f00df140c53291257ecb58e89e4815803f0f470000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
    data, err := hex.DecodeString(dataHex)
    if err != nil {
        log.Fatal(err)
    }
    res, err := typ.Decode(data)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%v", res)
}

The error on go run works.not.go is:

2024/10/25 22:41:04 length insufficient 1052 require 137438953472
exit status 1
ferranbt commented 3 weeks ago

Can you try with score as big.Int too?

rpuntaie commented 3 weeks ago

I tried. Still not working. (I updated the code above: score was Int instead of *big.Int, which did not make it equivalent to the js example)