|
| 1 | +import { |
| 2 | + type Account, |
| 3 | + type Address, |
| 4 | + type Chain, |
| 5 | + ChainNotFoundError, |
| 6 | + type Client, |
| 7 | + type GetChainParameter, |
| 8 | + type Transport |
| 9 | +} from "viem" |
| 10 | +import type { EntryPointVersion, UserOperation } from "viem/account-abstraction" |
| 11 | +import { getAction } from "viem/utils" |
| 12 | +import type { PimlicoRpcSchema } from "../../types/pimlico.js" |
| 13 | +import { getRequiredPrefund } from "../../utils/getRequiredPrefund.js" |
| 14 | +import { getTokenQuotes } from "./getTokenQuotes.js" |
| 15 | + |
| 16 | +/** |
| 17 | + * @costInToken represents the max amount of token that will be charged for this user operation in token decimals |
| 18 | + * @costInUsd represents the max amount of USD value of the token in 10^6 decimals |
| 19 | + */ |
| 20 | +export type EstimateErc20PaymasterCostReturnType = { |
| 21 | + costInToken: bigint |
| 22 | + costInUsd: bigint |
| 23 | +} |
| 24 | + |
| 25 | +export type EstimateErc20PaymasterCostParameters< |
| 26 | + entryPointVersion extends EntryPointVersion, |
| 27 | + TChain extends Chain | undefined, |
| 28 | + TChainOverride extends Chain | undefined = Chain | undefined |
| 29 | +> = { |
| 30 | + entryPoint: { version: entryPointVersion; address: Address } |
| 31 | + userOperation: UserOperation<entryPointVersion> |
| 32 | + token: Address |
| 33 | +} & GetChainParameter<TChain, TChainOverride> |
| 34 | + |
| 35 | +/** |
| 36 | + * Returns all related fields to calculate the potential cost of a userOperation in ERC-20 tokens. |
| 37 | + * |
| 38 | + * - Docs: https://docs.pimlico.io/permissionless/reference/pimlico-bundler-actions/EstimateErc20PaymasterCost |
| 39 | + * |
| 40 | + * @param client that you created using viem's createClient whose transport url is pointing to the Pimlico's bundler. |
| 41 | + * @returns quotes, see {@link EstimateErc20PaymasterCostReturnType} |
| 42 | + * |
| 43 | + */ |
| 44 | +export const estimateErc20PaymasterCost = async < |
| 45 | + entryPointVersion extends EntryPointVersion, |
| 46 | + TChain extends Chain | undefined, |
| 47 | + TChainOverride extends Chain | undefined = Chain | undefined |
| 48 | +>( |
| 49 | + client: Client< |
| 50 | + Transport, |
| 51 | + TChain, |
| 52 | + Account | undefined, |
| 53 | + PimlicoRpcSchema<entryPointVersion> |
| 54 | + >, |
| 55 | + args: EstimateErc20PaymasterCostParameters< |
| 56 | + entryPointVersion, |
| 57 | + TChain, |
| 58 | + TChainOverride |
| 59 | + > |
| 60 | +): Promise<EstimateErc20PaymasterCostReturnType> => { |
| 61 | + const chain = args.chain ?? client.chain |
| 62 | + |
| 63 | + if (!chain) { |
| 64 | + throw new ChainNotFoundError() |
| 65 | + } |
| 66 | + |
| 67 | + const { entryPoint, userOperation, token } = args |
| 68 | + |
| 69 | + const quotes = await getAction( |
| 70 | + client, |
| 71 | + getTokenQuotes, |
| 72 | + "getTokenQuotes" |
| 73 | + )({ |
| 74 | + tokens: [token], |
| 75 | + entryPointAddress: entryPoint.address, |
| 76 | + chain |
| 77 | + }) |
| 78 | + |
| 79 | + const postOpGas = quotes[0].postOpGas |
| 80 | + const exchangeRate = quotes[0].exchangeRate |
| 81 | + const exchangeRateNativeToUsd = quotes[0].exchangeRateNativeToUsd |
| 82 | + |
| 83 | + const userOperationMaxCost = getRequiredPrefund({ |
| 84 | + userOperation, |
| 85 | + entryPointVersion: entryPoint.version |
| 86 | + }) |
| 87 | + |
| 88 | + // represents the userOperation's max cost in denomination of wei |
| 89 | + const maxCostInWei = |
| 90 | + userOperationMaxCost + postOpGas * userOperation.maxFeePerGas |
| 91 | + |
| 92 | + // represents the userOperation's max cost in token denomination (wei) |
| 93 | + const costInToken = (maxCostInWei * exchangeRate) / BigInt(1e18) |
| 94 | + |
| 95 | + // represents the userOperation's max cost in usd (with 6 decimals of precision) |
| 96 | + const costInUsd = (maxCostInWei * exchangeRateNativeToUsd) / 10n ** 18n |
| 97 | + |
| 98 | + return { |
| 99 | + costInToken, |
| 100 | + costInUsd |
| 101 | + } |
| 102 | +} |
0 commit comments