-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathestimateErc20PaymasterCost.ts
More file actions
102 lines (90 loc) · 3.24 KB
/
Copy pathestimateErc20PaymasterCost.ts
File metadata and controls
102 lines (90 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
type Account,
type Address,
type Chain,
ChainNotFoundError,
type Client,
type GetChainParameter,
type Transport
} from "viem"
import type { EntryPointVersion, UserOperation } from "viem/account-abstraction"
import { getAction } from "viem/utils"
import type { PimlicoRpcSchema } from "../../types/pimlico.js"
import { getRequiredPrefund } from "../../utils/getRequiredPrefund.js"
import { getTokenQuotes } from "./getTokenQuotes.js"
/**
* @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>
token: Address
} & 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 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, token } = args
const quotes = await getAction(
client,
getTokenQuotes,
"getTokenQuotes"
)({
tokens: [token],
entryPointAddress: entryPoint.address,
chain
})
const postOpGas = quotes[0].postOpGas
const exchangeRate = quotes[0].exchangeRate
const exchangeRateNativeToUsd = quotes[0].exchangeRateNativeToUsd
const userOperationMaxCost = getRequiredPrefund({
userOperation,
entryPointVersion: entryPoint.version
})
// 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) / 1_000_000_000_000_000_000n
// represents the userOperation's max cost in usd (with 6 decimals of precision)
const costInUsd = (maxCostInWei * exchangeRateNativeToUsd) / 1_000_000_000_000_000_000n
return {
costInToken,
costInUsd
}
}