@ledgerhq/coin-multiversx
Version:
Ledger MultiversX Coin integration
52 lines • 2.08 kB
JavaScript
import { log } from "@ledgerhq/logs";
export function reconciliateSubAccounts(tokenAccounts, initialAccount) {
let subAccounts;
if (initialAccount) {
const initialSubAccounts = initialAccount.subAccounts;
let anySubAccountHaveChanged = false;
const stats = [];
if (initialSubAccounts && tokenAccounts.length !== initialSubAccounts.length) {
stats.push("length differ");
anySubAccountHaveChanged = true;
}
subAccounts = tokenAccounts.map((ta) => {
const existingTokenAccount = initialSubAccounts?.find(a => a.id === ta.id);
if (existingTokenAccount) {
let sameProperties = true;
if (existingTokenAccount !== ta) {
for (const property in existingTokenAccount) {
if (existingTokenAccount[property] !== ta[property]) {
sameProperties = false;
stats.push(`field ${property} changed for ${ta.id}`);
break;
}
}
}
if (sameProperties) {
return existingTokenAccount;
}
else {
anySubAccountHaveChanged = true;
}
}
else {
anySubAccountHaveChanged = true;
stats.push(`new token account ${ta.id}`);
}
return ta;
});
if (!anySubAccountHaveChanged && initialSubAccounts) {
// eslint-disable-next-line prettier/prettier
log("multiversx", `incremental sync: ${String(initialSubAccounts.length)} sub accounts have not changed`);
subAccounts = initialSubAccounts;
}
else {
log("multiversx", "incremental sync: sub accounts changed: " + stats.join(", "));
}
}
else {
subAccounts = tokenAccounts.map((a) => a);
}
return subAccounts;
}
//# sourceMappingURL=reconciliation.js.map