Add Owners

import { ethers } from 'ethers';
import 'dotenv/config';
import { NetworkNames, initializeSdkGateway } from '@kanalabs/mirai';
import { sleep } from '@etherspot/prime-sdk/dist/sdk/common';

async function main() {

  // initializating sdk...
  const sdk = await initializeSdkGateway(
    { privateKey: process.env.PRIVATE_KEY as string },
    {
      networks: [NetworkNames.Mumbai],
      bundlerApiKey: process.env.BUNDLER_API_KEY,
    },
  );

  const networkInstance = sdk.setCurrentInstance(NetworkNames.Mumbai);
  console.log('address: ', networkInstance.state.EOAAddress);

  // get address of EtherspotWallet
  const address: string = await networkInstance.getCounterFactualAddress();

  // update the addresses in this array with the owner addresses you want to set
  const ownerAddresses: string[] = [
    '0x838491825187FF13ed4a4476De3E28D4404da3Be',
    '0xC39Ccf6319A6c4079f21c3573B63956f7bbd851C'
  ];

  console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

  const addOwnerInterface = new ethers.utils.Interface(['function addOwner(address _newOwner)']);

  const addOwnerData1 = addOwnerInterface.encodeFunctionData('addOwner', [ownerAddresses[0]]);
  const addOwnerData2 = addOwnerInterface.encodeFunctionData('addOwner', [ownerAddresses[1]]);

  // clear the transaction batch
  await networkInstance.clearUserOpsFromBatch();

  // add transactions to the batch
  let userOpsBatch = await networkInstance.addUserOpsToBatch({ to: address, data: addOwnerData1 });
  userOpsBatch = await networkInstance.addUserOpsToBatch({ to: address, data: addOwnerData2 });


  console.log('transactions: ', userOpsBatch);

  // sign transactions added to the batch
  const op = await networkInstance.estimate();
  console.log('Estimated UserOp:', op);

  // sign the userOps and sending to the bundler...
  const uoHash = await networkInstance.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 networkInstance.getUserOpReceipt(uoHash);
  }
  console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
}

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

Last updated