Added load balancer support to PocketProvider (#1052).

This commit is contained in:
Richard Moore 2021-02-02 17:05:47 -05:00
parent 29be1e37bc
commit 27a981c84b
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 138 additions and 26 deletions

@ -1,6 +1,7 @@
"use strict";
import { Network } from "@ethersproject/networks";
import { Network, Networkish } from "@ethersproject/networks";
import { getStatic } from "@ethersproject/properties";
import { ConnectionInfo } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
@ -9,20 +10,63 @@ const logger = new Logger(version);
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
const defaultApplicationId = "5f7f8547b90218002e9ce9dd";
// These are load-balancer-based applicatoin IDs
const defaultApplicationIds: Record<string, string> = {
homestead: "6004bcd10040261633ade990",
ropsten: "6004bd4d0040261633ade991",
rinkeby: "6004bda20040261633ade994",
goerli: "6004bd860040261633ade992",
};
export class PocketProvider extends UrlJsonRpcProvider {
readonly applicationId: string;
readonly applicationSecretKey: string;
readonly loadBalancer: boolean;
constructor(network?: Networkish, apiKey?: any) {
// We need a bit of creativity in the constructor because
// Pocket uses different default API keys based on the network
if (apiKey == null) {
const n = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
if (n) {
const applicationId = defaultApplicationIds[n.name];
if (applicationId) {
apiKey = {
applicationId: applicationId,
loadBalancer: true
};
}
}
// If there was any issue above, we don't know this network
if (apiKey == null) {
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
argument: "network",
value: network
});
}
}
super(network, apiKey);
}
static getApiKey(apiKey: any): any {
const apiKeyObj: { applicationId: string, applicationSecretKey: string } = {
applicationId: defaultApplicationId,
// Most API Providers allow null to get the default configuration, but
// Pocket requires the network to decide the default provider, so we
// rely on hijacking the constructor to add a sensible default for us
if (apiKey == null) {
logger.throwArgumentError("PocketProvider.getApiKey does not support null apiKey", "apiKey", apiKey);
}
const apiKeyObj: { applicationId: string, applicationSecretKey: string, loadBalancer: boolean } = {
applicationId: null,
loadBalancer: false,
applicationSecretKey: null
};
if (apiKey == null) { return apiKeyObj; }
// Parse applicationId and applicationSecretKey
if (typeof (apiKey) === "string") {
apiKeyObj.applicationId = apiKey;
@ -35,9 +79,17 @@ export class PocketProvider extends UrlJsonRpcProvider {
apiKeyObj.applicationId = apiKey.applicationId;
apiKeyObj.applicationSecretKey = apiKey.applicationSecretKey;
apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
} else if (apiKey.applicationId) {
logger.assertArgument((typeof (apiKey.applicationId) === "string"),
"apiKey.applicationId must be a string", "apiKey.applicationId", apiKey.applicationId);
apiKeyObj.applicationId = apiKey.applicationId;
apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
} else {
logger.throwArgumentError("unsupported PocketProvider apiKey", "apiKey", apiKey);
}
return apiKeyObj;
@ -49,6 +101,15 @@ export class PocketProvider extends UrlJsonRpcProvider {
case "homestead":
host = "eth-mainnet.gateway.pokt.network";
break;
case "ropsten":
host = "eth-ropsten.gateway.pokt.network";
break;
case "rinkeby":
host = "eth-rinkeby.gateway.pokt.network";
break;
case "goerli":
host = "eth-goerli.gateway.pokt.network";
break;
default:
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
argument: "network",
@ -56,9 +117,14 @@ export class PocketProvider extends UrlJsonRpcProvider {
});
}
const connection: ConnectionInfo = {
url: (`https:/\/${ host }/v1/${ apiKey.applicationId }`),
};
let url = null;
if (apiKey.loadBalancer) {
url = `https:/\/${ host }/v1/lb/${ apiKey.applicationId }`
} else {
url = `https:/\/${ host }/v1/${ apiKey.applicationId }`
}
const connection: ConnectionInfo = { url };
// Initialize empty headers
connection.headers = {}
@ -73,6 +139,6 @@ export class PocketProvider extends UrlJsonRpcProvider {
}
isCommunityResource(): boolean {
return (this.applicationId === defaultApplicationId);
return (this.applicationId === defaultApplicationIds[this.network.name]);
}
}

File diff suppressed because one or more lines are too long