2023-02-02 10:39:16 +03:00
|
|
|
/**
|
|
|
|
* [[link-pocket]] provides a third-party service for connecting to
|
|
|
|
* various blockchains over JSON-RPC.
|
|
|
|
*
|
|
|
|
* **Supported Networks**
|
|
|
|
*
|
|
|
|
* - Ethereum Mainnet (``mainnet``)
|
|
|
|
* - Goerli Testnet (``goerli``)
|
|
|
|
* - Polygon (``matic``)
|
|
|
|
* - Arbitrum (``arbitrum``)
|
|
|
|
*
|
|
|
|
* @_subsection: api/providers/thirdparty:Pocket [providers-pocket]
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
defineProperties, FetchRequest, assertArgument
|
|
|
|
} from "../utils/index.js";
|
|
|
|
|
|
|
|
import { AbstractProvider } from "./abstract-provider.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
import { showThrottleMessage } from "./community.js";
|
|
|
|
import { Network } from "./network.js";
|
|
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
|
|
|
|
|
|
|
import type { CommunityResourcable } from "./community.js";
|
|
|
|
import type { Networkish } from "./network.js";
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
const defaultApplicationId = "62e1ad51b37b8e00394bda3b";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
function getHost(name: string): string {
|
2023-02-02 10:39:16 +03:00
|
|
|
switch (name) {
|
|
|
|
case "mainnet":
|
|
|
|
return "eth-mainnet.gateway.pokt.network";
|
2022-09-05 23:14:43 +03:00
|
|
|
case "goerli":
|
|
|
|
return "eth-goerli.gateway.pokt.network";
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
case "matic":
|
|
|
|
return "poly-mainnet.gateway.pokt.network";
|
|
|
|
case "maticmum":
|
|
|
|
return "polygon-mumbai-rpc.gateway.pokt.network";
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
assertArgument(false, "unsupported network", "network", name);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The **PocketProvider** connects to the [[link-pocket]]
|
|
|
|
* JSON-RPC end-points.
|
|
|
|
*
|
|
|
|
* By default, a highly-throttled API key is used, which is
|
|
|
|
* appropriate for quick prototypes and simple scripts. To
|
|
|
|
* gain access to an increased rate-limit, it is highly
|
|
|
|
* recommended to [sign up here](link-pocket-signup).
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
export class PocketProvider extends JsonRpcProvider implements CommunityResourcable {
|
2023-02-02 10:39:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Application ID for the Pocket connection.
|
|
|
|
*/
|
2022-09-05 23:14:43 +03:00
|
|
|
readonly applicationId!: string;
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
/**
|
|
|
|
* The Application Secret for making authenticated requests
|
|
|
|
* to the Pocket connection.
|
|
|
|
*/
|
|
|
|
readonly applicationSecret!: null | string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new **PocketProvider**.
|
|
|
|
*
|
|
|
|
* By default connecting to ``mainnet`` with a highly throttled
|
|
|
|
* API key.
|
|
|
|
*/
|
|
|
|
constructor(_network?: Networkish, applicationId?: null | string, applicationSecret?: null | string) {
|
|
|
|
if (_network == null) { _network = "mainnet"; }
|
2022-09-05 23:14:43 +03:00
|
|
|
const network = Network.from(_network);
|
2023-02-02 10:39:16 +03:00
|
|
|
if (applicationId == null) { applicationId = defaultApplicationId; }
|
|
|
|
if (applicationSecret == null) { applicationSecret = null; }
|
|
|
|
|
|
|
|
const options = { staticNetwork: network };
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
const request = PocketProvider.getRequest(network, applicationId, applicationSecret);
|
|
|
|
super(request, network, options);
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
defineProperties<PocketProvider>(this, { applicationId, applicationSecret });
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
_getProvider(chainId: number): AbstractProvider {
|
|
|
|
try {
|
|
|
|
return new PocketProvider(chainId, this.applicationId, this.applicationSecret);
|
|
|
|
} catch (error) { }
|
|
|
|
return super._getProvider(chainId);
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
/**
|
|
|
|
* Returns a prepared request for connecting to %%network%% with
|
|
|
|
* %%applicationId%%.
|
|
|
|
*/
|
|
|
|
static getRequest(network: Network, applicationId?: null | string, applicationSecret?: null | string): FetchRequest {
|
|
|
|
if (applicationId == null) { applicationId = defaultApplicationId; }
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v1/lb/${ applicationId }`);
|
2022-09-05 23:14:43 +03:00
|
|
|
request.allowGzip = true;
|
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
if (applicationSecret) {
|
|
|
|
request.setCredentials("", applicationSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (applicationId === defaultApplicationId) {
|
|
|
|
request.retryFunc = async (request, response, attempt) => {
|
|
|
|
showThrottleMessage("PocketProvider");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
|
2023-02-02 10:39:16 +03:00
|
|
|
return request;
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
2023-02-02 10:39:16 +03:00
|
|
|
return (this.applicationId === defaultApplicationId);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
}
|