Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabib committed Feb 14, 2025
1 parent b0ac52b commit fc55f64
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion frontend/src/lib/derived/tokens-list-user.derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { failedExistentImportedTokenLedgerIdsStore } from "$lib/derived/imported
import { tokensListBaseStore } from "$lib/derived/tokens-list-base.derived";
import { tokensByUniverseIdStore } from "$lib/derived/tokens.derived";
import { universesAccountsStore } from "$lib/derived/universes-accounts.derived";
import {
canistersErrorsStore,
type CanistersErrorsStore,
type CanistersErrorsStoreData,
} from "$lib/stores/canisters-errors.store";
import type { IcrcTokenMetadata } from "$lib/types/icrc";
import {
UserTokenAction,
Expand All @@ -19,7 +24,7 @@ import { sumAccounts } from "$lib/utils/accounts.utils";
import { buildAccountsUrl, buildWalletUrl } from "$lib/utils/navigation.utils";
import { isUniverseNns } from "$lib/utils/universe.utils";
import { toUserTokenFailed } from "$lib/utils/user-token.utils";
import { isNullish, TokenAmountV2 } from "@dfinity/utils";
import { isNullish, nonNullish, TokenAmountV2 } from "@dfinity/utils";
import { derived, type Readable } from "svelte/store";

const getUsdValue = ({
Expand Down Expand Up @@ -50,20 +55,33 @@ const convertToUserTokenData = ({
tokensByUniverse,
baseTokenData,
icpSwapUsdPrices,
canistersErrors,
}: {
accounts: UniversesAccounts;
tokensByUniverse: Record<string, IcrcTokenMetadata>;
baseTokenData: UserTokenBase;
icpSwapUsdPrices: IcpSwapUsdPricesStoreData;
canistersErrors: CanistersErrorsStoreData;
}): UserToken => {
const token = tokensByUniverse[baseTokenData.universeId.toText()];
const rowHref = isUniverseNns(baseTokenData.universeId)
? buildAccountsUrl({ universe: baseTokenData.universeId.toText() })
: buildWalletUrl({
universe: baseTokenData.universeId.toText(),
});
const ledgerCanisterError =
canistersErrors[baseTokenData.ledgerCanisterId.toString()];
const accountsList = accounts[baseTokenData.universeId.toText()];
const mainAccount = accountsList?.find(({ type }) => type === "main");

if (nonNullish(ledgerCanisterError)) {
return {
...baseTokenData,
balance: "failed",
domKey: rowHref,
};
}

if (isNullish(token) || isNullish(accountsList) || isNullish(mainAccount)) {
return {
...baseTokenData,
Expand Down Expand Up @@ -114,6 +132,7 @@ export const tokensListUserStore = derived<
Readable<Record<string, IcrcTokenMetadata>>,
Readable<Array<string>>,
IcpSwapUsdPricesStore,
CanistersErrorsStore,
],
UserToken[]
>(
Expand All @@ -123,20 +142,23 @@ export const tokensListUserStore = derived<
tokensByUniverseIdStore,
failedExistentImportedTokenLedgerIdsStore,
icpSwapUsdPricesStore,
canistersErrorsStore,
],
([
tokensList,
accounts,
tokensByUniverse,
failedImportedTokenLedgerIds,
icpSwapUsdPrices,
canistersErrors,
]) => [
...tokensList.map((baseTokenData) =>
convertToUserTokenData({
baseTokenData,
accounts,
tokensByUniverse,
icpSwapUsdPrices,
canistersErrors,
})
),
...failedImportedTokenLedgerIds.map(toUserTokenFailed),
Expand Down

0 comments on commit fc55f64

Please sign in to comment.