Browse Source

Merge #625

625: Disable go-long button if r=bonomat a=bonomat

- balance < required margin
- entered quantity > max_quantity or < min_quantity
- entered quantity <= 0

resolves #525
resolves  #522

Only a cosmetic change. Should we duplicate this logic into the controller? Let me know and I'll add it and convert this PR to `ready for review`

Co-authored-by: bonomat <philipp@hoenisch.at>
reconnect-to-maker
bors[bot] 3 years ago
committed by GitHub
parent
commit
996b09187d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      taker-frontend/src/App.tsx
  2. 66
      taker-frontend/src/components/Trade.tsx

7
taker-frontend/src/App.tsx

@ -141,10 +141,10 @@ export const App = () => {
<Route path="/"> <Route path="/">
<VStack divider={<StackDivider borderColor="gray.500" />} spacing={4}> <VStack divider={<StackDivider borderColor="gray.500" />} spacing={4}>
<Trade <Trade
order_id={order?.id} orderId={order?.id}
quantity={format(effectiveQuantity)} quantity={format(effectiveQuantity)}
max_quantity={max_quantity || 0} maxQuantity={max_quantity || 0}
min_quantity={min_quantity || 0} minQuantity={min_quantity || 0}
referencePrice={referencePrice} referencePrice={referencePrice}
askPrice={askPrice} askPrice={askPrice}
margin={margin} margin={margin}
@ -164,7 +164,6 @@ export const App = () => {
}; };
calculateMargin(payload); calculateMargin(payload);
}} }}
canSubmit={order != null && !isCreatingNewOrderRequest}
onLongSubmit={makeNewOrderRequest} onLongSubmit={makeNewOrderRequest}
isSubmitting={isCreatingNewOrderRequest} isSubmitting={isCreatingNewOrderRequest}
/> />

66
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,
@ -47,25 +51,38 @@ import { CfdOrderRequestPayload } from "./Types";
const MotionBox = motion<BoxProps>(Box); const MotionBox = motion<BoxProps>(Box);
interface TradeProps { interface TradeProps {
order_id?: string; orderId?: string;
min_quantity: number; minQuantity: number;
max_quantity: number; maxQuantity: number;
referencePrice?: number; referencePrice?: number;
askPrice?: number; askPrice?: number;
margin?: string; margin?: string;
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 = (
{ {
min_quantity, minQuantity,
max_quantity, maxQuantity,
referencePrice: referencePriceAsNumber, referencePrice: referencePriceAsNumber,
askPrice: askPriceAsNumber, askPrice: askPriceAsNumber,
quantity, quantity,
@ -73,9 +90,9 @@ const Trade = (
margin: marginAsNumber, margin: marginAsNumber,
leverage, leverage,
liquidationPrice: liquidationPriceAsNumber, liquidationPrice: liquidationPriceAsNumber,
canSubmit,
onLongSubmit, onLongSubmit,
order_id, orderId,
walletBalance,
}: TradeProps, }: TradeProps,
) => { ) => {
let outerCircleBg = useColorModeValue("gray.100", "gray.700"); let outerCircleBg = useColorModeValue("gray.100", "gray.700");
@ -93,7 +110,7 @@ const Trade = (
const quantityAsNumber = quantity.replace("$", ""); const quantityAsNumber = quantity.replace("$", "");
let payload: CfdOrderRequestPayload = { let payload: CfdOrderRequestPayload = {
order_id: order_id!, order_id: orderId!,
quantity: Number.parseFloat(quantityAsNumber), quantity: Number.parseFloat(quantityAsNumber),
}; };
await onLongSubmit(payload); await onLongSubmit(payload);
@ -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
@ -138,7 +183,7 @@ const Trade = (
</Center> </Center>
</GridItem> </GridItem>
<GridItem colSpan={1}> <GridItem colSpan={1}>
<Quantity min={min_quantity} max={max_quantity} quantity={quantity} onChange={onQuantityChange} /> <Quantity min={minQuantity} max={maxQuantity} quantity={quantity} onChange={onQuantityChange} />
</GridItem> </GridItem>
<GridItem colSpan={1}> <GridItem colSpan={1}>
<Leverage leverage={leverage} /> <Leverage leverage={leverage} />
@ -206,6 +251,7 @@ const Trade = (
</Modal> </Modal>
</ButtonGroup> </ButtonGroup>
</Center> </Center>
{alertBox}
</GridItem> </GridItem>
</Grid> </Grid>
</Center> </Center>

Loading…
Cancel
Save