From 9c0730e051f8cec2191869ef70dab6a066b140a3 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Thu, 13 Apr 2023 03:15:26 +0530 Subject: [PATCH] floBlockchainAPI v2.4.5: getBalance improvements - Updated getBalance: now chain queries for addr with more than 1000 tx to get the net balance. - (optional): can use 'after' parameter to get the balance(update) after a given txid --- floBlockchainAPI.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/floBlockchainAPI.js b/floBlockchainAPI.js index 3ddf7ee..e43a888 100644 --- a/floBlockchainAPI.js +++ b/floBlockchainAPI.js @@ -1,4 +1,4 @@ -(function (EXPORTS) { //floBlockchainAPI v2.4.4 +(function (EXPORTS) { //floBlockchainAPI v2.4.5 /* FLO Blockchain Operator to send/receive data from blockchain using API calls*/ 'use strict'; const floBlockchainAPI = EXPORTS; @@ -21,6 +21,7 @@ util.Sat_to_FLO = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8)); util.FLO_to_Sat = value => parseInt(value * SATOSHI_IN_BTC); + util.toFixed = value => parseFloat((value).toFixed(8)); Object.defineProperties(floBlockchainAPI, { sendAmt: { @@ -120,11 +121,21 @@ } //Get balance for the given Address - const getBalance = floBlockchainAPI.getBalance = function (addr) { + const getBalance = floBlockchainAPI.getBalance = function (addr, after = null) { return new Promise((resolve, reject) => { - promisedAPI(`api/addr/${addr}/balance`) - .then(balance => resolve(parseFloat(balance))) - .catch(error => reject(error)); + let api = `api/addr/${addr}/balance`; + if (after) { + if (typeof after === 'string' && /^[0-9a-z]{64}$/i.test(after)) + api += '?after=' + after; + else return reject("Invalid 'after' parameter"); + } + promisedAPI(api).then(result => { + if (typeof result === 'object' && result.lastItem) { + getBalance(addr, result.lastItem) + .then(r => resolve(util.toFixed(r + result.data))) + .catch(error => reject(error)) + } else resolve(result); + }).catch(error => reject(error)) }); }