Skip to content

Commit

Permalink
feat: allow overriding alchemy provider rpc url (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokok123 authored Aug 8, 2023
1 parent f3592ac commit 6b7c4b9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/alchemy/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ import {
type AlchemyGasManagerConfig,
} from "./middleware/gas-manager.js";

type ConnectionConfig =
| {
apiKey: string;
rpcUrl: undefined;
}
| { rpcUrl: string; apiKey: undefined };

export type AlchemyProviderConfig = {
apiKey: string;
chain: Chain | number;
entryPointAddress: Address;
account?: BaseSmartContractAccount;
Expand All @@ -33,24 +39,28 @@ export type AlchemyProviderConfig = {
/** this adds a percent buffer on top of the fee estimated (default 5%)*/
maxPriorityFeeBufferPercent?: bigint;
};
};
} & ConnectionConfig;

export class AlchemyProvider extends SmartAccountProvider<HttpTransport> {
constructor({
apiKey,
chain,
entryPointAddress,
account,
opts,
feeOpts,
...connectionConfig
}: AlchemyProviderConfig) {
const _chain =
typeof chain === "number" ? SupportedChains.get(chain) : chain;
if (!_chain || !_chain.rpcUrls["alchemy"]) {
throw new Error(`AlchemyProvider: chain (${chain}) not supported`);
}

const rpcUrl = `${_chain.rpcUrls.alchemy.http[0]}/${apiKey}`;
const rpcUrl =
connectionConfig.apiKey !== undefined
? `${_chain.rpcUrls.alchemy.http[0]}/${connectionConfig.apiKey}`
: connectionConfig.rpcUrl;

super(rpcUrl, entryPointAddress, _chain, account, opts);

withAlchemyGasFeeEstimator(
Expand Down

0 comments on commit 6b7c4b9

Please sign in to comment.