Skip to content

Commit cb2b6b9

Browse files
authored
Merge pull request #1763 from mars-protocol/develop
v3.0.8
2 parents 66675e3 + a70bb32 commit cb2b6b9

File tree

12 files changed

+209
-78
lines changed

12 files changed

+209
-78
lines changed

src/components/Modals/BorrowModal.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,27 @@ function BorrowModal(props: Props) {
501501
asset.denom,
502502
newAmount.isGreaterThan(max) ? max : newAmount,
503503
)
504-
const target = borrowToWallet ? 'wallet' : isAutoLendEnabled ? 'lend' : 'deposit'
504+
const target = borrowToWallet
505+
? 'wallet'
506+
: isAutoLendEnabled && asset.isAutoLendEnabled
507+
? 'lend'
508+
: 'deposit'
505509
simulateBorrow(target, borrowCoin)
506510
setTimeout(() => {
507511
setLoadingState({ isLoading: false, action: 'none' })
508512
}, 500)
509513
}
510514
},
511-
[amount, asset.denom, borrowToWallet, isAutoLendEnabled, isRepay, max, simulateBorrow],
515+
[
516+
amount,
517+
asset.denom,
518+
asset.isAutoLendEnabled,
519+
borrowToWallet,
520+
isAutoLendEnabled,
521+
isRepay,
522+
max,
523+
simulateBorrow,
524+
],
512525
)
513526

514527
const maxBorrow = useMemo(() => {

src/components/Modals/Deposit/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function DepositModal({ currentAccount, config }: Props) {
3333
const { data, action } = config
3434
const { asset } = data
3535

36-
const isDepositAndLend = action === 'deposit-and-lend'
36+
const isLendEnabled = !!asset.isAutoLendEnabled
37+
const isDepositAndLend = action === 'deposit-and-lend' && isLendEnabled
3738
const actionText = isDepositAndLend ? 'Deposit and Lend' : 'Deposit'
3839

3940
const walletBalance = useCurrentWalletBalance(asset.denom)

src/components/borrow/Table/Columns/Manage.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export default function Manage(props: Props) {
3535
}, [props.data])
3636

3737
const isDeprecatedAsset = props.data.asset.isDeprecated
38-
const isBorrowDisabled = isDeprecatedAsset || isUSDC
38+
const isBorrowEnabled = props.data.asset.isBorrowEnabled
39+
const canBorrowMore = isBorrowEnabled && !isUSDC
40+
const isBorrowDisabled = isDeprecatedAsset || isUSDC || !isBorrowEnabled
3941

4042
const ITEMS: DropDownItem[] = useMemo(
4143
() => [
@@ -44,7 +46,7 @@ export default function Manage(props: Props) {
4446
text: 'Repay',
4547
onClick: repayHandler,
4648
},
47-
...(!isUSDC
49+
...(canBorrowMore
4850
? [
4951
{
5052
icon: <Plus />,
@@ -54,7 +56,7 @@ export default function Manage(props: Props) {
5456
]
5557
: []),
5658
],
57-
[borrowHandler, repayHandler, isUSDC],
59+
[borrowHandler, repayHandler, canBorrowMore],
5860
)
5961

6062
if (!address) return null
Lines changed: 112 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { useCallback, useMemo } from 'react'
1+
import { useMemo } from 'react'
22

3+
import ActionButton from 'components/common/Button/ActionButton'
34
import DropDownButton from 'components/common/Button/DropDownButton'
4-
import { AccountArrowDown, ArrowUpLine, CoinsSwap, Enter } from 'components/common/Icons'
5+
import { ArrowUpLine, CoinsSwap } from 'components/common/Icons'
6+
import Text from 'components/common/Text'
7+
import { Tooltip } from 'components/common/Tooltip'
8+
import ConditionalWrapper from 'hocs/ConditionalWrapper'
59
import useCurrentAccount from 'hooks/accounts/useCurrentAccount'
610
import useDepositModal from 'hooks/common/useDepositModal'
711
import useLendAndReclaimModal from 'hooks/common/useLendAndReclaimModal'
812
import useCurrentAccountDeposits from 'hooks/wallet/useCurrentAccountDeposits'
913
import useCurrentWalletBalance from 'hooks/wallet/useCurrentWalletBalance'
14+
import useAutoLend from 'hooks/wallet/useAutoLend'
1015
import useStore from 'store'
1116
import { byDenom } from 'utils/array'
17+
import { isDepositOnlyAsset } from 'utils/assets'
1218
import { BN } from 'utils/helpers'
1319

1420
export const LEND_BUTTON_META = {
@@ -29,39 +35,35 @@ export default function LendButton(props: Props) {
2935
const accountDeposits = useCurrentAccountDeposits()
3036
const currentAccount = useCurrentAccount()
3137
const walletBalance = useCurrentWalletBalance(props.data.asset.denom)
32-
const isAutoLendEnabled = props.data.asset.isAutoLendEnabled
3338
const assetDepositAmount = accountDeposits.find(byDenom(props.data.asset.denom))?.amount
3439
const address = useStore((s) => s.address)
3540
const hasWalletBalance = walletBalance && BN(walletBalance.amount).isGreaterThan(0)
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-
}, [])
41+
const isDefaultAccount = currentAccount?.kind === 'default'
42+
const isLendEnabled = !!props.data.asset.isAutoLendEnabled
43+
const { isAutoLendEnabledForCurrentAccount } = useAutoLend()
44+
const isDepositOnly = isDepositOnlyAsset(props.data.asset)
4745

4846
const ITEMS: DropDownItem[] = useMemo(
4947
() => [
50-
...(hasWalletBalance
48+
{
49+
icon: <ArrowUpLine />,
50+
text: 'Deposit',
51+
onClick: () => {
52+
openDeposit(props.data)
53+
},
54+
},
55+
...(isLendEnabled
5156
? [
52-
{
53-
icon: <Enter />,
54-
text: 'Deposit',
55-
onClick: () => openDeposit(props.data),
56-
},
5757
{
5858
icon: <CoinsSwap />,
5959
text: 'Deposit & Lend',
60-
onClick: () => openDepositAndLend(props.data),
60+
onClick: () => {
61+
openDepositAndLend(props.data)
62+
},
6163
},
6264
]
6365
: []),
64-
...(assetDepositAmount
66+
...(isLendEnabled && assetDepositAmount
6567
? [
6668
{
6769
icon: <ArrowUpLine />,
@@ -70,35 +72,100 @@ export default function LendButton(props: Props) {
7072
},
7173
]
7274
: []),
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,
9275
],
76+
[assetDepositAmount, isLendEnabled, openLend, openDeposit, openDepositAndLend, props.data],
9377
)
9478

95-
if (!isAutoLendEnabled && address) return null
96-
if (!address) return null
97-
if (ITEMS.length === 0) return null
79+
// If user has wallet balance and it's a default account, show Manage dropdown
80+
if (hasWalletBalance && address && isDefaultAccount) {
81+
return (
82+
<div className='flex justify-end'>
83+
<DropDownButton items={ITEMS} text='Manage' color='tertiary' />
84+
</div>
85+
)
86+
}
87+
88+
// For LSTs, show Deposit button only
89+
if (isDepositOnly) {
90+
return (
91+
<div className='flex justify-end'>
92+
<ConditionalWrapper
93+
condition={!hasWalletBalance && !!address}
94+
wrapper={(children) => (
95+
<Tooltip
96+
type='warning'
97+
content={
98+
<Text size='sm'>{`You don't have any ${props.data.asset.symbol} in your wallet.`}</Text>
99+
}
100+
contentClassName='max-w-[200px]'
101+
className='ml-auto'
102+
>
103+
{children}
104+
</Tooltip>
105+
)}
106+
>
107+
<ActionButton
108+
leftIcon={<ArrowUpLine />}
109+
disabled={!hasWalletBalance}
110+
color='tertiary'
111+
onClick={(e) => {
112+
openDeposit(props.data)
113+
e.stopPropagation()
114+
}}
115+
text='Deposit'
116+
short
117+
/>
118+
</ConditionalWrapper>
119+
</div>
120+
)
121+
}
122+
123+
// If asset doesn't support lending, don't show anything
124+
if (!isLendEnabled) return null
98125

99126
return (
100127
<div className='flex justify-end'>
101-
<DropDownButton items={ITEMS} text='Manage' color='tertiary' />
128+
<ConditionalWrapper
129+
condition={!hasWalletBalance && !!address}
130+
wrapper={(children) => (
131+
<Tooltip
132+
type='warning'
133+
content={
134+
<Text size='sm'>{`You don't have any ${props.data.asset.symbol} in your wallet.`}</Text>
135+
}
136+
contentClassName='max-w-[200px]'
137+
className='ml-auto'
138+
>
139+
{children}
140+
</Tooltip>
141+
)}
142+
>
143+
{isAutoLendEnabledForCurrentAccount ? (
144+
<ActionButton
145+
leftIcon={<ArrowUpLine />}
146+
disabled={!hasWalletBalance}
147+
color='tertiary'
148+
onClick={(e) => {
149+
openLend(props.data)
150+
e.stopPropagation()
151+
}}
152+
text='Lend'
153+
short
154+
/>
155+
) : (
156+
<ActionButton
157+
leftIcon={<ArrowUpLine />}
158+
disabled={!hasWalletBalance}
159+
color='tertiary'
160+
onClick={(e) => {
161+
openDeposit(props.data)
162+
e.stopPropagation()
163+
}}
164+
text='Deposit'
165+
short
166+
/>
167+
)}
168+
</ConditionalWrapper>
102169
</div>
103170
)
104171
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default function Manage(props: Props) {
4040
const address = useStore((s) => s.address)
4141
const account = useCurrentAccount()
4242
const walletBalance = useCurrentWalletBalance(props.data.asset.denom)
43+
const isLendEnabled = !!props.data.asset.isAutoLendEnabled
4344

4445
const hasAssetInDeposits = useMemo(
4546
() => !!account?.deposits?.find((deposit) => deposit.denom === props.data.asset.denom)?.amount,
@@ -83,13 +84,17 @@ export default function Manage(props: Props) {
8384
icon: <ArrowUpLine />,
8485
text: 'Lend more',
8586
onClick: () => openLend(props.data),
86-
disabled: !hasAssetInDeposits,
87-
...(!hasAssetInDeposits && {
88-
disabledTooltip: `You don't have any ${props.data.asset.symbol}.
89-
Please first deposit ${props.data.asset.symbol} into your Credit Account before lending.`,
87+
disabled: !isLendEnabled || !hasAssetInDeposits,
88+
...(!isLendEnabled && {
89+
disabledTooltip: `${props.data.asset.symbol} can only be deposited, not lent.`,
9090
}),
91+
...(isLendEnabled &&
92+
!hasAssetInDeposits && {
93+
disabledTooltip: `You don't have any ${props.data.asset.symbol}.
94+
Please first deposit ${props.data.asset.symbol} into your Credit Account before lending.`,
95+
}),
9196
},
92-
...(hasWalletBalance
97+
...(hasWalletBalance && isLendEnabled
9398
? [
9499
{
95100
icon: <CoinsSwap />,
@@ -117,6 +122,7 @@ export default function Manage(props: Props) {
117122
openDepositAndLend,
118123
openLend,
119124
props.data,
125+
isLendEnabled,
120126
],
121127
)
122128

src/components/trade/TradeModule/SwapForm/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,21 @@ export default function SwapForm(props: Props) {
168168
])
169169

170170
const updateAccount = useCallback(
171-
(removeCoin: BNCoin, addCoin: BNCoin, debtCoin: BNCoin, isBorrowEnabled: boolean) => {
171+
(removeCoin: BNCoin, addCoin: BNCoin, debtCoin: BNCoin, isLendEnabled: boolean) => {
172172
if (addCoin.amount.isZero()) {
173173
simulateTrade(
174174
BNCoin.fromDenomAndBigNumber(inputAsset.denom, BN_ZERO),
175175
BNCoin.fromDenomAndBigNumber(outputAsset.denom, BN_ZERO),
176176
BNCoin.fromDenomAndBigNumber(inputAsset.denom, BN_ZERO),
177-
isAutoLendEnabled && isBorrowEnabled && !isAutoRepayChecked ? 'lend' : 'deposit',
177+
isAutoLendEnabled && isLendEnabled && !isAutoRepayChecked ? 'lend' : 'deposit',
178178
isAutoRepayChecked,
179179
)
180180
} else {
181181
simulateTrade(
182182
removeCoin,
183183
addCoin,
184184
debtCoin,
185-
isAutoLendEnabled && isBorrowEnabled && !isAutoRepayChecked ? 'lend' : 'deposit',
185+
isAutoLendEnabled && isLendEnabled && !isAutoRepayChecked ? 'lend' : 'deposit',
186186
isAutoRepayChecked,
187187
)
188188
}
@@ -305,7 +305,9 @@ export default function SwapForm(props: Props) {
305305
BNCoin.fromDenomAndBigNumber(inputAsset.denom, BN_ZERO),
306306
BNCoin.fromDenomAndBigNumber(outputAsset.denom, BN_ZERO),
307307
BNCoin.fromDenomAndBigNumber(inputAsset.denom, BN_ZERO),
308-
isAutoLendEnabled && outputAsset.isBorrowEnabled && !isAutoRepayChecked ? 'lend' : 'deposit',
308+
isAutoLendEnabled && outputAsset.isAutoLendEnabled && !isAutoRepayChecked
309+
? 'lend'
310+
: 'deposit',
309311
isAutoRepayChecked,
310312
)
311313
}, [
@@ -314,7 +316,7 @@ export default function SwapForm(props: Props) {
314316
useMargin,
315317
useAutoRepay,
316318
outputAsset.denom,
317-
outputAsset.isBorrowEnabled,
319+
outputAsset.isAutoLendEnabled,
318320
inputAsset.denom,
319321
isAutoLendEnabled,
320322
isAutoRepayChecked,
@@ -342,13 +344,13 @@ export default function SwapForm(props: Props) {
342344
const addCoin = BNCoin.fromDenomAndBigNumber(outputAsset.denom, outputAssetAmount)
343345
const debtCoin = BNCoin.fromDenomAndBigNumber(inputAsset.denom, addDebtAmount)
344346

345-
updateAccount(removeCoin, addCoin, debtCoin, outputAsset.isBorrowEnabled ?? true)
347+
updateAccount(removeCoin, addCoin, debtCoin, outputAsset.isAutoLendEnabled ?? false)
346348
}, [
347349
inputAssetAmount,
348350
outputAssetAmount,
349351
inputMarginThreshold,
350352
outputAsset.denom,
351-
outputAsset.isBorrowEnabled,
353+
outputAsset.isAutoLendEnabled,
352354
inputAsset.denom,
353355
updateAccount,
354356
modal,

0 commit comments

Comments
 (0)