|
| 1 | + |
| 2 | +/** |
| 3 | + * [[link-blockscout]] provides a third-party service for connecting to |
| 4 | + * various blockchains over JSON-RPC. |
| 5 | + * |
| 6 | + * **Supported Networks** |
| 7 | + * |
| 8 | + * - Ethereum Mainnet (``mainnet``) |
| 9 | + * - Sepolia Testnet (``sepolia``) |
| 10 | + * - Ethereum Classic (``classic``) |
| 11 | + * - Arbitrum (``arbitrum``) |
| 12 | + * - Base (``base``) |
| 13 | + * - Base Sepolia Testnet (``base-sepolia``) |
| 14 | + * - Gnosis (``xdai``) |
| 15 | + * - Optimism (``optimism``) |
| 16 | + * - Optimism Sepolia Testnet (``optimism-sepolia``) |
| 17 | + * - Polygon (``matic``) |
| 18 | + * |
| 19 | + * @_subsection: api/providers/thirdparty:Blockscout [providers-blockscout] |
| 20 | + */ |
| 21 | +import { |
| 22 | + defineProperties, FetchRequest, assertArgument |
| 23 | +} from "../utils/index.js"; |
| 24 | + |
| 25 | +import { Network } from "./network.js"; |
| 26 | +import { JsonRpcProvider } from "./provider-jsonrpc.js"; |
| 27 | + |
| 28 | +import type { AbstractProvider } from "./abstract-provider.js"; |
| 29 | +import type { CommunityResourcable } from "./community.js"; |
| 30 | +import type { Networkish } from "./network.js"; |
| 31 | + |
| 32 | + |
| 33 | +function getUrl(name: string): string { |
| 34 | + switch(name) { |
| 35 | + case "mainnet": |
| 36 | + return "https:/\/eth.blockscout.com/api/eth-rpc"; |
| 37 | + case "sepolia": |
| 38 | + return "https:/\/eth-sepolia.blockscout.com/api/eth-rpc"; |
| 39 | + case "holesky": |
| 40 | + return "https:/\/eth-holesky.blockscout.com/api/eth-rpc"; |
| 41 | + |
| 42 | + case "classic": |
| 43 | + return "https:/\/etc.blockscout.com/api/eth-rpc"; |
| 44 | + |
| 45 | + case "arbitrum": |
| 46 | + return "https:/\/arbitrum.blockscout.com/api/eth-rpc"; |
| 47 | + |
| 48 | + case "base": |
| 49 | + return "https:/\/base.blockscout.com/api/eth-rpc"; |
| 50 | + case "base-sepolia": |
| 51 | + return "https:/\/base-sepolia.blockscout.com/api/eth-rpc"; |
| 52 | + |
| 53 | + case "matic": |
| 54 | + return "https:/\/polygon.blockscout.com/api/eth-rpc"; |
| 55 | + |
| 56 | + case "optimism": |
| 57 | + return "https:/\/optimism.blockscout.com/api/eth-rpc"; |
| 58 | + case "optimism-sepolia": |
| 59 | + return "https:/\/optimism-sepolia.blockscout.com/api/eth-rpc"; |
| 60 | + |
| 61 | + case "xdai": |
| 62 | + return "https:/\/gnosis.blockscout.com/api/eth-rpc"; |
| 63 | + } |
| 64 | + |
| 65 | + assertArgument(false, "unsupported network", "network", name); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +/** |
| 70 | + * The **BlockscoutProvider** connects to the [[link-blockscout]] |
| 71 | + * JSON-RPC end-points. |
| 72 | + * |
| 73 | + * By default, a highly-throttled API key is used, which is |
| 74 | + * appropriate for quick prototypes and simple scripts. To |
| 75 | + * gain access to an increased rate-limit, it is highly |
| 76 | + * recommended to [sign up here](link-blockscout). |
| 77 | + */ |
| 78 | +export class BlockscoutProvider extends JsonRpcProvider implements CommunityResourcable { |
| 79 | + /** |
| 80 | + * The API key. |
| 81 | + */ |
| 82 | + readonly apiKey!: null | string; |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a new **BlockscoutProvider**. |
| 86 | + */ |
| 87 | + constructor(_network?: Networkish, apiKey?: null | string) { |
| 88 | + if (_network == null) { _network = "mainnet"; } |
| 89 | + const network = Network.from(_network); |
| 90 | + |
| 91 | + if (apiKey == null) { apiKey = null; } |
| 92 | + |
| 93 | + const request = BlockscoutProvider.getRequest(network); |
| 94 | + super(request, network, { staticNetwork: network }); |
| 95 | + |
| 96 | + defineProperties<BlockscoutProvider>(this, { apiKey }); |
| 97 | + } |
| 98 | + |
| 99 | + _getProvider(chainId: number): AbstractProvider { |
| 100 | + try { |
| 101 | + return new BlockscoutProvider(chainId, this.apiKey); |
| 102 | + } catch (error) { } |
| 103 | + return super._getProvider(chainId); |
| 104 | + } |
| 105 | + |
| 106 | + isCommunityResource(): boolean { |
| 107 | + return (this.apiKey === null); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns a prepared request for connecting to %%network%% |
| 112 | + * with %%apiKey%%. |
| 113 | + */ |
| 114 | + static getRequest(network: Network): FetchRequest { |
| 115 | + const request = new FetchRequest(getUrl(network.name)); |
| 116 | + request.allowGzip = true; |
| 117 | + return request; |
| 118 | + } |
| 119 | +} |
0 commit comments