0

I have a simple abi.encoded string data that has a series of raw types. In solidity I would decode data by using the abi.decode function.

(uint256 total, uint256 id) = abi.decode(data, (uint256, uint256))

Is there a way to do the same in golang. I can do it if I have a contract abi but don't understand how to do it with the primitive types.

I was looking at the code for abi.NewType but I think that may be a redherring

The same functionality in ethers is https://docs.ethers.io/v5/api/utils/abi/coder/

1 Answer 1

1

You can use go-web3, check the example in the github repository for abi, it is easy to understand https://github.com/umbracle/go-web3#abi.

To modify for your example of 2 uint256:

typ := abi.MustNewType("tuple(uint256 a, uint256 b)")

type Obj struct {
    A *big.Int
    B *big.Int
}


obj := &Obj{
    A: big.NewInt(2),
    B: big.NewInt(1),
}

// Encode
encoded, err := typ.Encode(obj)
if err != nil {
    panic(err)
}

// Decode output into a map
res, err := typ.Decode(encoded)
if err != nil {
    panic(err)
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.