Skip to content

Commit 34feeb9

Browse files
authored
CP-13150: Fix bugs on inapp defi (#3506)
1 parent 5b6e4dd commit 34feeb9

File tree

5 files changed

+66
-66
lines changed

5 files changed

+66
-66
lines changed

packages/core-mobile/app/new/common/components/ListScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export const ListScreen = <T,>({
321321
<Animated.View style={[animatedHeaderContainerStyle]}>
322322
<View
323323
style={{
324-
paddingTop: renderHeader ? headerHeight + 16 : headerHeight,
324+
paddingTop: headerHeight + 16,
325325
paddingBottom: renderHeader ? 12 : 0
326326
}}>
327327
<Animated.View

packages/core-mobile/app/new/features/defiMarket/components/RewardsBanner.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@ export const RewardsBanner = ({
2828
const { totalRewardsFiat, rewards } = availableRewards
2929

3030
const formattedTotalRewardsFiat = useMemo(() => {
31-
if (exchangeRate !== undefined) {
32-
const amountInCurrency = totalRewardsFiat.toNumber() * exchangeRate
33-
if (amountInCurrency < MINIMUM_DISPLAY_AMOUNT) {
34-
return `Less than ${formatCurrency({ amount: MINIMUM_DISPLAY_AMOUNT })}`
35-
}
31+
const baseAmount = totalRewardsFiat.toNumber()
32+
const hasExchangeRate = exchangeRate !== undefined
33+
const amountInCurrency = hasExchangeRate
34+
? baseAmount * exchangeRate
35+
: baseAmount
3636

37-
return formatCurrency({
38-
amount: amountInCurrency
39-
})
37+
if (amountInCurrency < MINIMUM_DISPLAY_AMOUNT) {
38+
return `Less than ${formatCurrency({ amount: MINIMUM_DISPLAY_AMOUNT })}`
4039
}
40+
41+
if (hasExchangeRate) {
42+
return formatCurrency({ amount: amountInCurrency })
43+
}
44+
4145
return rawFormatCurrency({
42-
amount: totalRewardsFiat.toNumber(),
46+
amount: amountInCurrency,
4347
currency: 'USD',
4448
boostSmallNumberPrecision: false
4549
})

packages/core-mobile/app/new/features/defiMarket/hooks/useDepositableTokens.ts

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,51 @@ export const useDepositableTokens = (
1010
return useMemo(() => {
1111
const uniqueAssets = new Map<string, DefiAssetDetails>()
1212

13-
markets.forEach(market => {
14-
const asset = market.asset
15-
// Use contractAddress if available, otherwise use lowercase symbol
16-
const key = asset.contractAddress ?? asset.symbol.toLowerCase()
17-
18-
// Only add if not already in map (keeps first occurrence)
19-
if (!uniqueAssets.has(key)) {
20-
uniqueAssets.set(key, asset)
21-
}
22-
})
23-
24-
return Array.from(uniqueAssets.values()).sort((a, b) => {
25-
if (a.symbol.toLowerCase() === 'avax') return -1
26-
if (b.symbol.toLowerCase() === 'avax') return 1
27-
28-
const tokenWithBalanceA = findMatchingTokenWithBalance(
29-
a,
30-
tokensWithBalance
31-
)
32-
const tokenWithBalanceB = findMatchingTokenWithBalance(
33-
b,
34-
tokensWithBalance
35-
)
36-
37-
if (
38-
tokenWithBalanceA?.balanceInCurrency &&
39-
tokenWithBalanceB?.balanceInCurrency
40-
) {
41-
return (
42-
tokenWithBalanceB.balanceInCurrency -
43-
tokenWithBalanceA.balanceInCurrency
44-
)
45-
}
46-
if (tokenWithBalanceA?.balanceInCurrency) return -1
47-
if (tokenWithBalanceB?.balanceInCurrency) return 1
48-
49-
return a.symbol.toLowerCase().localeCompare(b.symbol.toLowerCase())
50-
})
13+
markets
14+
.filter(market => market.supplyCapReached === false)
15+
.forEach(market => {
16+
const asset = market.asset
17+
const key = asset.contractAddress ?? asset.symbol.toLowerCase()
18+
19+
if (!uniqueAssets.has(key)) {
20+
uniqueAssets.set(key, asset)
21+
}
22+
})
23+
24+
return Array.from(uniqueAssets.values()).sort((a, b) =>
25+
sortByPriority(a, b, tokensWithBalance)
26+
)
5127
}, [markets, tokensWithBalance])
5228
}
29+
30+
const sortByPriority = (
31+
a: DefiAssetDetails,
32+
b: DefiAssetDetails,
33+
tokensWithBalance: LocalTokenWithBalance[]
34+
): number => {
35+
// AVAX always comes first
36+
if (a.symbol.toLowerCase() === 'avax') return -1
37+
if (b.symbol.toLowerCase() === 'avax') return 1
38+
39+
const tokenA = findMatchingTokenWithBalance(a, tokensWithBalance)
40+
const tokenB = findMatchingTokenWithBalance(b, tokensWithBalance)
41+
42+
// Sort by fiat balance (descending)
43+
if (tokenA?.balanceInCurrency && tokenB?.balanceInCurrency) {
44+
return tokenB.balanceInCurrency - tokenA.balanceInCurrency
45+
}
46+
if (tokenA?.balanceInCurrency) return -1
47+
if (tokenB?.balanceInCurrency) return 1
48+
49+
// Sort by token balance (descending)
50+
if (tokenA?.balance !== undefined && tokenB?.balance !== undefined) {
51+
if (tokenB.balance > tokenA.balance) return 1
52+
if (tokenB.balance < tokenA.balance) return -1
53+
return 0
54+
}
55+
if (tokenA?.balance !== undefined) return -1
56+
if (tokenB?.balance !== undefined) return 1
57+
58+
// Sort alphabetically
59+
return a.symbol.toLowerCase().localeCompare(b.symbol.toLowerCase())
60+
}

packages/core-mobile/app/new/features/defiMarket/screens/DepositDetailScreen.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ export function DepositDetailScreen(): JSX.Element {
106106
}}>
107107
<View
108108
sx={{
109-
maxWidth: 280,
110109
flexDirection: 'row',
111110
gap: 12
112111
}}>
113112
{data.map((item, index) => (
114-
<View key={index} sx={{ flex: 1 }}>
113+
<View key={index} sx={{ flex: index === 2 ? 0.8 : 0.5 }}>
115114
<Text variant="heading5" sx={{ color: '$textPrimary' }}>
116115
{item.value}
117116
</Text>

packages/core-mobile/app/new/features/defiMarket/screens/deposit/SelectAssetScreen.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { ErrorState } from 'common/components/ErrorState'
1313
import { useFormatCurrency } from 'common/hooks/useFormatCurrency'
1414
import { UNKNOWN_AMOUNT } from 'consts/amount'
1515
import { useRouter } from 'expo-router'
16-
import { useBuy } from 'features/meld/hooks/useBuy'
1716
import { useNavigateToSwap } from 'features/swap/hooks/useNavigateToSwap'
1817
import { AVAX_TOKEN_ID } from 'common/consts/swap'
1918
import useCChainNetwork from 'hooks/earn/useCChainNetwork'
@@ -46,7 +45,6 @@ export const SelectAssetScreen = (): JSX.Element => {
4645
theme: { colors }
4746
} = useTheme()
4847
const { formatCurrency } = useFormatCurrency()
49-
const { navigateToBuy, isBuyable } = useBuy()
5048
const { navigateToSwap } = useNavigateToSwap()
5149
const [, setSelectedAsset] = useDepositSelectedAsset()
5250

@@ -70,23 +68,11 @@ export const SelectAssetScreen = (): JSX.Element => {
7068
symbol: marketAsset.symbol
7169
}
7270
})
73-
} else if (isBuyable(undefined, marketAsset.contractAddress)) {
74-
navigateToBuy({
75-
showAvaxWarning: true,
76-
address: marketAsset.contractAddress
77-
})
7871
} else {
7972
navigateToSwap(AVAX_TOKEN_ID, marketAsset.contractAddress)
8073
}
8174
},
82-
[
83-
navigate,
84-
navigateToBuy,
85-
isBuyable,
86-
navigateToSwap,
87-
cChainTokensWithBalance,
88-
setSelectedAsset
89-
]
75+
[navigate, navigateToSwap, cChainTokensWithBalance, setSelectedAsset]
9076
)
9177

9278
const renderItem = useCallback(
@@ -134,7 +120,10 @@ export const SelectAssetScreen = (): JSX.Element => {
134120
type="secondary"
135121
size="small"
136122
onPress={() => handleSelectToken(item)}>
137-
{tokenWithBalance?.balanceInCurrency ? 'Deposit' : 'Buy'}
123+
{tokenWithBalance?.balance !== undefined &&
124+
tokenWithBalance.balance > 0n
125+
? 'Deposit'
126+
: 'Buy'}
138127
</Button>
139128
</View>
140129
</TouchableOpacity>

0 commit comments

Comments
 (0)