@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
135 lines (134 loc) • 5.82 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpRpcClient = void 0;
const ethers_1 = require("ethers");
const utils_1 = require("ethers/lib/utils");
const debug_1 = __importDefault(require("debug"));
const ERC4337Utils_1 = require("../common/ERC4337Utils");
const errorHandler_service_1 = require("../errorHandler/errorHandler.service");
const debug = (0, debug_1.default)('aa.rpc');
class HttpRpcClient {
constructor(bundlerUrl, entryPointAddress, chainId) {
this.bundlerUrl = bundlerUrl;
this.entryPointAddress = entryPointAddress;
this.chainId = chainId;
try {
this.userOpJsonRpcProvider = new ethers_1.ethers.providers.JsonRpcProvider({
url: this.bundlerUrl
}, {
name: 'Connected bundler network',
chainId,
});
this.initializing = this.validateChainId();
}
catch (err) {
if (err.message.includes('failed response'))
throw new errorHandler_service_1.ErrorHandler(err.message, 2);
if (err.message.includes('timeout'))
throw new errorHandler_service_1.ErrorHandler(err.message, 3);
throw new Error(err.message);
}
}
async validateChainId() {
try {
const chain = await this.userOpJsonRpcProvider.send('eth_chainId', []);
const bundlerChain = parseInt(chain);
if (bundlerChain !== this.chainId) {
throw new Error(`bundler ${this.bundlerUrl} is on chainId ${bundlerChain}, but provider is on chainId ${this.chainId}`);
}
}
catch (err) {
if (err.message.includes('failed response'))
throw new errorHandler_service_1.ErrorHandler(err.message, 400);
if (err.message.includes('timeout'))
throw new errorHandler_service_1.ErrorHandler(err.message, 404);
throw new Error(err.message);
}
}
async getVerificationGasInfo(tx) {
var _a;
const hexifiedUserOp = (0, ERC4337Utils_1.deepHexlify)(await (0, utils_1.resolveProperties)(tx));
try {
const response = await this.userOpJsonRpcProvider.send('eth_estimateUserOperationGas', [hexifiedUserOp, this.entryPointAddress]);
return response;
}
catch (err) {
const body = JSON.parse(err.body);
if ((_a = body === null || body === void 0 ? void 0 : body.error) === null || _a === void 0 ? void 0 : _a.code) {
throw new errorHandler_service_1.ErrorHandler(body.error.message, body.error.code);
}
throw new Error(err.message);
}
}
async sendUserOpToBundler(userOp1) {
var _a;
try {
await this.initializing;
const hexifiedUserOp = (0, ERC4337Utils_1.deepHexlify)(await (0, utils_1.resolveProperties)(userOp1));
const jsonRequestData = [hexifiedUserOp, this.entryPointAddress];
await this.printUserOperation('eth_sendUserOperation', jsonRequestData);
return await this.userOpJsonRpcProvider.send('eth_sendUserOperation', [hexifiedUserOp, this.entryPointAddress]);
}
catch (err) {
const body = JSON.parse(err.body);
if ((_a = body === null || body === void 0 ? void 0 : body.error) === null || _a === void 0 ? void 0 : _a.code) {
throw new errorHandler_service_1.ErrorHandler(body.error.message, body.error.code);
}
throw new Error(err);
}
}
async sendAggregatedOpsToBundler(userOps1) {
var _a;
try {
const hexifiedUserOps = await Promise.all(userOps1.map(async (userOp1) => await (0, utils_1.resolveProperties)(userOp1)));
return await this.userOpJsonRpcProvider.send('eth_sendAggregatedUserOperation', [
hexifiedUserOps,
this.entryPointAddress,
]);
}
catch (err) {
const body = JSON.parse(err.body);
if ((_a = body === null || body === void 0 ? void 0 : body.error) === null || _a === void 0 ? void 0 : _a.code) {
throw new errorHandler_service_1.ErrorHandler(body.error.message, body.error.code);
}
throw new Error(err);
}
}
async getSkandhaGasPrice() {
try {
const { maxFeePerGas, maxPriorityFeePerGas } = await this.userOpJsonRpcProvider.send('skandha_getGasPrice', []);
return { maxFeePerGas, maxPriorityFeePerGas };
}
catch (err) {
console.warn("getGas: skandha_getGasPrice failed, falling back to legacy gas price.");
const gas = await this.userOpJsonRpcProvider.getGasPrice();
return { maxFeePerGas: gas, maxPriorityFeePerGas: gas };
}
}
async getBundlerVersion() {
try {
const version = await this.userOpJsonRpcProvider.send('web3_clientVersion', []);
return version;
}
catch (err) {
return null;
}
}
async getUserOpsReceipt(uoHash) {
try {
const response = await this.userOpJsonRpcProvider.send('eth_getUserOperationReceipt', [uoHash]);
return response;
}
catch (err) {
return null;
}
}
async printUserOperation(method, [userOp1, entryPointAddress]) {
const userOp = await (0, utils_1.resolveProperties)(userOp1);
debug('sending', method, Object.assign({}, userOp), entryPointAddress);
}
}
exports.HttpRpcClient = HttpRpcClient;
;