Skip to content

Commit

Permalink
refactor: add chainId and extra to pre/post hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1NotMe committed Oct 9, 2024
1 parent 8277a93 commit 29c2e3a
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 162 deletions.
101 changes: 84 additions & 17 deletions src/backend/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ export interface IBackend {
/**
* Submit a user operation to the backend
* @summary Submit a user operation to the backend
* @param {number} chainId The chain ID
* @param {UserOperation} userOp The user operation
* @param {string[]} hints Hints for solvers
* @param {*} [extra] Extra parameters
* @returns {Promise<string>} The hash of the user operation
*/
submitUserOperation(
chainId: number,
userOp: UserOperation,
hints: string[],
extra?: any,
): Promise<string>;

_submitUserOperation(
chainId: number,
userOp: UserOperation,
hints: string[],
extra?: any,
Expand All @@ -27,20 +30,23 @@ export interface IBackend {
/**
* Get solver operations for a user operation previously submitted
* @summary Get solver operations for a user operation previously submitted
* @param {number} chainId The chain ID
* @param {UserOperation} userOp The user operation
* @param {string} userOpHash The hash of the user operation
* @param {boolean} [wait] Hold the request until having a response
* @param {*} [extra] Extra parameters
* @returns {Promise<SolverOperation[]>} The solver operations
*/
getSolverOperations(
chainId: number,
userOp: UserOperation,
userOpHash: string,
wait?: boolean,
extra?: any,
): Promise<SolverOperation[]>;

_getSolverOperations(
chainId: number,
userOp: UserOperation,
userOpHash: string,
wait?: boolean,
Expand All @@ -50,29 +56,33 @@ export interface IBackend {
/**
* Submit user/solvers/dApp operations to the backend for bundling
* @summary Submit a bundle of user/solvers/dApp operations to the backend
* @param {number} chainId The chain ID
* @param {Bundle} bundle The user/solvers/dApp operations to be bundled
* @param {*} [extra] Extra parameters
* @returns {Promise<string>} The result message
*/
submitBundle(bundle: Bundle, extra?: any): Promise<string>;
submitBundle(chainId: number, bundle: Bundle, extra?: any): Promise<string>;

_submitBundle(bundle: Bundle, extra?: any): Promise<string>;
_submitBundle(chainId: number, bundle: Bundle, extra?: any): Promise<string>;

/**
* Get the Atlas transaction hash from a previously submitted bundle
* @summary Get the Atlas transaction hash from a previously submitted bundle
* @param {number} chainId The chain ID
* @param {string} userOpHash The hash of the user operation
* @param {boolean} [wait] Hold the request until having a response
* @param {*} [extra] Extra parameters
* @returns {Promise<string>} The Atlas transaction hash
*/
getBundleHash(
chainId: number,
userOpHash: string,
wait?: boolean,
extra?: any,
): Promise<string>;

_getBundleHash(
chainId: number,
userOpHash: string,
wait?: boolean,
extra?: any,
Expand All @@ -81,19 +91,23 @@ export interface IBackend {
/**
* Get the full bundle for a given user operation
* @summary Get the full bundle for a given user operation
* @param {number} chainId The chain ID
* @param {UserOperation} userOp The user operation
* @param {string[]} hints Hints for solvers
* @param {boolean} [wait] Hold the request until having a response
* @param {*} [extra] Extra parameters
* @returns {Promise<Bundle>} The full bundle
*/
getBundleForUserOp(
chainId: number,
userOp: UserOperation,
hints: string[],
wait?: boolean,
extra?: any,
): Promise<Bundle>;

_getBundleForUserOp(
chainId: number,
userOp: UserOperation,
hints: string[],
wait?: boolean,
Expand All @@ -103,35 +117,40 @@ export interface IBackend {

export abstract class BaseBackend implements IBackend {
protected hooksControllers: IHooksController[] = [];
protected chainId: number;

constructor(protected params: { [k: string]: string } = {}) {
this.chainId = Number(params.chainId);
}
constructor(protected params: { [k: string]: string } = {}) {}

addHooksControllers(hooksControllers: IHooksController[]): void {
this.hooksControllers.push(...hooksControllers);
}

async submitUserOperation(
chainId: number,
userOp: UserOperation,
hints: string[],
extra?: any,
): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[userOp, hints, extra] = await hooksController.preSubmitUserOperation(
chainId,
userOp,
hints,
extra,
);
}
// Implemented by subclass
let userOpHash = await this._submitUserOperation(userOp, hints, extra);
let userOpHash = await this._submitUserOperation(
chainId,
userOp,
hints,
extra,
);

// Post hooks
for (const hooksController of this.hooksControllers) {
[userOp, userOpHash] = await hooksController.postSubmitUserOperation(
chainId,
userOp,
userOpHash,
);
Expand All @@ -141,6 +160,7 @@ export abstract class BaseBackend implements IBackend {
}

async getSolverOperations(
chainId: number,
userOp: UserOperation,
userOpHash: string,
wait?: boolean,
Expand All @@ -150,6 +170,7 @@ export abstract class BaseBackend implements IBackend {
for (const hooksController of this.hooksControllers) {
[userOp, userOpHash, wait, extra] =
await hooksController.preGetSolverOperations(
chainId,
userOp,
userOpHash,
wait,
Expand All @@ -159,6 +180,7 @@ export abstract class BaseBackend implements IBackend {

// Implemented by subclass
let solverOps = await this._getSolverOperations(
chainId,
userOp,
userOpHash,
wait,
Expand All @@ -168,6 +190,7 @@ export abstract class BaseBackend implements IBackend {
// Post hooks
for (const hooksController of this.hooksControllers) {
[userOp, solverOps] = await hooksController.postGetSolverOperations(
chainId,
userOp,
solverOps,
);
Expand All @@ -176,49 +199,69 @@ export abstract class BaseBackend implements IBackend {
return solverOps;
}

async submitBundle(bundle: Bundle, extra?: any): Promise<string> {
async submitBundle(
chainId: number,
bundle: Bundle,
extra?: any,
): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[bundle, extra] = await hooksController.preSubmitBundle(bundle, extra);
[bundle, extra] = await hooksController.preSubmitBundle(
chainId,
bundle,
extra,
);
}

// Implemented by subclass
let result = await this._submitBundle(bundle, extra);
let result = await this._submitBundle(chainId, bundle, extra);

// Post hooks
for (const hooksController of this.hooksControllers) {
result = await hooksController.postSubmitBundle(result);
result = await hooksController.postSubmitBundle(chainId, result, extra);
}

return result;
}

async getBundleHash(
chainId: number,
userOpHash: string,
wait?: boolean,
extra?: any,
): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[userOpHash, wait, extra] = await hooksController.preGetBundleHash(
chainId,
userOpHash,
wait,
extra,
);
}

// Implemented by subclass
let atlasTxHash = await this._getBundleHash(userOpHash, wait, extra);
let atlasTxHash = await this._getBundleHash(
chainId,
userOpHash,
wait,
extra,
);

// Post hooks
for (const hooksController of this.hooksControllers) {
atlasTxHash = await hooksController.postGetBundleHash(atlasTxHash);
atlasTxHash = await hooksController.postGetBundleHash(
chainId,
atlasTxHash,
userOpHash,
);
}

return atlasTxHash;
}

async getBundleForUserOp(
chainId: number,
userOp: UserOperation,
hints: string[],
wait?: boolean,
Expand All @@ -227,42 +270,66 @@ export abstract class BaseBackend implements IBackend {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[userOp, hints, wait, extra] =
await hooksController.preGetBundleForUserOp(userOp, hints, wait, extra);
await hooksController.preGetBundleForUserOp(
chainId,
userOp,
hints,
wait,
extra,
);
}

// Implemented by subclass
let bundle = await this._getBundleForUserOp(userOp, hints, wait, extra);
let bundle = await this._getBundleForUserOp(
chainId,
userOp,
hints,
wait,
extra,
);

// Post hooks
for (const hooksController of this.hooksControllers) {
bundle = await hooksController.postGetBundleForUserOp(bundle);
bundle = await hooksController.postGetBundleForUserOp(
chainId,
bundle,
userOp,
);
}

return bundle;
}

abstract _submitUserOperation(
chainId: number,
userOp: UserOperation,
hints: string[],
extra?: any,
): Promise<string>;

abstract _getSolverOperations(
chainId: number,
userOp: UserOperation,
userOpHash: string,
wait?: boolean,
extra?: any,
): Promise<SolverOperation[]>;

abstract _submitBundle(bundle: Bundle, extra?: any): Promise<string>;
abstract _submitBundle(
chainId: number,
bundle: Bundle,
extra?: any,
): Promise<string>;

abstract _getBundleHash(
chainId: number,
userOpHash: string,
wait?: boolean,
extra?: any,
): Promise<string>;

abstract _getBundleForUserOp(
chainId: number,
userOp: UserOperation,
hints: string[],
wait?: boolean,
Expand Down
Loading

0 comments on commit 29c2e3a

Please sign in to comment.