@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
88 lines (87 loc) • 3.94 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERC4337EthersProvider = void 0;
const providers_1 = require("@ethersproject/providers");
const ethers_1 = require("ethers");
const utils_1 = require("ethers/lib/utils");
const ERC4337EthersSigner_1 = require("./ERC4337EthersSigner");
const UserOperationEventListener_1 = require("./UserOperationEventListener");
const common_1 = require("../common");
class ERC4337EthersProvider extends providers_1.BaseProvider {
constructor(chainId, config, originalSigner, originalProvider, httpRpcClient, entryPoint, smartAccountAPI) {
super({
name: 'ERC-4337 Custom Network',
chainId,
});
this.chainId = chainId;
this.config = config;
this.originalSigner = originalSigner;
this.originalProvider = originalProvider;
this.httpRpcClient = httpRpcClient;
this.entryPoint = entryPoint;
this.smartAccountAPI = smartAccountAPI;
this.signer = new ERC4337EthersSigner_1.ERC4337EthersSigner(config, originalSigner, this, httpRpcClient, smartAccountAPI);
}
async init() {
this.initializedBlockNumber = await this.originalProvider.getBlockNumber();
await this.smartAccountAPI.init();
return this;
}
getSigner() {
return this.signer;
}
async perform(method, params) {
if (method === 'sendTransaction' || method === 'getTransactionReceipt') {
throw new Error('Should not get here. Investigate.');
}
return await this.originalProvider.perform(method, params);
}
async getTransaction(transactionHash) {
return await super.getTransaction(transactionHash);
}
async getTransactionReceipt(transactionHash) {
const userOpHash = await transactionHash;
const sender = await this.getSenderAccountAddress();
return await new Promise((resolve, reject) => {
new UserOperationEventListener_1.UserOperationEventListener(resolve, reject, this.entryPoint, sender, userOpHash).start();
});
}
async getSenderAccountAddress() {
return await this.smartAccountAPI.getAccountAddress();
}
async waitForTransaction(transactionHash, confirmations, timeout) {
const sender = await this.getSenderAccountAddress();
return await new Promise((resolve, reject) => {
const listener = new UserOperationEventListener_1.UserOperationEventListener(resolve, reject, this.entryPoint, sender, transactionHash, undefined, timeout);
listener.start();
});
}
async constructUserOpTransactionResponse(userOp1) {
const userOp = await (0, utils_1.resolveProperties)(userOp1);
const userOpHash = (0, common_1.getUserOpHash)(userOp, this.config.entryPointAddress, this.chainId);
const waitPromise = new Promise((resolve, reject) => {
new UserOperationEventListener_1.UserOperationEventListener(resolve, reject, this.entryPoint, userOp.sender, userOpHash, userOp.nonce).start();
});
return {
hash: userOpHash,
confirmations: 0,
from: userOp.sender,
nonce: ethers_1.BigNumber.from(userOp.nonce).toNumber(),
gasLimit: ethers_1.BigNumber.from(userOp.callGasLimit),
value: ethers_1.BigNumber.from(0),
data: (0, utils_1.hexValue)(userOp.callData),
chainId: this.chainId,
wait: async (confirmations) => {
const transactionReceipt = await waitPromise;
if (userOp.initCode.length !== 0) {
await this.smartAccountAPI.checkAccountPhantom();
}
return transactionReceipt;
},
};
}
async detectNetwork() {
return this.originalProvider.detectNetwork();
}
}
exports.ERC4337EthersProvider = ERC4337EthersProvider;
;