Swapping tokens on pancakeswap

I have this function to swap token on panckeswap

// Buy and Sell

async function buyToken(){
    const buyT = document.getElementById('buyAmount').value;
    const buy = Moralis.Units.Token(buyT, "18");
    const to = user.get("ethAddress");
    const deadline = Date.now() + 10000;
    const optionsBuy = {
      chain: "bsc",
      address: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
      functionName: "swapExactETHForTokensSupportingFeeOnTransferTokens",
      abi: routerAbi,
      msgValue: buy,
      params: {amountOutMin:0, path:['0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c', '0x42981d0bfbaf196529376ee702f2a9eb9092fcb5'], to:to, deadline: deadline }
    const buy = await Moralis.executeFunction(optionsBuy);
    const result = await buy.wait(2);

When I run the function I get this error:
Uncaught (in promise) Error: invalid contract address or ENS name (argument="addressOrName", value=undefined, code=INVALID_ARGUMENT, version=contracts/5.6.0)

can someone answer this question?

can anyone help here please?

If you’re calling any smart contract function, make sure you’re on the proper network rather. As chain isn’t part of the params to execute a contract function, rather, the connected wallets network will determine that

im on the correct network but it doesnt work

can anyone explain to me, why i cant execute a swap on pancakeswap?

Hi according to the docs, in your options, address should be contractAddress.

If that doesn’t work, can you check that the address is correct. And then check the function name, ABI, params and executeFunction syntax is correct.

im using a contract address, im using the pancakswap router address

Ok, have you renamed address to contractAddress as mentioned?

It works thank you ser

1 Like