Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wallet-dashboard): number of native tokens in migration summary #4619

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/wallet-dashboard/app/(protected)/migrations/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import { useState, useMemo, useCallback } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import clsx from 'clsx';
import { useGetStardustMigratableObjects } from '@/hooks';
import {
useGetStardustMigratableObjects,
useGroupedMigrationObjectsByExpirationDate,
} from '@/hooks';
import { summarizeMigratableObjectValues, summarizeTimelockedObjectValues } from '@/lib/utils';
import {
Button,
Expand Down Expand Up @@ -47,6 +50,11 @@ function MigrationDashboardPage(): JSX.Element {
timelockedNftOutputs,
} = stardustMigrationObjects || {};

const { data: resolvedObjects = [] } = useGroupedMigrationObjectsByExpirationDate(
[...(migratableBasicOutputs || []), ...(migratableNftOutputs || [])],
selectedStardustObjectsCategory === StardustOutputMigrationStatus.Migratable,
);

const {
totalIotaAmount,
totalNativeTokens: migratableNativeTokens,
Expand All @@ -55,6 +63,7 @@ function MigrationDashboardPage(): JSX.Element {
basicOutputs: migratableBasicOutputs,
nftOutputs: migratableNftOutputs,
address,
resolvedObjects,
});
const { totalTimelockedObjects } = summarizeTimelockedObjectValues({
basicOutputs: timelockedBasicOutputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import { CommonOutputObjectWithUc, MILLISECONDS_PER_SECOND } from '@iota/core';
import { IotaObjectData } from '@iota/iota-sdk/client';
import { filterMigrationObjects } from './filterMigrationObjectDetails';
import { CommonMigrationObjectType, StardustOutputDetailsFilter } from '@/lib/enums';
import { ResolvedObjectTypes } from '@/lib/types';

export type StardustMigrationGroupedObjects = {
migratable: IotaObjectData[];
Expand Down Expand Up @@ -58,25 +61,35 @@ interface SummarizeMigrationObjectParams {
basicOutputs: IotaObjectData[] | undefined;
nftOutputs: IotaObjectData[] | undefined;
address: string;
resolvedObjects?: ResolvedObjectTypes[];
}

export function summarizeMigratableObjectValues({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet this issue also happens in the unmigratable objects too, so we should replicate the same logic there too 🙏🏼

basicOutputs = [],
nftOutputs = [],
address,
resolvedObjects,
}: SummarizeMigrationObjectParams): MigratableObjectsData {
let totalNativeTokens = 0;
let totalIotaAmount: bigint = 0n;
let totalNotOwnedStorageDepositReturnAmount: bigint = 0n;
let filteredObjects: ResolvedObjectTypes[] = [];

const totalVisualAssets = nftOutputs.length;
const outputObjects = [...basicOutputs, ...nftOutputs];

if (resolvedObjects) {
filteredObjects = filterMigrationObjects(
resolvedObjects,
StardustOutputDetailsFilter.NativeTokens,
);
}
totalNativeTokens = calculateTotalNativeTokensByName(filteredObjects);

for (const output of outputObjects) {
const outputObjectFields = extractMigrationOutputFields(output);

totalIotaAmount += BigInt(outputObjectFields.balance);
totalNativeTokens += parseInt(outputObjectFields.native_tokens.fields.size);
totalIotaAmount +=
extractOwnedStorageDepositReturnAmount(outputObjectFields, address) || 0n;
totalNotOwnedStorageDepositReturnAmount +=
Expand Down Expand Up @@ -149,3 +162,18 @@ export function extractNotOwnedStorageDepositReturnAmount(
}
return null;
}

export function calculateTotalNativeTokensByName(objects: ResolvedObjectTypes[]): number {
const uniqueTokens: string[] = [];

return objects.reduce((total, obj) => {
if (
obj.commonObjectType === CommonMigrationObjectType.NativeToken &&
!uniqueTokens.includes(obj.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are be potentially 2 coins with the same name but the type must be different, so we should check against the type, not the name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg

export function getUniqueNativeTokensByCoinType(objects: ResolvedObjectTypes[]): number {
    return new Set(
        objects
            .filter((obj) => obj.commonObjectType === CommonMigrationObjectType.NativeToken)
            .map((obj) => obj.coinType),
    ).size;
}

) {
uniqueTokens.push(obj.name);
return total + 1;
}
return total;
}, 0);
}
Loading