Skip to content

Commit

Permalink
Add support for overriding Ethers ConnectionInfo (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebrianchen authored May 8, 2024
1 parent 057ad2b commit 96b3f62
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/api/alchemy-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ConnectionInfo } from '@ethersproject/web';

import { AlchemySettings, Network } from '../types/types';
import {
AlchemyApiType,
Expand Down Expand Up @@ -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<ConnectionInfo>;

/**
* The optional hardcoded URL to send requests to instead of using the network
* and apiKey.
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/api/alchemy-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TransactionReceipt
} from '@ethersproject/abstract-provider';
import { BigNumberish } from '@ethersproject/bignumber';
import { ConnectionInfo } from '@ethersproject/web';

import {
ERC1155Metadata,
Expand Down Expand Up @@ -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<ConnectionInfo>;
}

/**
Expand Down

0 comments on commit 96b3f62

Please sign in to comment.