Skip to content

Commit

Permalink
fix: improve getUnderlyingAssetIdsBalances safety (shapeshift#4437)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored May 10, 2023
1 parent b0ad66e commit 9e362d8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const LpPositionsByProvider: React.FC<LpPositionsByProviderProps> = ({ id
return (
<Flex direction='column'>
<Flex gap={1}>
<Amount.Fiat value={underlyingBalances[assetId].fiatAmount ?? 0} />
<Amount.Fiat value={underlyingBalances[assetId].fiatAmount} />
<Flex color='gray.500'>
{'('} <Amount.Fiat value={row.original.fiatAmount} /> {')'}
</Flex>
Expand All @@ -165,8 +165,7 @@ export const LpPositionsByProvider: React.FC<LpPositionsByProviderProps> = ({ id
variant='sub-text'
key={`${assetId}-balance`}
size='xs'
// This should never be undefined, but it may
value={underlyingBalances[assetId]?.cryptoBalancePrecision ?? '0'}
value={underlyingBalances[assetId].cryptoBalancePrecision}
symbol={assets[assetId]?.symbol ?? ''}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ export const OpportunityRow: React.FC<
return (
<List style={{ marginTop: 0 }}>
{nestedAssetIds.map(assetId => {
if (!underlyingAssetBalances[assetId]) return null
if (bnOrZero(underlyingAssetBalances[assetId].cryptoBalancePrecision).eq(0)) return null
if (bnOrZero(underlyingAssetBalances[assetId]?.cryptoBalancePrecision).eq(0)) return null
return (
<NestedAsset
key={assetId}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Equity/EquityLpRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export const EquityLpRow: React.FC<EquityLpRowProps> = ({
onClick={handleClick}
icon={DefiProviderMetadata[opportunity.provider].icon}
label={opportunity.provider}
fiatAmount={underlyingBalances[assetId]?.fiatAmount}
cryptoBalancePrecision={underlyingBalances[assetId]?.cryptoBalancePrecision}
fiatAmount={underlyingBalances[assetId].fiatAmount}
cryptoBalancePrecision={underlyingBalances[assetId].cryptoBalancePrecision}
symbol={asset.symbol}
totalFiatBalance={totalFiatBalance}
color={color}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,32 +368,30 @@ export const Withdraw: React.FC<WithdrawProps> = ({
assets,
marketData,
})
if (underlyingAssetBalances) {
trackOpportunityEvent(
MixPanelEvents.WithdrawContinue,
{
opportunity: osmosisOpportunity,
fiatAmounts: [
underlyingAssetBalances[underlyingAsset0.assetId].fiatAmount,
underlyingAssetBalances[underlyingAsset1.assetId].fiatAmount,
],
cryptoAmounts: [
{ assetId: lpAsset.assetId, amountCryptoHuman: formValues.cryptoAmount },
{
assetId: underlyingAsset0.assetId,
amountCryptoHuman:
underlyingAssetBalances[underlyingAsset0.assetId].cryptoBalancePrecision,
},
{
assetId: underlyingAsset1.assetId,
amountCryptoHuman:
underlyingAssetBalances[underlyingAsset1.assetId].cryptoBalancePrecision,
},
],
},
assets,
)
}
trackOpportunityEvent(
MixPanelEvents.WithdrawContinue,
{
opportunity: osmosisOpportunity,
fiatAmounts: [
underlyingAssetBalances[underlyingAsset0.assetId].fiatAmount,
underlyingAssetBalances[underlyingAsset1.assetId].fiatAmount,
],
cryptoAmounts: [
{ assetId: lpAsset.assetId, amountCryptoHuman: formValues.cryptoAmount },
{
assetId: underlyingAsset0.assetId,
amountCryptoHuman:
underlyingAssetBalances[underlyingAsset0.assetId].cryptoBalancePrecision,
},
{
assetId: underlyingAsset1.assetId,
amountCryptoHuman:
underlyingAssetBalances[underlyingAsset1.assetId].cryptoBalancePrecision,
},
],
},
assets,
)
} catch (error) {
moduleLogger.error({ fn: 'handleContinue', error }, 'Error on continue')
toast({
Expand Down
10 changes: 8 additions & 2 deletions src/state/slices/opportunitiesSlice/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ export const getUnderlyingAssetIdsBalances: GetUnderlyingAssetIdsBalances = ({
const underlyingAsset = assets[underlyingAssetId]
const asset = assets[assetId ?? '']
const marketDataPrice = marketData[underlyingAssetId]?.price
if (!underlyingAsset) return acc
if (!asset) return acc

if (!(underlyingAsset && asset)) {
acc[underlyingAssetId] = {
fiatAmount: '0',
cryptoBalancePrecision: '0',
}
return acc
}

const cryptoBalancePrecision = bnOrZero(cryptoAmountBaseUnit)
.times(fromBaseUnit(underlyingAssetRatiosBaseUnit[index], underlyingAsset.precision))
Expand Down
5 changes: 2 additions & 3 deletions src/state/slices/portfolioSlice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,8 @@ export const selectAssetEquityItemsByFilter = createDeepEqualOutputSelector(
return {
id: lpOpportunity.id,
type: AssetEquityType.LP,
// This should never happen but it may whilst data is loading
fiatAmount: underlyingBalances[assetId]?.fiatAmount ?? '0',
amountCryptoPrecision: underlyingBalances[assetId]?.cryptoBalancePrecision ?? '0',
fiatAmount: underlyingBalances[assetId].fiatAmount,
amountCryptoPrecision: underlyingBalances[assetId].cryptoBalancePrecision,
provider: lpOpportunity.provider,
color: DefiProviderMetadata[lpOpportunity.provider].color,
}
Expand Down

0 comments on commit 9e362d8

Please sign in to comment.