Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rajranjan0608 committed Feb 8, 2024
1 parent e09579a commit 8e30d16
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
32 changes: 23 additions & 9 deletions mainnetCheckService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,30 +41,37 @@ export class MainnetCheckService {
* d. If diff == 0
* i. fail
*/
async checkAddressValidity(address: string): Promise<boolean> {
async checkAddressValidity(address: string): Promise<ResponseType> {
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
Expand Down Expand Up @@ -89,12 +102,12 @@ export class MainnetCheckService {
}
}

async updateAddressStatus(address: string, checkCount: number = 0, nonce?: number): Promise<AddressStatus> {
async updateAddressStatus(address: string, checkCount: number = 0, nonce?: number): Promise<AddressStatus | undefined> {
// 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
}
Expand All @@ -118,6 +131,7 @@ export class MainnetCheckService {
type: 'PuttingAddressTrackerError',
item: error
}));
return
}

return {
Expand Down
7 changes: 5 additions & 2 deletions utils/pipelineChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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! "
Expand Down

0 comments on commit 8e30d16

Please sign in to comment.