Skip to content

Commit

Permalink
fix: positions crashing the app (#322)
Browse files Browse the repository at this point in the history
* fix: positions crushing the app

* fix: apply try-catch to a throwing code block, add comments
  • Loading branch information
VanishMax authored Jan 29, 2025
1 parent 1c61af1 commit 3771ad1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/pages/trade/model/positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,22 @@ class PositionsStore {
return [];
}

return [...this.positionsById.entries()].map(([id, position]) => {
const positions = [...this.positionsById.entries()].map(([id, position]) => {
const { phi, state } = position as ExecutedPosition;
const { component } = phi;
const [asset1, asset2] = this.getCalculatedAssets(position as ExecutedPosition);

if (!this.currentPair) {
throw new Error('No current pair or assets');
const [asset1, asset2] = (() => {
try {
return this.getCalculatedAssets(position as ExecutedPosition);
} catch (e) {
// this.getCalculatedAssets() throws if the assets are not found in the registry
// e.g. if the position was created in `pcli`
return [];
}
})();

if (!this.currentPair || !asset1 || !asset2) {
return undefined;
}

const [baseAsset, quoteAsset] = this.currentPair;
Expand Down Expand Up @@ -487,6 +496,8 @@ class PositionsStore {
state: state.state,
};
});

return positions.filter(Boolean) as DisplayPosition[];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/trade/ui/positions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Tooltip, TooltipProvider } from '@penumbra-zone/ui/Tooltip';
import { stateToString, usePositions } from '@/pages/trade/api/positions.ts';
import { positionsStore } from '@/pages/trade/model/positions';
import { pnum } from '@penumbra-zone/types/pnum';
import { useRegistryAssets } from '@/shared/api/registry';
import { useAssets } from '@/shared/api/assets';
import { SquareArrowOutUpRight } from 'lucide-react';
import { usePathToMetadata } from '../../model/use-path';
import { PositionsCurrentValue } from '../positions-current-value';
Expand All @@ -27,7 +27,7 @@ import { Dash } from './dash';
const Positions = observer(({ showInactive }: { showInactive: boolean }) => {
const { connected } = connectionStore;
const { baseAsset, quoteAsset } = usePathToMetadata();
const { data: assets } = useRegistryAssets();
const { data: assets } = useAssets();
const { data, isLoading, error } = usePositions();
const { displayPositions, setPositions, setAssets } = positionsStore;

Expand Down

0 comments on commit 3771ad1

Please sign in to comment.