From 96b3f62ae096f4249366cfd2eff9fe15554882be Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Wed, 8 May 2024 10:59:28 -0500 Subject: [PATCH] Add support for overriding Ethers ConnectionInfo (#430) --- src/api/alchemy-config.ts | 5 +++++ src/api/alchemy-provider.ts | 10 +++++++++- src/types/types.ts | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/api/alchemy-config.ts b/src/api/alchemy-config.ts index 15584fb54..b075eb3f4 100644 --- a/src/api/alchemy-config.ts +++ b/src/api/alchemy-config.ts @@ -1,3 +1,5 @@ +import { ConnectionInfo } from '@ethersproject/web'; + import { AlchemySettings, Network } from '../types/types'; import { AlchemyApiType, @@ -31,6 +33,8 @@ export class AlchemyConfig { /** Setting to enable automatic batching on json-rpc requests. Defaults to false.*/ readonly batchRequests: boolean; + readonly connectionInfoOverrides?: Partial; + /** * The optional hardcoded URL to send requests to instead of using the network * and apiKey. @@ -69,6 +73,7 @@ export class AlchemyConfig { this.authToken = config?.authToken; this.batchRequests = config?.batchRequests || false; this.requestTimeout = config?.requestTimeout || DEFAULT_REQUEST_TIMEOUT; + this.connectionInfoOverrides = config?.connectionInfoOverrides; } /** diff --git a/src/api/alchemy-provider.ts b/src/api/alchemy-provider.ts index 77381aac9..4ed8b5767 100644 --- a/src/api/alchemy-provider.ts +++ b/src/api/alchemy-provider.ts @@ -55,7 +55,7 @@ export class AlchemyProvider // Generate our own connection info with the correct endpoint URLs. const alchemyNetwork = AlchemyProvider.getAlchemyNetwork(config.network); - const connection = AlchemyProvider.getAlchemyConnectionInfo( + let connection = AlchemyProvider.getAlchemyConnectionInfo( alchemyNetwork, apiKey, 'http' @@ -69,6 +69,14 @@ export class AlchemyProvider connection.throttleLimit = config.maxRetries; + // Add user provided overrides if they exist. + if (config.connectionInfoOverrides) { + connection = { + ...connection, + ...config.connectionInfoOverrides + }; + } + // Normalize the Alchemy named network input to the network names used by // ethers. This allows the parent super constructor in JsonRpcProvider to // correctly set the network. diff --git a/src/types/types.ts b/src/types/types.ts index d84aef43c..1864f275f 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -4,6 +4,7 @@ import { TransactionReceipt } from '@ethersproject/abstract-provider'; import { BigNumberish } from '@ethersproject/bignumber'; +import { ConnectionInfo } from '@ethersproject/web'; import { ERC1155Metadata, @@ -68,6 +69,22 @@ export interface AlchemySettings { * This implementation is based on the `JsonRpcBatchProvider` in ethers. */ batchRequests?: boolean; + + /** + * Optional overrides on the Ethers `ConnectionInfo` object used to configure + * the underlying JsonRpcProvider. This field is for advanced users who want + * to customize the provider's behavior. + * + * This override is applied last, so it will override any other + * AlchemySettings properties that affect the connection. + * + * Note that modifying the ConnectionInfo may break Alchemy SDK's default + * connection/url logic. It also does not affect `nft` and `notify` + * namespaces. + * + * {@link https://docs.ethers.org/v5/api/utils/web/#ConnectionInfo} + */ + connectionInfoOverrides?: Partial; } /**