abi-decoder 解析以太坊交易 abi-decoder

ajing2022-07-31 17:53:12 • 2793 次点击    
这是一个分享于 2022-07-31 17:53:12 的项目,其中的信息可能已经有所发展或是发生改变。

ABI-DECODER

Go library for decoding data params and events from etherem transactions. Inspired by abi-decoder

Example

var myContractAbi = `
[
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "internalType": "string",
                "name": "name",
                "type": "string"
            },
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "age",
                "type": "uint256"
            },
            {
                "indexed": false,
                "internalType": "address",
                "name": "_addr",
                "type": "address"
            }
        ],
        "name": "StudentAdded",
        "type": "event"
    },
    {
        "inputs": [],
        "name": "addr",
        "outputs": [
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "age",
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "name",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "string",
                "name": "_name",
                "type": "string"
            },
            {
                "internalType": "uint256",
                "name": "_age",
                "type": "uint256"
            },
            {
                "internalType": "address",
                "name": "_addr",
                "type": "address"
            }
        ],
        "name": "save",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    }
]
`

func main() {
    txData := "0x19e7a9660000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000005a4728ca063b522c0b728f8000000000000000000000000000000000839c6f5a014cbfa319e8fdfa01aac186638945a80000000000000000000000000000000000000000000000000000000000000006e5b08fe6988e0000000000000000000000000000000000000000000000000000"

    txDataDecoder := decoder.NewABIDecoder()
    txDataDecoder.SetABI(myContractAbi)
    method, err := txDataDecoder.DecodeMethod(txData)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(method.Name)
    for _, param := range method.Params {
        fmt.Println(param)
        fmt.Println(reflect.TypeOf(param.Value))
    }

    client, err := ethclient.Dial("rinkeby-rpc-url")
    if err != nil {
        log.Fatal(err)
    }

    txHash := common.HexToHash("0x38687ffd5526c125c0c4074e9c39855fad31cbcd1c77b52650bebfa11b365bc0")
    r, err := client.TransactionReceipt(context.Background(), txHash)
    if err != nil {
        log.Fatal(err)
    }
    decodedLogs, err := txDataDecoder.DecodeLogs(r.Logs)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(decodedLogs)
}

Contribution

  • Fork this repository
  • Clone your repository
  • Install dependencies
  • Checkout a feature branch
  • Feel free to add your features
  • Make sure your features are fully tested
  • Open a pull request, and enjoy (:

LICENSE

Package abi-decoder is licensed under the MIT License.

Latest commit to the main branch on 9-27-2024
Download as zip
授权协议:
开发语言:
Golang 查看源码»
2793 次点击  
加入收藏 微博
2 回复  |  直到
ajing
ajing · #1 · 3年之前

有啥功能可建议欢迎提出

ajing
ajing · #2 · 3年之前

也可以提交PR哈

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传