Skip to content

Commit

Permalink
Merge pull request #61 from DistributedCollective/fit/tx-limits
Browse files Browse the repository at this point in the history
reintroduced limits to trade, lend and borrow
  • Loading branch information
creed-victor authored Nov 20, 2020
2 parents ace08d6 + 48ff95d commit 4dfec2f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 36 deletions.
30 changes: 25 additions & 5 deletions src/app/containers/LendBorrowSovryn/BorrowingContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { actions } from '../slice';
import { useCanInteract } from '../../../hooks/useCanInteract';
import { useLending_transactionLimit } from '../../../hooks/lending/useLending_transactionLimit';

type Props = {
currency: Asset;
Expand Down Expand Up @@ -79,12 +80,20 @@ const BorrowingContainer: React.FC<Props> = ({ 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));
Expand Down Expand Up @@ -114,9 +123,20 @@ const BorrowingContainer: React.FC<Props> = ({ currency }) => {
</FieldGroup>
</div>
<div className="col-8">
<FieldGroup label="Collateral amount">
<FieldGroup
label={
<>
Collateral amount{' '}
{maxAmount !== '0' && !loadingLimit && (
<span className="text-muted">
(Max: {weiTo4(maxAmount)} {tokenToCollarate})
</span>
)}
</>
}
>
<DummyField>
{weiTo4(collateralTokenSent)} {tokenToCollarate}
{weiToFixed(collateralTokenSent, 6)} {tokenToCollarate}
</DummyField>
</FieldGroup>
</div>
Expand Down
10 changes: 7 additions & 3 deletions src/app/containers/LendBorrowSovryn/LendingContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,11 +37,15 @@ const LendingContainer: React.FC<Props> = ({ 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;
}
Expand Down Expand Up @@ -78,8 +83,6 @@ const LendingContainer: React.FC<Props> = ({ currency }) => {
}
}, [unlendTx.loading, unlend]);

const { value: maxAmount } = useLending_transactionLimit(currency, currency);

const valid = useIsAmountWithinLimits(
weiAmount,
'1',
Expand Down Expand Up @@ -115,6 +118,7 @@ const LendingContainer: React.FC<Props> = ({ currency }) => {
currency={currency}
amountName="Deposit Amount"
maxValue={maxAmount}
loadingLimit={loadingLimit}
/>
);
};
Expand Down
24 changes: 14 additions & 10 deletions src/app/containers/LendBorrowSovryn/components/Amount/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Props = {
currency: string;
minValue?: number | string;
maxValue?: number | string;
loadingLimit?: boolean;
};

const Amount: React.FC<Props> = ({
Expand All @@ -23,27 +24,30 @@ const Amount: React.FC<Props> = ({
onChangeAmount,
amountValue,
onMaxChange,
loadingLimit,
}) => {
return (
<div className="d-flex flex-row justify-content-between mb-3">
<div className="d-flex flex-grow-1 flex-column">
<FieldGroup label={amountName}>
<FieldGroup
label={
<>
{amountName}{' '}
{maxValue !== '0' && !loadingLimit && (
<span className="text-muted">
(Max: {weiTo4(maxValue)} {currency})
</span>
)}
</>
}
>
<AmountField
onChange={onChangeAmount}
value={amountValue}
onMaxClicked={() => onMaxChange()}
/>
</FieldGroup>
</div>
{maxValue !== '0' && maxValue !== '' && (
<div className="d-flex flex-column min-max-btc p-3 align-items-center justify-content-center">
<div>Max:</div>
<div>
<span className="text-muted">{currency}</span>{' '}
<strong>{weiTo4(maxValue)}</strong>
</div>
</div>
)}
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Props = {
currency: Asset;
amountName: string;
maxValue: string;
loadingLimit: boolean;
minValue?: string;
amountValue: string;
leftButton: string;
Expand Down Expand Up @@ -57,6 +58,7 @@ const TabContainer: React.FC<Props> = ({
txState,
onMaxChange,
setBorrowAmount,
loadingLimit,
}) => {
const [currentButton, setCurrentButton] = useState(leftButton);
return (
Expand All @@ -74,7 +76,8 @@ const TabContainer: React.FC<Props> = ({
onMaxChange={() => onMaxChange(currentButton)}
currency={currency}
amountName={amountName}
maxValue={maxValue}
maxValue={currentButton === 'Deposit' ? maxValue : '0'}
loadingLimit={loadingLimit}
/>
<AccountBalance
title={currentButton}
Expand Down
6 changes: 3 additions & 3 deletions src/app/containers/LiquidityRemoveContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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,
label: `${item.tokenLabel} - Pool token`,
}));

const [sourceToken, setSourceToken] = useState(tokens[0].key);
Expand Down
36 changes: 29 additions & 7 deletions src/app/containers/MarginTradeForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ 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 { useCanInteract } from 'app/hooks/useCanInteract';
import { useLending_transactionLimit } from '../../hooks/lending/useLending_transactionLimit';
import { min } from 'mathjs';

const s = translations.marginTradeForm;

Expand Down Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -122,7 +132,19 @@ export function MarginTradeForm(props: Props) {
</FieldGroup>
</div>
<div className="col-6 pl-1">
<FieldGroup label={t(s.fields.amount)} labelColor={color}>
<FieldGroup
label={
<>
{t(s.fields.amount)}{' '}
{maxAmount !== '0' && !loadingLimit && (
<span className="text-muted">
(Max: {weiTo4(maxAmount)} {collateral})
</span>
)}
</>
}
labelColor={color}
>
<AmountField
onChange={value => setAmount(value)}
onMaxClicked={() => setAmount(weiTo18(tokenBalance))}
Expand Down
5 changes: 2 additions & 3 deletions src/app/containers/RepayPositionHandler/RepayPositionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/app/containers/SwapTradeForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 0 additions & 3 deletions src/store/global/events-store/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ const slice = createSlice({
>,
) {
prepareEventDataState(state, payload);

console.log('got events', payload);

const proxy =
state[payload.address][payload.contractName][payload.eventName];

Expand Down

0 comments on commit 4dfec2f

Please sign in to comment.