UNPKG

near-safe

Version:

An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.

69 lines (68 loc) 2.61 kB
import { createPublicClient, http, rpcSchema, } from "viem"; import { PLACEHOLDER_SIG } from "../util"; import { Pimlico } from "./pimlico"; export class Erc4337Bundler { client; entryPointAddress; pimlico; chainId; constructor(entryPointAddress, apiKey, chainId) { this.entryPointAddress = entryPointAddress; this.pimlico = new Pimlico(apiKey); this.chainId = chainId; this.client = createPublicClient({ transport: http(this.pimlico.bundlerUrl(chainId)), rpcSchema: rpcSchema(), }); } async getPaymasterData(rawUserOp, sponsorshipPolicy) { const userOp = { ...rawUserOp, signature: PLACEHOLDER_SIG }; if (sponsorshipPolicy) { return this.pimlico.handleRequest(() => this.client.request({ method: "pm_sponsorUserOperation", params: [ userOp, this.entryPointAddress, { sponsorshipPolicyId: sponsorshipPolicy }, ], })); } return this.pimlico.handleRequest(() => this.client.request({ method: "eth_estimateUserOperationGas", params: [userOp, this.entryPointAddress], })); } async sendUserOperation(userOp) { return this.pimlico.handleRequest(() => this.client.request({ method: "eth_sendUserOperation", params: [userOp, this.entryPointAddress], })); // throw new Error(`Failed to send user op with: ${error.message}`); } async getGasPrice() { return this.pimlico.handleRequest(() => this.client.request({ method: "pimlico_getUserOperationGasPrice", params: [], })); } async getUserOpReceipt(userOpHash) { let userOpReceipt = null; while (!userOpReceipt) { // Wait 2 seconds before checking the status again await new Promise((resolve) => setTimeout(resolve, 2000)); userOpReceipt = await this._getUserOpReceiptInner(userOpHash); } return userOpReceipt; } async getSponsorshipPolicies() { // Chain ID doesn't matter for this bundler endpoint. const allPolicies = await this.pimlico.getSponsorshipPolicies(); return allPolicies.filter((p) => p.chain_ids.allowlist.includes(this.chainId)); } async _getUserOpReceiptInner(userOpHash) { return this.pimlico.handleRequest(() => this.client.request({ method: "eth_getUserOperationReceipt", params: [userOpHash], })); } }