ethers.js/lib.commonjs/providers/provider-ankr.js
2022-12-30 16:35:04 -05:00

77 lines
2.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnkrProvider = void 0;
const index_js_1 = require("../utils/index.js");
const community_js_1 = require("./community.js");
const network_js_1 = require("./network.js");
const provider_jsonrpc_js_1 = require("./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";
}
(0, index_js_1.assertArgument)(false, "unsupported network", "network", name);
}
/**
* About Ankr...
*
* @_docloc: api/providers/thirdparty
*/
class AnkrProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
apiKey;
constructor(_network, apiKey) {
if (_network == null) {
_network = "mainnet";
}
const network = network_js_1.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);
(0, index_js_1.defineProperties)(this, { apiKey });
}
_getProvider(chainId) {
try {
return new AnkrProvider(chainId, this.apiKey);
}
catch (error) { }
return super._getProvider(chainId);
}
static getRequest(network, apiKey) {
if (apiKey == null) {
apiKey = defaultApiKey;
}
const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/${apiKey}`);
request.allowGzip = true;
if (apiKey === defaultApiKey) {
request.retryFunc = async (request, response, attempt) => {
(0, community_js_1.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);
}
}
exports.AnkrProvider = AnkrProvider;
//# sourceMappingURL=provider-ankr.js.map