-
Notifications
You must be signed in to change notification settings - Fork 2
feat(portal): centralize position valuation and presence utilities with test coverage #129
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
Open
yeziR4
wants to merge
1
commit into
autonomys:main
Choose a base branch
from
yeziR4:feat/centralize-position-utils-and-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { PositionBreakdown } from '@/components/positions/PositionBreakdown'; | |
| import type { Operator } from '@/types/operator'; | ||
| import { useOperatorPosition } from '@/hooks/use-positions'; | ||
| import { formatAI3, formatNumber, formatPercentage, getAPYColor } from '@/lib/formatting'; | ||
| import { calculateTotalPositionValue } from '@/lib/position-utils'; | ||
|
|
||
| interface OperatorSummaryProps { | ||
| operator: Operator; | ||
|
|
@@ -115,12 +116,7 @@ export const OperatorSummary: React.FC<OperatorSummaryProps> = ({ operator }) => | |
| {userPosition ? ( | ||
| <Tooltip content={<PositionBreakdown position={userPosition} />} side="top"> | ||
| <span className="text-sm font-medium text-foreground font-mono cursor-help whitespace-nowrap"> | ||
| {formatAI3( | ||
| userPosition.positionValue + | ||
| userPosition.storageFeeDeposit + | ||
| (userPosition.pendingDeposit?.amount || 0), | ||
| 2, | ||
| )} | ||
| {formatAI3(calculateTotalPositionValue(userPosition), 2)} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you missing an import for this? |
||
| </span> | ||
| </Tooltip> | ||
| ) : ( | ||
|
|
||
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,106 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { hasUserPosition, calculateTotalPositionValue } from '../position-utils'; | ||
| import type { UserPosition } from '@/types/position'; | ||
|
|
||
| describe('hasUserPosition', () => { | ||
| it('returns false for null, undefined, or empty position', () => { | ||
| expect(hasUserPosition(null)).toBe(false); | ||
| expect(hasUserPosition(undefined)).toBe(false); | ||
|
|
||
| const emptyPosition: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 0, | ||
| storageFeeDeposit: 0, | ||
| pendingDeposit: null, | ||
| pendingWithdrawals: [], | ||
| status: 'active', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(hasUserPosition(emptyPosition)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true when positionValue > 0', () => { | ||
| const pos: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 100, | ||
| storageFeeDeposit: 0, | ||
| pendingDeposit: null, | ||
| pendingWithdrawals: [], | ||
| status: 'active', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(hasUserPosition(pos)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true when storageFeeDeposit > 0', () => { | ||
| const pos: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 0, | ||
| storageFeeDeposit: 20, | ||
| pendingDeposit: null, | ||
| pendingWithdrawals: [], | ||
| status: 'active', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(hasUserPosition(pos)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true when pendingDeposit.amount > 0', () => { | ||
| const pos: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 0, | ||
| storageFeeDeposit: 0, | ||
| pendingDeposit: { amount: 50, effectiveEpoch: 2 }, | ||
| pendingWithdrawals: [], | ||
| status: 'pending', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(hasUserPosition(pos)).toBe(true); | ||
| }); | ||
|
|
||
| it('omits pendingWithdrawals from active position presence', () => { | ||
| const exitedPos: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 0, | ||
| storageFeeDeposit: 0, | ||
| pendingDeposit: null, | ||
| pendingWithdrawals: [ | ||
| { | ||
| grossWithdrawalAmount: 100, | ||
| stakeWithdrawalAmount: 80, | ||
| storageFeeRefund: 20, | ||
| unlockAtBlock: 1000, | ||
| }, | ||
| ], | ||
| status: 'withdrawing', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(hasUserPosition(exitedPos)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('calculateTotalPositionValue', () => { | ||
| it('returns 0 for null or undefined position', () => { | ||
| expect(calculateTotalPositionValue(null)).toBe(0); | ||
| expect(calculateTotalPositionValue(undefined)).toBe(0); | ||
| }); | ||
|
|
||
| it('sums positionValue, storageFeeDeposit, and pendingDeposit amount accurately', () => { | ||
| const pos: UserPosition = { | ||
| operatorId: '0', | ||
| operatorName: 'Operator 0', | ||
| positionValue: 100, | ||
| storageFeeDeposit: 25, | ||
| pendingDeposit: { amount: 50, effectiveEpoch: 2 }, | ||
| pendingWithdrawals: [], | ||
| status: 'active', | ||
| lastUpdated: new Date(), | ||
| }; | ||
| expect(calculateTotalPositionValue(pos)).toBe(175); | ||
| }); | ||
| }); |
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,22 @@ | ||
| import type { UserPosition } from '@/types/position'; | ||
|
|
||
| /** | ||
| * Check if a user has an active or pending stake position with an operator. | ||
| * Note: Intentionally omits pending withdrawals as those represent exited positions awaiting unlock. | ||
| */ | ||
| export const hasUserPosition = (userPosition?: UserPosition | null): boolean => | ||
| Boolean( | ||
| userPosition && | ||
| (userPosition.positionValue > 0 || | ||
| userPosition.storageFeeDeposit > 0 || | ||
| (userPosition.pendingDeposit?.amount || 0) > 0), | ||
| ); | ||
|
|
||
| /** | ||
| * Calculate total position value including current staked value, live storage fund deposit, and pending deposits. | ||
| */ | ||
| export const calculateTotalPositionValue = (userPosition?: UserPosition | null): number => { | ||
| if (!userPosition) return 0; | ||
| const pendingAmount = userPosition.pendingDeposit?.amount || 0; | ||
| return userPosition.positionValue + userPosition.storageFeeDeposit + pendingAmount; | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the no-data branch from a muted
--toformatAI3(operator.totalStaked), which renders a staked-only figure under the "Operator Total Value" label on the next line. WhentotalPoolValueis absent the storage fund is excluded, so the number understates the pool rather than signalling "no data".It also diverges from the other two views, which still show
--in this branch -OperatorTable.tsx:123andOperatorSummary.tsx:98. Same operator, three different renderings.Suggest restoring the placeholder, keeping the new
text-lg/spanstyling and using the muted colour to match the siblings:If the intent was actually to always show something here, that's a reasonable product call - but it should land as its own change that updates all three views together, rather than inside the utils refactor.