Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Created February 14, 2022 16:22
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fxfactorial/475bf4a50d0f9f6653b4dc4b610d86cd to your computer and use it in GitHub Desktop.
Save fxfactorial/475bf4a50d0f9f6653b4dc4b610d86cd to your computer and use it in GitHub Desktop.
ABI encode struct in golang
package main
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
var (
structThing, _ = abi.NewType("tuple", "struct thing", []abi.ArgumentMarshaling{
{Name: "field_one", Type: "uint256"},
{Name: "field_two", Type: "address"},
})
args = abi.Arguments{
{Type: structThing, Name: "param_one"},
}
)
func main() {
record := struct {
FieldOne *big.Int
FieldTwo common.Address
}{
big.NewInt(2e18),
common.HexToAddress("0x0002"),
}
packed, err := args.Pack(&record)
if err != nil {
fmt.Println("bad bad ", err)
return
} else {
fmt.Println("abi encoded", hexutil.Encode(packed))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment