From 04e283bf53b1ee71bb7ab539a425e2fc44c95830 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Fri, 24 Jan 2025 10:27:18 +0100 Subject: [PATCH] refactor: simplify NoRPC interface --- src/curve.ts | 13 +++++-------- src/index.ts | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/curve.ts b/src/curve.ts index 931a9233..5648f9ff 100644 --- a/src/curve.ts +++ b/src/curve.ts @@ -92,7 +92,6 @@ export type ContractItem = { contract: Contract, multicallContract: MulticallCon class Curve implements ICurve { provider: ethers.BrowserProvider | ethers.JsonRpcProvider; - chainOptions?: {chainId: number, name: string}; isNoRPC: boolean; multicallProvider: MulticallProvider; signer: ethers.Signer | null; @@ -147,7 +146,7 @@ class Curve implements ICurve { async init( providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy' | 'NoRPC', - providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string } | {chainId: number, networkName: string}, + providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string } | 'NoRPC', options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number } = {} // gasPrice in Gwei ): Promise { // @ts-ignore @@ -230,18 +229,16 @@ class Curve implements ICurve { this.provider = new ethers.AlchemyProvider(providerSettings.network, providerSettings.apiKey); this.signer = null; } else if (providerType.toLowerCase() === 'NoRPC'.toLowerCase()) { - providerSettings = providerSettings as { chainId: number, networkName: string }; this.isNoRPC = true; - this.chainOptions = { - chainId: providerSettings.chainId as number, - name: providerSettings.networkName as string, - } this.signer = null; + if (!options.chainId) { + throw Error('ChainId is required for NoRPC provider'); + } } else { throw Error('Wrong providerType'); } - const network = this.chainOptions || await this.provider.getNetwork(); + const network = this.isNoRPC ? { chainId: options.chainId!, name: 'NoRPC' } : await this.provider.getNetwork(); console.log("CURVE-JS IS CONNECTED TO NETWORK:", { name: network.name.toUpperCase(), chainId: Number(network.chainId) }); this.chainId = Number(network.chainId) === 133 || Number(network.chainId) === 31337 ? 1 : Number(network.chainId) as IChainId; diff --git a/src/index.ts b/src/index.ts index 8f94f1f2..b005ec03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -140,8 +140,8 @@ import { } from "./dao.js"; async function init ( - providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy', - providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string }, + providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy' | 'NoRPC', + providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string } | 'NoRPC', options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number } = {} ): Promise { await _curve.init(providerType, providerSettings, options);