2023-01-28 09:52:20 +03:00
|
|
|
/**
|
|
|
|
* [[link-ankr]] provides a third-party service for connecting to
|
|
|
|
* various blockchains over JSON-RPC.
|
|
|
|
*
|
|
|
|
* **Supported Networks**
|
|
|
|
*
|
|
|
|
* - Ethereum Mainnet (``mainnet``)
|
|
|
|
* - Goerli Testnet (``goerli``)
|
|
|
|
* - Polygon (``matic``)
|
|
|
|
* - Arbitrum (``arbitrum``)
|
|
|
|
*
|
|
|
|
* @_subsection: api/providers/thirdparty:Ankr [providers-ankr]
|
|
|
|
*/
|
2022-09-09 06:21:08 +03:00
|
|
|
import {
|
2022-10-25 11:06:00 +03:00
|
|
|
defineProperties, FetchRequest, assertArgument
|
2022-09-09 06:21:08 +03:00
|
|
|
} from "../utils/index.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
import { AbstractProvider } from "./abstract-provider.js";
|
|
|
|
import { showThrottleMessage } from "./community.js";
|
|
|
|
import { Network } from "./network.js";
|
|
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
|
|
|
|
|
|
|
import type { CommunityResourcable } from "./community.js";
|
|
|
|
import type { Networkish } from "./network.js";
|
2022-12-31 00:25:44 +03:00
|
|
|
import type { JsonRpcError, JsonRpcPayload } from "./provider-jsonrpc.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
|
|
|
|
|
|
|
|
function getHost(name: string): string {
|
|
|
|
switch (name) {
|
2022-09-30 04:55:13 +03:00
|
|
|
case "mainnet":
|
2022-09-05 23:14:43 +03:00
|
|
|
return "rpc.ankr.com/eth";
|
|
|
|
case "goerli":
|
|
|
|
return "rpc.ankr.com/eth_goerli";
|
|
|
|
case "matic":
|
|
|
|
return "rpc.ankr.com/polygon";
|
|
|
|
case "arbitrum":
|
|
|
|
return "rpc.ankr.com/arbitrum";
|
|
|
|
}
|
2022-10-25 11:06:00 +03:00
|
|
|
|
|
|
|
assertArgument(false, "unsupported network", "network", name);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-30 21:44:47 +03:00
|
|
|
/**
|
2023-01-28 09:52:20 +03:00
|
|
|
* The **AnkrProvider** connects to the [[link-ankr]]
|
|
|
|
* JSON-RPC end-points.
|
2022-11-30 21:44:47 +03:00
|
|
|
*
|
2023-01-28 09:52:20 +03:00
|
|
|
* 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-ankr-signup).
|
2022-11-30 21:44:47 +03:00
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
export class AnkrProvider extends JsonRpcProvider implements CommunityResourcable {
|
2023-01-28 09:52:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The API key for the Ankr connection.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
readonly apiKey!: string;
|
|
|
|
|
2023-01-28 09:52:20 +03:00
|
|
|
/**
|
|
|
|
* Create a new **AnkrProvider**.
|
|
|
|
*
|
|
|
|
* By default connecting to ``mainnet`` with a highly throttled
|
|
|
|
* API key.
|
|
|
|
*/
|
2022-11-28 05:58:07 +03:00
|
|
|
constructor(_network?: Networkish, apiKey?: null | string) {
|
|
|
|
if (_network == null) { _network = "mainnet"; }
|
2022-09-05 23:14:43 +03:00
|
|
|
const network = Network.from(_network);
|
|
|
|
if (apiKey == null) { apiKey = defaultApiKey; }
|
|
|
|
|
|
|
|
// Ankr does not support filterId, so we force polling
|
|
|
|
const options = { polling: true, staticNetwork: network };
|
|
|
|
|
|
|
|
const request = AnkrProvider.getRequest(network, apiKey);
|
|
|
|
super(request, network, options);
|
|
|
|
|
|
|
|
defineProperties<AnkrProvider>(this, { apiKey });
|
|
|
|
}
|
|
|
|
|
|
|
|
_getProvider(chainId: number): AbstractProvider {
|
|
|
|
try {
|
|
|
|
return new AnkrProvider(chainId, this.apiKey);
|
|
|
|
} catch (error) { }
|
|
|
|
return super._getProvider(chainId);
|
|
|
|
}
|
|
|
|
|
2023-01-28 09:52:20 +03:00
|
|
|
/**
|
|
|
|
* Returns a prepared request for connecting to %%network%% with
|
|
|
|
* %%apiKey%%.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
static getRequest(network: Network, apiKey?: null | string): FetchRequest {
|
|
|
|
if (apiKey == null) { apiKey = defaultApiKey; }
|
|
|
|
|
|
|
|
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/${ apiKey }`);
|
|
|
|
request.allowGzip = true;
|
|
|
|
|
|
|
|
if (apiKey === defaultApiKey) {
|
|
|
|
request.retryFunc = async (request, response, attempt) => {
|
|
|
|
showThrottleMessage("AnkrProvider");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2022-12-31 00:25:44 +03:00
|
|
|
getRpcError(payload: JsonRpcPayload, error: JsonRpcError): Error {
|
|
|
|
if (payload.method === "eth_sendRawTransaction") {
|
|
|
|
if (error && error.error && error.error.message === "INTERNAL_ERROR: could not replace existing tx") {
|
|
|
|
error.error.message = "replacement transaction underpriced";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.getRpcError(payload, error);
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:14:43 +03:00
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.apiKey === defaultApiKey);
|
|
|
|
}
|
|
|
|
}
|