diff --git a/apps/minifront/src/fetchers/balances/by-priority-score.ts b/apps/minifront/src/fetchers/balances/by-priority-score.ts index a1399fd1da..9cf98616b5 100644 --- a/apps/minifront/src/fetchers/balances/by-priority-score.ts +++ b/apps/minifront/src/fetchers/balances/by-priority-score.ts @@ -2,6 +2,7 @@ import { BalancesResponse } from '@penumbra-zone/protobuf/penumbra/view/v1/view_ import { getMetadataFromBalancesResponseOptional, getAmount, + getAddressIndex, } from '@penumbra-zone/getters/balances-response'; import { multiplyAmountByNumber, joinLoHiAmount } from '@penumbra-zone/types/amount'; @@ -21,3 +22,14 @@ export const sortByPriorityScore = (a: BalancesResponse, b: BalancesResponse) => return Number(bPriority - aPriority); }; + +export const sortByPriorityScoreAndAccountIndex = (a: BalancesResponse, b: BalancesResponse) => { + const aIndex = getAddressIndex.optional()(a)?.account ?? Infinity; + const bIndex = getAddressIndex.optional()(b)?.account ?? Infinity; + + if (aIndex === bIndex) { + return sortByPriorityScore(a, b); + } + + return aIndex - bIndex; +}; diff --git a/apps/minifront/src/state/swap/helpers.ts b/apps/minifront/src/state/swap/helpers.ts index 761ef7a794..a05796e250 100644 --- a/apps/minifront/src/state/swap/helpers.ts +++ b/apps/minifront/src/state/swap/helpers.ts @@ -20,7 +20,7 @@ import { isKnown } from '../helpers'; import { AbridgedZQueryState } from '@penumbra-zone/zquery/src/types'; import { penumbra } from '../../prax'; import { DexService, SimulationService } from '@penumbra-zone/protobuf'; -import { sortByPriorityScore } from '../../fetchers/balances/by-priority-score.ts'; +import { sortByPriorityScoreAndAccountIndex } from '../../fetchers/balances/by-priority-score.ts'; export const sendSimulateTradeRequest = ({ assetIn, @@ -173,7 +173,7 @@ export const swappableBalancesResponsesSelector = ( data: zQueryState.data ?.filter(isKnown) .filter(balance => isSwappable(getMetadata(balance.balanceView))) - .sort(sortByPriorityScore), + .sort(sortByPriorityScoreAndAccountIndex), }); export const swappableAssetsSelector = (zQueryState: AbridgedZQueryState) => ({