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 native position filter [WLT-4174] #417

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions src/ui/shared/requests/useNativeBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getDecimals } from 'src/modules/networks/asset';
import { useAddressPositions } from 'defi-sdk';
import { isTruthy } from 'is-truthy-ts';
import { useNetworks } from 'src/modules/networks/useNetworks';
import { invariant } from 'src/shared/invariant';
import { useEvmNativeAddressPosition } from './useEvmNativeAddressPosition';
import { useNativeAssetId } from './useNativeAsset';

Expand All @@ -19,6 +20,7 @@ function useNativeAddressPosition({
enabled?: boolean;
}) {
const id = useNativeAssetId(chain);

const { value, isLoading } = useAddressPositions(
{
address,
Expand All @@ -29,10 +31,18 @@ function useNativeAddressPosition({
);

return useMemo(() => {
const nativePositions = value?.positions?.filter(
(item) =>
item.chain === chain.toString() &&
item.type === 'asset' &&
!item.protocol
Copy link
Contributor

Choose a reason for hiding this comment

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

As I understand, if item.type === asset, it can't be inside protocol. So this filter can be simplified to
(item) => item.chain === chain.toString() && item.type === 'asset'

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see. Do we really get position with type: asset and non-empty protocol?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this was exactly the reason why I had to add this condition

Copy link
Contributor

Choose a reason for hiding this comment

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

Wow. Just faced this. Approve then!

);
invariant(
!nativePositions || nativePositions.length <= 1,
'multiple native positions'
);
Copy link
Member

Choose a reason for hiding this comment

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

Do we really want to crash here?

Copy link
Member

Choose a reason for hiding this comment

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

Also this condition is quite hard to read :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to make it easier to debug in case anything similar happens again. So we'll have a clear message indicating that we received multiple native positions from the backend api

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I simplified the invariant condition ✅

return {
data: value?.positions?.find(
(item) => item.chain === chain.toString() && item.type === 'asset'
),
data: nativePositions?.[0],
isLoading,
};
}, [chain, isLoading, value?.positions]);
Expand Down
Loading