-
Notifications
You must be signed in to change notification settings - Fork 96
Add estimateErc20PaymasterCost #426
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b133f11
Add estimateErc20PaymasterCost
plusminushalf 9d26992
Fix build
plusminushalf 3f5a513
Change costInUsd to number
plusminushalf da632c4
Take explicit token
plusminushalf cd7e72e
Fix build
plusminushalf 4f63640
Simplify the logic & use getRequiredPrefund
plusminushalf 5a3af22
fix import
plusminushalf 1759735
return costInUsd in bigint
plusminushalf ba0e2bd
Added changeset
plusminushalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
packages/permissionless/actions/pimlico/estimateErc20PaymasterCost.ts
This file contains hidden or 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,154 @@ | ||
| import { | ||
| type Account, | ||
| type Address, | ||
| type Chain, | ||
| ChainNotFoundError, | ||
| type Client, | ||
| type GetChainParameter, | ||
| type Transport, | ||
| getAddress, | ||
| slice | ||
| } from "viem" | ||
| import type { EntryPointVersion, UserOperation } from "viem/account-abstraction" | ||
| import { getAction } from "viem/utils" | ||
| import type { PimlicoRpcSchema } from "../../types/pimlico.js" | ||
| import { getTokenQuotes } from "./getTokenQuotes" | ||
|
Check failure on line 15 in packages/permissionless/actions/pimlico/estimateErc20PaymasterCost.ts
|
||
|
|
||
| /** | ||
| * @costInToken represents the max amount of token that will be charged for this user operation in token decimals | ||
| * @costInUsd represents the max amount of USD value of the token in 10^6 decimals | ||
| */ | ||
| export type EstimateErc20PaymasterCostReturnType = { | ||
| costInToken: bigint | ||
| costInUsd: bigint | ||
| } | ||
|
|
||
| export type EstimateErc20PaymasterCostParameters< | ||
| entryPointVersion extends EntryPointVersion, | ||
| TChain extends Chain | undefined, | ||
| TChainOverride extends Chain | undefined = Chain | undefined | ||
| > = { | ||
| entryPoint: { version: entryPointVersion; address: Address } | ||
| userOperation: UserOperation<entryPointVersion> | ||
| } & GetChainParameter<TChain, TChainOverride> | ||
|
|
||
| /** | ||
| * Returns all related fields to calculate the potential cost of a userOperation in ERC-20 tokens. | ||
| * | ||
| * - Docs: https://docs.pimlico.io/permissionless/reference/pimlico-bundler-actions/EstimateErc20PaymasterCost | ||
| * | ||
| * @param client that you created using viem's createClient whose transport url is pointing to the Pimlico's bundler. | ||
| * @returns slow, standard & fast values for maxFeePerGas & maxPriorityFeePerGas | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this comment can be removed |
||
| * @returns quotes, see {@link EstimateErc20PaymasterCostReturnType} | ||
| * | ||
| */ | ||
| export const estimateErc20PaymasterCost = async < | ||
| entryPointVersion extends EntryPointVersion, | ||
| TChain extends Chain | undefined, | ||
| TChainOverride extends Chain | undefined = Chain | undefined | ||
| >( | ||
| client: Client< | ||
| Transport, | ||
| TChain, | ||
| Account | undefined, | ||
| PimlicoRpcSchema<entryPointVersion> | ||
| >, | ||
| args: EstimateErc20PaymasterCostParameters< | ||
| entryPointVersion, | ||
| TChain, | ||
| TChainOverride | ||
| > | ||
| ): Promise<EstimateErc20PaymasterCostReturnType> => { | ||
| const chain = args.chain ?? client.chain | ||
|
|
||
| if (!chain) { | ||
| throw new ChainNotFoundError() | ||
| } | ||
|
|
||
| const { entryPoint, userOperation } = args | ||
|
|
||
| const paymasterData = (() => { | ||
| if ("paymasterAndData" in userOperation) { | ||
| return userOperation.paymasterAndData | ||
| } | ||
|
|
||
| if ("paymasterData" in userOperation) { | ||
| return userOperation.paymasterData | ||
| } | ||
|
|
||
| return undefined | ||
| })() | ||
|
|
||
| if (!paymasterData || paymasterData === "0x") { | ||
| throw new Error("Paymaster data not found in the user operation.") | ||
| } | ||
|
|
||
| const token: Address = (() => { | ||
| try { | ||
| if (entryPoint.version === "0.6") { | ||
| return getAddress(slice(paymasterData, 34, 54)) | ||
| } | ||
|
|
||
| return getAddress(slice(paymasterData, 46, 66)) | ||
| } catch { | ||
| throw new Error( | ||
| "Invalid paymaster data, cannot find token address." | ||
| ) | ||
| } | ||
| })() | ||
|
|
||
| const quotes = await getAction( | ||
| client, | ||
| getTokenQuotes, | ||
| "getTokenQuotes" | ||
| )({ | ||
| tokens: [token], | ||
| entryPointAddress: entryPoint.address, | ||
| chain | ||
| }) | ||
|
|
||
| const postOpGas: bigint = quotes[0].postOpGas | ||
| const exchangeRate: bigint = quotes[0].exchangeRate | ||
| const exchangeRateNativeToUsd: bigint = quotes[0].exchangeRateNativeToUsd | ||
|
|
||
| const userOperationMaxGas = (() => { | ||
| const paymasterVerificationGasLimit = | ||
| "paymasterVerificationGasLimit" in userOperation | ||
| ? (userOperation.paymasterVerificationGasLimit ?? 0n) | ||
| : 0n | ||
|
|
||
| const paymasterPostOpGasLimit = | ||
| "paymasterPostOpGasLimit" in userOperation | ||
| ? (userOperation.paymasterPostOpGasLimit ?? 0n) | ||
| : 0n | ||
|
|
||
| const { preVerificationGas, verificationGasLimit, callGasLimit } = | ||
| userOperation | ||
|
|
||
| return ( | ||
| BigInt(preVerificationGas) + | ||
| BigInt(verificationGasLimit) + | ||
| BigInt(callGasLimit) + | ||
| paymasterVerificationGasLimit + | ||
| paymasterPostOpGasLimit | ||
| ) | ||
| })() | ||
|
|
||
| const userOperationMaxCost = | ||
| userOperationMaxGas * userOperation.maxFeePerGas | ||
|
|
||
| // represents the userOperation's max cost in denomination of wei | ||
| const maxCostInWei = | ||
| userOperationMaxCost + postOpGas * userOperation.maxFeePerGas | ||
|
|
||
| // represents the userOperation's max cost in token denomination (wei) | ||
| const costInToken = (maxCostInWei * exchangeRate) / BigInt(1e18) | ||
|
|
||
| // represents the userOperation's max cost in usd (with 6 decimals of precision) | ||
| const costInUsd = (maxCostInWei * exchangeRateNativeToUsd) / 10n ** 18n | ||
|
|
||
| return { | ||
| costInToken, | ||
| costInUsd | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering if it make more sense to have this as type number