-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathprovider-alchemy.ts
More file actions
172 lines (146 loc) · 5.6 KB
/
Copy pathprovider-alchemy.ts
File metadata and controls
172 lines (146 loc) · 5.6 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
/**
* [[link-alchemy]] 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``)
* - 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:Alchemy [providers-alchemy]
*/
import {
defineProperties, resolveProperties, assert, assertArgument,
FetchRequest
} from "../utils/index.js";
import { showThrottleMessage } from "./community.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";
const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC"
function getHost(name: string): string {
switch(name) {
case "mainnet":
return "eth-mainnet.g.alchemy.com";
case "goerli":
return "eth-goerli.g.alchemy.com";
case "sepolia":
return "eth-sepolia.g.alchemy.com";
case "arbitrum":
return "arb-mainnet.g.alchemy.com";
case "arbitrum-goerli":
return "arb-goerli.g.alchemy.com";
case "arbitrum-sepolia":
return "arb-sepolia.g.alchemy.com";
case "base":
return "base-mainnet.g.alchemy.com";
case "base-goerli":
return "base-goerli.g.alchemy.com";
case "base-sepolia":
return "base-sepolia.g.alchemy.com";
case "bnb":
return "bnb-mainnet.g.alchemy.com";
case "bnbt":
return "bnb-testnet.g.alchemy.com";
case "matic":
return "polygon-mainnet.g.alchemy.com";
case "matic-amoy":
return "polygon-amoy.g.alchemy.com";
case "matic-mumbai":
return "polygon-mumbai.g.alchemy.com";
case "optimism":
return "opt-mainnet.g.alchemy.com";
case "optimism-goerli":
return "opt-goerli.g.alchemy.com";
case "optimism-sepolia":
return "opt-sepolia.g.alchemy.com";
}
assertArgument(false, "unsupported network", "network", name);
}
/**
* The **AlchemyProvider** connects to the [[link-alchemy]]
* 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-alchemy-signup).
*
* @_docloc: api/providers/thirdparty
*/
export class AlchemyProvider extends JsonRpcProvider implements CommunityResourcable {
readonly apiKey!: string;
constructor(_network?: Networkish, apiKey?: null | string) {
if (_network == null) { _network = "mainnet"; }
const network = Network.from(_network);
if (apiKey == null) { apiKey = defaultApiKey; }
const request = AlchemyProvider.getRequest(network, apiKey);
super(request, network, { staticNetwork: network });
defineProperties<AlchemyProvider>(this, { apiKey });
}
_getProvider(chainId: number): AbstractProvider {
try {
return new AlchemyProvider(chainId, this.apiKey);
} catch (error) { }
return super._getProvider(chainId);
}
async _perform(req: PerformActionRequest): Promise<any> {
// https://docs.alchemy.com/reference/trace-transaction
if (req.method === "getTransactionResult") {
const { trace, tx } = await resolveProperties({
trace: this.send("trace_transaction", [ req.hash ]),
tx: this.getTransaction(req.hash)
});
if (trace == null || tx == null) { return null; }
let data: undefined | string;
let error = false;
try {
data = trace[0].result.output;
error = (trace[0].error === "Reverted");
} catch (error) { }
if (data) {
assert(!error, "an error occurred during transaction executions", "CALL_EXCEPTION", {
action: "getTransactionResult",
data,
reason: null,
transaction: tx,
invocation: null,
revert: null // @TODO
});
return data;
}
assert(false, "could not parse trace result", "BAD_DATA", { value: trace });
}
return await super._perform(req);
}
isCommunityResource(): boolean {
return (this.apiKey === defaultApiKey);
}
static getRequest(network: Network, apiKey?: string): FetchRequest {
if (apiKey == null) { apiKey = defaultApiKey; }
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v2/${ apiKey }`);
request.allowGzip = true;
if (apiKey === defaultApiKey) {
request.retryFunc = async (request, response, attempt) => {
showThrottleMessage("alchemy");
return true;
}
}
return request;
}
}