I'm trying to integrate 1inch AggregationRouterV4 into my contract but the transaction always get reverted
The approach i'm following is :
- 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
-
Approve source token for MySwapContract
-
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.