-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.ts
More file actions
330 lines (302 loc) · 11 KB
/
chain.ts
File metadata and controls
330 lines (302 loc) · 11 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import type { Address } from 'abitype'
import type { EstimateFeesPerGasReturnType } from '../actions/public/estimateFeesPerGas.js'
import type {
VerifyHashParameters,
VerifyHashReturnType,
} from '../actions/public/verifyHash.js'
import type { PrepareTransactionRequestParameters } from '../actions/wallet/prepareTransactionRequest.js'
import type { Client } from '../clients/createClient.js'
import type { Transport } from '../clients/transports/createTransport.js'
import type { Account } from '../types/account.js'
import type { FeeValuesType } from '../types/fee.js'
import type {
TransactionSerializable,
TransactionSerializableGeneric,
TransactionSerializedGeneric,
} from '../types/transaction.js'
import type { IsNarrowable, IsUndefined, Prettify } from '../types/utils.js'
import type { FormattedBlock } from '../utils/formatters/block.js'
import type { SerializeTransactionFn } from '../utils/transaction/serializeTransaction.js'
export type Chain<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
extendSchema extends Record<string, unknown> | undefined =
| Record<string, unknown>
| undefined,
> = {
/** Collection of block explorers */
blockExplorers?:
| {
[key: string]: ChainBlockExplorer
default: ChainBlockExplorer
}
| undefined
/** Block time in milliseconds. */
blockTime?: number | undefined
/** Collection of contracts */
contracts?:
| Prettify<
{
[key: string]:
| ChainContract
| { [sourceId: number]: ChainContract | undefined }
| undefined
} & {
ensRegistry?: ChainContract | undefined
ensUniversalResolver?: ChainContract | undefined
multicall3?: ChainContract | undefined
erc6492Verifier?: ChainContract | undefined
}
>
| undefined
/** Collection of ENS TLDs for the chain. */
ensTlds?: readonly string[] | undefined
/** ID in number form */
id: number
/** Human-readable name */
name: string
/** Currency used by chain */
nativeCurrency: ChainNativeCurrency
/** Preconfirmation time in milliseconds. */
experimental_preconfirmationTime?: number | undefined
/** Collection of RPC endpoints */
rpcUrls: {
[key: string]: ChainRpcUrls
default: ChainRpcUrls
}
/** Source Chain ID (ie. the L1 chain) */
sourceId?: number | undefined
/** Flag for test networks */
testnet?: boolean | undefined
} & ChainConfig<formatters, extendSchema>
/////////////////////////////////////////////////////////////////////
// Config
/////////////////////////////////////////////////////////////////////
type PrepareTransactionRequestPhase =
| 'beforeFillTransaction'
| 'beforeFillParameters'
| 'afterFillParameters'
type PrepareTransactionRequestFn = (
args: PrepareTransactionRequestParameters,
options: { phase: PrepareTransactionRequestPhase },
) => Promise<PrepareTransactionRequestParameters>
type ChainVerifyHashFn = (
client: Client,
parameters: VerifyHashParameters,
) => Promise<VerifyHashReturnType>
export type ChainConfig<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
extendSchema extends Record<string, unknown> | undefined =
| Record<string, unknown>
| undefined,
> = {
/** Custom chain data. @deprecated use `.extend` instead. */
custom?: extendSchema | undefined
/** Extend schema. */
extendSchema?: extendSchema | undefined
/** Modifies how fees are derived. */
fees?: ChainFees<formatters | undefined> | undefined
/** Modifies how data is formatted and typed (e.g. blocks and transactions) */
formatters?: formatters | undefined
/** Function to prepare a transaction request. Runs before the transaction is filled. */
prepareTransactionRequest?:
| PrepareTransactionRequestFn
| [
fn: PrepareTransactionRequestFn | undefined,
options: {
/**
* Phases to run the function at.
*
* - `beforeFillTransaction`: Before the transaction is attempted to be filled via `eth_fillTransaction`.
* - `beforeFillParameters`: Before missing parameters are filled.
* - `afterFillParameters`: After missing parameters are filled.
*/
runAt: readonly PrepareTransactionRequestPhase[]
},
]
| undefined
/** Modifies how data is serialized (e.g. transactions). */
serializers?: ChainSerializers<formatters> | undefined
/** Chain-specific signature verification. */
verifyHash?: ChainVerifyHashFn | undefined
}
/////////////////////////////////////////////////////////////////////
// Fees
/////////////////////////////////////////////////////////////////////
export type ChainFeesFnParameters<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = {
/** The latest block. */
block: Prettify<
FormattedBlock<Omit<Chain, 'formatters'> & { formatters: formatters }>
>
client: Client<Transport, Chain>
/**
* A transaction request. This value will be undefined if the caller
* is outside of a transaction request context (e.g. a direct call to
* the `estimateFeesPerGas` Action).
*/
request?:
| PrepareTransactionRequestParameters<
Omit<Chain, 'formatters'> & { formatters: formatters },
Account | undefined,
undefined
>
| undefined
}
export type ChainEstimateFeesPerGasFnParameters<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = {
/** A function to multiply the base fee based on the `baseFeeMultiplier` value. */
multiply: (x: bigint) => bigint
/** The type of fees to return. */
type: FeeValuesType
} & ChainFeesFnParameters<formatters>
export type ChainEstimateFeesPerGasFn<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = (
args: ChainEstimateFeesPerGasFnParameters<formatters>,
) => Promise<EstimateFeesPerGasReturnType | null>
export type ChainMaxPriorityFeePerGasFn<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = (
args: ChainFeesFnParameters<formatters>,
) => Promise<bigint | null> | bigint | null
export type ChainFees<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
> = {
/**
* The fee multiplier to use to account for fee fluctuations.
* Used in the [`estimateFeesPerGas` Action](/docs/actions/public/estimateFeesPerGas).
*
* @default 1.2
*/
baseFeeMultiplier?:
| number
| ((args: ChainFeesFnParameters<formatters>) => Promise<number> | number)
/**
* The default `maxPriorityFeePerGas` to use when a priority
* fee is not defined upon sending a transaction.
*
* Overrides the return value in the [`estimateMaxPriorityFeePerGas` Action](/docs/actions/public/estimateMaxPriorityFeePerGas).
*/
maxPriorityFeePerGas?:
| bigint
| ChainMaxPriorityFeePerGasFn<formatters>
| undefined
/** @deprecated Use `maxPriorityFeePerGas` instead. */
defaultPriorityFee?:
| bigint
| ChainMaxPriorityFeePerGasFn<formatters>
| undefined
/**
* Allows customization of fee per gas values (e.g. `maxFeePerGas`/`maxPriorityFeePerGas`).
*
* Overrides the return value in the [`estimateFeesPerGas` Action](/docs/actions/public/estimateFeesPerGas).
*/
estimateFeesPerGas?: ChainEstimateFeesPerGasFn<formatters> | undefined
}
/////////////////////////////////////////////////////////////////////
// Formatters
/////////////////////////////////////////////////////////////////////
export type ChainFormatters = {
/** Modifies how the Block structure is formatted & typed. */
block?: ChainFormatter<'block'> | undefined
/** Modifies how the Transaction structure is formatted & typed. */
transaction?: ChainFormatter<'transaction'> | undefined
/** Modifies how the TransactionReceipt structure is formatted & typed. */
transactionReceipt?: ChainFormatter<'transactionReceipt'> | undefined
/** Modifies how the TransactionRequest structure is formatted & typed. */
transactionRequest?: ChainFormatter<'transactionRequest'> | undefined
}
export type ChainFormatter<type extends string = string> = {
format: (args: any, action?: string | undefined) => any
type: type
}
/////////////////////////////////////////////////////////////////////
// Serializers
/////////////////////////////////////////////////////////////////////
export type ChainSerializers<
formatters extends ChainFormatters | undefined = undefined,
///
transaction extends
TransactionSerializableGeneric = formatters extends ChainFormatters
? formatters['transactionRequest'] extends ChainFormatter
? TransactionSerializableGeneric &
Parameters<formatters['transactionRequest']['format']>[0]
: TransactionSerializable
: TransactionSerializable,
> = {
/** Modifies how Transactions are serialized. */
transaction?:
| SerializeTransactionFn<transaction, TransactionSerializedGeneric>
| undefined
}
/////////////////////////////////////////////////////////////////////
// Utils
/////////////////////////////////////////////////////////////////////
export type ExtractChainFormatterExclude<
chain extends Chain | undefined,
type extends keyof ChainFormatters,
> = chain extends { formatters?: infer formatters extends ChainFormatters }
? formatters[type] extends { exclude: infer exclude }
? Extract<exclude, readonly string[]>[number]
: ''
: ''
export type ExtractChainFormatterParameters<
chain extends Chain | undefined,
type extends keyof ChainFormatters,
fallback,
> = chain extends { formatters?: infer formatters extends ChainFormatters }
? formatters[type] extends ChainFormatter
? Parameters<formatters[type]['format']>[0]
: fallback
: fallback
export type ExtractChainFormatterReturnType<
chain extends Chain | undefined,
type extends keyof ChainFormatters,
fallback,
> = IsNarrowable<chain, Chain> extends true
? chain extends {
formatters?:
| { [_ in type]?: infer formatter extends ChainFormatter }
| undefined
}
? chain['formatters'] extends undefined
? fallback
: IsNarrowable<formatter, ChainFormatter<type>> extends true
? ReturnType<formatter['format']>
: fallback
: fallback
: fallback
export type DeriveChain<
chain extends Chain | undefined,
chainOverride extends Chain | undefined,
> = chainOverride extends Chain ? chainOverride : chain
export type GetChainParameter<
chain extends Chain | undefined,
chainOverride extends Chain | undefined = Chain | undefined,
> = IsUndefined<chain> extends true
? { chain: chainOverride | null }
: { chain?: chainOverride | null | undefined }
/////////////////////////////////////////////////////////////////////
// Constants
/////////////////////////////////////////////////////////////////////
type ChainBlockExplorer = {
name: string
url: string
apiUrl?: string | undefined
}
export type ChainContract = {
address: Address
blockCreated?: number | undefined
}
type ChainNativeCurrency = {
name: string
/** 2-6 characters long */
symbol: string
decimals: number
}
type ChainRpcUrls = {
http: readonly string[]
webSocket?: readonly string[] | undefined
}