Sponsor Transactions
Using this API call we sponsor a transactions.
To sponsor a transaction, you need to whitelist the address beforehand.
import { utils } from 'ethers';
import 'dotenv/config';
import { NetworkNames, initializeSdkGateway } from '@kanalabs/mirai';
import { sleep } from '@etherspot/prime-sdk/dist/sdk/common';
const receiverAddress = '0x97a57d9CE2889E2E8DFb6019f8Eb51F5d119Bde3'; // Receiver address
const value = '0.0001';
(async () => {
// initializating sdk...
const sdk = await initializeSdkGateway(
{ privateKey: process.env.PRIVATE_KEY as string },
{
networks: [NetworkNames.Mumbai],
bundlerApiKey: process.env.BUNDLER_API_KEY
},
);
// Retrieve the chainId corresponding to the network name
const chainId = networkNameToChainId(NetworkNames.Mumbai)
// set mumbai as default current instance
const networkInstance = sdk.setCurrentInstance(NetworkNames.Mumbai);
// clear the transaction batch
await networkInstance.clearUserOpsFromBatch();
// add transactions to the batch
await networkInstance.addUserOpsToBatch({ to: receiverAddress, value: utils.parseEther(value) });
// get balance of the account address
const balance = await networkInstance.getNativeBalance();
console.log('balances: ', balance);
// Estimate transaction details using paymaster for sponsorship mode.
const op = await networkInstance.estimate({
paymasterDetails: {
url: `https://evm-paymaster.kanalabs.io?apiKey=${process.env.PAYMASTER_API_KEY}&chainId=${chainId}`,
context: { mode: 'sponsor' },
},
});
console.log("op: ", op);
// sign the UserOp and sending to the bundler...
const uoHash = await networkInstance.send(op);
// log the uoHash
console.log(`UserOpHash: ${uoHash}`)
let userOpsReceipt = null;
const timeout = Date.now() + 60000 // 1 minute timeout
while (userOpsReceipt == null && Date.now() < timeout) {
await sleep(2)
userOpsReceipt = await networkInstance.getUserOpReceipt(uoHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
})()
* PAYMASTER_API_KEY - Contact kanalabs to obtain the API key for authentication.
Last updated