Added initial QuickNodeProvider.

This commit is contained in:
Richard Moore 2022-12-05 22:17:08 -05:00
parent 16033139ff
commit c84674dcd8
4 changed files with 113 additions and 4 deletions

@ -54,6 +54,8 @@ export {
} from "./hash/index.js";
export {
getDefaultProvider,
Block, FeeData, Log, TransactionReceipt, TransactionResponse,
AbstractProvider,
@ -63,11 +65,13 @@ export {
BrowserProvider,
AlchemyProvider, AnkrProvider, CloudflareProvider, EtherscanProvider, InfuraProvider,
AlchemyProvider, AnkrProvider, CloudflareProvider, EtherscanProvider,
InfuraProvider, QuickNodeProvider,
//PocketProvider } from "./provider-pocket.js";
IpcSocketProvider, SocketProvider, WebSocketProvider,
EnsResolver,
Network
} from "./providers/index.js";

@ -1,10 +1,13 @@
import { assert } from "../utils/index.js";
import { AnkrProvider } from "./provider-ankr.js";
import { AlchemyProvider } from "./provider-alchemy.js";
import { CloudflareProvider } from "./provider-cloudflare.js";
import { EtherscanProvider } from "./provider-etherscan.js";
import { InfuraProvider } from "./provider-infura.js";
//import { PocketProvider } from "./provider-pocket.js";
import { QuickNodeProvider } from "./provider-quicknode.js";
import { FallbackProvider } from "./provider-fallback.js";
import { JsonRpcProvider } from "./provider-jsonrpc.js";
@ -82,7 +85,17 @@ export function getDefaultProvider(network: string | Networkish | WebSocketLike,
} catch (error) { console.log(error); }
}
*/
if (providers.length === 0) { throw new Error("TODO"); }
if (options.quicknode !== "-") {
try {
let token = options.qquicknode;
providers.push(new QuickNodeProvider(network, token));
} catch (error) { console.log(error); }
}
assert(providers.length, "unsupported default network", "UNSUPPORTED_OPERATION", {
operation: "getDefaultProvider"
});
if (providers.length === 1) { return providers[0]; }
return new FallbackProvider(providers);

@ -15,7 +15,7 @@ export {
VoidSigner,
WrappedSigner
} from "./abstract-signer.js";
/*
export {
showThrottleMessage
} from "./community.js";
@ -23,7 +23,6 @@ export {
export { getDefaultProvider } from "./default-provider.js";
export { EnsResolver } from "./ens-resolver.js";
*/
export { Network } from "./network.js";
@ -58,6 +57,7 @@ export { CloudflareProvider } from "./provider-cloudflare.js";
export { BaseEtherscanProvider, EtherscanPlugin } from "./provider-etherscan-base.js";
export { EtherscanProvider } from "./provider-etherscan.js";
export { InfuraProvider, InfuraWebSocketProvider } from "./provider-infura.js";
export { QuickNodeProvider } from "./provider-quicknode.js";
import { IpcSocketProvider } from "./provider-ipcsocket.js"; /*-browser*/
export { IpcSocketProvider };

@ -0,0 +1,92 @@
/**
* About Quicknode
*
* @_subsection: api/providers/thirdparty:QuickNode [backend-quicknode]
*/
import {
defineProperties, FetchRequest, assertArgument
} from "../utils/index.js";
import { showThrottleMessage } from "./community.js";
import { Network } from "./network.js";
import { JsonRpcProvider } from "./provider-jsonrpc.js";
import type { AbstractProvider } from "./abstract-provider.js";
import type { CommunityResourcable } from "./community.js";
import type { Networkish } from "./network.js";
const defaultToken = "919b412a057b5e9c9b6dce193c5a60242d6efadb";
function getHost(name: string): string {
switch(name) {
case "mainnet":
return "ethers.quiknode.pro";
case "goerli":
return "ethers.ethereum-goerli.quiknode.pro";
//case "sepolia":
// return "sepolia.infura.io";
case "arbitrum":
return "ethers.arbitrum-mainnet.quiknode.pro";
case "arbitrum-goerli":
return "ethers.arbitrum-goerli.quiknode.pro";
case "matic":
return "ethers.matic.quiknode.pro";
case "maticmum":
return "ethers.matic-testnet.quiknode.pro";
case "optimism":
return "ethers.optimism.quiknode.pro";
case "optimism-goerli":
return "ethers.optimism-goerli.quiknode.pro";
}
assertArgument(false, "unsupported network", "network", name);
}
/**
* About QuickNode
*/
export class QuickNodeProvider extends JsonRpcProvider implements CommunityResourcable {
readonly token!: string;
constructor(_network?: Networkish, token?: null | string) {
if (_network == null) { _network = "mainnet"; }
const network = Network.from(_network);
if (token == null) { token = defaultToken; }
const request = QuickNodeProvider.getRequest(network, token);
super(request, network, { staticNetwork: network });
defineProperties<QuickNodeProvider>(this, { token });
}
_getProvider(chainId: number): AbstractProvider {
try {
return new QuickNodeProvider(chainId, this.token);
} catch (error) { }
return super._getProvider(chainId);
}
isCommunityResource(): boolean {
return (this.token === defaultToken);
}
static getRequest(network: Network, token?: null | string): FetchRequest {
if (token == null) { token = defaultToken; }
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/${ token }`);
request.allowGzip = true;
//if (projectSecret) { request.setCredentials("", projectSecret); }
if (token === defaultToken) {
request.retryFunc = async (request, response, attempt) => {
showThrottleMessage("QuickNodeProvider");
return true;
};
}
return request;
}
}