UNPKG

@ledgerhq/live-common

Version:
67 lines 3.01 kB
import memoize from "lodash/memoize"; import { getCryptoCurrencyById } from "../currencies"; import { decodeAccountId } from "@ledgerhq/ledger-wallet-framework/account/index"; import { fromAccountRaw as commonFromAccountRaw, toAccountRaw as commonToAccountRaw, fromOperationRaw as commonFromOperationRaw, toOperationRaw as commonToOperationRaw, } from "@ledgerhq/ledger-wallet-framework/serialization/index"; import { getAccountBridge } from "../bridge"; import { getAccountBridgeByFamily } from "../bridge/impl"; export function toBalanceHistoryRaw(b) { return b.map(({ date, value }) => [date.toISOString(), value.toString()]); } export const toOperationRaw = (operation, preserveSubOperation) => { let toOperationRaw; if (operation.extra) { const family = inferFamilyFromAccountId(operation.accountId); if (family) { const bridge = getAccountBridgeByFamily(family, operation.accountId); toOperationRaw = bridge.toOperationExtraRaw; } } return commonToOperationRaw(operation, preserveSubOperation, toOperationRaw); }; export const fromOperationRaw = (operationRaw, accountId, subAccounts) => { let fromOperationRaw; if (operationRaw.extra) { const family = inferFamilyFromAccountId(operationRaw.accountId); if (family) { const bridge = getAccountBridgeByFamily(family, accountId); fromOperationRaw = bridge.fromOperationExtraRaw; } } return commonFromOperationRaw(operationRaw, accountId, subAccounts, fromOperationRaw); }; export async function fromAccountRaw(rawAccount) { const currency = getCryptoCurrencyById(rawAccount.currencyId); const bridge = getAccountBridgeByFamily(currency.family, rawAccount.id); return await commonFromAccountRaw(rawAccount, { assignFromAccountRaw: bridge.assignFromAccountRaw, assignFromTokenAccountRaw: bridge.assignFromTokenAccountRaw, fromOperationExtraRaw: bridge.fromOperationExtraRaw, }); } export function toAccountRaw(account, userData) { const bridge = getAccountBridge(account); const commonAccountRaw = commonToAccountRaw(account, { assignToAccountRaw: bridge.assignToAccountRaw, assignToTokenAccountRaw: bridge.assignToTokenAccountRaw, toOperationExtraRaw: bridge.toOperationExtraRaw, }); // extend with user data fields if (userData) { commonAccountRaw.name = userData.name; commonAccountRaw.starred = userData.starredIds.includes(commonAccountRaw.id); for (const tokenAccount of commonAccountRaw.subAccounts || []) { tokenAccount.starred = userData.starredIds.includes(tokenAccount.id); } } return commonAccountRaw; } const inferFamilyFromAccountId = memoize(accountId => { try { const { currencyId } = decodeAccountId(accountId); return getCryptoCurrencyById(currencyId).family; } catch { return null; } }); //# sourceMappingURL=serialization.js.map