2020-10-08 20:21:32 -04:00
|
|
|
"use strict";
|
|
|
|
|
2022-08-18 14:36:57 -04:00
|
|
|
import { Network } from "@ethersproject/networks";
|
2020-10-08 20:21:32 -04:00
|
|
|
import { ConnectionInfo } from "@ethersproject/web";
|
|
|
|
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
|
|
|
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
|
|
|
|
2022-08-18 14:36:57 -04:00
|
|
|
const defaultApplicationId = "62e1ad51b37b8e00394bda3b";
|
|
|
|
|
2020-10-08 20:21:32 -04:00
|
|
|
|
|
|
|
export class PocketProvider extends UrlJsonRpcProvider {
|
|
|
|
readonly applicationId: string;
|
|
|
|
readonly applicationSecretKey: string;
|
2021-02-02 17:05:47 -05:00
|
|
|
readonly loadBalancer: boolean;
|
|
|
|
|
2020-10-08 20:21:32 -04:00
|
|
|
static getApiKey(apiKey: any): any {
|
2021-02-02 17:05:47 -05:00
|
|
|
const apiKeyObj: { applicationId: string, applicationSecretKey: string, loadBalancer: boolean } = {
|
|
|
|
applicationId: null,
|
2022-08-18 14:36:57 -04:00
|
|
|
loadBalancer: true,
|
2020-10-08 20:21:32 -04:00
|
|
|
applicationSecretKey: null
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse applicationId and applicationSecretKey
|
2022-08-18 14:36:57 -04:00
|
|
|
if (apiKey == null) {
|
|
|
|
apiKeyObj.applicationId = defaultApplicationId;
|
|
|
|
|
|
|
|
} else if (typeof (apiKey) === "string") {
|
2020-10-08 20:21:32 -04:00
|
|
|
apiKeyObj.applicationId = apiKey;
|
|
|
|
|
|
|
|
} else if (apiKey.applicationSecretKey != null) {
|
|
|
|
apiKeyObj.applicationId = apiKey.applicationId;
|
|
|
|
apiKeyObj.applicationSecretKey = apiKey.applicationSecretKey;
|
|
|
|
|
|
|
|
} else if (apiKey.applicationId) {
|
|
|
|
apiKeyObj.applicationId = apiKey.applicationId;
|
2021-02-02 17:05:47 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
logger.throwArgumentError("unsupported PocketProvider apiKey", "apiKey", apiKey);
|
2020-10-08 20:21:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeyObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getUrl(network: Network, apiKey: any): ConnectionInfo {
|
|
|
|
let host: string = null;
|
|
|
|
switch (network ? network.name : "unknown") {
|
2022-08-18 14:36:57 -04:00
|
|
|
case "goerli":
|
|
|
|
host = "eth-goerli.gateway.pokt.network";
|
|
|
|
break;
|
2020-10-08 20:21:32 -04:00
|
|
|
case "homestead":
|
|
|
|
host = "eth-mainnet.gateway.pokt.network";
|
|
|
|
break;
|
2022-08-18 14:36:57 -04:00
|
|
|
case "kovan":
|
|
|
|
host = "poa-kovan.gateway.pokt.network";
|
|
|
|
break;
|
|
|
|
case "matic":
|
|
|
|
host = "poly-mainnet.gateway.pokt.network";
|
|
|
|
break;
|
|
|
|
case "maticmum":
|
|
|
|
host = "polygon-mumbai-rpc.gateway.pokt.network";
|
2021-02-02 17:05:47 -05:00
|
|
|
break;
|
|
|
|
case "rinkeby":
|
|
|
|
host = "eth-rinkeby.gateway.pokt.network";
|
|
|
|
break;
|
2022-08-18 14:36:57 -04:00
|
|
|
case "ropsten":
|
|
|
|
host = "eth-ropsten.gateway.pokt.network";
|
2021-02-02 17:05:47 -05:00
|
|
|
break;
|
2020-10-08 20:21:32 -04:00
|
|
|
default:
|
|
|
|
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "network",
|
|
|
|
value: network
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-18 14:36:57 -04:00
|
|
|
const url = `https:/\/${ host }/v1/lb/${ apiKey.applicationId }`
|
2020-10-08 20:21:32 -04:00
|
|
|
|
2022-08-18 14:36:57 -04:00
|
|
|
const connection: ConnectionInfo = { headers: { }, url };
|
2020-10-08 20:21:32 -04:00
|
|
|
if (apiKey.applicationSecretKey != null) {
|
|
|
|
connection.user = "";
|
|
|
|
connection.password = apiKey.applicationSecretKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
2022-08-18 14:36:57 -04:00
|
|
|
return (this.applicationId === defaultApplicationId);
|
2020-10-08 20:21:32 -04:00
|
|
|
}
|
|
|
|
}
|