UNPKG

near-safe

Version:

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

69 lines (68 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Erc4337Bundler = void 0; const viem_1 = require("viem"); const util_1 = require("../util"); const pimlico_1 = require("./pimlico"); class Erc4337Bundler { constructor(entryPointAddress, apiKey, chainId) { this.entryPointAddress = entryPointAddress; this.pimlico = new pimlico_1.Pimlico(apiKey); this.chainId = chainId; this.client = (0, viem_1.createPublicClient)({ transport: (0, viem_1.http)(this.pimlico.bundlerUrl(chainId)), rpcSchema: (0, viem_1.rpcSchema)(), }); } async getPaymasterData(rawUserOp, sponsorshipPolicy) { const userOp = { ...rawUserOp, signature: util_1.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], })); } } exports.Erc4337Bundler = Erc4337Bundler;