Helper: merge/sum balance objects
See original GitHub issueAs discussed in Discord, I leave our function to merge/sum balance objects here:
module.exports.mergeIntoBalance = (balanceObjectToMerge, intoBalance) => {
if (!balanceObjectToMerge) {
return intoBalance;
}
// copy object
const tempIntoBalance = { ...intoBalance };
// merge main COIN objects
Object.keys(balanceObjectToMerge).forEach((balanceKey) => {
const coinObject = balanceObjectToMerge[balanceKey];
// console.log('mergeIntoBalance -> balanceKey', balanceKey, coinObject);
// check if its a coin object: FIL { free: 0, used: 0, total: 0 }
if (
typeof coinObject === 'undefined' ||
!Object.keys(coinObject).includes('free') ||
!Object.keys(coinObject).includes('used') ||
!Object.keys(coinObject).includes('total')
) {
// console.warn('ignoring key:', balanceKey);
return;
}
// check if we have it in the main (spot) balance object
if (!tempIntoBalance[balanceKey]) {
// not in balance, just add it onto it
tempIntoBalance[balanceKey] = coinObject;
} else {
// already in balance, merge it
tempIntoBalance[balanceKey].free = tempIntoBalance[balanceKey].free + coinObject.free;
tempIntoBalance[balanceKey].used = tempIntoBalance[balanceKey].used + coinObject.used;
tempIntoBalance[balanceKey].total = tempIntoBalance[balanceKey].total + coinObject.total;
// console.log('after merge', balanceKey, tempIntoBalance[balanceKey]);
}
});
// merge free, used, total
['free', 'used', 'total'].forEach((key) => {
if (!balanceObjectToMerge[key]) {
return;
}
// console.log('merge for key:', key);
Object.keys(balanceObjectToMerge[key]).forEach((balanceKey) => {
const coinValue = balanceObjectToMerge[key][balanceKey];
// console.log('mergeIntoBalance -> free, balanceKey', balanceKey, coinValue);
// check if we have it in the main (spot) balance object
if (tempIntoBalance[key][balanceKey] === undefined) {
// not in balance, just add it onto it
tempIntoBalance[key][balanceKey] = coinValue;
} else {
// already in balance, merge it
tempIntoBalance[key][balanceKey] = tempIntoBalance[key][balanceKey] + coinValue;
// console.log('after merge', balanceKey, tempIntoBalance[key][balanceKey]);
}
});
});
return tempIntoBalance;
};
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
php - How to sum all column values in multi-dimensional array?
a) Initialize the 'sum' array keys outside of the loop (Gumbo). Should help with performance on very large arrays (not tested yet!)
Read more >Javascript: how to merge multiple objects with sum of values
How can we merge all these objects (baskets) into one and count total sum of each fruit? Let's create helper method.
Read more >SPEX Help Center Documentation - Zenodo
enjoy all the capabilities of SPEX, two things need to be arranged ... ebal (T1): the energy balance contributions of each layer (only...
Read more >Using Plant Epidemiological Methods To Track Computer ...
I learned several things from him that I will carry with me for the rest ... would help build an early warning system...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I understand what you say, I mean, when the error is ‘account_not_found’ or something like which doesn’t play any role in summing the balance, then the error can be muted inside ccxt’s implementation. but if the error is other (like exchange error, network error, rate-limits or other issue, that might cause failure to correctly sum up the balance) in such cases the error should be thrown.
@ttodua yes, but then the
fetchBalanceAll
would never work in such cases, and the user needs to build it themselves like we do, merging multiple balance responses.