@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
39 lines • 1.76 kB
JavaScript
import { getAccountCurrency, getMainAccount } from "../../account";
export default ({ account, operation, transaction, swap, swapId, }) => {
const { exchange, exchangeRate } = swap;
const { fromAccount, toAccount, toParentAccount } = exchange;
const mainToAccount = getMainAccount(toAccount, toParentAccount);
const toCurrency = getAccountCurrency(toAccount);
const fromCurrency = getAccountCurrency(fromAccount);
const subAccounts = account.type === "Account" && account.subAccounts;
const tokenId = toCurrency.type === "TokenCurrency" ? toCurrency.id : undefined;
const isFromToken = fromCurrency.type === "TokenCurrency";
const operationId = isFromToken && operation.subOperations && operation.subOperations.length > 0
? operation.subOperations[0].id
: operation.id;
const toAmount = transaction.amount.times(exchangeRate.magnitudeAwareRate);
const swapOperation = {
status: "pending",
provider: exchangeRate.provider,
operationId,
swapId,
// NB We store the reciever main account + tokenId in case the token account doesn't exist yet.
receiverAccountId: mainToAccount.id,
tokenId,
fromAmount: transaction.amount,
toAmount,
};
return isFromToken && subAccounts
? {
...account,
subAccounts: subAccounts.map((a) => {
const subAccount = {
...a,
swapHistory: [...a.swapHistory, swapOperation],
};
return a.id === fromAccount?.id ? subAccount : a;
}),
}
: { ...account, swapHistory: [...account.swapHistory, swapOperation] };
};
//# sourceMappingURL=addToSwapHistory.js.map