-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support one-off percentage overrides for user operations
- Loading branch information
Showing
9 changed files
with
143 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,48 @@ | ||
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, | ||
maxPriorityFeePerGas, | ||
maxFeePerGas, | ||
}; | ||
}, provider.feeOptions); | ||
}); | ||
return provider; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.