103 lines
3.4 KiB
JavaScript
103 lines
3.4 KiB
JavaScript
/**
|
|
* [[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]
|
|
*/
|
|
import { defineProperties, FetchRequest, assertArgument } from "../utils/index.js";
|
|
import { showThrottleMessage } from "./community.js";
|
|
import { Network } from "./network.js";
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
|
const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
|
|
function getHost(name) {
|
|
switch (name) {
|
|
case "mainnet":
|
|
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";
|
|
}
|
|
assertArgument(false, "unsupported network", "network", name);
|
|
}
|
|
/**
|
|
* The **AnkrProvider** connects to the [[link-ankr]]
|
|
* 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-ankr-signup).
|
|
*/
|
|
export class AnkrProvider extends JsonRpcProvider {
|
|
/**
|
|
* The API key for the Ankr connection.
|
|
*/
|
|
apiKey;
|
|
/**
|
|
* Create a new **AnkrProvider**.
|
|
*
|
|
* By default connecting to ``mainnet`` with a highly throttled
|
|
* API key.
|
|
*/
|
|
constructor(_network, apiKey) {
|
|
if (_network == null) {
|
|
_network = "mainnet";
|
|
}
|
|
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(this, { apiKey });
|
|
}
|
|
_getProvider(chainId) {
|
|
try {
|
|
return new AnkrProvider(chainId, this.apiKey);
|
|
}
|
|
catch (error) { }
|
|
return super._getProvider(chainId);
|
|
}
|
|
/**
|
|
* Returns a prepared request for connecting to %%network%% with
|
|
* %%apiKey%%.
|
|
*/
|
|
static getRequest(network, apiKey) {
|
|
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;
|
|
}
|
|
getRpcError(payload, 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);
|
|
}
|
|
isCommunityResource() {
|
|
return (this.apiKey === defaultApiKey);
|
|
}
|
|
}
|
|
//# sourceMappingURL=provider-ankr.js.map
|