我想购买使用 Base 上的 Uniswap 通用路由器以及 Node js 和 ether js 库在 Base 上推出的新代币。我正在调用执行函数,但我遇到了很多问题。
这是代码:
`
const {ethers} = require("ethers")
const AbiCoder = new ethers.utils.AbiCoder()
// const tokenToBuy = '0xe67aB9Efb46d987d2aF816597F6716069A8a7426' //KIF
//Router
const routerAddress = '0x198EF79F1F515F02dFE9e3115eD9fC07183f02fC' // router
const file = fs.readFileSync("./B_Base/ABIUniversal.json", 'utf8') // Calls sushi ABI
const routerAbi = JSON.parse(file)
const routerContract = new ethers.Contract(routerAddress, routerAbi, signer)
// token input
const prompt = ps()
const tokenToBuy = String(prompt("Enter token name: "))
//wbnb to spend
const spend = ethers.utils.parseEther("0.0001")
const commands = 0x00
const path = [AbiCoder.encode(WETH), AbiCoder.encode(5000), AbiCoder.encode(tokenToBuy)]
// Encode the parameters
const inputs = AbiCoder.encode(WALLET_ADDRESS, spend, 0, path, false)
const buy = async () => { // .execute execute(bytes commands,bytes[] inputs,uint256 deadline)
const buy = await routerContract.execute(
commands,
inputs,
Date.now() + 1000 * 60 * 10)
receipt = await buy.wait()
transactionHash = receipt.transactionHash
console.log(transactionHash)
}
buy()`
对调用执行函数的路径和输入进行编码时出现错误。我不明白这个错误表明我也一直在关注以太文档和 Uniswap 文档。
错误:
` if (types.length !==values.length) { ^
TypeError: Cannot read properties of undefined (reading 'length')
at AbiCoder.encode (/home/oriok/DEV/degen/Arbitrum/node_modules/@ethersproject/abi/lib/abi-coder.js:82:37)
at Object.<anonymous> (/home/oriok/DEV/degen/Arbitrum/B_Base/testinUni.js:52:24)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47`
我做错了什么?还有其他路由器允许您购买任何类型的令牌吗?我一直在使用 SwapRouterV2,但不允许我购买一些代币。 Uniswap 社区不和谐也没有太多解决方案。
询问版本,因为 etherjs 从 v5 到 v6 有很多修改:请参阅 5.7/packages/abi/lib/abi-coder.js 了解更改。
看起来,当您创建
encode
变量时传递给 path
的结构就是这个问题所在,因为您应该将一对已定义的 types
和 values
传递给 encode()
功能。
所以你通过以下内容:
const path = [AbiCoder.encode(WETH), AbiCoder.encode(5000), AbiCoder.encode(tokenToBuy)
基于 abi-coder.js(v5.7),类型和值看起来成对匹配:
AbiCoder.prototype.encode = function (types, values) {
var _this = this;
if (types.length !== values.length) {
logger.throwError("types/values length mismatch", logger_1.Logger.errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
这应该看起来更像是:
const path = [
AbiCoder.encode(["string"], [WETH]),
AbiCoder.encode(["uint"], [5000]),
AbiCoder.encode(["string"], [tokenToBuy])
]
因此,如果您只定义值而不是为每个值预定义类型,您会发现值的数量(长度)与长度中类型的数量也不匹配。不提供这对意味着其中一个实际上是未定义的。
但是,我还会在下一行重新审视编码的重用 - 不确定 ABI 结构如何与嵌套编码配合使用。
我对 etherjs 不太熟悉,我指的是 etherjs 文档上提供的结构。