import { createSmartAccountClient, toNexusAccount } from "@biconomy/abstractjs"
import { http, encodeFunctionData, erc20Abi, parseEther } from "viem"
import { FREE_MINT_ERC20, toClients } from "../src"
import type { Infra } from "../src/toEcosystem"

export const benchmarkRegular = async ({
  bundler,
  network: { rpcUrl, chain, privateKey }
}: Infra) => {
  const { testClient, accounts } = await toClients({
    rpcUrl,
    chain
  })

  const someWalletAccount = accounts[9]

  const nexusAccount = await toNexusAccount({
    signer: accounts[0],
    chain,
    transport: http(rpcUrl)
  })

  const bundlerClient = createSmartAccountClient({
    account: nexusAccount,
    chain,
    transport: http(bundler.url),
    mock: true
  })

  await testClient.setBalance({
    address: nexusAccount.address,
    value: parseEther("10")
  })

  const hash = await bundlerClient.sendUserOperation({
    calls: [
      {
        to: FREE_MINT_ERC20,
        value: 0n,
        data: encodeFunctionData({
          abi: erc20Abi,
          functionName: "transfer",
          args: [someWalletAccount.address, parseEther("1")]
        })
      }
    ]
  })
  const { success, receipt } = await bundlerClient.waitForUserOperationReceipt({
    hash
  })

  if (!success) {
    throw new Error("User operation failed")
  }
  console.log("Deploy Nexus via factory + send ERC20", receipt.gasUsed)
}
