@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
119 lines • 5.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDetailedAccountsCore = void 0;
const react_1 = require("react");
const logic_1 = require("@ledgerhq/live-countervalues/logic");
const derivation_1 = require("@ledgerhq/coin-framework/derivation");
const sortAccountsByFiatValue_1 = require("../utils/sortAccountsByFiatValue");
/**
* Core hook for creating detailed accounts with shared logic
* This contains the business logic that can be reused across platforms
*/
function useDetailedAccountsCore(counterValuesState, counterValueCurrency) {
/**
* Calculate fiat value for an account using counter values
*/
const calculateFiatValue = (0, react_1.useCallback)((account) => {
const currency = account.type === "Account" ? account.currency : account.token;
const fiatValue = (0, logic_1.calculate)(counterValuesState, {
from: currency,
to: counterValueCurrency,
value: account.balance.toNumber(),
});
return fiatValue || 0;
}, [counterValuesState, counterValueCurrency]);
/**
* Create base detailed accounts from account tuples
* This is the core transformation logic shared between platforms
*/
const createBaseDetailedAccounts = (0, react_1.useCallback)((params) => {
const { accountTuples, accountNameMap, isTokenCurrency } = params;
const baseAccounts = accountTuples.map(tuple => {
const { account, subAccount } = tuple;
const address = account.freshAddress;
if (isTokenCurrency && subAccount) {
const parentAccountName = accountNameMap[account.id];
const details = subAccount.token;
return {
id: subAccount.id,
name: parentAccountName ?? details.name,
ticker: details.ticker,
balance: subAccount.balance,
balanceUnit: subAccount.token.units[0],
fiatValue: calculateFiatValue(subAccount),
address,
cryptoId: details.id,
parentId: details.parentCurrency.id,
};
}
else {
const accountName = accountNameMap[account.id];
const details = account.currency;
return {
id: account.id,
name: accountName ?? details.name,
ticker: details.ticker,
balance: account.balance,
balanceUnit: account.currency.units[0],
fiatValue: calculateFiatValue(account),
address,
cryptoId: details.id,
};
}
});
return (0, sortAccountsByFiatValue_1.sortAccountsByFiatValue)(baseAccounts);
}, [calculateFiatValue]);
/**
* Create extended detailed accounts that include account references
* Used by mobile implementation that needs access to the full account objects
*/
const createExtendedDetailedAccounts = (0, react_1.useCallback)((params) => {
const { accountTuples, accountNameMap, isTokenCurrency } = params;
const extendedAccounts = accountTuples.map(tuple => {
const { account, subAccount } = tuple;
const protocol = (0, derivation_1.getTagDerivationMode)(account.currency, account.derivationMode) ?? "";
const address = account.freshAddress;
if (isTokenCurrency && subAccount) {
const parentAccountName = accountNameMap[account.id];
const details = subAccount.token;
return {
id: subAccount.id,
name: parentAccountName ?? details.name,
ticker: details.ticker,
balance: subAccount.balance,
balanceUnit: subAccount.token.units[0],
fiatValue: calculateFiatValue(subAccount),
address,
cryptoId: details.id,
parentId: details.parentCurrency.id,
account: subAccount,
parentAccount: account,
};
}
else {
const accountName = accountNameMap[account.id];
const details = account.currency;
return {
id: account.id,
name: accountName ?? details.name,
ticker: details.ticker,
balance: account.balance,
balanceUnit: account.currency.units[0],
fiatValue: calculateFiatValue(account),
address,
cryptoId: details.id,
account,
protocol,
};
}
});
return (0, sortAccountsByFiatValue_1.sortAccountsByFiatValue)(extendedAccounts);
}, [calculateFiatValue]);
return {
calculateFiatValue,
createBaseDetailedAccounts,
createExtendedDetailedAccounts,
};
}
exports.useDetailedAccountsCore = useDetailedAccountsCore;
//# sourceMappingURL=useDetailedAccountsCore.js.map