我正在尝试使用 Ethers.js 文档中的说明对智能合约测试中的交易数据进行解码,但我一直认为第一个参数(片段)无效:
Ethers.js
interface.decodeFunctionData( fragment , data ) ⇒ Result
Returns the decoded values from transaction data for fragment (see Specifying Fragments) for the given data.
ABI
:
const abi = require('../artifacts/contracts/CoinX.sol/CoinX.json').abi;
Interface
:
let ICoinX = new ethers.utils.Interface(abi);
AddLiquidityETH function on UniswapV2Router02.sol
:
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
Main snippet on my test
:
const tx = await router.addLiquidityETH(coinX.address, supply, supply, supply, addr1, MaxUint256, {
value: supply
});
const { data } = tx;
console.log("Decoded data: ", await ICoinX.decodeFunctionData("addLiquidityETH", data));
我试过:
- 函数名称:
"addLiquidityETH"
. - 函数签名:
"addLiquidityETH(address,uint,uint,uint,address,uint)"
和"addLiquidityETH(address,uint,uint,uint,address,uint) external payable returns (uint,uint,uint)"
- 两个签名的叹息:
"0x1a3042d8"
和"0x251511cc"
interface.decodeFunctionResult( fragment , data )
...但错误仍然出现。
Error
:
Error: no matching function (argument="name", value="addLiquidityETH", code=INVALID_ARGUMENT, version=abi/5.3.1)
谢谢您的帮助!
Full test
:
const { parseEther, formatEther } = ethers.utils;
const { MaxUint256 } = ethers.constants;
const abi = require('../artifacts/contracts/CoinX.sol/CoinX.json').abi;
const routerAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
const factoryAddress = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
describe("Uniswap", function() {
let router, coinX, ICoinX, factory;
const supply = parseEther('100');
before(async () => {
router = await ethers.getContractAt("IUniswapV2Router02", routerAddress);
factory = await ethers.getContractAt("IUniswapV2Factory", factoryAddress);
const CoinX = await ethers.getContractFactory('CoinX');
coinX = await CoinX.deploy(supply);
await coinX.deployed();
ICoinX = new ethers.utils.Interface(abi);
});
it("should allow trades", async function() {
const wethAddr = await router.WETH();
const [addr1] = await ethers.provider.listAccounts();
console.log("coins before: ", formatEther(await coinX.balanceOf(addr1)));
await coinX.approve(routerAddress, MaxUint256);
const tx = await router.addLiquidityETH(coinX.address, supply, supply, supply, addr1, MaxUint256, {
value: supply
});
const { data } = tx;
console.log("Decoded data: ", await ICoinX.decodeFunctionData("addLiquidityETH", data)); // --------> Problem
console.log("coins after: ", formatEther(await coinX.balanceOf(addr1)));
const pairAddress = await factory.getPair(coinX.address, wethAddr);
console.log(pairAddress);
});
});