-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathprovider-infura.ts
219 lines (192 loc) · 7.3 KB
/
provider-infura.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
/**
* [[link-infura]] provides a third-party service for connecting to
* various blockchains over JSON-RPC.
*
* **Supported Networks**
*
* - Ethereum Mainnet (``mainnet``)
* - Goerli Testnet (``goerli``)
* - Sepolia Testnet (``sepolia``)
* - Arbitrum (``arbitrum``)
* - Arbitrum Goerli Testnet (``arbitrum-goerli``)
* - Arbitrum Sepolia Testnet (``arbitrum-sepolia``)
* - Base (``base``)
* - Base Goerlia Testnet (``base-goerli``)
* - Base Sepolia Testnet (``base-sepolia``)
* - BNB Smart Chain Mainnet (``bnb``)
* - BNB Smart Chain Testnet (``bnbt``)
* - Linea (``linea``)
* - Linea Goerli Testnet (``linea-goerli``)
* - Linea Sepolia Testnet (``linea-sepolia``)
* - Optimism (``optimism``)
* - Optimism Goerli Testnet (``optimism-goerli``)
* - Optimism Sepolia Testnet (``optimism-sepolia``)
* - Polygon (``matic``)
* - Polygon Amoy Testnet (``matic-amoy``)
* - Polygon Mumbai Testnet (``matic-mumbai``)
*
* @_subsection: api/providers/thirdparty:INFURA [providers-infura]
*/
import {
defineProperties, FetchRequest, assert, assertArgument
} from "../utils/index.js";
import { showThrottleMessage } from "./community.js";
import { Network } from "./network.js";
import { JsonRpcProvider } from "./provider-jsonrpc.js";
import { WebSocketProvider } from "./provider-websocket.js";
import type { AbstractProvider } from "./abstract-provider.js";
import type { CommunityResourcable } from "./community.js";
import type { Networkish } from "./network.js";
const defaultProjectId = "84842078b09946638c03157f83405213";
function getHost(name: string): string {
switch(name) {
case "mainnet":
return "mainnet.infura.io";
case "goerli":
return "goerli.infura.io";
case "sepolia":
return "sepolia.infura.io";
case "arbitrum":
return "arbitrum-mainnet.infura.io";
case "arbitrum-goerli":
return "arbitrum-goerli.infura.io";
case "arbitrum-sepolia":
return "arbitrum-sepolia.infura.io";
case "base":
return "base-mainnet.infura.io";
case "base-goerlia":
return "base-goerli.infura.io";
case "base-sepolia":
return "base-sepolia.infura.io";
case "bnb":
return "bnbsmartchain-mainnet.infura.io";
case "bnbt":
return "bnbsmartchain-testnet.infura.io";
case "linea":
return "linea-mainnet.infura.io";
case "linea-goerli":
return "linea-goerli.infura.io";
case "linea-sepolia":
return "linea-sepolia.infura.io";
case "matic":
return "polygon-mainnet.infura.io";
case "matic-amoy":
return "polygon-amoy.infura.io";
case "matic-mumbai":
return "polygon-mumbai.infura.io";
case "optimism":
return "optimism-mainnet.infura.io";
case "optimism-goerli":
return "optimism-goerli.infura.io";
case "optimism-sepolia":
return "optimism-sepolia.infura.io";
}
assertArgument(false, "unsupported network", "network", name);
}
/**
* The **InfuraWebSocketProvider** connects to the [[link-infura]]
* WebSocket end-points.
*
* By default, a highly-throttled API key is used, which is
* appropriate for quick prototypes and simple scripts. To
* gain access to an increased rate-limit, it is highly
* recommended to [sign up here](link-infura-signup).
*/
export class InfuraWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
/**
* The Project ID for the INFURA connection.
*/
readonly projectId!: string;
/**
* The Project Secret.
*
* If null, no authenticated requests are made. This should not
* be used outside of private contexts.
*/
readonly projectSecret!: null | string;
/**
* Creates a new **InfuraWebSocketProvider**.
*/
constructor(network?: Networkish, projectId?: string) {
const provider = new InfuraProvider(network, projectId);
const req = provider._getConnection();
assert(!req.credentials, "INFURA WebSocket project secrets unsupported",
"UNSUPPORTED_OPERATION", { operation: "InfuraProvider.getWebSocketProvider()" });
const url = req.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
super(url, provider._network);
defineProperties<InfuraWebSocketProvider>(this, {
projectId: provider.projectId,
projectSecret: provider.projectSecret
});
}
isCommunityResource(): boolean {
return (this.projectId === defaultProjectId);
}
}
/**
* The **InfuraProvider** connects to the [[link-infura]]
* JSON-RPC end-points.
*
* By default, a highly-throttled API key is used, which is
* appropriate for quick prototypes and simple scripts. To
* gain access to an increased rate-limit, it is highly
* recommended to [sign up here](link-infura-signup).
*/
export class InfuraProvider extends JsonRpcProvider implements CommunityResourcable {
/**
* The Project ID for the INFURA connection.
*/
readonly projectId!: string;
/**
* The Project Secret.
*
* If null, no authenticated requests are made. This should not
* be used outside of private contexts.
*/
readonly projectSecret!: null | string;
/**
* Creates a new **InfuraProvider**.
*/
constructor(_network?: Networkish, projectId?: null | string, projectSecret?: null | string) {
if (_network == null) { _network = "mainnet"; }
const network = Network.from(_network);
if (projectId == null) { projectId = defaultProjectId; }
if (projectSecret == null) { projectSecret = null; }
const request = InfuraProvider.getRequest(network, projectId, projectSecret);
super(request, network, { staticNetwork: network });
defineProperties<InfuraProvider>(this, { projectId, projectSecret });
}
_getProvider(chainId: number): AbstractProvider {
try {
return new InfuraProvider(chainId, this.projectId, this.projectSecret);
} catch (error) { }
return super._getProvider(chainId);
}
isCommunityResource(): boolean {
return (this.projectId === defaultProjectId);
}
/**
* Creates a new **InfuraWebSocketProvider**.
*/
static getWebSocketProvider(network?: Networkish, projectId?: string): InfuraWebSocketProvider {
return new InfuraWebSocketProvider(network, projectId);
}
/**
* Returns a prepared request for connecting to %%network%%
* with %%projectId%% and %%projectSecret%%.
*/
static getRequest(network: Network, projectId?: null | string, projectSecret?: null | string): FetchRequest {
if (projectId == null) { projectId = defaultProjectId; }
if (projectSecret == null) { projectSecret = null; }
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v3/${ projectId }`);
request.allowGzip = true;
if (projectSecret) { request.setCredentials("", projectSecret); }
if (projectId === defaultProjectId) {
request.retryFunc = async (request, response, attempt) => {
showThrottleMessage("InfuraProvider");
return true;
};
}
return request;
}
}