-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.ts
355 lines (278 loc) · 10.7 KB
/
index.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"use strict";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { BytesLike, isHexString } from "@ethersproject/bytes";
import { Network } from "@ethersproject/networks";
import { Deferrable, Description, defineReadOnly, resolveProperties } from "@ethersproject/properties";
import { AccessListish, Transaction } from "@ethersproject/transactions";
import { OnceBlockable } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
///////////////////////////////
// Exported Types
export type TransactionRequest = {
to?: string,
from?: string,
nonce?: BigNumberish,
gasLimit?: BigNumberish,
gasPrice?: BigNumberish,
data?: BytesLike,
value?: BigNumberish,
chainId?: number
type?: number;
accessList?: AccessListish;
maxPriorityFeePerGas?: BigNumberish;
maxFeePerGas?: BigNumberish;
customData?: Record<string, any>;
ccipReadEnabled?: boolean;
}
export interface TransactionResponse extends Transaction {
hash: string;
// Only if a transaction has been mined
blockNumber?: number,
blockHash?: string,
timestamp?: number,
confirmations: number,
// Not optional (as it is in Transaction)
from: string;
// The raw transaction
raw?: string,
// This function waits until the transaction has been mined
wait: (confirmations?: number) => Promise<TransactionReceipt>
};
export type BlockTag = string | number;
export interface _Block {
hash: string;
parentHash: string;
number: number;
timestamp: number;
nonce: string;
difficulty: number;
_difficulty: BigNumber;
gasLimit: BigNumber;
gasUsed: BigNumber;
miner: string;
extraData: string;
baseFeePerGas?: null | BigNumber;
}
export interface Block extends _Block {
transactions: Array<string>;
}
export interface BlockWithTransactions extends _Block {
transactions: Array<TransactionResponse>;
}
export interface Log {
blockNumber: number;
blockHash: string;
transactionIndex: number;
removed: boolean;
address: string;
data: string;
topics: Array<string>;
transactionHash: string;
logIndex: number;
}
export interface TransactionReceipt {
to: string;
from: string;
contractAddress: string,
transactionIndex: number,
root?: string,
gasUsed: BigNumber,
logsBloom: string,
blockHash: string,
transactionHash: string,
logs: Array<Log>,
blockNumber: number,
confirmations: number,
cumulativeGasUsed: BigNumber,
effectiveGasPrice: BigNumber,
byzantium: boolean,
type: number;
status?: number
};
export interface FeeData {
lastBaseFeePerGas: null | BigNumber;
maxFeePerGas: null | BigNumber;
maxPriorityFeePerGas: null | BigNumber;
gasPrice: null | BigNumber;
}
export interface EventFilter {
address?: string;
topics?: Array<string | Array<string> | null>;
}
export interface Filter extends EventFilter {
fromBlock?: BlockTag,
toBlock?: BlockTag,
}
export interface FilterByBlockHash extends EventFilter {
blockHash?: string;
}
//export type CallTransactionable = {
// call(transaction: TransactionRequest): Promise<TransactionResponse>;
//};
export abstract class ForkEvent extends Description {
readonly expiry: number;
readonly _isForkEvent?: boolean;
static isForkEvent(value: any): value is ForkEvent {
return !!(value && value._isForkEvent);
}
}
export class BlockForkEvent extends ForkEvent {
readonly blockHash: string;
readonly _isBlockForkEvent?: boolean;
constructor(blockHash: string, expiry?: number) {
if (!isHexString(blockHash, 32)) {
logger.throwArgumentError("invalid blockHash", "blockHash", blockHash);
}
super({
_isForkEvent: true,
_isBlockForkEvent: true,
expiry: (expiry || 0),
blockHash: blockHash
});
}
}
export class TransactionForkEvent extends ForkEvent {
readonly hash: string;
readonly _isTransactionOrderForkEvent?: boolean;
constructor(hash: string, expiry?: number) {
if (!isHexString(hash, 32)) {
logger.throwArgumentError("invalid transaction hash", "hash", hash);
}
super({
_isForkEvent: true,
_isTransactionForkEvent: true,
expiry: (expiry || 0),
hash: hash
});
}
}
export class TransactionOrderForkEvent extends ForkEvent {
readonly beforeHash: string;
readonly afterHash: string;
constructor(beforeHash: string, afterHash: string, expiry?: number) {
if (!isHexString(beforeHash, 32)) {
logger.throwArgumentError("invalid transaction hash", "beforeHash", beforeHash);
}
if (!isHexString(afterHash, 32)) {
logger.throwArgumentError("invalid transaction hash", "afterHash", afterHash);
}
super({
_isForkEvent: true,
_isTransactionOrderForkEvent: true,
expiry: (expiry || 0),
beforeHash: beforeHash,
afterHash: afterHash
});
}
}
export type EventType = string | Array<string | Array<string>> | EventFilter | ForkEvent;
export type Listener = (...args: Array<any>) => void;
///////////////////////////////
// Exported Abstracts
export abstract class Provider implements OnceBlockable {
// Network
abstract getNetwork(): Promise<Network>;
// Latest State
abstract getBlockNumber(): Promise<number>;
abstract getGasPrice(): Promise<BigNumber>;
async getFeeData(): Promise<FeeData> {
const { block, gasPrice } = await resolveProperties({
block: this.getBlock("latest"),
gasPrice: this.getGasPrice().catch((error) => {
// @TODO: Why is this now failing on Calaveras?
//console.log(error);
return null;
})
});
let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null;
if (block && block.baseFeePerGas) {
// We may want to compute this more accurately in the future,
// using the formula "check if the base fee is correct".
// See: https://eips.ethereum.org/EIPS/eip-1559
lastBaseFeePerGas = block.baseFeePerGas;
maxPriorityFeePerGas = BigNumber.from("1500000000");
maxFeePerGas = block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas);
}
return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice };
}
// Account
abstract getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
abstract getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number>;
abstract getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> ;
abstract getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
// Execution
abstract sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
abstract call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
abstract estimateGas(transaction: Deferrable<TransactionRequest>): Promise<BigNumber>;
// Queries
abstract getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block>;
abstract getBlockWithTransactions(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<BlockWithTransactions>;
abstract getTransaction(transactionHash: string): Promise<TransactionResponse>;
abstract getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;
// Bloom-filter Queries
abstract getLogs(filter: Filter): Promise<Array<Log>>;
// ENS
abstract resolveName(name: string | Promise<string>): Promise<null | string>;
abstract lookupAddress(address: string | Promise<string>): Promise<null | string>;
// Event Emitter (ish)
abstract on(eventName: EventType, listener: Listener): Provider;
abstract once(eventName: EventType, listener: Listener): Provider;
abstract emit(eventName: EventType, ...args: Array<any>): boolean
abstract listenerCount(eventName?: EventType): number;
abstract listeners(eventName?: EventType): Array<Listener>;
abstract off(eventName: EventType, listener?: Listener): Provider;
abstract removeAllListeners(eventName?: EventType): Provider;
// Alias for "on"
addListener(eventName: EventType, listener: Listener): Provider {
return this.on(eventName, listener);
}
// Alias for "off"
removeListener(eventName: EventType, listener: Listener): Provider {
return this.off(eventName, listener);
}
// @TODO: This *could* be implemented here, but would pull in events...
abstract waitForTransaction(transactionHash: string, confirmations?: number, timeout?: number): Promise<TransactionReceipt>;
readonly _isProvider: boolean;
constructor() {
logger.checkAbstract(new.target, Provider);
defineReadOnly(this, "_isProvider", true);
}
static isProvider(value: any): value is Provider {
return !!(value && value._isProvider);
}
/*
static getResolver(network: Network, callable: CallTransactionable, namehash: string): string {
// No ENS...
if (!network.ensAddress) {
errors.throwError(
"network does support ENS",
errors.UNSUPPORTED_OPERATION,
{ operation: "ENS", network: network.name }
);
}
// Not a namehash
if (!isHexString(namehash, 32)) {
errors.throwArgumentError("invalid name hash", "namehash", namehash);
}
// keccak256("resolver(bytes32)")
let data = "0x0178b8bf" + namehash.substring(2);
let transaction = { to: network.ensAddress, data: data };
return provider.call(transaction).then((data) => {
return provider.formatter.callAddress(data);
});
}
static resolveNamehash(network: Network, callable: CallTransactionable, namehash: string): string {
return this.getResolver(network, callable, namehash).then((resolverAddress) => {
if (!resolverAddress) { return null; }
// keccak256("addr(bytes32)")
let data = "0x3b3b57de" + namehash(name).substring(2);
let transaction = { to: resolverAddress, data: data };
return callable.call(transaction).then((data) => {
return this.formatter.callAddress(data);
});
})
}
*/
}