Skip to content

Commit

Permalink
refactor: enhance pre-hooks with additional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1NotMe committed Oct 9, 2024
1 parent 5794f0a commit 8277a93
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 33 deletions.
26 changes: 17 additions & 9 deletions src/backend/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ export abstract class BaseBackend implements IBackend {
): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[userOp, hints] = await hooksController.preSubmitUserOperation(
[userOp, hints, extra] = await hooksController.preSubmitUserOperation(
userOp,
hints,
extra,
);
}

// Implemented by subclass
let userOpHash = await this._submitUserOperation(userOp, hints, extra);

Expand All @@ -148,10 +148,13 @@ export abstract class BaseBackend implements IBackend {
): Promise<SolverOperation[]> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
[userOp, userOpHash] = await hooksController.preGetSolverOperations(
userOp,
userOpHash,
);
[userOp, userOpHash, wait, extra] =
await hooksController.preGetSolverOperations(
userOp,
userOpHash,
wait,
extra,
);
}

// Implemented by subclass
Expand All @@ -176,7 +179,7 @@ export abstract class BaseBackend implements IBackend {
async submitBundle(bundle: Bundle, extra?: any): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
bundle = await hooksController.preSubmitBundle(bundle);
[bundle, extra] = await hooksController.preSubmitBundle(bundle, extra);
}

// Implemented by subclass
Expand All @@ -197,7 +200,11 @@ export abstract class BaseBackend implements IBackend {
): Promise<string> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
userOpHash = await hooksController.preGetBundleHash(userOpHash);
[userOpHash, wait, extra] = await hooksController.preGetBundleHash(
userOpHash,
wait,
extra,
);
}

// Implemented by subclass
Expand All @@ -219,7 +226,8 @@ export abstract class BaseBackend implements IBackend {
): Promise<Bundle> {
// Pre hooks
for (const hooksController of this.hooksControllers) {
userOp = await hooksController.preGetBundleForUserOp(userOp);
[userOp, hints, wait, extra] =
await hooksController.preGetBundleForUserOp(userOp, hints, wait, extra);
}

// Implemented by subclass
Expand Down
54 changes: 39 additions & 15 deletions src/backend/hooks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface IHooksController {
preSubmitUserOperation(
userOp: UserOperation,
hints: string[],
): Promise<[UserOperation, string[]]>;
extra?: any,
): Promise<[UserOperation, string[], any]>;

postSubmitUserOperation(
userOp: UserOperation,
Expand All @@ -15,22 +16,33 @@ export interface IHooksController {
preGetSolverOperations(
userOp: UserOperation,
userOphash: string,
): Promise<[UserOperation, string]>;
wait?: boolean,
extra?: any,
): Promise<[UserOperation, string, boolean, any]>;

postGetSolverOperations(
userOp: UserOperation,
solverOps: SolverOperation[],
): Promise<[UserOperation, SolverOperation[]]>;

preSubmitBundle(bundleOps: Bundle): Promise<Bundle>;
preSubmitBundle(bundle: Bundle, extra?: any): Promise<[Bundle, any]>;

postSubmitBundle(result: string): Promise<string>;

preGetBundleHash(userOphash: string): Promise<string>;
preGetBundleHash(
userOphash: string,
wait?: boolean,
extra?: any,
): Promise<[string, boolean, any]>;

postGetBundleHash(atlasTxHash: string): Promise<string>;

preGetBundleForUserOp(userOp: UserOperation): Promise<UserOperation>;
preGetBundleForUserOp(
userOp: UserOperation,
hints: string[],
wait?: boolean,
extra?: any,
): Promise<[UserOperation, string[], boolean | undefined, any]>;

postGetBundleForUserOp(bundle: Bundle): Promise<Bundle>;
}
Expand All @@ -48,8 +60,9 @@ export abstract class BaseHooksController implements IHooksController {
async preSubmitUserOperation(
userOp: UserOperation,
hints: string[],
): Promise<[UserOperation, string[]]> {
return [userOp, hints];
extra?: any,
): Promise<[UserOperation, string[], any]> {
return [userOp, hints, extra];
}

async postSubmitUserOperation(
Expand All @@ -62,8 +75,10 @@ export abstract class BaseHooksController implements IHooksController {
async preGetSolverOperations(
userOp: UserOperation,
userOphash: string,
): Promise<[UserOperation, string]> {
return [userOp, userOphash];
wait?: boolean,
extra?: any,
): Promise<[UserOperation, string, boolean, any]> {
return [userOp, userOphash, wait || false, extra];
}

async postGetSolverOperations(
Expand All @@ -73,24 +88,33 @@ export abstract class BaseHooksController implements IHooksController {
return [userOp, solverOps];
}

async preSubmitBundle(bundleOps: Bundle): Promise<Bundle> {
return bundleOps;
async preSubmitBundle(bundle: Bundle, extra?: any): Promise<[Bundle, any]> {
return [bundle, extra];
}

async postSubmitBundle(result: string): Promise<string> {
return result;
}

async preGetBundleHash(userOphash: string): Promise<string> {
return userOphash;
async preGetBundleHash(
userOphash: string,
wait: boolean,
extra?: any,
): Promise<[string, boolean, any]> {
return [userOphash, wait, extra];
}

async postGetBundleHash(atlasTxHash: string): Promise<string> {
return atlasTxHash;
}

async preGetBundleForUserOp(userOp: UserOperation): Promise<UserOperation> {
return userOp;
async preGetBundleForUserOp(
userOp: UserOperation,
hints: string[],
wait?: boolean,
extra?: any,
): Promise<[UserOperation, string[], boolean | undefined, any]> {
return [userOp, hints, wait, extra];
}

async postGetBundleForUserOp(bundle: Bundle): Promise<Bundle> {
Expand Down
12 changes: 8 additions & 4 deletions src/backend/hooks/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class SimulationHooksController extends BaseHooksController {
async preSubmitUserOperation(
userOp: UserOperation,
hints: string[],
): Promise<[UserOperation, string[]]> {
extra?: any,
): Promise<[UserOperation, string[], any]> {
const [success, result, validCallsResult] = await this.simulator
.getFunction("simUserOperation")
.staticCall(userOp.toStruct());
Expand All @@ -57,7 +58,7 @@ export class SimulationHooksController extends BaseHooksController {
);
}

return [userOp, hints];
return [userOp, hints, extra];
}

async postGetSolverOperations(
Expand Down Expand Up @@ -141,7 +142,10 @@ export class SimulationHooksController extends BaseHooksController {
return [userOp, simulatedSolverOps];
}

async preSubmitBundle(bundleOps: Bundle): Promise<Bundle> {
async preSubmitBundle(
bundleOps: Bundle,
extra?: any,
): Promise<[Bundle, any]> {
// Simulation will throw if the bundle is invalid
await this.atlas
.connect(
Expand All @@ -157,6 +161,6 @@ export class SimulationHooksController extends BaseHooksController {
bundleOps.dAppOperation.toStruct(),
);

return bundleOps;
return [bundleOps, extra];
}
}
33 changes: 28 additions & 5 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,27 +358,47 @@ describe("Atlas SDK main tests", () => {
const mockHooksController = {
preSubmitUserOperation: jest
.fn()
.mockImplementation(async (userOp, hints) => [userOp, hints]),
.mockImplementation(async (userOp, hints, extra) => [
userOp,
hints,
extra,
]),
postSubmitUserOperation: jest
.fn()
.mockImplementation(async (userOp, userOpHash) => [userOp, userOpHash]),
preGetSolverOperations: jest
.fn()
.mockImplementation(async (userOp, userOpHash) => [userOp, userOpHash]),
.mockImplementation(async (userOp, userOpHash, wait, extra) => [
userOp,
userOpHash,
wait,
extra,
]),
postGetSolverOperations: jest
.fn()
.mockImplementation(async (userOp, solverOps) => [userOp, solverOps]),
preSubmitBundle: jest.fn().mockImplementation(async (bundle) => bundle),
preSubmitBundle: jest
.fn()
.mockImplementation(async (bundle, extra) => [bundle, extra]),
postSubmitBundle: jest.fn().mockImplementation(async (result) => result),
preGetBundleHash: jest
.fn()
.mockImplementation(async (userOpHash) => userOpHash),
.mockImplementation(async (userOpHash, wait, extra) => [
userOpHash,
wait,
extra,
]),
postGetBundleHash: jest
.fn()
.mockImplementation(async (atlasTxHash) => atlasTxHash),
preGetBundleForUserOp: jest
.fn()
.mockImplementation(async (userOp) => userOp),
.mockImplementation(async (userOp, hints, wait, extra) => [
userOp,
hints,
wait,
extra,
]),
postGetBundleForUserOp: jest
.fn()
.mockImplementation(async (bundle) => bundle),
Expand All @@ -401,6 +421,9 @@ describe("Atlas SDK main tests", () => {

expect(mockHooksController.preGetBundleForUserOp).toHaveBeenCalledWith(
userOp,
[],
true,
{ chainId: chainId },
);
expect(mockHooksController.postGetBundleForUserOp).toHaveBeenCalled();
});
Expand Down

0 comments on commit 8277a93

Please sign in to comment.