UNPKG

@fastlane-labs/shbundler-sdk

Version:

SDK for interacting with Fastlane's 4337 ShBundler

203 lines (198 loc) 6.79 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { createShBundlerClient: () => createShBundlerClient, createShBundlerClientFromSmartAccount: () => createShBundlerClientFromSmartAccount }); module.exports = __toCommonJS(index_exports); // src/client.ts var import_viem2 = require("viem"); var import_account_abstraction = require("viem/account-abstraction"); var import_accounts = require("permissionless/accounts"); var import_accounts2 = require("viem/accounts"); var import_permissionless = require("permissionless"); // src/utils/gas.ts var import_viem = require("viem"); async function getUserOperationGasPrice(bundlerClient) { const resultEncoded = await bundlerClient.request({ method: "gas_getUserOperationGasPrice", params: [] }); return { maxFeePerGas: (0, import_viem.hexToBigInt)(resultEncoded.standard.maxFeePerGas), maxPriorityFeePerGas: (0, import_viem.hexToBigInt)(resultEncoded.standard.maxPriorityFeePerGas) }; } // src/utils/networks.ts async function fetchNetworkDefaults(chainId) { const url = "https://raw.githubusercontent.com/FastLane-Labs/shbundler-sdk/main/configs/networks.json"; const res = await fetch(url); const data = await res.json(); return data[chainId] || null; } // src/client.ts async function buildShBundlerSDK({ smartAccount, rpcUrl, chain, bundlerUrl, paymasterUrl, paymasterAddress }) { const publicClient = (0, import_viem2.createPublicClient)({ transport: (0, import_viem2.http)(rpcUrl), chain }); const walletClient = (0, import_viem2.createWalletClient)({ transport: (0, import_viem2.http)(rpcUrl), account: smartAccount }); const paymasterClient = (0, import_account_abstraction.createPaymasterClient)({ transport: (0, import_viem2.http)(paymasterUrl) }); let bundlerClient; bundlerClient = (0, import_account_abstraction.createBundlerClient)({ transport: (0, import_viem2.http)(bundlerUrl), name: "shBundler", account: smartAccount, client: publicClient, paymaster: paymasterClient, userOperation: { estimateFeesPerGas: async () => getUserOperationGasPrice(bundlerClient) } }); const sendUserOperation = async ({ to, data, chain: chain2, paymasterContext }) => { const dynamicSmartAccountClient = (0, import_permissionless.createSmartAccountClient)({ client: publicClient, chain: chain2, bundlerTransport: (0, import_viem2.http)(bundlerUrl), account: smartAccount, userOperation: { estimateFeesPerGas: async () => getUserOperationGasPrice(bundlerClient) }, paymaster: paymasterClient, paymasterContext }); return (0, import_account_abstraction.sendUserOperation)(dynamicSmartAccountClient, { account: smartAccount, calls: [{ to, data }] }); }; return { publicClient, walletClient, smartAccount, paymasterClient, bundlerClient, sendUserOperation }; } async function createShBundlerClient(opts) { const { signer, rpcUrl, chain, bundlerUrl: inputBundlerUrl, paymasterUrl: inputPaymasterUrl, paymasterAddress: inputPaymasterAddress, entryPointVersion = "0.8" } = opts; if (!signer || !rpcUrl || !chain) { throw new Error("signer, rpcUrl, and chain are required"); } const publicClient = (0, import_viem2.createPublicClient)({ transport: (0, import_viem2.http)(rpcUrl), chain }); const chainId = await publicClient.getChainId(); const defaults = await fetchNetworkDefaults(chainId) || {}; const bundlerUrl = inputBundlerUrl || defaults.bundlerUrl; const paymasterUrl = inputPaymasterUrl || defaults.paymasterUrl; const paymasterAddress = inputPaymasterAddress || defaults.paymasterAddress; if (!bundlerUrl || !paymasterUrl || !paymasterAddress) { throw new Error("Missing bundlerUrl, paymasterUrl, or paymasterAddress and no defaults found"); } const entryPointAddress = entryPointVersion === "0.7" ? import_account_abstraction.entryPoint07Address : import_account_abstraction.entryPoint08Address; const smartAccountSimple = await (0, import_accounts.toSimpleSmartAccount)({ client: publicClient, entryPoint: { address: entryPointAddress, version: entryPointVersion }, owner: (0, import_accounts2.toAccount)(signer) }); const smartAccountV08MonadTestnet = await (0, import_accounts.toSafeSmartAccount)({ client: publicClient, entryPoint: { address: import_account_abstraction.entryPoint08Address, version: "0.7" }, owners: [signer], version: "1.4.1", safe4337ModuleAddress: "0x02b336F533F2de3F221540eF56583e9cb8E65203", safeProxyFactoryAddress: "0xd9d2Ba03a7754250FDD71333F444636471CACBC4", safeSingletonAddress: "0x639245e8476E03e789a244f279b5843b9633b2E7", safeModuleSetupAddress: "0x2dd68b007B46fBe91B9A7c3EDa5A7a1063cB5b47", multiSendAddress: "0x7B21BBDBdE8D01Df591fdc2dc0bE9956Dde1e16C", multiSendCallOnlyAddress: "0x32228dDEA8b9A2bd7f2d71A958fF241D79ca5eEC" }); const smartAccount = chainId !== 10143 ? smartAccountSimple : entryPointVersion === "0.7" ? smartAccountSimple : smartAccountV08MonadTestnet; return buildShBundlerSDK({ smartAccount, rpcUrl, chain, bundlerUrl, paymasterUrl, paymasterAddress }); } async function createShBundlerClientFromSmartAccount(opts) { const { smartAccount, rpcUrl, chain, bundlerUrl, paymasterUrl, paymasterAddress } = opts; if (!rpcUrl || !chain || !bundlerUrl || !paymasterUrl || !paymasterAddress) { throw new Error("All fields are required to use a precomputed smartAccount"); } return buildShBundlerSDK({ smartAccount, rpcUrl, chain, bundlerUrl, paymasterUrl, paymasterAddress }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createShBundlerClient, createShBundlerClientFromSmartAccount }); //# sourceMappingURL=index.js.map