-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathprovider-blockscout.ts
More file actions
169 lines (140 loc) · 5.72 KB
/
Copy pathprovider-blockscout.ts
File metadata and controls
169 lines (140 loc) · 5.72 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
/**
* [[link-blockscout]] provides a third-party service for connecting to
* various blockchains over JSON-RPC.
*
* **Supported Networks**
*
* - Ethereum Mainnet (``mainnet``)
* - Sepolia Testnet (``sepolia``)
* - Holesky Testnet (``holesky``)
* - Ethereum Classic (``classic``)
* - Arbitrum (``arbitrum``)
* - Base (``base``)
* - Base Sepolia Testnet (``base-sepolia``)
* - Gnosis (``xdai``)
* - Optimism (``optimism``)
* - Optimism Sepolia Testnet (``optimism-sepolia``)
* - Polygon (``matic``)
*
* @_subsection: api/providers/thirdparty:Blockscout [providers-blockscout]
*/
import {
assertArgument, defineProperties, FetchRequest, isHexString
} from "../utils/index.js";
import { Network } from "./network.js";
import { JsonRpcProvider } from "./provider-jsonrpc.js";
import type { AbstractProvider, PerformActionRequest } from "./abstract-provider.js";
import type { CommunityResourcable } from "./community.js";
import type { Networkish } from "./network.js";
import type { JsonRpcPayload, JsonRpcError } from "./provider-jsonrpc.js";
function getUrl(name: string): string {
switch(name) {
case "mainnet":
return "https:/\/eth.blockscout.com/api/eth-rpc";
case "sepolia":
return "https:/\/eth-sepolia.blockscout.com/api/eth-rpc";
case "holesky":
return "https:/\/eth-holesky.blockscout.com/api/eth-rpc";
case "classic":
return "https:/\/etc.blockscout.com/api/eth-rpc";
case "arbitrum":
return "https:/\/arbitrum.blockscout.com/api/eth-rpc";
case "base":
return "https:/\/base.blockscout.com/api/eth-rpc";
case "base-sepolia":
return "https:/\/base-sepolia.blockscout.com/api/eth-rpc";
case "matic":
return "https:/\/polygon.blockscout.com/api/eth-rpc";
case "optimism":
return "https:/\/optimism.blockscout.com/api/eth-rpc";
case "optimism-sepolia":
return "https:/\/optimism-sepolia.blockscout.com/api/eth-rpc";
case "xdai":
return "https:/\/gnosis.blockscout.com/api/eth-rpc";
}
assertArgument(false, "unsupported network", "network", name);
}
const defaultApiKey = "proapi_gwcAAagyQKJ4r2KrAATyyCshiyUEpWfr2x7aKDOWFc56ZY8mBFcT9KZuP5Ce2jIfr_kHzUm";
/**
* The **BlockscoutProvider** connects to the [[link-blockscout]]
* 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-blockscout-signup).
*/
export class BlockscoutProvider extends JsonRpcProvider implements CommunityResourcable {
/**
* The API key.
*/
readonly apiKey!: null | string;
/**
* Creates a new **BlockscoutProvider**.
*/
constructor(_network?: Networkish, apiKey?: null | string) {
if (_network == null) { _network = "mainnet"; }
const network = Network.from(_network);
if (apiKey == null) { apiKey = defaultApiKey; }
const request = BlockscoutProvider.getRequest(network);
super(request, network, { staticNetwork: network });
defineProperties<BlockscoutProvider>(this, { apiKey });
}
_getProvider(chainId: number): AbstractProvider {
try {
return new BlockscoutProvider(chainId, this.apiKey);
} catch (error) { }
return super._getProvider(chainId);
}
isCommunityResource(): boolean {
return (this.apiKey === null);
}
getRpcRequest(req: PerformActionRequest): null | { method: string, args: Array<any> } {
// Blockscout enforces the TAG argument for estimateGas
const resp = super.getRpcRequest(req);
if (resp && resp.method === "eth_estimateGas" && resp.args.length == 1) {
resp.args = resp.args.slice();
resp.args.push("latest");
}
return resp;
}
getRpcError(payload: JsonRpcPayload, _error: JsonRpcError): Error {
const error = _error ? _error.error: null;
// Blockscout currently drops the VM result and replaces it with a
// human-readable string, so we need to make it machine-readable.
if (error && error.code === -32015 && !isHexString(error.data || "", true)) {
const panicCodes = <Record<string, string>>{
"assert(false)": "01",
"arithmetic underflow or overflow": "11",
"division or modulo by zero": "12",
"out-of-bounds array access; popping on an empty array": "31",
"out-of-bounds access of an array or bytesN": "32"
};
let panicCode = "";
if (error.message === "VM execution error.") {
// eth_call passes this message
panicCode = panicCodes[error.data] || "";
} else if (panicCodes[error.message || ""]) {
panicCode = panicCodes[error.message || ""];
}
if (panicCode) {
error.message += ` (reverted: ${ error.data })`;
error.data = "0x4e487b7100000000000000000000000000000000000000000000000000000000000000" + panicCode;
}
} else if (error && error.code === -32000) {
if (error.message === "wrong transaction nonce") {
error.message += " (nonce too low)";
}
}
return super.getRpcError(payload, _error);
}
/**
* Returns a prepared request for connecting to %%network%%
* with %%apiKey%%.
*/
static getRequest(network: Network): FetchRequest {
const request = new FetchRequest(getUrl(network.name));
request.allowGzip = true;
return request;
}
}