1inch Aggregator integration

I'm trying to integrate 1inch AggregationRouterV4 into my contract but the transaction always get reverted
The approach i'm following is :

  1. get swap data from 1inch api
 const swapParams: SwapParams = {
    fromTokenAddress: usdtAddress,
    toTokenAddress: usdcAddress,
    amount: amount,
    fromAddress: MySwapContractAddress,
    destReceiver: destReceiver,
    slippage: 3,
    disableEstimate: true,
  };

  const data: SwapResponse = await swap(chainId, swapParams);

  const OneInchRouter = await ethers.getContractFactory(
    "MockAggregationRouterV4"
  );
  const decodedData = OneInchRouter.interface.decodeFunctionData(
    "swap",
    data.tx.data
  );ere
  1. Approve source token for MySwapContract

  2. Do the actual swap

  const contract = await ethers.getContractAt("MySwapContract", MySwapContractAddress);
  const gasEstimate = await contract
    .connect(deployer)
    .swap(decodedData.caller, decodedData.desc, decodedData.data);

Swap function code

   function swap(
        IAggregationExecutor caller,
        SwapDescription memory desc,
        bytes memory data
    ) external payable {
        bool isNotNative = !_isNative(desc.srcToken);

        if (isNotNative) {
            desc.srcToken.safeTransferFrom(
                _msgSender(),
                address(this),
                desc.amount
            );
            desc.srcToken.approve(_INCH_ROUTER, desc.amount);
        }

        (uint256 returnAmount, uint256 spentAmount, ) = IAggregationRouterV4(
            _INCH_ROUTER
        ).swap{value: msg.value}(caller, desc, data);

        if (isNotNative) {
            uint256 unspentAmount = desc.srcToken.balanceOf(address(this));
            if (unspentAmount > 0)
                desc.srcToken.safeTransfer(_msgSender(), unspentAmount);
        } else {
            uint256 unspentAmount = address(this).balance;
            if (unspentAmount > 0)
                _safeNativeTransfer(_msgSender(), unspentAmount);
        }

        emit Swap(_msgSender(), desc.amount, spentAmount, returnAmount);
    }

The errr which i get

Any help regarding this is appreciated.

Hi, welcome to the community! :wave:

Is there a failed transaction hash?

the tx hash is https://polygonscan.com/tx/0x31c31fc2d1ef92823507b38474459a47b47e447fbd89437248ce39f71a5a40fd
i have also verified the contract

the api which i am using is this

const url = `https://api.1inch.io/v4.0/${chainId}/swap?fromTokenAddress=${swapParams.fromTokenAddress}&toTokenAddress=${swapParams.toTokenAddress}&amount=${swapParams.amount}&fromAddress=${swapParams.fromAddress}&destReceiver=${swapParams.destReceiver}&slippage=${swapParams.slippage}&disableEstimate=${swapParams.disableEstimate}

u can use this for decoding 1inch respose.tx.data

  const iface = new ethers.utils.Interface([
   "function swap(address,tuple(address,address,address,address,uint256,uint256,uint256,bytes),bytes)",
  ]);

   const decodedData = iface.decodeFunctionData("swap", data.tx.data);
  console.log(decodedData.caller, decodedData.desc, decodedData.data);

Found the solution, interface issue from my end

I have the same problem as you, how did you solve it?

For 1inchv4 ethereum and other chains have different contract. So use the correct interface here.

pls share for me about code of MockAggregationRouterV4 to decodeFunctionData from data.tx.data. thank u

@DANTE can you please share the code of calling swap function of 1inch in your smart contract?