From cb0fd036c64af8ba548134a9330e719d6c562843 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Tue, 26 Nov 2024 21:42:51 +0700 Subject: [PATCH] fix, test: chainId to RPC map --- lib/handlers/hard-quote/handler.ts | 12 ++++++------ lib/handlers/hard-quote/injector.ts | 4 ++-- test/handlers/hard-quote/handler.test.ts | 4 ++++ test/handlers/hard-quote/handlerDutchV3.test.ts | 4 ++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index 7746963..b119014 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -56,10 +56,6 @@ export class QuoteHandler extends APIGLambdaHandler< metric.putMetric(Metric.QUOTE_REQUESTED, 1, MetricLoggerUnit.Count); const provider = chainIdRpcMap.get(requestBody.tokenInChainId); - if (!provider) - throw new Error( - `No rpc provider found for chain: ${requestBody.tokenInChainId}` - ); const orderParser = new UniswapXOrderParser(); const orderType: OrderType = orderParser.getOrderTypeFromEncoded( @@ -230,7 +226,7 @@ export function getCosignerData( export async function getDefaultCosignerData( request: HardQuoteRequest, orderType: OrderType, - provider: ethers.providers.Provider + provider: ethers.providers.JsonRpcProvider | undefined ): Promise { switch (orderType) { case OrderType.Dutch_V2: @@ -316,7 +312,11 @@ function getDefaultV2CosignerData(request: HardQuoteRequest): CosignerData { }; } -async function getDefaultV3CosignerData(request: HardQuoteRequest, provider: ethers.providers.Provider): Promise { +async function getDefaultV3CosignerData(request: HardQuoteRequest, provider: ethers.providers.JsonRpcProvider | undefined): Promise { + if (!provider) + throw new Error( + `No rpc provider found for chain: ${request.tokenInChainId}, which is required for V3 Dutch orders` + ); const currentBlock = await provider.getBlockNumber(); return { diff --git a/lib/handlers/hard-quote/injector.ts b/lib/handlers/hard-quote/injector.ts index 24a063c..821dd04 100644 --- a/lib/handlers/hard-quote/injector.ts +++ b/lib/handlers/hard-quote/injector.ts @@ -78,14 +78,14 @@ export class QuoteInjector extends ApiInjector(); + const chainIdRpcMap = new Map(); supportedChains.forEach( chainId => { const rpcUrl = checkDefined( process.env[`RPC_${chainId}`], `RPC_${chainId} is not defined` ); - const provider = new ethers.providers.StaticJsonRpcProvider(rpcUrl, chainId); // specify chainId to avoid detecctNetwork() call on initialization + const provider = new ethers.providers.JsonRpcProvider(rpcUrl, chainId); chainIdRpcMap.set(chainId, provider); } ); diff --git a/test/handlers/hard-quote/handler.test.ts b/test/handlers/hard-quote/handler.test.ts index be570bb..3df4cea 100644 --- a/test/handlers/hard-quote/handler.test.ts +++ b/test/handlers/hard-quote/handler.test.ts @@ -107,6 +107,10 @@ describe('Quote handler', () => { return { quoters, orderServiceProvider: new MockOrderServiceProvider(), + // Mock chainIdRpcMap + chainIdRpcMap: new Map([ + [42161, new ethers.providers.StaticJsonRpcProvider()], + ]), }; }, getRequestInjected: () => requestInjectedMock, diff --git a/test/handlers/hard-quote/handlerDutchV3.test.ts b/test/handlers/hard-quote/handlerDutchV3.test.ts index 0e3660f..c3b7101 100644 --- a/test/handlers/hard-quote/handlerDutchV3.test.ts +++ b/test/handlers/hard-quote/handlerDutchV3.test.ts @@ -109,6 +109,10 @@ describe('Quote handler', () => { return { quoters, orderServiceProvider: new MockOrderServiceProvider(), + // Mock chainIdRpcMap + chainIdRpcMap: new Map([ + [42161, new ethers.providers.StaticJsonRpcProvider()], + ]), }; }, getRequestInjected: () => requestInjectedMock,