我正在尝试使用 ethers 库和 JS 构建一个多重调用方法(标准交换),但遇到了这个错误:
(0, properties_1.defineReadOnly)(this, "fragments", abi.map(function (fragment) {
^
TypeError: abi.map is not a function
这是我的代码,我完全确定我做错了什么,因为整个多重调用对我来说是新的。
另外,如何将另一个方法添加到同一个多重调用中?就像批准令牌一样。有人有一个可行的例子吗?
p.s.:我不想使用 web3,而是坚持使用 ethers lib。
const params = {
amountIn: purchaseAmount,
amountOutMin: 0,
path: tokenspair,
to: publicKey
}
const data = await ethers.utils.Interface(abi).encodeFunctionData("swapExactTokensForTokens", params)
let unsignedTx = await _router.populateTransaction.multicall(
data,
{
type: 2,
maxFeePerGas: maxFeePerGas,
maxPriorityFeePerGas: maxPriorityFeePerGas,
gasLimit: gasLimit,
nonce: currentNonce,
deadline: Date.now() + 1000 * 60 * 5
}
)
unsignedTx.chainId = chainID
signedTx = await _wallet.signTransaction(unsignedTx)
我检查了 ABI,其中的“swapExactTokensForTokens”方法正确。
非常感谢您的帮助!
这是一个老问题,但我将与您分享我的一些经验。首先,UniswapV2 Router 不支持多重调用,您需要使用像 de MakerDao 多重调用合约这样的多重调用代理,或者使用继承自自身多重调用合约的 UniswapV3 Router。
我发现您正在使用 UniswapV2 Router 因为
swapExactTokensForTokens
是该版本的一种方法。
以下视频展示了如何使用以太币和 uniswapV3 进行多重调用写入交易: https://www.youtube.com/watch?v=xKiLdpshg6w
要理解视频,请先阅读此 Uniswap V3 文档: https://docs.uniswap.org/contracts/v3/guides/swaps/single-swaps
考虑到视频的开发者正在使用uniswapV3 SDK来使用他们的ABI接口,但你不需要它。库工件单独附带 ABI 继承合约,但您可以从 etherscan.io 下载完整的 ABI 并将其用作路由器 v3 合约 ABI。