Evm解析交易中合约数据

这篇文章主要介绍了Evm解析交易中合约数据 ,文中通过代码以及文档配合进行讲解,很详细,它对在座的每个人的研究和工作具有很经典的参考价值。 如果需要,让我们与区块链资料网一起学习。

https://www.interchains.cc/16883.html

Evm解析交易中合约数据是很好的区块链资料,他说明了区块链当中的经典原理,可以给我们提供资料,Evm解析交易中合约数据学习起来其实是很简单的,

不多的几个较为抽象的概念也很容易理解,之所以很多人感觉Evm解析交易中合约数据比较复杂,一方面是因为大多数的文档没有做到由浅入深地讲解,概念上没有注意先后顺序,给读者的理解带来困难

Evm解析交易中合约数据

  • 合约

以太坊eth交易Input交易解析

以下是在以太坊eth部署Uniswap调用合约的TxHash。 https://cn.etherscan.com/tx/0x06e68f7061f4d7da8117dc0ed97cd9888305590002d05811baa6594b10eb9cd1/

此交易调用合约方法swapETHForExactTokens,通过解析Input可以拿到合约输入参数。

有如下要点

  • 加载合约ABI
  • 通过Input获取合约方法
  • Unpack解包输入参数

测试用例代码

func TestParseDepositInput(t *testing.T) {     input := "fb3bdb41000000000000000000000000000000000000000000108b2a2c280290940000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000b634058bd3ac146b128824e4b9ab59561b0568b9000000000000000000000000000000000000000000000000000000005f3f6a000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000ed1199093b1abd07a368dd1c0cdc77d8517ba2a0"     inputData, _ := hex.DecodeString(input)     abiStaking, _ := abi.JSON(strings.NewReader(Json))     methodName, err := abiStaking.MethodById(inputData)     data := inputData[4:]     args := struct {         AmountOut *big.Int         Path    []common.Address         To    common.Address         Deadline  *big.Int     }{}     method, _ := abiStaking.Methods[methodName.Name]      err = method.Inputs.Unpack(&args, data)     if err != nil {         log.Error("Unpack deposit pubkey error", "err", err)     }     fmt.Println("AmountOut ", hex.EncodeToString(args.AmountOut.Bytes()), " Deadline ", hex.EncodeToString(args.Deadline.Bytes())," ",len(args.Path)," ",args.To.String()) } 

以下是Uniswap的factory abi json

const Json = ` [     {         "inputs": [             {                 "internalType": "address",                 "name": "_factory",                 "type": "address"             },             {                 "internalType": "address",                 "name": "_WETH",                 "type": "address"             }         ],         "stateMutability": "nonpayable",         "type": "constructor"     },     {         "inputs": [],         "name": "WETH",         "outputs": [             {                 "internalType": "address",                 "name": "",                 "type": "address"             }         ],         "stateMutability": "view",         "type": "function"     },     {         "inputs": [             {                 "internalType": "address",                 "name": "token",                 "type": "address"             },             {                 "internalType": "uint256",                 "name": "amountTokenDesired",                 "type": "uint256"             },             {                 "internalType": "uint256",                 "name": "amountTokenMin",                 "type": "uint256"             },             {                 "internalType": "uint256",                 "name": "amountETHMin",                 "type": "uint256"             },             {                 "internalType": "address",                 "name": "to",                 "type": "address"             },             {                 "internalType": "uint256",                 "name": "deadline",                 "type": "uint256"             }         ],         "name": "addLiquidityETH",         "outputs": [             {                 "internalType": "uint256",                 "name": "amountToken",                 "type": "uint256"             },             {                 "internalType": "uint256",                 "name": "amountETH",                 "type": "uint256"             },             {                 "internalType": "uint256",                 "name": "liquidity",                 "type": "uint256"             }         ],         "stateMutability": "payable",         "type": "function"     },     {         "inputs": [],         "name": "factory",         "outputs": [             {                 "internalType": "address",                 "name": "",                 "type": "address"             }         ],         "stateMutability": "view",         "type": "function"     }, ] `

部分转自网络,侵权联系删除www.interchains.cchttps://www.interchains.cc/16883.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 !
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » Evm解析交易中合约数据

提供最优质的资源集合

立即查看 了解详情