@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
31 lines • 1.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateProviderTotals = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const calculateProviderTotals = (currencies, groupedAccountsByAsset) => {
let totalBalance = new bignumber_js_1.default(0);
let totalFiatValue = new bignumber_js_1.default(0);
let hasAccounts = false;
let referenceCurrency = null;
for (const currency of currencies) {
const assetGroup = groupedAccountsByAsset[currency.id];
if (assetGroup?.accounts.length > 0) {
const refCurrency = referenceCurrency ?? assetGroup.referenceCurrency;
const magnitudeDiff = refCurrency.units[0].magnitude - assetGroup.referenceCurrency.units[0].magnitude;
const normalizedBalance = assetGroup.totalBalance.shiftedBy(magnitudeDiff);
totalBalance = totalBalance.plus(normalizedBalance);
totalFiatValue = totalFiatValue.plus(assetGroup.totalFiatValue);
hasAccounts = true;
// Use the referenceCurrency from the first asset group with accounts
if (!referenceCurrency) {
referenceCurrency = assetGroup.referenceCurrency;
}
}
}
return { totalBalance, totalFiatValue, hasAccounts, referenceCurrency };
};
exports.calculateProviderTotals = calculateProviderTotals;
//# sourceMappingURL=calculateProviderTotal.js.map