From 8b883fe3667f6984a3dbbd21530e426a93042a29 Mon Sep 17 00:00:00 2001 From: Atharva Date: Wed, 8 May 2024 20:39:23 +0530 Subject: [PATCH] fix: get balance for new accounts --- src/api.ts | 7 +++---- src/utils.ts | 14 +++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/api.ts b/src/api.ts index eaf9fd87..24978326 100755 --- a/src/api.ts +++ b/src/api.ts @@ -1063,9 +1063,8 @@ export const methods = { countSuccessResponse(api_name, 'success', 'validator') } else { if (verbose) console.log('Shardeum balance', typeof account.balance, account.balance) - const SHD = intStringToHex(account.balance) - if (verbose) console.log('SHD', typeof SHD, SHD) - balance = intStringToHex(account.balance) + const balance = intStringToHex(account.balance) + if (verbose) console.log('SHD', typeof balance, balance) logEventEmitter.emit('fn_end', ticket, { nodeUrl, success: true }, performance.now()) callback(null, balance) countSuccessResponse(api_name, 'success', 'validator') @@ -3632,7 +3631,7 @@ export const methods = { countFailedResponse(api_name, 'Invalid address') return } - + if (!isValidAddress(callObj.from)) { if (verbose) console.log('Invalid params: `from` is not valid address', callObj.from) callback({ code: -32000, message: 'Invalid params: `from` is not valid address' }, null) diff --git a/src/utils.ts b/src/utils.ts index 8963facb..843c0d24 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -317,6 +317,11 @@ export async function requestWithRetry( if (retry <= maxRetry) { if (verbose) console.log(`(Attempt ${retry}) Node is unable to respond. Trying a new node.`) await waitRandomSecond() + } else if (route.includes('/account/')) { + // Not able to find account after all retries + // This can happen in the case of accounts that have not been used yet + // or are uninitialized. Return null account to be handled by the caller + return { data: { account: null } } } else { if (verbose) console.log('Request was unsuccessful after all retries.') } @@ -1038,9 +1043,12 @@ export async function getTransactionReceipt(hash: string) { */ export function getFilterId(): string { - return '0x' + createHash('sha256') - .update(randomBytes(16).toString('hex') + Date.now()) - .digest('hex') + return ( + '0x' + + createHash('sha256') + .update(randomBytes(16).toString('hex') + Date.now()) + .digest('hex') + ) } export function parseFilterDetails(filter: Filter): { address: string; topics: string[] } {