I’m trying to make a swap using uniswap v3 and batch transaction with smart account in biconomy. This includes 3 transaction, the approval of WETH, Wrapping on ETH to WETH and the SWAP between WETH and user provided token.
The approval and wrapping is going through but as I include the swap transaction in batch it’s failing, can you help me out?
This is my wethCheckApproval function
const wethCheckApproval = async (userAddress, amount) => {
try {
const checkAllowance = await wethContract.functions.allowance(
userAddress,
routerAddress
);
const allowanceAmount = BigInt(checkAllowance.toString());
console.log("checkAllowance", allowanceAmount);
const approveMax =
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
const value = ethers.BigNumber.from(
await ethers.utils.parseUnits(amount.toString(), "ether")
);
const amountInWei = BigInt(value.toString());
console.log("amountInWei", amountInWei);
if (allowanceAmount < amountInWei) {
console.log("In approval");
const approveData = await wethContract.populateTransaction.approve(
routerAddress,
approveMax
);
const gas = await wethContract.estimateGas.approve(
routerAddress,
approveMax
);
const approveTx = {
to: wethAddress,
data: approveData.data,
gas: gas,
};
return approveTx;
} else {
console.log("Have allowance");
return { to: wethAddress, data: "0x" };
}
} catch (err) {
console.log("wethCheckApprovalError", err);
return { to: wethAddress, data: "0x" };
}
};
This is my wrapping of ETH-WETH function
const checkWethBalance = async (userAddress, amount) => {
try {
const wethBalance = await wethContract.functions.balanceOf(userAddress);
const balance = BigInt(wethBalance.toString());
console.log("weth_balance", balance);
const value = ethers.BigNumber.from(
await ethers.utils.parseUnits(amount.toString(), "ether")
);
const amountInWei = BigInt(value.toString());
if (balance < amountInWei) {
const wrapData = await wethContract.populateTransaction.deposit();
const gas = await wethContract.estimateGas.deposit();
const wrapTx = {
to: wethAddress,
data: wrapData.data,
gas: gas,
value: amountInWei,
};
return wrapTx;
} else {
console.log("Have enough weth balance");
return { to: wethAddress, data: "0x" };
}
} catch (err) {
console.log("checkWethBalanceError", err);
return { to: wethAddress, data: "0x" };
}
};
This is my swap function where I’m sending the batch transaction
const Buy = async (tokenAddress, amount, fee) => {
try {
const pairExist = await checkPair(wethAddress, tokenAddress, fee);
if (pairExist) {
const account = await createSmartAccount();
const address = await account.getAccountAddress();
const wethCheckTx = await wethCheckApproval(address, amount);
const wethBalanceTx = await checkWethBalance(address, amount);
console.log("Swapping token to Buy");
const latestBlock = await provider.getBlock("latest");
const timestamp = BigInt(latestBlock.timestamp);
const deadline = timestamp + BigInt(60) * BigInt(10);
const value = ethers.BigNumber.from(
await ethers.utils.parseUnits(amount.toString(), "ether")
);
const amountInWei = BigInt(value.toString());
// console.log(deadline);
const swapData =
await routerContract.populateTransaction.exactInputSingle({
tokenIn: wethAddress,
tokenOut: tokenAddress,
fee: fee,
recipient: address,
deadline: Number(deadline),
amountIn: amountInWei,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
});
console.log("swapData", swapData);
const gas = 250000;
const swapTx = {
to: routerAddress,
data: swapData.data,
gas: gas,
};
console.log("swapTx", swapTx);
let userOp = await account.buildUserOp(
[wethCheckTx, wethBalanceTx, swapTx],
{ skipBundlerGasEstimation: true }
);
console.log("userOp", userOp);
const userOpResponse = await account.sendUserOp(userOp);
console.log("userOpHash", userOpResponse);
const { receipt } = await userOpResponse.wait(1);
console.log("txHash", receipt.transactionHash);
} else {
console.log("Cannot swap pair doesn't exist");
return "Cannot swap pair doesn't exist";
}
} catch (err) {
console.log("BuyError", err);
}
};
can someone help me out
This is the error I’m facing