-
-
Notifications
You must be signed in to change notification settings - Fork 913
/
estimateFeesPerGas.ts
178 lines (163 loc) · 5.63 KB
/
estimateFeesPerGas.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import {
BaseFeeScalarError,
type BaseFeeScalarErrorType,
Eip1559FeesNotSupportedError,
type Eip1559FeesNotSupportedErrorType,
} from '../../errors/fee.js'
import type { ErrorType } from '../../errors/utils.js'
import type { Account } from '../../types/account.js'
import type { Block } from '../../types/block.js'
import type {
Chain,
ChainEstimateFeesPerGasFnParameters,
ChainFeesFnParameters,
GetChainParameter,
} from '../../types/chain.js'
import type {
FeeValuesEIP1559,
FeeValuesLegacy,
FeeValuesType,
} from '../../types/fee.js'
import { getAction } from '../../utils/getAction.js'
import type { PrepareTransactionRequestParameters } from '../wallet/prepareTransactionRequest.js'
import {
type EstimateMaxPriorityFeePerGasErrorType,
internal_estimateMaxPriorityFeePerGas,
} from './estimateMaxPriorityFeePerGas.js'
import { getBlock } from './getBlock.js'
import { type GetGasPriceErrorType, getGasPrice } from './getGasPrice.js'
export type EstimateFeesPerGasParameters<
chain extends Chain | undefined = Chain | undefined,
chainOverride extends Chain | undefined = Chain | undefined,
type extends FeeValuesType = FeeValuesType,
> = {
/**
* The type of fee values to return.
*
* - `legacy`: Returns the legacy gas price.
* - `eip1559`: Returns the max fee per gas and max priority fee per gas.
*
* @default 'eip1559'
*/
type?: type | FeeValuesType | undefined
} & GetChainParameter<chain, chainOverride>
export type EstimateFeesPerGasReturnType<
type extends FeeValuesType = FeeValuesType,
> =
| (type extends 'legacy' ? FeeValuesLegacy : never)
| (type extends 'eip1559' ? FeeValuesEIP1559 : never)
export type EstimateFeesPerGasErrorType =
| BaseFeeScalarErrorType
| EstimateMaxPriorityFeePerGasErrorType
| GetGasPriceErrorType
| Eip1559FeesNotSupportedErrorType
| ErrorType
/**
* Returns an estimate for the fees per gas (in wei) for a
* transaction to be likely included in the next block.
* Defaults to [`chain.fees.estimateFeesPerGas`](/docs/clients/chains#fees-estimatefeespergas) if set.
*
* - Docs: https://viem.sh/docs/actions/public/estimateFeesPerGas
*
* @param client - Client to use
* @param parameters - {@link EstimateFeesPerGasParameters}
* @returns An estimate (in wei) for the fees per gas. {@link EstimateFeesPerGasReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { estimateFeesPerGas } from 'viem/actions'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const maxPriorityFeePerGas = await estimateFeesPerGas(client)
* // { maxFeePerGas: ..., maxPriorityFeePerGas: ... }
*/
export async function estimateFeesPerGas<
chain extends Chain | undefined,
chainOverride extends Chain | undefined,
type extends FeeValuesType = 'eip1559',
>(
client: Client<Transport, chain>,
args?: EstimateFeesPerGasParameters<chain, chainOverride, type> | undefined,
): Promise<EstimateFeesPerGasReturnType<type>> {
return internal_estimateFeesPerGas(client, args as any)
}
export async function internal_estimateFeesPerGas<
chain extends Chain | undefined,
chainOverride extends Chain | undefined,
type extends FeeValuesType = 'eip1559',
>(
client: Client<Transport, chain>,
args: EstimateFeesPerGasParameters<chain, chainOverride, type> & {
block?: Block | undefined
request?: PrepareTransactionRequestParameters<Chain, Account> | undefined
},
): Promise<EstimateFeesPerGasReturnType<type>> {
const {
block: block_,
chain = client.chain,
request,
type = 'eip1559',
} = args || {}
const baseFeeMultiplier = await (async () => {
if (typeof chain?.fees?.baseFeeMultiplier === 'function')
return chain.fees.baseFeeMultiplier({
block: block_ as Block,
client,
request,
} as ChainFeesFnParameters)
return chain?.fees?.baseFeeMultiplier ?? 1.2
})()
if (baseFeeMultiplier < 1) throw new BaseFeeScalarError()
const decimals = baseFeeMultiplier.toString().split('.')[1]?.length ?? 0
const denominator = 10 ** decimals
const multiply = (base: bigint) =>
(base * BigInt(Math.ceil(baseFeeMultiplier * denominator))) /
BigInt(denominator)
const block = block_
? block_
: await getAction(client, getBlock, 'getBlock')({})
if (typeof chain?.fees?.estimateFeesPerGas === 'function') {
const fees = (await chain.fees.estimateFeesPerGas({
block: block_ as Block,
client,
multiply,
request,
type,
} as ChainEstimateFeesPerGasFnParameters)) as unknown as EstimateFeesPerGasReturnType<type>
if (fees !== null) return fees
}
if (type === 'eip1559') {
if (typeof block.baseFeePerGas !== 'bigint')
throw new Eip1559FeesNotSupportedError()
const maxPriorityFeePerGas =
typeof request?.maxPriorityFeePerGas === 'bigint'
? request.maxPriorityFeePerGas
: await internal_estimateMaxPriorityFeePerGas(
client as Client<Transport, Chain>,
{
block: block as Block,
chain,
request,
},
)
const baseFeePerGas = multiply(block.baseFeePerGas)
const maxFeePerGas =
request?.maxFeePerGas ?? baseFeePerGas + maxPriorityFeePerGas
return {
maxFeePerGas,
maxPriorityFeePerGas,
} as EstimateFeesPerGasReturnType<type>
}
const gasPrice =
request?.gasPrice ??
multiply(await getAction(client, getGasPrice, 'getGasPrice')({}))
return {
gasPrice,
} as EstimateFeesPerGasReturnType<type>
}