2

I want to buy new tokens launched on base using Uniswap Universal router on Base with node js and ether js libraries. Im calling execute function but I'm having many problems.

Here is the code:

//addresses
const WETH = '0x4200000000000000000000000000000000000006' // WETH base

// 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()

There is an error when encoding the path and input to call the execute function. I don't understand the error is showing me I have been following ethers documentation and Uniswap docs too.

Error:

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

What am I doing wrong? Is there any other router that allows you to buy any kind of token? I have been using SwapRouterV2 but doesnt allow me to buy some tokens.

1

1 Answer 1

1

the problem seems to be the path you are encoding each element of the array separately, when you encode data you get a new hexadecimal string that is 64 bytes long.

try

path = [AbiCoder.encode(['address','uint','address'],[WETH,500,tokenToBuy])]

this should solve the length problem as you encode all the array elements at ones not one by one.

also you can see the official ethers.js documentation: https://docs.ethers.org/v5/api/utils/abi/coder/

8
  • Does it not need packed encoding (encodePacked) for the path. Encoding without the padding.
    – Maka
    Commented Apr 22 at 19:18
  • well i dont think so, as if you look at this call data for example you can notice the addresses of weth and the token to buy are padded. basescan.org/tx/…
    – yahdielo
    Commented Apr 25 at 15:40
  • 0b08 is for a v2 swap, v2 path is padded not v3. | github.com/Uniswap/universal-router/blob/main/contracts/…
    – Maka
    Commented Apr 25 at 19:42
  • 1
    facts my bad for that
    – yahdielo
    Commented Apr 25 at 23:37
  • 1
    yes , appreciate your time and your good vibes to help man
    – yahdielo
    Commented May 1 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.