资讯详情

以太坊EVM智能合约交易信息中Input和Logs解码

一、以太坊交易获取

通常以两种方式获得以太坊交易:getTransaction、getTransactionReceipt web3.eth.getTransaction:

{ 
           blockHash: '0x61b488038c4e08298b4970a50a07cd795bad6e84bd82f280fb1227b0ab4eb19c',   blockNumber: 17835097,   from: '0xF73d8f5BFb7f03b0AF375b1b5cF6581C367890e8',   gas: 36922,   gasPrice: '10000000000',   hash: '0xb2e675f13b895e159d8a14ad39dae9ff90d043e459a1341481e196b1bcea98b6',   input: '0x40c10f190000000000000000000000009c97efb29a3d453af239d133e867bf4c647a313c00000000000000000000000000000000000004ee2d6d415b85acef8100000000',   nonce: 405,   to: '0x20948400E68434010cFcfC978b66CFa656629D9b',   transactionIndex: 7,   value: '0',   type: 0,   v: '0xe6',   r: '0x546e3662631e683574d098506604dda2ae13d1f10da6a25d60e462acb29f28b9',   s: '0x548db669b21310288ace2e5a7557df78c3de83a037092de2328d65cad8ed8188' } 

web3.eth.getTransactionReceipt:

{ 
           blockHash: '0x61b488038c4e08298b4970a50a07cd795bad6e84bd82f280fb1227b0ab4eb19c',   blockNumber: 17835097,   contractAddress: null,   cumulativeGasUsed: 753442,   from: '0xf73d8f5bfb7f03b0af375b1b5cf6581c367890e8',   gasUsed: 36922,   logs: [     { 
               address: '0x20948400E68434010cFcfC978b66CFa656629D9b',       topics: [Array],       data: '0x00000000000000000000000000000000000004ee2d6d415b85acef8100000000',       blockNumber: 17835097,       transactionHash: '0xb2e675f13b895e159d8a14ad39dae9ff90d043e459a134148e196b1bcea98b6',
      transactionIndex: 7,
      blockHash: '0x61b488038c4e08298b4970a50a07cd795bad6e84bd82f280fb1227b0ab4eb19c',
      logIndex: 11,
      removed: false,
      id: 'log_b7283928'
    }
  ],
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000408000000000000000000000000000000000000000000000000020000000000000000000800004000000000000000000010000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000002000000000000000800000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000',
  status: true,
  to: '0x20948400e68434010cfcfc978b66cfa656629d9b',
  transactionHash: '0xb2e675f13b895e159d8a14ad39dae9ff90d043e459a1341481e196b1bcea98b6',
  transactionIndex: 7,
  type: '0x0'
}

二、使用abi-parser解析input和logs

abi-parser 是一个用于解析ethereum/web3 日志、事件和方法的 NodeJS/JS 库。

安装

npm i abi-parser

实例

const AbiParser = require('abi-parser');
const abiParser = new AbiParser();

// Add the abis
const testABI1 = [
    { 
        
        "inputs":[
            { 
        
                "type":"address",
                "name":""
            }
        ],
        "constant":true,
        "name":"isInstantiation",
        "payable":false,
        "outputs":[
            { 
        
                "type":"bool",
                "name":""
            }
        ],
        "type":"function"
    },
    { 
        
        "inputs":[
            { 
        
                "type":"address[]",
                "name":"_owners"
            },
            { 
        
                "type":"uint256",
                "name":"_required"
            },
            { 
        
                "type":"uint256",
                "name":"_dailyLimit"
            }
        ],
        "constant":false,
        "name":"create",
        "payable":false,
        "outputs":[
            { 
        
                "type":"address",
                "name":"wallet"
            }
        ],
        "type":"function"
    },
    { 
        
        "inputs":[
            { 
        
                "type":"address",
                "name":""
            },
            { 
        
                "type":"uint256",
                "name":""
            }
        ],
        "constant":true,
        "name":"instantiations",
        "payable":false,
        "outputs":[
            { 
        
                "type":"address",
                "name":""
            }
        ],
        "type":"function"
    },
    { 
        
        "inputs":[
            { 
        
                "type":"address",
                "name":"creator"
            }
        ],
        "constant":true,
        "name":"getInstantiationCount",
        "payable":false,
        "outputs":[
            { 
        
                "type":"uint256",
                "name":""
            }
        ],
        "type":"function"
    },
    { 
        
        "inputs":[
            { 
        
                "indexed":false,
                "type":"address",
                "name":"sender"
            },
            { 
        
                "indexed":false,
                "type":"address",
                "name":"instantiation"
            }
        ],
        "type":"event",
        "name":"ContractInstantiation",
        "anonymous":false
    }
]
const testABI2 = [
    { 
        
        "anonymous":false,
        "inputs":[
            { 
        
                "indexed":true,
                "internalType":"address",
                "name":"from",
                "type":"address"
            },
            { 
        
                "indexed":true,
                "internalType":"address",
                "name":"to",
                "type":"address"
            },
            { 
        
                "indexed":false,
                "internalType":"uint256",
                "name":"value",
                "type":"uint256"
            }
        ],
        "name":"Transfer",
        "type":"event"
    }
];

abiParser.addABI(testABI1);
abiParser.addABI(testABI2);



// Decode an encoded method
const testMethod = "0x53d9d91000000000000000000000000000000000000000000000000000000000000000
6000000000000000000000000000000000000000000000000000000000000000010000000
0000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000002000000000000000000000000a
6d9c5f7d4de3cef51ad3b7235d79ccc95114de5000000000000000000000000a6d9c5f7d4
de3cef51ad3b7235d79ccc95114daa";
const decodedMethod = abiParser.decodeMethod(testMethod, true);
console.log('Decoded Method:', decodedMethod);




//Decode an encoded log
const testLog = { 
        
    "address":"0x8076C74C5e3F5852037F31Ff0093Eeb8c8ADd8D3",
    "topics":[
        "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "0x000000000000000000000000ff3dd404afba451328de089424c74685bf0a43c9",
        "0x0000000000000000000000008591aa322b6ce54a781b025a594fcf5b0cc20d22"
    ],
    "data":"0x000000000000000000000000000000000000000000000000005af314c8ef9900",
    "blockNumber":11951393,
    "transactionHash":"0x7a125d37e18a3f37924970a08f0449a82be1cf15fabdf7631dbdc221330fba41",
    "transactionIndex":67,
    "blockHash":"0x5a0837ffcd732c23974878d70e4e3742299b34e05f1e1e4c14cf12fd5b4431ef",
    "logIndex":155,
    "removed":false,
    "id":"log_bd721dfd"
};


const decodedLog = abiParser.decodeLog(testLog, true);
console.log('Decoded Log:', decodedLog);

标签: evm2gsx80b24电阻

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台