question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Helper: merge/sum balance objects

See original GitHub issue

As 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:open
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ttoduacommented, Feb 23, 2022

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.

0reactions
phips28commented, Feb 22, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found