All files / ethers.js/src.ts/providers provider-pocket.ts

32.23% Statements 39/121
0% Branches 0/1
0% Functions 0/2
32.23% Lines 39/121

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 1221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                        
/**
 *  [[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";
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";
 
const defaultApplicationId = "62e1ad51b37b8e00394bda3b";
 
function getHost(name: string): string {
    switch (name) {
        case "mainnet":
            return  "eth-mainnet.gateway.pokt.network";
        case "goerli":
            return "eth-goerli.gateway.pokt.network";

        case "matic":
            return "poly-mainnet.gateway.pokt.network";
        case "matic-mumbai":
            return "polygon-mumbai-rpc.gateway.pokt.network";
    }

    assertArgument(false, "unsupported network", "network", name);
}
 
 
/**
 *  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).
 */
export class PocketProvider extends JsonRpcProvider implements CommunityResourcable {

    /**
     *  The Application ID for the Pocket connection.
     */
    readonly applicationId!: string;

    /**
     *  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"; }
        const network = Network.from(_network);
        if (applicationId == null) { applicationId = defaultApplicationId; }
        if (applicationSecret == null) { applicationSecret = null; }

        const options = { staticNetwork: network };

        const request = PocketProvider.getRequest(network, applicationId, applicationSecret);
        super(request, network, options);

        defineProperties<PocketProvider>(this, { applicationId, applicationSecret });
    }

    _getProvider(chainId: number): AbstractProvider {
        try {
            return new PocketProvider(chainId, this.applicationId, this.applicationSecret);
        } catch (error) { }
        return super._getProvider(chainId);
    }

    /**
     *  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; }

        const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v1/lb/${ applicationId }`);
        request.allowGzip = true;

        if (applicationSecret) {
            request.setCredentials("", applicationSecret);
        }

        if (applicationId === defaultApplicationId) {
            request.retryFunc = async (request, response, attempt) => {
                showThrottleMessage("PocketProvider");
                return true;
            };
        }

        return request;
    }

    isCommunityResource(): boolean {
        return (this.applicationId === defaultApplicationId);
    }
}