Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Dec 4, 2024
1 parent 45be733 commit 92073c8
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/stores/walletStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,6 @@ export const useWalletStore = defineStore("wallet", () => {
return summary.sort(rankAssets);
});

/**
* Compares two assets and ranks them based on predefined criteria.
*
* The ranking criteria are as follows:
* 1. The asset with the token ID matching `ERG_TOKEN_ID` is ranked higher.
* 2. If one asset's token ID is in the `KNOWN_ASSETS` set and the other is not, the known asset is ranked higher.
* 3. If both assets are either known or unknown, they are compared alphabetically by their metadata name.
* If the metadata name is not available, the token ID is used for comparison.
*
* @param a - The first asset to compare.
* @param b - The second asset to compare.
* @returns A negative number if `a` should be ranked higher, a positive number if `b` should be ranked higher,
* or zero if they are considered equal in ranking.
*/
function rankAssets(a: StateAssetSummary, b: StateAssetSummary) {
if (a.tokenId === ERG_TOKEN_ID) return -1;
if (b.tokenId === ERG_TOKEN_ID) return 1;
if (KNOWN_ASSETS.has(a.tokenId) && !KNOWN_ASSETS.has(b.tokenId)) return -1;
if (KNOWN_ASSETS.has(b.tokenId) && !KNOWN_ASSETS.has(a.tokenId)) return 1;
return (a.metadata?.name ?? a.tokenId).localeCompare(b.metadata?.name ?? b.tokenId);
}

const health = computed(() => ({
utxoCount: 0, // see: https://github.com/nautls/nautilus-wallet/issues/176
hasOldUtxos: privateState.hasOldUtxos
Expand Down Expand Up @@ -493,6 +471,28 @@ function getChanges(
return { changedAddresses, changedAssets, removedAssets };
}

/**
* Compares two assets and ranks them based on predefined criteria.
*
* The ranking criteria are as follows:
* 1. The asset with the token ID matching `ERG_TOKEN_ID` is ranked higher.
* 2. If one asset's token ID is in the `KNOWN_ASSETS` set and the other is not, the known asset is ranked higher.
* 3. If both assets are either known or unknown, they are compared alphabetically by their metadata name.
* If the metadata name is not available, the token ID is used for comparison.
*
* @param a - The first asset to compare.
* @param b - The second asset to compare.
* @returns A negative number if `a` should be ranked higher, a positive number if `b` should be ranked higher,
* or zero if they are considered equal in ranking.
*/
function rankAssets(a: StateAssetSummary, b: StateAssetSummary) {
if (a.tokenId === ERG_TOKEN_ID) return -1;
if (b.tokenId === ERG_TOKEN_ID) return 1;
if (KNOWN_ASSETS.has(a.tokenId) && !KNOWN_ASSETS.has(b.tokenId)) return -1;
if (KNOWN_ASSETS.has(b.tokenId) && !KNOWN_ASSETS.has(a.tokenId)) return 1;
return (a.metadata?.name ?? a.tokenId).localeCompare(b.metadata?.name ?? b.tokenId);
}

function getOrDerive(derived: IndexedAddress[], deriver: HdKey, count: number, offset: number) {
const chunk = derived.slice(offset, offset + count);
if (chunk.length < count) {
Expand Down

0 comments on commit 92073c8

Please sign in to comment.