首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ethers.js中解码Uniswap通用路由器事务

在ethers.js中解码Uniswap通用路由器事务
EN

Ethereum用户
提问于 2023-01-18 22:53:40
回答 1查看 1.6K关注 0票数 4

Uniswap最近推出了一款新的通用路由器(博客此处:https://docs.uniswap.org/contracts/universal-router/technical-reference)

我使用Uniswap和以太将一个事务解码为如下所示:

代码语言:javascript
运行
复制
TransactionDescription {
  args: [
    '0x0b00',
    [
      '0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000009184e72a000',
      '0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000038e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000'
    ],
    BigNumber { _hex: '0x63c87f23', _isBigNumber: true },
    commands: '0x0b00',
    inputs: [
      '0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000009184e72a000',
      '0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000038e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000'
    ],
    deadline: BigNumber { _hex: '0x63c87f23', _isBigNumber: true }
  ],
  functionFragment: {
    type: 'function',
    name: 'execute',
    constant: false,
    inputs: [ [ParamType], [ParamType], [ParamType] ],
    outputs: [],
    payable: true,
    stateMutability: 'payable',
    gas: null,
    _isFragment: true,
    constructor: [Function: FunctionFragment] {
      from: [Function (anonymous)],
      fromObject: [Function (anonymous)],
      fromString: [Function (anonymous)],
      isFunctionFragment: [Function (anonymous)]
    },
    format: [Function (anonymous)]
  },
  name: 'execute',
  signature: 'execute(bytes,bytes[],uint256)',
  sighash: '0x3593564c',
  value: BigNumber {
    _hex: '0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000063c87f2300000000000000000000000000000000000000000000000000000000000000020b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000038e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000',
    _isBigNumber: true
  }
}

但是,我无法理解如何将输入解码为正确的值。

技术参考建议输入的字节数组应该包含以下内容:

代码语言:javascript
运行
复制
The second parameter for the function is an array of bytes strings. Each element in the array is the abi-encoded input that will be used for the respective command.


V3_SWAP_EXACT_IN
address The recipient of the output of the trade
uint256 The amount of input tokens for the trade
uint256 The minimum amount of output tokens the user wants
bytes The UniswapV3 encoded path to trade along
bool A flag for whether the input tokens should come from the msg.sender (through Permit2) or whether the funds are already in the UniversalRouter

但是,字节数组的长度仅为2:

代码语言:javascript
运行
复制
        inputs: [
      '0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000009184e72a000',
      '0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000038e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000'
    ],

我在这里误解了什么?我如何解码一个通用路由器事务?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2023-01-21 16:30:13

这种误解似乎来自于投入。

您可以看到,在您提到的对象中有以下几行:

代码语言:javascript
运行
复制
    commands: '0x0b00',
inputs: [
  '0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000009184e72a000',
  '0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000038e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000'
],

其中命令(Commands.sol)被解释为:

  • 0x:十六进制符号
  • 0b: WRAP_ETH (第一个函数)
  • 00: V3_SWAP_EXACT_IN (第二功能)

这就是为什么在输入数组中看到两个元素的原因。第一个元素是对应于第一个函数(WRAP_ETH)的输入,数组中的第二个元素是对应于V3_SWAP_EXACT_IN的输入。

例如,让我们看一下第一个函数,并对其进行解码。

代码语言:javascript
运行
复制
  //inputs[0] split every 64 characters
  0000000000000000000000000000000000000000000000000000000000000002 //addres of recipient
  000000000000000000000000000000000000000000000000000009184e72a000 //amount of ETH
  1. 收件人地址:Hexfor2 (2表示合同是收件人Constants.sol)
  2. 在小数中0x9184e72a000等于10000000000000 (10000000000000魏位= 0.00001以太)

使用同样的方法,您可以为V3_SWAP_EXACT_IN做同样的事情。

注意,如果您有ABI,可能会有一些工具来对这些信息进行默认解码,但这就是为什么您只看到两个输入。

票数 7
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/143239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档