umbracle / ethgo

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

abi struct tags only work in lower case #223

Closed cyberhorsey closed 2 years ago

cyberhorsey commented 2 years ago
package main

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

var (
    t = abi.MustNewType("tuple(tuple(bytes32 camelCase) header)")
)

type Header struct {
    Slice []byte `abi:"camelCase"`
}

type Proof struct {
    Header Header
}

func main() {

    b := Header{
        Slice: []byte{01},
    }

    s := Proof{
        Header: b,
    }
    _, err := t.Encode(s)
    if err != nil {
        panic(err)
    }
}

gives error cannot get key camelCase, yet removing the capital C works.

cyberhorsey commented 2 years ago

additionally, is there any way to do a bytes, bytes type? ie: i have (bytes account, bytes storage = abi.decode(b) where b is a bytes[] in my Solidity contract.

In Ethers, I would do: ethers.defaultAbiCoder.Encode(["bytes", "bytes"], [account, storage]

ferranbt commented 2 years ago

gives error cannot get key camelCase, yet removing the capital C works.

Merging your PR, thank you for the contribution!

additionally, is there any way to do a bytes, bytes type?

Yes, it should be possible doing something like:

t := abi.MustNewType("tuple(bytes, bytes)")
t.Decode([]byte{...})