Transfer ERC20 Tokens
import { BigNumber } from 'ethers';
import 'dotenv/config';
import { NetworkNames, initializeSdkGateway } from '@kanalabs/mirai';
const tokenAddress = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F'; // Sender address
const receiverAddress = '0x97a57d9CE2889E2E8DFb6019f8Eb51F5d119Bde3'; // Receiver address
(async () => {
// initializating sdk...
const sdk = await initializeSdkGateway(
{ privateKey: process.env.PRIVATE_KEY as string },
{
networks: [NetworkNames.Mumbai],
bundlerApiKey: process.env.BUNDLER_API_KEY
},
);
// set mumbai as default current instance
const networkInstance = sdk.setCurrentInstance(NetworkNames.Mumbai);
//initialize erc20 sdk instance
const erc20 = sdk.erc20(tokenAddress, NetworkNames.Mumbai);
const native = await sdk.getNativeBalance();
console.log('native: ', native);
// clear any previous transactions in batch
await networkInstance.clearUserOpsFromBatch();
// add erc20 transfer function to the batch
await erc20.transfer(receiverAddress, 1000000 as unknown as BigNumber);
// estimate transactions added to the batch and get the fee data for the UserOp
const op = await networkInstance.estimate();
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}`);
})()
Last updated