Skip to content

Commit

Permalink
tron: keep empty subAccounts (#7027)
Browse files Browse the repository at this point in the history
* tron: keep empty subAccounts

* chore: changeset

* chore: added comment

* fix: remove blacklist token from init sub acc
  • Loading branch information
CremaFR committed Jun 7, 2024
1 parent 6eec3f9 commit d155ff9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-toes-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

keep empty tron subAccounts
46 changes: 44 additions & 2 deletions libs/ledger-live-common/src/families/tron/synchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ export const getAccountShape: GetAccountShape<TronAccount> = async (
return accumulator;
}, []);

const { blacklistedTokenIds = [] } = syncConfig;

const subAccounts: SubAccount[] = compact(
trc10Tokens.concat(trc20Tokens).map(({ key, tokenId, balance }) => {
const { blacklistedTokenIds = [] } = syncConfig;
const token = findTokenById(tokenId);
if (!token || blacklistedTokenIds.includes(tokenId)) return;
const id = encodeTokenAccountId(accountId, token);
Expand All @@ -167,6 +168,16 @@ export const getAccountShape: GetAccountShape<TronAccount> = async (
return sub;
}),
);

// Filter blacklisted tokens from the initial account's subAccounts
// Could be use to filter out tokens that got their CAL id changed
const filteredInitialSubAccounts = (initialAccount?.subAccounts || []).filter(
subAccount => !blacklistedTokenIds.includes(subAccount.token.id),
);

// keep old account with emptyBalance and a history not returned by the BE fixes LIVE-12797
const mergedSubAccounts = mergeSubAccounts(subAccounts, filteredInitialSubAccounts);

// get 'OUT' token operations with fee
const subOutOperationsWithFee: TronOperation[] = subAccounts
.flatMap(s => s.operations)
Expand Down Expand Up @@ -199,7 +210,7 @@ export const getAccountShape: GetAccountShape<TronAccount> = async (
spendableBalance,
operationsCount: parentOpsAndSubOutOpsWithFee.length,
operations: parentOpsAndSubOutOpsWithFee,
subAccounts,
subAccounts: mergedSubAccounts,
tronResources,
blockHeight,
};
Expand All @@ -225,4 +236,35 @@ export const postSync = (initial: TronAccount, parent: TronAccount): TronAccount
return parent;
};

/**
* Merges two arrays of subAccounts according to specific rules:
* - The first array (subAccounts1) is up-to-date and should not be modified.
* - Old duplicates from the second array (subAccounts2) should be filtered out.
* - Only new subAccounts with a unique ID from the second array should be included.
* - The balance and spendableBalance fields of the second array's subAccounts should be set to 0.
*
* @param {Array} subAccounts1 - The first array of subAccounts, which is up-to-date and should not be modified.
* @param {Array} subAccounts2 - The second array of subAccounts, from which only new unique subAccounts should be included.
* @returns {Array} - The merged array of subAccounts.
*/
const mergeSubAccounts = (subAccounts1, subAccounts2) => {
const existingIds = new Set(subAccounts1.map(subAccount => subAccount.id));
const filteredSubAccounts2 = subAccounts2
.map(subAccount => {
if (existingIds.has(subAccount.id)) {
return null;
} else {
// Set balance and spendableBalance to 0 has if they are not here it means balance is 0
return {
...subAccount,
balance: new BigNumber(0),
spendableBalance: new BigNumber(0),
};
}
})
.filter(subAccount => subAccount !== null);

return subAccounts1.concat(filteredSubAccounts2);
};

export const sync = makeSync({ getAccountShape, postSync });

0 comments on commit d155ff9

Please sign in to comment.