-
Notifications
You must be signed in to change notification settings - Fork 47
feat(explorer): add balance breakdown to address page #7198
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
Merged
begonaalvarezd
merged 21 commits into
develop
from
tooling-explorer/add-balance-breakdown
Jun 4, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9e0c753
feat(explorer): add balance breakdown to address page
VmMad 1add7ee
fix: import
VmMad fab92e2
fix: jest config
VmMad 8791c61
fix: imports for test
VmMad 693ff7b
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
VmMad dbc0bf8
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
evavirseda 7c69c08
refactor: improve balance breakdown styles
VmMad d168740
fix ci
evavirseda a657e52
feat: add tooltips to each balance item
VmMad 4c92084
chore: set open by default
VmMad fe175f7
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
evavirseda 6a06377
fix: tooltip showing up with keyboard navigation
VmMad 17977de
fix: avoid overlapping with tooltip
VmMad 9570fc1
fix: bring back on focus
VmMad bc736f8
chore: improve balance breakdown texts
VmMad 76173f7
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
VmMad f7488fd
refactor: use hook to calculate delegated stakes
VmMad 1c8bd45
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
VmMad ab1989f
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
brancoder 4b8bb3b
Merge branch 'develop' into tooling-explorer/add-balance-breakdown
VmMad 7f70566
chore: update tooltip texts
VmMad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@iota/apps-ui-kit': patch | ||
--- | ||
|
||
Fix tooltip being cut by other boxes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DelegatedTimelockedStakeSchema } from './stake/types'; | ||
import type { DelegatedTimelockedStake } from '@iota/iota-sdk/client'; | ||
import type { ExtendedDelegatedTimelockedStake } from '../interfaces'; | ||
|
||
export function formatDelegatedTimelockedStake( | ||
delegatedTimelockedStakeData: DelegatedTimelockedStake[], | ||
): ExtendedDelegatedTimelockedStake[] { | ||
return delegatedTimelockedStakeData.flatMap((delegatedTimelockedStake) => { | ||
const validatedDelegatedTimelockedStake = | ||
DelegatedTimelockedStakeSchema.parse(delegatedTimelockedStake); | ||
|
||
return validatedDelegatedTimelockedStake.stakes.map((stake) => { | ||
return { | ||
validatorAddress: delegatedTimelockedStake.validatorAddress, | ||
stakingPool: delegatedTimelockedStake.stakingPool, | ||
estimatedReward: stake.status === 'Active' ? stake.estimatedReward : '', | ||
stakeActiveEpoch: stake.stakeActiveEpoch, | ||
stakeRequestEpoch: stake.stakeRequestEpoch, | ||
status: stake.status, | ||
timelockedStakedIotaId: stake.timelockedStakedIotaId, | ||
principal: stake.principal, | ||
expirationTimestampMs: stake.expirationTimestampMs, | ||
label: stake.label, | ||
}; | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { IotaObjectData } from '@iota/iota-sdk/client'; | ||
import { TimelockedIotaObjectSchema, TimelockedObjectFieldsSchema } from './stake/types'; | ||
import { TimelockedIotaResponse, TimelockedObject } from '../interfaces/timelock.interfaces'; | ||
|
||
export function mapTimelockObjects(iotaObjects: IotaObjectData[]): TimelockedObject[] { | ||
return iotaObjects.map((iotaObject) => { | ||
const validIotaObject = TimelockedIotaObjectSchema.parse(iotaObject); | ||
|
||
if ( | ||
!validIotaObject?.content?.dataType || | ||
validIotaObject.content.dataType !== 'moveObject' | ||
) { | ||
return { | ||
id: { id: '' }, | ||
locked: { value: 0n }, | ||
expirationTimestampMs: 0, | ||
}; | ||
} | ||
const fields = validIotaObject.content.fields as unknown as TimelockedIotaResponse; | ||
|
||
const validFields = TimelockedObjectFieldsSchema.parse(fields); | ||
|
||
return { | ||
id: validFields.id, | ||
locked: { value: BigInt(validFields?.locked || '0') }, | ||
expirationTimestampMs: Number(validFields.expiration_timestamp_ms), | ||
label: validFields.label, | ||
}; | ||
}); | ||
} |
222 changes: 222 additions & 0 deletions
222
apps/explorer/src/pages/address-result/AddressBalanceBreakdown.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
Collapsible, | ||
formatDelegatedStake, | ||
formatDelegatedTimelockedStake, | ||
mapTimelockObjects, | ||
TIMELOCK_IOTA_TYPE, | ||
useBalance, | ||
useFormatCoin, | ||
useGetAllOwnedObjects, | ||
useGetDelegatedStake, | ||
useGetTimelockedStakedObjects, | ||
useTotalDelegatedStake, | ||
} from '@iota/core'; | ||
import { Divider, KeyValueInfo, Panel, Skeleton, Title, TitleSize } from '@iota/apps-ui-kit'; | ||
import { useState } from 'react'; | ||
|
||
const TOOLTIP_TEXT = 'This balance breakdown does not include unmigrated stardust funds.'; | ||
interface BalanceBreakdownElement { | ||
keyText: string; | ||
value: string; | ||
supportingLabel: string; | ||
isLoading?: boolean; | ||
isError?: boolean; | ||
tooltipText?: string; | ||
} | ||
|
||
export function AddressBalanceBreakdown({ address }: { address: string }): React.JSX.Element { | ||
const [open, setOpen] = useState(true); | ||
const { | ||
data: balance, | ||
isLoading: isLoadingBalance, | ||
isError: isBalanceErrored, | ||
} = useBalance(address); | ||
|
||
const [totalAvailableBalance, symbol] = useFormatCoin({ | ||
balance: balance?.totalBalance, | ||
}); | ||
|
||
const { | ||
data: delegatedStake, | ||
isLoading: isLoadingDelegatedStakes, | ||
isError: isDelegatedStakeErrored, | ||
} = useGetDelegatedStake({ | ||
address, | ||
}); | ||
const delegatedStakes = delegatedStake ? formatDelegatedStake(delegatedStake) : []; | ||
const totalDelegatedStake = useTotalDelegatedStake(delegatedStakes); | ||
const [formattedDelegatedStake] = useFormatCoin({ | ||
balance: totalDelegatedStake, | ||
}); | ||
|
||
const { | ||
data: timelockedStakedObjects, | ||
isLoading: isLoadingTimelockedStakeObjects, | ||
isError: isTimelockedStakedObjectsErrored, | ||
} = useGetTimelockedStakedObjects(address); | ||
|
||
const extendedDelegatedTimelockedStakes = formatDelegatedTimelockedStake( | ||
timelockedStakedObjects || [], | ||
); | ||
|
||
const totalTimelockedStaked = useTotalDelegatedStake(extendedDelegatedTimelockedStakes); | ||
const [formattedTimelockedStake] = useFormatCoin({ | ||
balance: totalTimelockedStaked, | ||
}); | ||
|
||
const { | ||
data: timelockedObjects, | ||
isLoading: isTimelockedObjectsLoading, | ||
isError: isTimelockedObjectsError, | ||
} = useGetAllOwnedObjects(address, { | ||
StructType: TIMELOCK_IOTA_TYPE, | ||
}); | ||
|
||
const mappedTimelockedObjects = mapTimelockObjects(timelockedObjects || []); | ||
|
||
const totalTimelockedTokens = mappedTimelockedObjects.reduce( | ||
(acc, obj) => acc + BigInt(obj.locked.value), | ||
BigInt(0), | ||
); | ||
|
||
const [formattedTimelockedTokens] = useFormatCoin({ | ||
balance: totalTimelockedTokens, | ||
}); | ||
|
||
const totalBalanceBreakdown = | ||
BigInt(balance?.totalBalance || 0) + | ||
BigInt(totalDelegatedStake || 0) + | ||
BigInt(totalTimelockedStaked || 0) + | ||
BigInt(totalTimelockedTokens || 0); | ||
|
||
const [formattedTotalBalance] = useFormatCoin({ | ||
balance: totalBalanceBreakdown, | ||
}); | ||
|
||
const isLoadingTotalBalance = | ||
isLoadingBalance || | ||
isLoadingDelegatedStakes || | ||
isLoadingTimelockedStakeObjects || | ||
isTimelockedObjectsLoading; | ||
|
||
const isTotalBalanceErrored = | ||
isBalanceErrored || | ||
isDelegatedStakeErrored || | ||
isTimelockedStakedObjectsErrored || | ||
isTimelockedObjectsError; | ||
|
||
const BALANCE_BREAKDOWN: BalanceBreakdownElement[] = [ | ||
{ | ||
keyText: 'Available', | ||
value: totalAvailableBalance, | ||
supportingLabel: symbol, | ||
isLoading: isLoadingBalance, | ||
isError: isBalanceErrored, | ||
tooltipText: 'IOTA that can be used or transferred immediately.', | ||
}, | ||
{ | ||
keyText: 'Staked', | ||
value: formattedDelegatedStake, | ||
supportingLabel: symbol, | ||
isLoading: isLoadingDelegatedStakes, | ||
isError: isDelegatedStakeErrored, | ||
tooltipText: 'IOTA currently locked in staking. Cannot be used until unstaked.', | ||
}, | ||
{ | ||
keyText: 'Timelocked Staked', | ||
value: formattedTimelockedStake, | ||
supportingLabel: symbol, | ||
isLoading: isLoadingTimelockedStakeObjects, | ||
isError: isTimelockedStakedObjectsErrored, | ||
tooltipText: | ||
'IOTA both timelocked and staked. To access these funds, they must first be unstaked, and then handled according to their timelock conditions.', | ||
}, | ||
{ | ||
keyText: 'Timelocked', | ||
value: formattedTimelockedTokens, | ||
supportingLabel: symbol, | ||
isLoading: isTimelockedObjectsLoading, | ||
isError: isTimelockedObjectsError, | ||
tooltipText: | ||
"IOTA locked until a specific time. Depending on the lock's expiration, these funds can either be used for staking or collected when the timelock allows it.", | ||
}, | ||
]; | ||
return ( | ||
<Panel> | ||
<div className="relative overflow-visible"> | ||
<Collapsible | ||
hideBorder | ||
isOpen={open} | ||
onOpenChange={(isOpen) => setOpen(isOpen)} | ||
render={() => ( | ||
<div className="flex w-full flex-row items-center justify-between"> | ||
<Title | ||
size={TitleSize.Small} | ||
title="Balance Breakdown" | ||
tooltipText={TOOLTIP_TEXT} | ||
/> | ||
</div> | ||
)} | ||
> | ||
<div className="flex flex-col gap-y-sm p-md--rs"> | ||
{BALANCE_BREAKDOWN.map((item) => ( | ||
<KeyValueInfo | ||
key={item.keyText} | ||
keyText={item.keyText} | ||
tooltipText={item.tooltipText} | ||
fullwidth | ||
value={ | ||
<RenderBalanceValue | ||
value={item.value} | ||
isLoading={item.isLoading} | ||
isError={item.isError} | ||
/> | ||
} | ||
supportingLabel={item.supportingLabel} | ||
/> | ||
))} | ||
</div> | ||
</Collapsible> | ||
<div className="flex flex-col gap-y-sm px-md pb-md"> | ||
<Divider /> | ||
<KeyValueInfo | ||
keyText="Total" | ||
value={ | ||
<RenderBalanceValue | ||
value={formattedTotalBalance} | ||
isLoading={isLoadingTotalBalance} | ||
isError={isTotalBalanceErrored} | ||
/> | ||
} | ||
fullwidth | ||
supportingLabel={symbol} | ||
/> | ||
</div> | ||
</div> | ||
</Panel> | ||
); | ||
} | ||
|
||
interface RenderBalanceValueProps { | ||
value: string; | ||
isLoading?: boolean; | ||
isError?: boolean; | ||
} | ||
|
||
function RenderBalanceValue({ | ||
value, | ||
isLoading, | ||
isError, | ||
}: RenderBalanceValueProps): React.JSX.Element | string { | ||
if (isLoading) { | ||
return <Skeleton widthClass="w-20" heightClass="h-4" />; | ||
} | ||
if (isError) { | ||
return '--'; | ||
} | ||
|
||
return value; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.