From 8e30d16a7c2b71a20e111e0b9906122718f607b3 Mon Sep 17 00:00:00 2001 From: Raj Ranjan Date: Thu, 8 Feb 2024 18:09:40 +0530 Subject: [PATCH] error handling --- mainnetCheckService.ts | 32 +++++++++++++++++++++++--------- utils/pipelineChecks.ts | 7 +++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/mainnetCheckService.ts b/mainnetCheckService.ts index c823cc3..caf5179 100644 --- a/mainnetCheckService.ts +++ b/mainnetCheckService.ts @@ -10,6 +10,12 @@ type AddressStatus = { lastUsedNonce: number, } +type ResponseType = { + isValid: boolean, + internalError: boolean, + message: string +} + export class MainnetCheckService { private readonly documentClient: DynamoDBDocumentClient private readonly RPC: string @@ -35,30 +41,37 @@ export class MainnetCheckService { * d. If diff == 0 * i. fail */ - async checkAddressValidity(address: string): Promise { + async checkAddressValidity(address: string): Promise { + const response: ResponseType = { isValid: false, internalError: false, message: "" }; + let addressStatus = await this.getAddressStatus(address) if (!addressStatus) { // update address status addressStatus = await this.updateAddressStatus(address) + if (!addressStatus) { + response.internalError = true + return response + } } if (addressStatus.checkCount < MAX_ADDRESS_CHECK_COUNT) { - this.updateAddressStatus(address, ++addressStatus.checkCount, addressStatus.lastUsedNonce) - return true + this.updateAddressStatus(address, ++addressStatus.checkCount, addressStatus.lastUsedNonce); + response.isValid = true } else { const currentNonce = await getNonce(this.RPC, address) if (!currentNonce) { - throw "Error fetching nonce..." + response.internalError = true + return response } const diff = currentNonce - addressStatus.lastUsedNonce if (diff > 0) { const updatedCheckCount = Math.max(0, addressStatus.checkCount - diff) + 1 this.updateAddressStatus(address, updatedCheckCount, currentNonce) - return true - } else { - return false + response.isValid = true } } + + return response } // Utility @@ -89,12 +102,12 @@ export class MainnetCheckService { } } - async updateAddressStatus(address: string, checkCount: number = 0, nonce?: number): Promise { + async updateAddressStatus(address: string, checkCount: number = 0, nonce?: number): Promise { // if nonce is not provided, fetch from network if (!nonce) { const currentNonce = await getNonce(this.RPC, address) if (!currentNonce) { - throw "Error fetching nonce..." + return } nonce = currentNonce } @@ -118,6 +131,7 @@ export class MainnetCheckService { type: 'PuttingAddressTrackerError', item: error })); + return } return { diff --git a/utils/pipelineChecks.ts b/utils/pipelineChecks.ts index 4568a5c..9990f6e 100644 --- a/utils/pipelineChecks.ts +++ b/utils/pipelineChecks.ts @@ -57,12 +57,15 @@ export async function checkMainnetBalancePipeline( ) { const {isValid, balance} = await checkMainnetBalance(rpc, address) if (isValid) { - if (await mainnetCheckService.checkAddressValidity(address)) { + const mainnetAddressValidity = await mainnetCheckService.checkAddressValidity(address) + if (mainnetAddressValidity.isValid) { pipelineCheckValidity.isValid = true pipelineCheckValidity.checkPassedType = PIPELINE_CHECKS.MAINNET_BALANCE pipelineCheckValidity.mainnetBalance = balance + } else if (mainnetAddressValidity.internalError) { + pipelineCheckValidity.errorMessage = "Some internal error occurred during mainnet check. " } else { - pipelineCheckValidity.errorMessage = "Address has exhausted maximum balance checks! Please do some mainnet transactinos." + pipelineCheckValidity.errorMessage = "Address has exhausted maximum balance checks! Please do some mainnet transactions. " } } else { pipelineCheckValidity.errorMessage = "Mainnet balance check failed! "