abidec

package module
v0.0.0-...-80207f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 7 Imported by: 0

README

abidec

ABI Decoder From Solidity To Golang

Problem

Imagine you have a Solidity function and struct like this:

struct Task {
	string title;
	string description;
	address reporter;
	address assignee;
	uint256[2] deadline;
}

function encode(string calldata title, string calldata description, address reporter, address assignee, uint256 startDate, uint256 endDate) external pure returns(bytes memory) {
    uint256[2] memory deadline = [startDate, endDate];
   
    Task memory task = Task(title, description, reporter, assignee, deadline);
   
    return abi.encode(task);
}

And you need to decode ABI encoded bytes to a struct, like that:

function decode(bytes calldata params) external pure returns (Task memory) {
    (Task memory task) = abi.decode(params, (Task));
    return task;
}

But in Golang.

Usage

Just paste your struct definition and use it in abidec.NewAbiDecoder options.

decoder := abidec.NewAbiDecoder(
    abidec.WithStruct(`
		struct Task {
			string title;
			string description;
			address reporter;
			address assignee
			uint256[2] deadline;
		}
    `),
)

And then you can decode ABI encoded bytes to values:

// params = 
// [1]  0000000000000000000000000000000000000000000000000000000000000020
// [2]  00000000000000000000000000000000000000000000000000000000000000c0
// [3]  0000000000000000000000000000000000000000000000000000000000000010
// [4]  000000000000000000000000acdad15d8f07d8df258fe11332b752785d6b1d22
// [5]  0000000000000000000000008b1383709d1e80a291de5d67993252dfc52c3700
// [6]  00000000000000000000000000000000000000000000000000000000672f269b
// [7]  00000000000000000000000000000000000000000000000000000000672f34ab
// [8]  0000000000000000000000000000000000000000000000000000000000000006
// [9]  526f636b657400000000000000000000000000000000000000000000000000000
// [10] 0000000000000000000000000000000000000000000000000000000000019
// [11] 43726561746520526f636b657420546f20546865204d6f6f6e00000000000000
paramsBytes := hex.DecodeString(params)

type Task struct {
	Title       string
	Description string
	Reporter    [20]byte // or common.Address from go-ethereum
	Assignee    [20]byte
	Deadline    [2]*big.Int	
}

var task Task
decoder.DecodeStruct(paramsBytes, &task)

Pretty print the values in JSON format

{
    "assignee": "0x8B1383709D1e80A291DE5d67993252dFC52C3700",
    "deadline": [
        1731143323,
        1731146923
    ],
    "description": "Create Rocket To The Moon",
    "reporter": "0xaCDaD15d8F07D8Df258fe11332b752785d6b1d22",
    "title": "Rocket"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateGetterMethodForStruct

func CreateGetterMethodForStruct(s StructDef) (string, error)

func ParseEventSignature

func ParseEventSignature(sig string) (abi.Event, error)

func ParseLogIntoMap

func ParseLogIntoMap(event abi.Event, out map[string]any, log *types.Log) error

Types

type AbiDecoder

type AbiDecoder struct {
	// contains filtered or unexported fields
}

func NewAbiDecoder

func NewAbiDecoder(opts ...DecoderOpts) (*AbiDecoder, error)

func (*AbiDecoder) Decode

func (d *AbiDecoder) Decode(data []byte) (map[string]any, error)

func (*AbiDecoder) DecodeStruct

func (d *AbiDecoder) DecodeStruct(data []byte, out any) error

type DecoderOpts

type DecoderOpts func(*AbiDecoder) error

func WithStruct

func WithStruct(structDef string) DecoderOpts

type StructDef

type StructDef struct {
	Name          string
	Fields        []StructField
	InitialStrDef string
}

func StructDefFromString

func StructDefFromString(structDef string) (StructDef, error)

func (StructDef) Getter

func (s StructDef) Getter() string

type StructField

type StructField struct {
	Name string
	Type string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL