Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the pocket-provider to align with more recent updates #2980

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 15 additions & 48 deletions packages/providers/src.ts/pocket-provider.ts
Expand Up @@ -10,47 +10,10 @@ const logger = new Logger(version);

import { UrlJsonRpcProvider } from "./url-json-rpc-provider";

// These are load-balancer-based application 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 {
// Most API Providers allow null to get the default configuration, but
Expand All @@ -61,9 +24,8 @@ export class PocketProvider extends UrlJsonRpcProvider {
logger.throwArgumentError("PocketProvider.getApiKey does not support null apiKey", "apiKey", apiKey);
}

const apiKeyObj: { applicationId: string, applicationSecretKey: string, loadBalancer: boolean } = {
const apiKeyObj: { applicationId: string, applicationSecretKey: string } = {
applicationId: null,
loadBalancer: false,
applicationSecretKey: null
};

Expand All @@ -79,14 +41,12 @@ 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);
Expand All @@ -110,19 +70,26 @@ export class PocketProvider extends UrlJsonRpcProvider {
case "goerli":
host = "eth-goerli.gateway.pokt.network";
break;
case "kovan":
host = "poa-kovan.gateway.pokt.network";
break;
case "xdai":
host = "gnosischain-mainnet.gateway.pokt.network";
break;
case "matic":
host = "poly-mainnet.gateway.pokt.network";
break;
case "bnb":
host = "bsc-mainnet.gateway.pokt.network";
break;
default:
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
argument: "network",
value: network
});
}

let url = null;
if (apiKey.loadBalancer) {
url = `https:/\/${ host }/v1/lb/${ apiKey.applicationId }`
} else {
url = `https:/\/${ host }/v1/${ apiKey.applicationId }`
}
const url = `https:/\/${ host }/v1/lb/${ apiKey.applicationId }`

const connection: ConnectionInfo = { url };

Expand All @@ -139,6 +106,6 @@ export class PocketProvider extends UrlJsonRpcProvider {
}

isCommunityResource(): boolean {
return (this.applicationId === defaultApplicationIds[this.network.name]);
return false;
}
}