From 6cc84965039a522622f38312497e81b2c4a7e8eb Mon Sep 17 00:00:00 2001 From: Victor Creed Date: Fri, 20 Nov 2020 16:29:26 +0200 Subject: [PATCH 1/2] reintroduced limits to trade, lend and borrow --- .../BorrowingContainer/index.tsx | 30 +++++++++++++--- .../LendingContainer/index.tsx | 10 ++++-- .../components/Amount/index.tsx | 24 +++++++------ .../components/TabContainer/index.tsx | 5 ++- .../LiquidityRemoveContainer/index.tsx | 2 +- src/app/containers/MarginTradeForm/index.tsx | 36 +++++++++++++++---- src/app/containers/SwapTradeForm/index.tsx | 6 +++- src/store/global/events-store/slice.ts | 3 -- 8 files changed, 85 insertions(+), 31 deletions(-) diff --git a/src/app/containers/LendBorrowSovryn/BorrowingContainer/index.tsx b/src/app/containers/LendBorrowSovryn/BorrowingContainer/index.tsx index 7e6596f7f..9c045366d 100644 --- a/src/app/containers/LendBorrowSovryn/BorrowingContainer/index.tsx +++ b/src/app/containers/LendBorrowSovryn/BorrowingContainer/index.tsx @@ -13,12 +13,13 @@ import { FieldGroup } from '../../../components/FieldGroup'; import { AmountField } from '../../AmountField'; import { AssetWalletBalance } from '../../../components/AssetWalletBalance'; import { DummyField } from '../../../components/DummyField/Loadable'; -import { weiTo4 } from '../../../../utils/blockchain/math-helpers'; +import { weiTo4, weiToFixed } from '../../../../utils/blockchain/math-helpers'; import { TradeButton } from '../../../components/TradeButton'; import { SendTxProgress } from '../../../components/SendTxProgress'; -import { bignumber } from 'mathjs'; +import { bignumber, min } from 'mathjs'; import { useDispatch } from 'react-redux'; import { actions } from '../slice'; +import { useLending_transactionLimit } from '../../../hooks/lending/useLending_transactionLimit'; type Props = { currency: Asset; @@ -79,12 +80,20 @@ const BorrowingContainer: React.FC = ({ currency }) => { ); const { value: tokenBalance } = useAssetBalanceOf(tokenToCollarate); + const { + value: maxAmount, + loading: loadingLimit, + } = useLending_transactionLimit(currency, tokenToCollarate); const handleSubmitBorrow = () => { borrow(); }; - const valid = useIsAmountWithinLimits(collateralTokenSent, '1', tokenBalance); + const valid = useIsAmountWithinLimits( + collateralTokenSent, + '1', + maxAmount !== '0' ? min(maxAmount, tokenBalance) : tokenBalance, + ); useEffect(() => { dispatch(actions.changeBorrowAmount(amount)); @@ -114,9 +123,20 @@ const BorrowingContainer: React.FC = ({ currency }) => {
- + + Collateral amount{' '} + {maxAmount !== '0' && !loadingLimit && ( + + (Max: {weiTo4(maxAmount)} {tokenToCollarate}) + + )} + + } + > - {weiTo4(collateralTokenSent)} {tokenToCollarate} + {weiToFixed(collateralTokenSent, 6)} {tokenToCollarate}
diff --git a/src/app/containers/LendBorrowSovryn/LendingContainer/index.tsx b/src/app/containers/LendBorrowSovryn/LendingContainer/index.tsx index df8294946..76380d71a 100644 --- a/src/app/containers/LendBorrowSovryn/LendingContainer/index.tsx +++ b/src/app/containers/LendBorrowSovryn/LendingContainer/index.tsx @@ -15,6 +15,7 @@ import { useLending_approveAndLend } from '../../../hooks/lending/useLending_app import { useLending_approveAndUnlend } from '../../../hooks/lending/useLending_approveAndUnlend'; import { actions } from '../slice'; import { useDispatch } from 'react-redux'; +import { min } from 'mathjs'; type Props = { currency: Asset; @@ -36,11 +37,15 @@ const LendingContainer: React.FC = ({ currency }) => { currency as Asset, useAccount(), ); + const { + value: maxAmount, + loading: loadingLimit, + } = useLending_transactionLimit(currency, currency); const onMaxChange = (type: string) => { let amount = '0'; if (type === 'Deposit') { - amount = userBalance; + amount = min(userBalance, maxAmount); } else if (type === 'Withdraw') { amount = depositedBalance; } @@ -78,8 +83,6 @@ const LendingContainer: React.FC = ({ currency }) => { } }, [unlendTx.loading, unlend]); - const { value: maxAmount } = useLending_transactionLimit(currency, currency); - const valid = useIsAmountWithinLimits( weiAmount, '1', @@ -115,6 +118,7 @@ const LendingContainer: React.FC = ({ currency }) => { currency={currency} amountName="Deposit Amount" maxValue={maxAmount} + loadingLimit={loadingLimit} /> ); }; diff --git a/src/app/containers/LendBorrowSovryn/components/Amount/index.tsx b/src/app/containers/LendBorrowSovryn/components/Amount/index.tsx index 964665250..0d95aad53 100644 --- a/src/app/containers/LendBorrowSovryn/components/Amount/index.tsx +++ b/src/app/containers/LendBorrowSovryn/components/Amount/index.tsx @@ -13,6 +13,7 @@ type Props = { currency: string; minValue?: number | string; maxValue?: number | string; + loadingLimit?: boolean; }; const Amount: React.FC = ({ @@ -23,11 +24,23 @@ const Amount: React.FC = ({ onChangeAmount, amountValue, onMaxChange, + loadingLimit, }) => { return (
- + + {amountName}{' '} + {maxValue !== '0' && !loadingLimit && ( + + (Max: {weiTo4(maxValue)} {currency}) + + )} + + } + > = ({ />
- {maxValue !== '0' && maxValue !== '' && ( -
-
Max:
-
- {currency}{' '} - {weiTo4(maxValue)} -
-
- )}
); }; diff --git a/src/app/containers/LendBorrowSovryn/components/TabContainer/index.tsx b/src/app/containers/LendBorrowSovryn/components/TabContainer/index.tsx index bcd37218e..a7822d841 100644 --- a/src/app/containers/LendBorrowSovryn/components/TabContainer/index.tsx +++ b/src/app/containers/LendBorrowSovryn/components/TabContainer/index.tsx @@ -20,6 +20,7 @@ type Props = { currency: Asset; amountName: string; maxValue: string; + loadingLimit: boolean; minValue?: string; amountValue: string; leftButton: string; @@ -57,6 +58,7 @@ const TabContainer: React.FC = ({ txState, onMaxChange, setBorrowAmount, + loadingLimit, }) => { const [currentButton, setCurrentButton] = useState(leftButton); return ( @@ -74,7 +76,8 @@ const TabContainer: React.FC = ({ onMaxChange={() => onMaxChange(currentButton)} currency={currency} amountName={amountName} - maxValue={maxValue} + maxValue={currentButton === 'Deposit' ? maxValue : '0'} + loadingLimit={loadingLimit} /> ({ key: item.source, - label: item.tokenLabel, + label: `${item.tokenLabel} - Pool token`, })); const [sourceToken, setSourceToken] = useState(tokens[0].key); diff --git a/src/app/containers/MarginTradeForm/index.tsx b/src/app/containers/MarginTradeForm/index.tsx index 84f66531b..064805c7d 100644 --- a/src/app/containers/MarginTradeForm/index.tsx +++ b/src/app/containers/MarginTradeForm/index.tsx @@ -25,9 +25,11 @@ import { TradeButton } from '../../components/TradeButton'; import { SendTxProgress } from '../../components/SendTxProgress'; import { useApproveAndTrade } from '../../hooks/trading/useApproveAndTrade'; import { useIsAmountWithinLimits } from '../../hooks/useIsAmountWithinLimits'; -import { weiTo18 } from '../../../utils/blockchain/math-helpers'; +import { weiTo18, weiTo4 } from '../../../utils/blockchain/math-helpers'; import { useAssetBalanceOf } from '../../hooks/useAssetBalanceOf'; import { AssetsDictionary } from '../../../utils/blockchain/assets-dictionary'; +import { useLending_transactionLimit } from '../../hooks/lending/useLending_transactionLimit'; +import { min } from 'mathjs'; const s = translations.marginTradeForm; @@ -71,11 +73,19 @@ export function MarginTradeForm(props: Props) { ); const { value: tokenBalance } = useAssetBalanceOf(collateral); - // const { value: maxAmount } = useLending_transactionLimit( - // pair.getAssetForPosition(position), - // collateral, - // ); - const valid = useIsAmountWithinLimits(weiAmount, '1', tokenBalance); + const { + value: maxAmount, + loading: loadingLimit, + } = useLending_transactionLimit( + pair.getAssetForPosition(position), + collateral, + ); + + const valid = useIsAmountWithinLimits( + weiAmount, + '1', + maxAmount !== '0' ? min(tokenBalance, maxAmount) : tokenBalance, + ); return ( <> @@ -122,7 +132,19 @@ export function MarginTradeForm(props: Props) {
- + + {t(s.fields.amount)}{' '} + {maxAmount !== '0' && !loadingLimit && ( + + (Max: {weiTo4(maxAmount)} {collateral}) + + )} + + } + labelColor={color} + > setAmount(value)} onMaxClicked={() => setAmount(weiTo18(tokenBalance))} diff --git a/src/app/containers/SwapTradeForm/index.tsx b/src/app/containers/SwapTradeForm/index.tsx index 1897be08f..61e1e71b3 100644 --- a/src/app/containers/SwapTradeForm/index.tsx +++ b/src/app/containers/SwapTradeForm/index.tsx @@ -168,7 +168,11 @@ export function SwapTradeForm(props: Props) { text={t(s.buttons.submit)} onClick={() => send()} disabled={ - !isConnected || tx.loading || amount <= '0' || rateByPath <= '0' + !isConnected || + tx.loading || + amount <= '0' || + rateByPath <= '0' || + targetToken === sourceToken } loading={tx.loading} textColor={color} diff --git a/src/store/global/events-store/slice.ts b/src/store/global/events-store/slice.ts index 67d26cd18..9e2b849ab 100644 --- a/src/store/global/events-store/slice.ts +++ b/src/store/global/events-store/slice.ts @@ -44,9 +44,6 @@ const slice = createSlice({ >, ) { prepareEventDataState(state, payload); - - console.log('got events', payload); - const proxy = state[payload.address][payload.contractName][payload.eventName]; From 48ff95d6801eddd9deccbbf10ddf7af34dbffb28 Mon Sep 17 00:00:00 2001 From: Victor Creed Date: Fri, 20 Nov 2020 17:29:12 +0200 Subject: [PATCH 2/2] allow non-whitelisted users to withdraw their liquidity and repay loans --- src/app/containers/LiquidityRemoveContainer/index.tsx | 4 ++-- .../containers/RepayPositionHandler/RepayPositionForm.tsx | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/containers/LiquidityRemoveContainer/index.tsx b/src/app/containers/LiquidityRemoveContainer/index.tsx index a0b086907..787077caf 100644 --- a/src/app/containers/LiquidityRemoveContainer/index.tsx +++ b/src/app/containers/LiquidityRemoveContainer/index.tsx @@ -24,12 +24,12 @@ import { FieldGroup } from '../../components/FieldGroup'; import { AmountField } from '../AmountField'; import { TradeButton } from '../../components/TradeButton'; import { useApproveAndRemoveLiquidity } from '../../hooks/amm/useApproveAndRemoveLiquidity'; -import { useCanInteract } from 'app/hooks/useCanInteract'; +import { useIsConnected } from '../../hooks/useAccount'; interface Props {} export function LiquidityRemoveContainer(props: Props) { - const isConnected = useCanInteract(); + const isConnected = useIsConnected(); const tokens = liquidityPools.map(item => ({ key: item.source, label: `${item.tokenLabel} - Pool token`, diff --git a/src/app/containers/RepayPositionHandler/RepayPositionForm.tsx b/src/app/containers/RepayPositionHandler/RepayPositionForm.tsx index 2ff78e285..cd20ec78b 100644 --- a/src/app/containers/RepayPositionHandler/RepayPositionForm.tsx +++ b/src/app/containers/RepayPositionHandler/RepayPositionForm.tsx @@ -6,7 +6,7 @@ import React, { useCallback, useState } from 'react'; import { min, bignumber } from 'mathjs'; -import { useAccount } from '../../hooks/useAccount'; +import { useAccount, useIsConnected } from '../../hooks/useAccount'; import { useIsAmountWithinLimits } from '../../hooks/useIsAmountWithinLimits'; import { ActiveLoan } from '../../hooks/trading/useGetLoan'; import { useWeiAmount } from '../../hooks/useWeiAmount'; @@ -20,14 +20,13 @@ import { useAssetBalanceOf } from '../../hooks/useAssetBalanceOf'; import { weiTo18, weiTo4 } from '../../../utils/blockchain/math-helpers'; import { DummyField } from '../../components/DummyField'; import { useApproveAndCloseWithDeposit } from '../../hooks/trading/useApproveAndCloseWithDeposit'; -import { useCanInteract } from '../../hooks/useCanInteract'; interface Props { loan: ActiveLoan; } export function RepayPositionForm({ loan }: Props) { - const canInteract = useCanInteract(); + const canInteract = useIsConnected(); const { asset } = AssetsDictionary.getByTokenContractAddress(loan.loanToken); const { value: balance } = useAssetBalanceOf(asset);