Browse Source

Add warnings if input quantity is not valid

reconnect-to-maker
bonomat 3 years ago
parent
commit
e2d174f262
No known key found for this signature in database GPG Key ID: E5F8E74C672BC666
  1. 9
      taker-frontend/src/App.tsx
  2. 50
      taker-frontend/src/components/Trade.tsx

9
taker-frontend/src/App.tsx

@ -130,14 +130,6 @@ export const App = () => {
const format = (val: any) => `$` + val; const format = (val: any) => `$` + val;
const parse = (val: any) => val.replace(/^\$/, ""); const parse = (val: any) => val.replace(/^\$/, "");
const balanceTooLow = walletInfo && walletInfo.balance < parse(margin);
const quantityTooHigh = order && order.max_quantity < parse(effectiveQuantity);
const quantityTooLow = order && order.min_quantity > parse(effectiveQuantity);
const quantityGreaterZero = parse(effectiveQuantity) > 0;
const canSubmit = order != null && !isCreatingNewOrderRequest && walletInfo != null && !balanceTooLow
&& !quantityTooHigh && !quantityTooLow && quantityGreaterZero;
return ( return (
<> <>
<Nav walletInfo={walletInfo} /> <Nav walletInfo={walletInfo} />
@ -172,7 +164,6 @@ export const App = () => {
}; };
calculateMargin(payload); calculateMargin(payload);
}} }}
canSubmit={canSubmit}
onLongSubmit={makeNewOrderRequest} onLongSubmit={makeNewOrderRequest}
isSubmitting={isCreatingNewOrderRequest} isSubmitting={isCreatingNewOrderRequest}
/> />

50
taker-frontend/src/components/Trade.tsx

@ -1,5 +1,9 @@
import { BoxProps } from "@chakra-ui/layout"; import { BoxProps } from "@chakra-ui/layout";
import { import {
Alert,
AlertDescription,
AlertIcon,
AlertTitle,
Box, Box,
Button, Button,
ButtonGroup, ButtonGroup,
@ -56,12 +60,25 @@ interface TradeProps {
leverage?: number; leverage?: number;
quantity: string; quantity: string;
liquidationPrice?: number; liquidationPrice?: number;
canSubmit: boolean;
isSubmitting: boolean; isSubmitting: boolean;
onQuantityChange: any; onQuantityChange: any;
walletBalance?: number;
onLongSubmit: (payload: CfdOrderRequestPayload) => void; onLongSubmit: (payload: CfdOrderRequestPayload) => void;
} }
interface AlertBoxProps {
title: string;
description: string;
}
function AlertBox({ title, description }: AlertBoxProps) {
return (<Alert status="error">
<AlertIcon />
<AlertTitle mr={2}>{title}</AlertTitle>
<AlertDescription>{description}</AlertDescription>
</Alert>);
}
const Trade = ( const Trade = (
{ {
minQuantity, minQuantity,
@ -73,9 +90,9 @@ const Trade = (
margin: marginAsNumber, margin: marginAsNumber,
leverage, leverage,
liquidationPrice: liquidationPriceAsNumber, liquidationPrice: liquidationPriceAsNumber,
canSubmit,
onLongSubmit, onLongSubmit,
orderId, orderId,
walletBalance,
}: TradeProps, }: TradeProps,
) => { ) => {
let outerCircleBg = useColorModeValue("gray.100", "gray.700"); let outerCircleBg = useColorModeValue("gray.100", "gray.700");
@ -101,6 +118,34 @@ const Trade = (
}, },
}); });
const parse = (val: any) => Number.parseInt(val.replace(/^\$/, ""));
const balanceTooLow = walletBalance && walletBalance < parse(margin);
const quantityTooHigh = maxQuantity < parse(quantity);
const quantityTooLow = minQuantity > parse(quantity);
const quantityGreaterZero = parse(quantity) > 0;
const canSubmit = orderId && !isSubmitting && !balanceTooLow
&& !quantityTooHigh && !quantityTooLow && quantityGreaterZero;
let alertBox;
if (balanceTooLow) {
alertBox = <AlertBox title={"Your balance is too low!"} description={"Pleas deposit more into you wallet."} />;
}
if (quantityTooHigh) {
alertBox = <AlertBox title={"Quantity too high!"} description={`Max available liquidity is ${maxQuantity}`} />;
}
if (quantityTooLow || !quantityGreaterZero) {
alertBox = <AlertBox title={"Quantity too low!"} description={`Min quantity is ${minQuantity}`} />;
}
if (!orderId) {
alertBox = <AlertBox
title={"No liquidity!"}
description={"The maker you are connected has not create any offers"}
/>;
}
return ( return (
<Center> <Center>
<Grid <Grid
@ -206,6 +251,7 @@ const Trade = (
</Modal> </Modal>
</ButtonGroup> </ButtonGroup>
</Center> </Center>
{alertBox}
</GridItem> </GridItem>
</Grid> </Grid>
</Center> </Center>

Loading…
Cancel
Save