Skip to content

Commit 5e09aa1

Browse files
committed
Update EtherscanProvider to use their v2 API (#4975).
1 parent a3bd900 commit 5e09aa1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src.ts/providers/provider-etherscan.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ export class EtherscanProvider extends AbstractProvider {
144144
this.#plugin = network.getPlugin<EtherscanPlugin>(EtherscanPluginId);
145145

146146
defineProperties<EtherscanProvider>(this, { apiKey, network });
147-
148-
// Test that the network is supported by Etherscan
149-
this.getBaseUrl();
150147
}
151148

152149
/**
@@ -155,6 +152,11 @@ export class EtherscanProvider extends AbstractProvider {
155152
* If an [[EtherscanPlugin]] is configured on the
156153
* [[EtherscanBaseProvider_network]], returns the plugin's
157154
* baseUrl.
155+
*
156+
* Deprecated; for Etherscan v2 the base is no longer a simply
157+
* host, but instead a URL including a chainId parameter. Changing
158+
* this to return a URL prefix could break some libraries, so it
159+
* is left intact but will be removed in the future as it is unused.
158160
*/
159161
getBaseUrl(): string {
160162
if (this.#plugin) { return this.#plugin.baseUrl; }
@@ -202,22 +204,22 @@ export class EtherscanProvider extends AbstractProvider {
202204
* Returns the URL for the %%module%% and %%params%%.
203205
*/
204206
getUrl(module: string, params: Record<string, string>): string {
205-
const query = Object.keys(params).reduce((accum, key) => {
207+
let query = Object.keys(params).reduce((accum, key) => {
206208
const value = params[key];
207209
if (value != null) {
208210
accum += `&${ key }=${ value }`
209211
}
210212
return accum
211213
}, "");
212-
const apiKey = ((this.apiKey) ? `&apikey=${ this.apiKey }`: "");
213-
return `${ this.getBaseUrl() }/api?module=${ module }${ query }${ apiKey }`;
214+
if (this.apiKey) { query += `&apikey=${ this.apiKey }`; }
215+
return `https:/\/api.etherscan.io/v2/api?chainid=${ this.network.chainId }&module=${ module }${ query }`;
214216
}
215217

216218
/**
217219
* Returns the URL for using POST requests.
218220
*/
219221
getPostUrl(): string {
220-
return `${ this.getBaseUrl() }/api`;
222+
return `https:/\/api.etherscan.io/v2/api?chainid=${ this.network.chainId }`;
221223
}
222224

223225
/**
@@ -226,6 +228,7 @@ export class EtherscanProvider extends AbstractProvider {
226228
getPostData(module: string, params: Record<string, any>): Record<string, any> {
227229
params.module = module;
228230
params.apikey = this.apiKey;
231+
params.chainid = this.network.chainId;
229232
return params;
230233
}
231234

@@ -367,7 +370,6 @@ export class EtherscanProvider extends AbstractProvider {
367370
* Throws the normalized Etherscan error.
368371
*/
369372
_checkError(req: PerformActionRequest, error: Error, transaction: any): never {
370-
371373
// Pull any message out if, possible
372374
let message = "";
373375
if (isError(error, "SERVER_ERROR")) {

0 commit comments

Comments
 (0)