-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alchemy): use rundler_getLocalRequiredFees for fee estimate
- Loading branch information
Showing
8 changed files
with
72 additions
and
168 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
type PublicErc4337Client, | ||
type UserOperationRequest, | ||
} from "@alchemy/aa-core"; | ||
import type { Address, Hex } from "viem"; | ||
|
||
export type ClientWithAlchemyMethods = PublicErc4337Client & { | ||
request: PublicErc4337Client["request"] & | ||
{ | ||
request(args: { | ||
method: "alchemy_requestPaymasterAndData"; | ||
params: [ | ||
{ | ||
policyId: string; | ||
entryPoint: Address; | ||
userOperation: UserOperationRequest; | ||
} | ||
]; | ||
}): Promise<{ paymasterAndData: Hex }>; | ||
|
||
request(args: { | ||
method: "alchemy_requestGasAndPaymasterAndData"; | ||
params: [ | ||
{ | ||
policyId: string; | ||
entryPoint: Address; | ||
userOperation: UserOperationRequest; | ||
dummySignature: Hex; | ||
} | ||
]; | ||
}): Promise<{ | ||
paymasterAndData: Hex; | ||
callGasLimit: Hex; | ||
verificationGasLimit: Hex; | ||
preVerificationGas: Hex; | ||
maxFeePerGas: Hex; | ||
maxPriorityFeePerGas: Hex; | ||
}>; | ||
|
||
request(args: { | ||
method: "rundler_getLocalRequiredFees"; | ||
params: []; | ||
}): Promise<{ | ||
blockNumber: Hex; | ||
baseFeePerGas: Hex; | ||
minimum: { | ||
maxFeePerGas: Hex; | ||
maxPriorityFeePerGas: Hex; | ||
}; | ||
recommended: { | ||
maxFeePerGas: Hex; | ||
maxPriorityFeePerGas: Hex; | ||
}; | ||
}>; | ||
}["request"]; | ||
}; |
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,55 +1,17 @@ | ||
import type { AlchemyProvider } from "../provider.js"; | ||
|
||
export enum GasFeeStrategy { | ||
DEFAULT = "DEFAULT", | ||
PERCENT_OF_BASE_FEE = "PERCENT_OF_BASE_FEE", | ||
PRIORITY_FEE_INCREASE_PERCENT = "PRIORITY_FEE_INCREASE_PERCENT", | ||
} | ||
|
||
export interface GasFeeMode { | ||
strategy: GasFeeStrategy; | ||
value: bigint; | ||
} | ||
import type { ClientWithAlchemyMethods } from "./client.js"; | ||
|
||
export const withAlchemyGasFeeEstimator = ( | ||
provider: AlchemyProvider, | ||
feeMode: GasFeeMode, | ||
maxPriorityFeeBufferPercent: bigint | ||
provider: AlchemyProvider | ||
): AlchemyProvider => { | ||
if (feeMode.strategy === GasFeeStrategy.DEFAULT) { | ||
return provider; | ||
} | ||
|
||
provider.withFeeDataGetter(async () => { | ||
const block = await provider.rpcClient.getBlock({ blockTag: "latest" }); | ||
const baseFeePerGas = block.baseFeePerGas; | ||
if (baseFeePerGas == null) { | ||
throw new Error("baseFeePerGas is null"); | ||
} | ||
// add a buffer here to account for potential spikes in priority fee | ||
const maxPriorityFeePerGas = | ||
(BigInt(await provider.rpcClient.getMaxPriorityFeePerGas()) * | ||
(100n + maxPriorityFeeBufferPercent)) / | ||
100n; | ||
// add 25% overhead to ensure mine | ||
const baseFeeScaled = (baseFeePerGas * 5n) / 4n; | ||
|
||
const prioFee = ((): bigint => { | ||
switch (feeMode.strategy) { | ||
case GasFeeStrategy.PERCENT_OF_BASE_FEE: | ||
return (baseFeeScaled * feeMode.value) / 100n; | ||
case GasFeeStrategy.PRIORITY_FEE_INCREASE_PERCENT: | ||
// add 10% to required priority fee to ensure mine | ||
return (maxPriorityFeePerGas * (100n + feeMode.value)) / 100n; | ||
default: | ||
throw new Error("fee mode not supported"); | ||
} | ||
})(); | ||
|
||
return { | ||
maxPriorityFeePerGas: prioFee, | ||
maxFeePerGas: baseFeeScaled + prioFee, | ||
}; | ||
const result = await ( | ||
provider.rpcClient as ClientWithAlchemyMethods | ||
).request({ | ||
method: "rundler_getLocalRequiredFees", | ||
params: [], | ||
}); | ||
return result.recommended; | ||
}); | ||
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