UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

146 lines 5.7 kB
import { http, createWalletClient, parseEther } from "viem"; import { baseSepolia, optimismSepolia } from "viem/chains"; import { toMultichainNexusAccount } from "../../account/toMultiChainNexusAccount.js"; import { isP256Signer } from "../../account/utils/toP256Signer.js"; import { createMeeClient } from "../../clients/createMeeClient.js"; import { MEEVersion } from "../../constants/index.js"; import { getMEEVersion } from "../../modules/index.js"; // RPC URLs for testnets const TESTNET_RPC_URLS = { [optimismSepolia.id]: `https://opt-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`, [baseSepolia.id]: `https://base-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}` }; const MIN_BALANCE = parseEther("0.0005"); const FUNDING_AMOUNT = parseEther("0.001"); /** * Funds smart accounts on all chains if their balance is below MIN_BALANCE */ async function fundAccountsIfNeeded(mcNexus, eoaAccount) { for (const deployment of mcNexus.deployments) { const chain = deployment.client.chain; if (!chain) continue; const publicClient = deployment.client; const balance = await publicClient.getBalance({ address: deployment.address }); if (balance < MIN_BALANCE) { console.log(`Funding ${deployment.address} on ${chain.name} (balance: ${balance})`); const walletClient = createWalletClient({ account: eoaAccount, chain, transport: http(TESTNET_RPC_URLS[chain.id]) }); const hash = await walletClient.sendTransaction({ to: deployment.address, value: FUNDING_AMOUNT }); await publicClient.waitForTransactionReceipt({ hash }); console.log(`Funded ${deployment.address} on ${chain.name} (tx: ${hash})`); } } } /** * Creates multi-version account configurations for testing * @param options Setup options * @returns Array of account configs for each version */ export async function setupMultiVersionAccounts(options) { const { eoaAccount, versions = [MEEVersion.V2_0_0, MEEVersion.V2_2_1, MEEVersion.V3_0_0], chains = [baseSepolia, optimismSepolia], index = 333n, apiKey } = options; const accountConfigs = []; for (const version of versions) { const mcNexus = await toMultichainNexusAccount({ signer: eoaAccount, index, chainConfigurations: chains.map((chain) => ({ chain, transport: http(TESTNET_RPC_URLS[chain.id]), version: getMEEVersion(version) })) }); await fundAccountsIfNeeded(mcNexus, eoaAccount); const meeClient = await createMeeClient({ account: mcNexus, ...(apiKey && { apiKey }) }); const versionName = version === MEEVersion.V2_0_0 ? "V2.0.0 (MeeK1, no 712 for simple mode)" : version === MEEVersion.V2_2_1 ? "V2.2.1 (MeeK1, TypedData for simple mode)" : version === MEEVersion.V3_0_0 ? "V3.0.0 (StxValidator)" : version; accountConfigs.push({ name: versionName, version, mcNexus, meeClient, eoaAccount }); } return accountConfigs; } /** * Creates account config for a single version * Useful when only one version needs testing */ export async function setupSingleVersion(eoaAccount, version, chains = [baseSepolia, optimismSepolia]) { const configs = await setupMultiVersionAccounts({ eoaAccount, versions: [version], chains }); return configs[0]; } /** * Creates multi-version account configurations with a custom signer * Universal method that works with any signer type (EOA, P256, etc.) * @param options Setup options with custom signer * @returns Array of account configs for each version * * @example * // Create P256 account for V3.0.0 * const p256Signer = toP256Signer(privateKey) * const configs = await setupAccountsWithSigner({ * signer: p256Signer, * eoaAccount: network.account, * versions: [MEEVersion.V3_0_0] * }) */ export async function setupAccountsWithSigner(options) { const { signer, eoaAccount, versions = [MEEVersion.V3_0_0], chains = [baseSepolia, optimismSepolia], index = 1n, apiKey } = options; const accountConfigs = []; for (const version of versions) { const mcNexus = await toMultichainNexusAccount({ signer, index, chainConfigurations: chains.map((chain) => ({ chain, transport: http(TESTNET_RPC_URLS[chain.id]), version: getMEEVersion(version) })) }); await fundAccountsIfNeeded(mcNexus, eoaAccount); const meeClient = await createMeeClient({ account: mcNexus, ...(apiKey && { apiKey }) }); const signerType = isP256Signer(signer) ? " with P256" : ""; const versionName = version === MEEVersion.V2_0_0 ? `V2.0.0 (MeeK1, no 712 for simple mode)${signerType}` : version === MEEVersion.V2_2_1 ? `V2.2.1 (MeeK1, TypedData for simple mode)${signerType}` : version === MEEVersion.V3_0_0 ? `V3.0.0 (StxValidator)${signerType}` : version; accountConfigs.push({ name: versionName, version, mcNexus, meeClient, eoaAccount }); } return accountConfigs; } //# sourceMappingURL=setupMultiVersion.js.map