To make a multi call, you have to encode functions.
// assuming you created swapRouterContract properly and set params1, params2
// interface.encodeFunctionData available in ether
const encData1 = swapRouterContract.interface.encodeFunctionData(
"exactInputSingle",
[params1]
);
const encData2 = swapRouterContract.interface.encodeFunctionData(
"exactInputSingle",
[params2]
);
Multicall means we are going to call multiple functions with a single request.
const calls = [encData1, encData2];
// To send data to the contract, we need to send it in a way that the contract can read it. That is, they need to be encoded.
// data is transformed into byte
const encMultiCall = swapRouterContract.interface.encodeFunctionData(
"multicall",
[calls]
);
To send transaction, we need, "to", "from" and "data"
const txArgs = {
to: V3SwapRouter,
from: WALLET_ADDRESS,
data: encMultiCall,
};
this transaction has to be sent by a signer. If you send the trasaction via metamask, metamask automatically signs the transaction with the account's private key. But if you sent transaction via code, you have to create a signer
const provider = new ethers.providers.JsonRpcProvider(TEST_URL);
// get the secret of the account
const wallet = new ethers.Wallet(WALLET_SECRET);
// connect the wallet to the provider
const signer = wallet.connect(provider);
Now you can send the transaction:
const tx = await signer.sendTransaction(txArgs);
console.log("tx", tx);
// wait for the tx processed and added to the blockchain
const receipt = await tx.wait();
console.log(receipt);