Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support one-off percentage overrides for user operations #289

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions packages/alchemy/src/middleware/gas-fees.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import { applyFeeOption, type BigNumberish } from "@alchemy/aa-core";
import { applyFeeOption, applyUserOperationOverride } from "@alchemy/aa-core";
import type { AlchemyProvider } from "../provider.js";
import type { ClientWithAlchemyMethods } from "./client.js";

export const withAlchemyGasFeeEstimator = (
provider: AlchemyProvider
): AlchemyProvider => {
provider.withFeeDataGetter(async (struct, overrides, feeOptions) => {
const maxPriorityFeePerGas =
overrides?.maxPriorityFeePerGas != null
? overrides?.maxPriorityFeePerGas
: // it's a fair assumption that if someone is using this Alchemy Middleware, then they are using Alchemy RPC
applyFeeOption(
await (provider.rpcClient as ClientWithAlchemyMethods).request({
method: "rundler_maxPriorityFeePerGas",
params: [],
}),
feeOptions?.maxPriorityFeePerGas
);
let [block, maxPriorityFeePerGasEstimate] = await Promise.all([
provider.rpcClient.getBlock({ blockTag: "latest" }),
// it's a fair assumption that if someone is using this Alchemy Middleware, then they are using Alchemy RPC
(provider.rpcClient as ClientWithAlchemyMethods).request({
method: "rundler_maxPriorityFeePerGas",
params: [],
}),
]);
const baseFeePerGas = block.baseFeePerGas;
if (baseFeePerGas == null) {
throw new Error("baseFeePerGas is null");
}

const estimateMaxFeePerGas = async (priorityFeePerGas: BigNumberish) => {
const block = await provider.rpcClient.getBlock({ blockTag: "latest" });
const baseFeePerGas = block.baseFeePerGas;
if (baseFeePerGas == null) {
throw new Error("baseFeePerGas is null");
}
return applyFeeOption(
baseFeePerGas + BigInt(priorityFeePerGas),
feeOptions?.maxFeePerGas
const maxPriorityFeePerGas =
applyUserOperationOverride(
maxPriorityFeePerGasEstimate,
overrides?.maxPriorityFeePerGas
) ||
applyFeeOption(
maxPriorityFeePerGasEstimate,
feeOptions?.maxPriorityFeePerGas
);
};

const maxFeePerGas =
overrides?.maxFeePerGas != null
? overrides?.maxFeePerGas
: await estimateMaxFeePerGas(maxPriorityFeePerGas);
applyUserOperationOverride(
baseFeePerGas + BigInt(maxPriorityFeePerGas),
overrides?.maxPriorityFeePerGas
) ||
applyFeeOption(
baseFeePerGas + BigInt(maxPriorityFeePerGas),
feeOptions?.maxPriorityFeePerGas
);

return {
...struct,
Expand Down
17 changes: 14 additions & 3 deletions packages/alchemy/src/middleware/gas-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
deepHexlify,
filterUndefined,
isBigNumberish,
isPercentage,
resolveProperties,
type AccountMiddlewareFn,
Expand Down Expand Up @@ -169,15 +170,25 @@ const withAlchemyGasAndPaymasterAndDataMiddleware = <P extends AlchemyProvider>(
const overrideField = (
field: keyof UserOperationFeeOptions
): Hex | Percentage | undefined => {
// one-off absolute override
if (overrides?.[field] != null) {
return deepHexlify(overrides[field]);
// one-off absolute override
if (isBigNumberish(overrides[field])) {
return deepHexlify(overrides[field]);
}
// one-off percentage overrides
else {
return {
percentage:
100 + Number((overrides[field] as Percentage).percentage),
};
}
}

// provider level fee options with percentage
if (isPercentage(feeOptions?.[field])) {
return {
percentage: 100 + Number(feeOptions?.[field]?.percentage),
percentage:
100 + Number((feeOptions![field] as Percentage).percentage),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type * from "./utils/index.js";
export {
ChainSchema,
applyFeeOption,
applyUserOperationOverride,
asyncPipe,
bigIntMax,
bigIntPercent,
Expand Down
110 changes: 54 additions & 56 deletions packages/core/src/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from "../types.js";
import {
applyFeeOption,
applyUserOperationOverride,
asyncPipe,
bigIntMax,
bigIntPercent,
Expand Down Expand Up @@ -521,41 +522,39 @@ export class SmartAccountProvider<
overrides,
feeOptions
) => {
let { callGasLimit, verificationGasLimit, preVerificationGas } =
overrides ?? {};
const request = deepHexlify(await resolveProperties(struct));
const estimates = await this.rpcClient.estimateUserOperationGas(
request,
this.getEntryPointAddress()
);

if (
callGasLimit == null ||
verificationGasLimit == null ||
preVerificationGas == null
) {
const request = deepHexlify(await resolveProperties(struct));
const estimates = await this.rpcClient.estimateUserOperationGas(
request,
this.getEntryPointAddress()
const callGasLimit =
applyUserOperationOverride(
estimates.callGasLimit,
overrides?.callGasLimit
) || applyFeeOption(estimates.callGasLimit, feeOptions?.callGasLimit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you wrap this in a util? seems repeated below but generalizable on input

const verificationGasLimit =
applyUserOperationOverride(
estimates.verificationGasLimit,
overrides?.verificationGasLimit
) ||
applyFeeOption(
estimates.verificationGasLimit,
feeOptions?.verificationGasLimit
);
const preVerificationGas =
applyUserOperationOverride(
estimates.preVerificationGas,
overrides?.preVerificationGas
) ||
applyFeeOption(
estimates.preVerificationGas,
feeOptions?.preVerificationGas
);

callGasLimit =
callGasLimit ??
applyFeeOption(estimates.callGasLimit, feeOptions?.callGasLimit);
verificationGasLimit =
verificationGasLimit ??
applyFeeOption(
estimates.verificationGasLimit,
feeOptions?.verificationGasLimit
);
preVerificationGas =
preVerificationGas ??
applyFeeOption(
estimates.preVerificationGas,
feeOptions?.preVerificationGas
);
}

struct.callGasLimit = callGasLimit;
struct.verificationGasLimit = verificationGasLimit;
struct.preVerificationGas = preVerificationGas;

return struct;
};

Expand All @@ -564,11 +563,6 @@ export class SmartAccountProvider<
overrides,
feeOptions
) => {
const estimateMaxPriorityFeePerGas = async () => {
const estimate = await this.rpcClient.estimateMaxPriorityFeePerGas();
return applyFeeOption(estimate, feeOptions?.maxPriorityFeePerGas);
};

// maxFeePerGas must be at least the sum of maxPriorityFeePerGas and baseFee
// so we need to accommodate for the fee option applied maxPriorityFeePerGas for the maxFeePerGas
//
Expand All @@ -577,30 +571,34 @@ export class SmartAccountProvider<
//
// Refer to https://docs.alchemy.com/docs/maxpriorityfeepergas-vs-maxfeepergas
// for more information about maxFeePerGas and maxPriorityFeePerGas
const estimateMaxFeePerGas = async (maxPriorityFeePerGas: BigNumberish) => {
const feeData = await this.rpcClient.estimateFeesPerGas();
if (!feeData.maxFeePerGas || !feeData.maxPriorityFeePerGas) {
throw new Error(
"feeData is missing maxFeePerGas or maxPriorityFeePerGas"
);
}
const baseFee = applyFeeOption(
feeData.maxFeePerGas - feeData.maxPriorityFeePerGas,
feeOptions?.maxFeePerGas
);

return BigInt(baseFee) + BigInt(maxPriorityFeePerGas);
};

struct.maxPriorityFeePerGas =
overrides?.maxPriorityFeePerGas != null
? overrides?.maxPriorityFeePerGas
: await estimateMaxPriorityFeePerGas();
struct.maxFeePerGas =
overrides?.maxFeePerGas != null
? overrides?.maxFeePerGas
: await estimateMaxFeePerGas(struct.maxPriorityFeePerGas);
const feeData = await this.rpcClient.estimateFeesPerGas();
if (!feeData.maxFeePerGas || !feeData.maxPriorityFeePerGas) {
throw new Error(
"feeData is missing maxFeePerGas or maxPriorityFeePerGas"
);
}

let maxPriorityFeePerGas: BigNumberish =
await this.rpcClient.estimateMaxPriorityFeePerGas();
maxPriorityFeePerGas =
applyUserOperationOverride(
maxPriorityFeePerGas,
overrides?.maxPriorityFeePerGas
) ||
applyFeeOption(maxPriorityFeePerGas, feeOptions?.maxPriorityFeePerGas);

let maxFeePerGas: BigNumberish =
feeData.maxFeePerGas -
feeData.maxPriorityFeePerGas +
BigInt(maxPriorityFeePerGas);

maxFeePerGas =
applyUserOperationOverride(maxFeePerGas, overrides?.maxFeePerGas) ||
applyFeeOption(maxFeePerGas, feeOptions?.maxFeePerGas);

struct.maxFeePerGas = maxFeePerGas;
struct.maxPriorityFeePerGas = maxPriorityFeePerGas;
return struct;
};

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ export interface ISmartAccountProvider<
* prior to execution.
*
* @param override - a function for overriding the default feeDataGetter middleware
* @param feeOptions - optional FeeDataFeeOptions to set at the global level of the provider.
* @returns
*/
withFeeDataGetter: (override: FeeDataMiddleware) => this;
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ export type UserOperationFeeOptions = z.infer<
typeof UserOperationFeeOptionsSchema
>;

export type UserOperationOverrides = Partial<
Pick<
UserOperationStruct,
| "callGasLimit"
| "maxFeePerGas"
| "maxPriorityFeePerGas"
| "paymasterAndData"
| "preVerificationGas"
| "verificationGasLimit"
>
>;
export type UserOperationOverrides = Partial<{
callGasLimit: UserOperationStruct["callGasLimit"] | Percentage;
maxFeePerGas: UserOperationStruct["maxFeePerGas"] | Percentage;
maxPriorityFeePerGas:
| UserOperationStruct["maxPriorityFeePerGas"]
| Percentage;
preVerificationGas: UserOperationStruct["preVerificationGas"] | Percentage;
verificationGasLimit:
| UserOperationStruct["verificationGasLimit"]
| Percentage;
paymasterAndData: UserOperationStruct["paymasterAndData"];
}>;

// represents the request as it needs to be formatted for RPC requests
export interface UserOperationRequest {
Expand Down
25 changes: 22 additions & 3 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,33 @@ export function deepHexlify(obj: any): any {
);
}

export function applyUserOperationOverride(
value: BigNumberish | undefined,
override?: BigNumberish | Percentage
): BigNumberish | undefined {
if (override == null) {
return value;
}

if (isBigNumberish(override)) {
return override;
}
// percentage override
else {
return value != null
? bigIntPercent(value, BigInt(100 + override.percentage))
: value;
}
}

export function applyFeeOption(
value: BigNumberish | undefined,
feeOption?: UserOperationFeeOptionsField
): BigNumberish {
if (feeOption == null) {
return value ?? 0n;
}
return value
return value != null
? bigIntClamp(
feeOption.percentage
? bigIntPercent(value, BigInt(100 + feeOption.percentage))
Expand Down Expand Up @@ -183,11 +202,11 @@ export function defineReadOnly<T, K extends keyof T>(
}

export function isBigNumberish(x: any): x is BigNumberish {
return BigNumberishSchema.safeParse(x).success;
return x != null && BigNumberishSchema.safeParse(x).success;
}

export function isPercentage(x: any): x is Percentage {
return PercentageSchema.safeParse(x).success;
return x != null && PercentageSchema.safeParse(x).success;
}

export function filterUndefined(
Expand Down
Loading