Transfer Funds

import { ethers } from 'ethers'; //v5.7.2
import { ByzanlinkAASdk } from '@byzanlink-tech/aa-sdk';
import { sleep } from '@byzanlink-tech/aa-sdk/dist/sdk/common';

async function main() {

  const recipient = 'xxxx'; // Recipient wallet address
  const value = '0.0001'; // Transfer value

  // Instantiate SDK
  const byzanlinkAASdk = new ByzanlinkAASdk(
    { privateKey: process.env.PRIVATE_KEY }, // Get the private key using any wallet provider
    {
      chainId: Number(process.env.CHAIN_ID), // Ex: 80002
      apiKey: process.env.BYZANLINK_API_KEY // Get API key from Byzanlink dashboard
    })
  )

  // Get address of Byzanlink Wallet
  const address: string = await byzanlinkAASdk.getCounterFactualAddress();
  console.log(`Byzanlink Wallet address: ${address}`);

  // Clear the transaction batch
  await byzanlinkAASdk.clearUserOpsFromBatch();

  // Add transactions to the batch
  await byzanlinkAASdk.addUserOpsToBatch({ to: recipient, value: ethers.utils.parseEther(value) });

  // Get balance of the account address
  const balance: string = await byzanlinkAASdk.getNativeBalance();
  console.log(`Byzanlink Wallet native balance: ${balance}`);

  // Estimate transactions added to the batch and get the fee data for the UserOp
  const op = await byzanlinkAASdk.estimate();
  console.log(`User Op: ${JSON.stringify(op)}`);

  // Sign the UserOp and send to the bundler
  const uoHash = await byzanlinkAASdk.send(op);
  console.log(`UserOpHash: ${uoHash}`);

  // Get transaction hash
  console.log('Waiting for transaction...');
  let userOpsReceipt = null;
  const timeout = Date.now() + 60000; // 1 minute timeout
  while ((userOpsReceipt == null) && (Date.now() < timeout)) {
    await sleep(2);
    userOpsReceipt = await byzanlinkAASdk.getUserOpReceipt(uoHash);
  }
  console.log('Transaction Receipt: ', userOpsReceipt);
}

main()
  .catch(console.error)
  .finally(() => process.exit());

Last updated