Skip to content

Commit 804aa67

Browse files
linkielinkPatricie29dependabot[bot]StefChatz
authored
v3.0.8 (#1754)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Patricie <99055449+Patricie29@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Patricie29 <patricie@marsprotocol.foundation> Co-authored-by: Monkmansteve <sxatzakis@yahoo.gr> Co-authored-by: Monkmansteve <47855432+StefChatz@users.noreply.github.com>
1 parent 4ff761c commit 804aa67

File tree

3 files changed

+84
-69
lines changed

3 files changed

+84
-69
lines changed
Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import { useMemo } from 'react'
1+
import { useCallback, useMemo } from 'react'
22

3-
import ActionButton from 'components/common/Button/ActionButton'
43
import DropDownButton from 'components/common/Button/DropDownButton'
5-
import { ArrowUpLine, CoinsSwap, Enter } from 'components/common/Icons'
6-
import Text from 'components/common/Text'
7-
import { Tooltip } from 'components/common/Tooltip'
8-
import ConditionalWrapper from 'hocs/ConditionalWrapper'
9-
import useAccountId from 'hooks/accounts/useAccountId'
4+
import { AccountArrowDown, ArrowUpLine, CoinsSwap, Enter } from 'components/common/Icons'
105
import useCurrentAccount from 'hooks/accounts/useCurrentAccount'
116
import useDepositModal from 'hooks/common/useDepositModal'
127
import useLendAndReclaimModal from 'hooks/common/useLendAndReclaimModal'
@@ -37,27 +32,35 @@ export default function LendButton(props: Props) {
3732
const isAutoLendEnabled = props.data.asset.isAutoLendEnabled
3833
const assetDepositAmount = accountDeposits.find(byDenom(props.data.asset.denom))?.amount
3934
const address = useStore((s) => s.address)
40-
const accountId = useAccountId()
41-
const hasNoDeposit = !!(!assetDepositAmount && accountId)
4235
const hasWalletBalance = walletBalance && BN(walletBalance.amount).isGreaterThan(0)
43-
const isDefaultAccount = currentAccount?.kind === 'default'
36+
37+
// Check if user has this specific asset in deposits or lends
38+
const hasThisAssetInAccount = useMemo(() => {
39+
const inDeposits = currentAccount?.deposits?.find(byDenom(props.data.asset.denom))?.amount
40+
const inLends = currentAccount?.lends?.find(byDenom(props.data.asset.denom))?.amount
41+
return (inDeposits && !inDeposits.isZero()) || (inLends && !inLends.isZero())
42+
}, [currentAccount?.deposits, currentAccount?.lends, props.data.asset.denom])
43+
44+
const handleWithdraw = useCallback(() => {
45+
useStore.setState({ fundAndWithdrawModal: 'withdraw' })
46+
}, [])
4447

4548
const ITEMS: DropDownItem[] = useMemo(
4649
() => [
47-
{
48-
icon: <Enter />,
49-
text: 'Deposit',
50-
onClick: () => {
51-
openDeposit(props.data)
52-
},
53-
},
54-
{
55-
icon: <CoinsSwap />,
56-
text: 'Deposit & Lend',
57-
onClick: () => {
58-
openDepositAndLend(props.data)
59-
},
60-
},
50+
...(hasWalletBalance
51+
? [
52+
{
53+
icon: <Enter />,
54+
text: 'Deposit',
55+
onClick: () => openDeposit(props.data),
56+
},
57+
{
58+
icon: <CoinsSwap />,
59+
text: 'Deposit & Lend',
60+
onClick: () => openDepositAndLend(props.data),
61+
},
62+
]
63+
: []),
6164
...(assetDepositAmount
6265
? [
6366
{
@@ -67,52 +70,35 @@ export default function LendButton(props: Props) {
6770
},
6871
]
6972
: []),
73+
...(hasThisAssetInAccount
74+
? [
75+
{
76+
icon: <AccountArrowDown />,
77+
text: 'Withdraw',
78+
onClick: handleWithdraw,
79+
},
80+
]
81+
: []),
82+
],
83+
[
84+
assetDepositAmount,
85+
hasThisAssetInAccount,
86+
hasWalletBalance,
87+
handleWithdraw,
88+
openDeposit,
89+
openDepositAndLend,
90+
openLend,
91+
props.data,
7092
],
71-
[assetDepositAmount, openLend, openDeposit, openDepositAndLend, props.data],
7293
)
7394

7495
if (!isAutoLendEnabled && address) return null
96+
if (!address) return null
97+
if (ITEMS.length === 0) return null
7598

76-
// If user has wallet balance and it's a default account, show Manage dropdown
77-
if (hasWalletBalance && address && isDefaultAccount) {
78-
return (
79-
<div className='flex justify-end'>
80-
<DropDownButton items={ITEMS} text='Manage' color='tertiary' />
81-
</div>
82-
)
83-
}
84-
85-
// Otherwise show the original Lend button
8699
return (
87100
<div className='flex justify-end'>
88-
<ConditionalWrapper
89-
condition={hasNoDeposit && !!address}
90-
wrapper={(children) => (
91-
<Tooltip
92-
type='warning'
93-
content={
94-
<Text size='sm'>{`You don't have any ${props.data.asset.symbol}.
95-
Please first deposit ${props.data.asset.symbol} into your Credit Account before lending.`}</Text>
96-
}
97-
contentClassName='max-w-[200px]'
98-
className='ml-auto'
99-
>
100-
{children}
101-
</Tooltip>
102-
)}
103-
>
104-
<ActionButton
105-
leftIcon={<ArrowUpLine />}
106-
disabled={hasNoDeposit}
107-
color='tertiary'
108-
onClick={(e) => {
109-
openLend(props.data)
110-
e.stopPropagation()
111-
}}
112-
text='Lend'
113-
short
114-
/>
115-
</ConditionalWrapper>
101+
<DropDownButton items={ITEMS} text='Manage' color='tertiary' />
116102
</div>
117103
)
118104
}

src/components/earn/lend/Table/Columns/Manage.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ACCOUNT_MENU_BUTTON_ID } from 'components/account/AccountMenuContent'
55
import AlertDialog from 'components/common/AlertDialog'
66
import DropDownButton from 'components/common/Button/DropDownButton'
77
import {
8+
AccountArrowDown,
89
ArrowDownLine,
910
ArrowUpLine,
1011
CoinsSwap,
@@ -72,6 +73,10 @@ export default function Manage(props: Props) {
7273
handleDialogClose()
7374
}
7475

76+
const handleWithdraw = useCallback(() => {
77+
useStore.setState({ fundAndWithdrawModal: 'withdraw' })
78+
}, [])
79+
7580
const ITEMS: DropDownItem[] = useMemo(
7681
() => [
7782
{
@@ -98,8 +103,21 @@ export default function Manage(props: Props) {
98103
text: 'Unlend',
99104
onClick: handleUnlend,
100105
},
106+
{
107+
icon: <AccountArrowDown />,
108+
text: 'Withdraw',
109+
onClick: handleWithdraw,
110+
},
111+
],
112+
[
113+
handleUnlend,
114+
handleWithdraw,
115+
hasAssetInDeposits,
116+
hasWalletBalance,
117+
openDepositAndLend,
118+
openLend,
119+
props.data,
101120
],
102-
[handleUnlend, hasAssetInDeposits, hasWalletBalance, openDepositAndLend, openLend, props.data],
103121
)
104122

105123
if (!address) return null

src/hooks/health-computer/useHealthComputer.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,22 @@ export default function useHealthComputer(account?: Account) {
150150
)
151151
}, [vaultConfigs])
152152

153+
// Get denoms of perps positions the account has open
154+
const accountPerpsDenoms = useMemo(() => {
155+
if (!account?.perps) return new Set<string>()
156+
return new Set(account.perps.map((p) => p.denom))
157+
}, [account?.perps])
158+
153159
const perpsParamsData = useMemo(() => {
154160
if (!perpsParams) return {}
155161
return perpsParams.reduce(
156162
(prev, curr) => {
163+
// For markets where the account has open positions, force enabled to true
164+
// so the health computer can properly calculate health
165+
const hasOpenPosition = accountPerpsDenoms.has(curr.denom)
157166
prev[curr.denom] = {
158167
...curr,
168+
enabled: hasOpenPosition ? true : curr.enabled,
159169
max_long_oi_value: BN(curr.max_long_oi_value).toString(),
160170
max_short_oi_value: BN(curr.max_short_oi_value).toString(),
161171
max_net_oi_value: BN(curr.max_net_oi_value).toString(),
@@ -165,7 +175,7 @@ export default function useHealthComputer(account?: Account) {
165175
},
166176
{} as { [key: string]: PerpParams },
167177
)
168-
}, [perpsParams])
178+
}, [perpsParams, accountPerpsDenoms])
169179

170180
const marketStates = useMemo(() => {
171181
const marketStates: { [key: string]: MarketResponse } = {}
@@ -324,9 +334,10 @@ export default function useHealthComputer(account?: Account) {
324334

325335
const computeMaxPerpAmount = useCallback(
326336
(denom: string, tradeDirection: TradeDirection) => {
327-
if (!healthComputer || !perpsVault || !marketStates) return BN_ZERO
328-
// Return early if the market is disabled
329-
if (!perpsParamsData[denom]?.enabled) return BN_ZERO
337+
if (!healthComputer || !perpsVault || !marketStates || !perpsParams) return BN_ZERO
338+
// Return early if the market is disabled (check original params, not modified perpsParamsData)
339+
const originalParams = perpsParams.find((p) => p.denom === denom)
340+
if (!originalParams?.enabled) return BN_ZERO
330341
const positions = [
331342
...healthComputer.positions.deposits,
332343
...healthComputer.positions.lends,
@@ -354,7 +365,7 @@ export default function useHealthComputer(account?: Account) {
354365
return BN_ZERO
355366
}
356367
},
357-
[healthComputer, perpsVault, marketStates, perpsParamsData, assets],
368+
[healthComputer, perpsVault, marketStates, perpsParams, assets],
358369
)
359370

360371
const health = useMemo(() => {

0 commit comments

Comments
 (0)