-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -19,6 +20,7 @@ function useNativeAddressPosition({ | |
enabled?: boolean; | ||
}) { | ||
const id = useNativeAssetId(chain); | ||
|
||
const { value, isLoading } = useAddressPositions( | ||
{ | ||
address, | ||
|
@@ -29,10 +31,18 @@ function useNativeAddressPosition({ | |
); | ||
|
||
return useMemo(() => { | ||
const nativePositions = value?.positions?.filter( | ||
(item) => | ||
item.chain === chain.toString() && | ||
item.type === 'asset' && | ||
!item.protocol | ||
); | ||
invariant( | ||
!nativePositions || nativePositions.length <= 1, | ||
'multiple native positions' | ||
); | ||
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. Do we really want to crash here? 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. Also this condition is quite hard to read :) 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. 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 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. 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]); | ||
|
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.
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'
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.
Oh I see. Do we really get position with type: asset and non-empty protocol?
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.
Yeah, this was exactly the reason why I had to add this condition
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.
Wow. Just faced this. Approve then!