@@ -29,10 +29,12 @@ import { useICXUnstakingTime } from '@/store/application/hooks';
2929import {
3030 useCollateralActionHandlers ,
3131 useCollateralState ,
32+ useCollateralTokens ,
3233 useDerivedCollateralInfo ,
3334 useIsHandlingICX ,
3435 useSupportedCollateralTokens ,
3536} from '@/store/collateral/hooks' ;
37+ import { getActionMaximum , isCollateralEnabled , isIncreaseAllowed } from '@/store/collateral/eligibility' ;
3638import { Field } from '@/store/collateral/reducer' ;
3739import { useLoanActionHandlers , useLockedCollateralAmount } from '@/store/loan/hooks' ;
3840import { useRatio } from '@/store/ratio/hooks' ;
@@ -150,7 +152,12 @@ const CollateralPanel = () => {
150152 const { isAdjusting, inputType } = useCollateralState ( ) ;
151153 const ratio = useRatio ( ) ;
152154 const isHandlingICX = useIsHandlingICX ( ) ;
155+ const { data : collateralTokens } = useCollateralTokens ( ) ;
153156 const { data : supportedCollateralTokens } = useSupportedCollateralTokens ( ) ;
157+ const contractSymbol = useWrongSymbol ( collateralType ) ;
158+ const increaseEnabled = isCollateralEnabled ( supportedCollateralTokens , contractSymbol ) ;
159+ const actionMaximum = getActionMaximum ( collateralDeposit , collateralTotal , increaseEnabled ) ;
160+ const canAdjustCollateral = increaseEnabled || collateralDeposit . isGreaterThan ( 0 ) ;
154161 const [ ICXWithdrawOption , setICXWithdrawOption ] = useState < ICXWithdrawOptions > ( ICXWithdrawOptions . KEEPSICX ) ;
155162 const { data : icxUnstakingTime } = useICXUnstakingTime ( ) ;
156163 const isSuperSmall = useMedia ( `(max-width: 359px)` ) ;
@@ -177,6 +184,7 @@ const CollateralPanel = () => {
177184 } ) ;
178185
179186 const handleEnableAdjusting = ( ) => {
187+ if ( ! canAdjustCollateral ) return ;
180188 adjust ( true ) ;
181189 adjustLoan ( false ) ;
182190 } ;
@@ -193,6 +201,11 @@ const CollateralPanel = () => {
193201 const isCrossChain = ! ( sourceChain === '0x1.icon' || sourceChain === '0x2.icon' ) ;
194202
195203 const toggleOpen = ( ) => {
204+ if ( open ) {
205+ setOpen ( false ) ;
206+ return ;
207+ }
208+ if ( ! isIncreaseAllowed ( collateralDeposit , parsedAmount [ Field . LEFT ] , increaseEnabled ) ) return ;
196209 if ( isCrossChain ) {
197210 setStoredModalValues ( {
198211 amount : `${ differenceAmount . dp ( collateralDecimalPlaces ) . toFormat ( ) } ${ formatSymbol ( collateralType ) } ` ,
@@ -209,10 +222,12 @@ const CollateralPanel = () => {
209222 const addTransaction = useTransactionAdder ( ) ;
210223
211224 const handleCollateralConfirm = async ( ) => {
225+ if ( shouldDeposit && ! increaseEnabled ) return ;
226+ const collateralTokenAddress = collateralTokens ?. [ contractSymbol ] ;
227+ if ( ! collateralTokenAddress ) return ;
228+
212229 window . addEventListener ( 'beforeunload' , showMessageOnBeforeUnload ) ;
213- const collateralTokenAddress =
214- supportedCollateralTokens && supportedCollateralTokens [ useWrongSymbol ( collateralType ) ] ;
215- const cx = bnJs . inject ( { account } ) . getContract ( collateralTokenAddress ! ) ;
230+ const cx = bnJs . inject ( { account } ) . getContract ( collateralTokenAddress ) ;
216231 const decimals : string = await cx . decimals ( ) ;
217232
218233 if ( shouldDeposit ) {
@@ -356,8 +371,8 @@ const CollateralPanel = () => {
356371
357372 // add small amount of collateral to lock to avoid tx errors.
358373 const tLockedAmount = React . useMemo (
359- ( ) => BigNumber . min ( lockedCollateral . times ( shouldShowLock ? 1.005 : 1 ) , collateralTotal ) ,
360- [ lockedCollateral , collateralTotal , shouldShowLock ] ,
374+ ( ) => BigNumber . min ( lockedCollateral . times ( shouldShowLock ? 1.005 : 1 ) , actionMaximum ) ,
375+ [ lockedCollateral , actionMaximum , shouldShowLock ] ,
361376 ) ;
362377
363378 const percent = collateralTotal . isZero ( ) ? 0 : tLockedAmount . div ( collateralTotal ) . times ( 100 ) . toNumber ( ) ;
@@ -391,7 +406,7 @@ const CollateralPanel = () => {
391406 < CollateralTypeSwitcher width = { width } containerRef = { ref . current } />
392407 </ CollateralTypeSwitcherWrap >
393408
394- { account && collateralTotal ?. isGreaterThan ( 0 ) && (
409+ { account && collateralTotal ?. isGreaterThan ( 0 ) && canAdjustCollateral && (
395410 < Flex flexDirection = { isSuperSmall ? 'column' : 'row' } ml = "auto" paddingTop = { isSuperSmall ? '4px' : '0' } >
396411 { isAdjusting ? (
397412 < >
@@ -403,7 +418,11 @@ const CollateralPanel = () => {
403418 >
404419 < Trans > Cancel</ Trans >
405420 </ TextButton >
406- < Button onClick = { toggleOpen } fontSize = { 14 } disabled = { ! isSliderStateChanged } >
421+ < Button
422+ onClick = { toggleOpen }
423+ fontSize = { 14 }
424+ disabled = { ! isSliderStateChanged || ( shouldDeposit && ! increaseEnabled ) }
425+ >
407426 < Trans > Confirm</ Trans >
408427 </ Button >
409428 </ >
@@ -451,9 +470,9 @@ const CollateralPanel = () => {
451470 range = { {
452471 min : [ 0 ] ,
453472 max : [
454- collateralTotal . isZero ( )
473+ actionMaximum . isZero ( )
455474 ? SLIDER_RANGE_MAX_BOTTOM_THRESHOLD
456- : collateralTotal . dp ( collateralDecimalPlaces ) . toNumber ( ) ,
475+ : actionMaximum . dp ( collateralDecimalPlaces ) . toNumber ( ) ,
457476 ] ,
458477 } }
459478 instanceRef = { instance => {
@@ -475,14 +494,14 @@ const CollateralPanel = () => {
475494 value = { formattedAmounts [ Field . LEFT ] }
476495 decimalPlaces = { collateralDecimalPlaces }
477496 currency = { isHandlingICX ? 'ICX' : formatSymbol ( collateralType ) }
478- maxValue = { collateralTotal }
497+ maxValue = { actionMaximum }
479498 onUserInput = { onFieldAInput }
480499 />
481500 </ PanelInfoItem >
482501
483502 < PanelInfoItem >
484503 < CurrencyField
485- editable = { isAdjusting }
504+ editable = { isAdjusting && increaseEnabled }
486505 isActive = { false }
487506 label = "Wallet"
488507 tooltipText = {
@@ -510,6 +529,7 @@ const CollateralPanel = () => {
510529 < XCollateralModal
511530 account = { account }
512531 currencyAmount = { xTokenAmount }
532+ increaseEnabled = { increaseEnabled }
513533 sourceChain = { sourceChain }
514534 storedModalValues = { storedModalValues }
515535 />
@@ -617,7 +637,9 @@ const CollateralPanel = () => {
617637 onClick = { handleCollateralConfirm }
618638 fontSize = { 14 }
619639 disabled = {
620- ! hasEnoughICX || ( isHandlingICX && ! shouldDeposit && ICXWithdrawOption === ICXWithdrawOptions . EMPTY )
640+ ! hasEnoughICX ||
641+ ( shouldDeposit && ! increaseEnabled ) ||
642+ ( isHandlingICX && ! shouldDeposit && ICXWithdrawOption === ICXWithdrawOptions . EMPTY )
621643 }
622644 >
623645 { shouldDeposit ? t `Deposit` : t `Withdraw` }
0 commit comments