ethers.js/lib.esm/providers/provider-ankr.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-11-09 10:57:02 +03:00
import { defineProperties, FetchRequest, assertArgument } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { showThrottleMessage } from "./community.js";
import { Network } from "./network.js";
import { JsonRpcProvider } from "./provider-jsonrpc.js";
const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
function getHost(name) {
switch (name) {
2022-09-30 05:57:27 +03:00
case "mainnet":
2022-09-05 23:57:11 +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-11-09 10:57:02 +03:00
assertArgument(false, "unsupported network", "network", name);
2022-09-05 23:57:11 +03:00
}
2022-11-30 23:44:23 +03:00
/**
* About Ankr...
*
* @_docloc: api/providers/thirdparty
*/
2022-09-05 23:57:11 +03:00
export class AnkrProvider extends JsonRpcProvider {
apiKey;
2022-11-30 23:44:23 +03:00
constructor(_network, apiKey) {
if (_network == null) {
_network = "mainnet";
}
2022-09-05 23:57:11 +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(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 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:35:04 +03:00
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);
}
2022-09-05 23:57:11 +03:00
isCommunityResource() {
return (this.apiKey === defaultApiKey);
}
}
//# sourceMappingURL=provider-ankr.js.map