Browse Source

Fmt and removed unused file

fix-bad-api-calls
Philipp Hoenisch 4 years ago
parent
commit
dff0f83a5c
No known key found for this signature in database GPG Key ID: E5F8E74C672BC666
  1. 1
      frontend/src/Maker.tsx
  2. 33
      frontend/src/Taker.tsx
  3. 138
      frontend/src/components/CfdTile.tsx

1
frontend/src/Maker.tsx

@ -127,7 +127,6 @@ export default function App() {
>
{order ? "Update Sell Order" : "Create Sell Order"}
</Button>
</VStack>
</VStack>
</GridItem>

33
frontend/src/Taker.tsx

@ -1,7 +1,9 @@
import {
Button,
Container,
Flex, Grid, GridItem,
Flex,
Grid,
GridItem,
HStack,
Tab,
TabList,
@ -12,13 +14,13 @@ import {
useToast,
VStack,
} from "@chakra-ui/react";
import React, {useState} from "react";
import {useAsync} from "react-async";
import {useEventSource} from "react-sse-hooks";
import {CfdTable} from "./components/cfdtables/CfdTable";
import React, { useState } from "react";
import { useAsync } from "react-async";
import { useEventSource } from "react-sse-hooks";
import { CfdTable } from "./components/cfdtables/CfdTable";
import CurrencyInputField from "./components/CurrencyInputField";
import useLatestEvent from "./components/Hooks";
import {Cfd, Order, WalletInfo} from "./components/Types";
import { Cfd, Order, WalletInfo } from "./components/Types";
import Wallet from "./components/Wallet";
interface CfdOrderRequestPayload {
@ -37,7 +39,7 @@ interface MarginResponse {
}
async function postCfdOrderRequest(payload: CfdOrderRequestPayload) {
let res = await fetch(`/api/cfd`, {method: "POST", body: JSON.stringify(payload)});
let res = await fetch(`/api/cfd`, { method: "POST", body: JSON.stringify(payload) });
if (!res.status.toString().startsWith("2")) {
throw new Error("failed to create new CFD order request: " + res.status + ", " + res.statusText);
@ -45,7 +47,7 @@ async function postCfdOrderRequest(payload: CfdOrderRequestPayload) {
}
async function getMargin(payload: MarginRequestPayload): Promise<MarginResponse> {
let res = await fetch(`/api/calculate/margin`, {method: "POST", body: JSON.stringify(payload)});
let res = await fetch(`/api/calculate/margin`, { method: "POST", body: JSON.stringify(payload) });
if (!res.status.toString().startsWith("2")) {
throw new Error("failed to create new CFD order request: " + res.status + ", " + res.statusText);
@ -55,7 +57,7 @@ async function getMargin(payload: MarginRequestPayload): Promise<MarginResponse>
}
export default function App() {
let source = useEventSource({source: "/api/feed"});
let source = useEventSource({ source: "/api/feed" });
const cfdsOrUndefined = useLatestEvent<Cfd[]>(source, "cfds");
let cfds = cfdsOrUndefined ? cfdsOrUndefined! : [];
@ -66,7 +68,7 @@ export default function App() {
let [quantity, setQuantity] = useState("0");
let [margin, setMargin] = useState("0");
let {run: calculateMargin} = useAsync({
let { run: calculateMargin } = useAsync({
deferFn: async ([payload]: any[]) => {
try {
let res = await getMargin(payload as MarginRequestPayload);
@ -88,7 +90,7 @@ export default function App() {
const format = (val: any) => `$` + val;
const parse = (val: any) => val.replace(/^\$/, "");
let {run: makeNewOrderRequest, isLoading: isCreatingNewOrderRequest} = useAsync({
let { run: makeNewOrderRequest, isLoading: isCreatingNewOrderRequest } = useAsync({
deferFn: async ([payload]: any[]) => {
try {
await postCfdOrderRequest(payload as CfdOrderRequestPayload);
@ -120,7 +122,7 @@ export default function App() {
<VStack>
<Grid templateColumns="repeat(6, 1fr)" gap={4}>
<GridItem colStart={1} colSpan={2}>
<Wallet walletInfo={walletInfo}/>
<Wallet walletInfo={walletInfo} />
<VStack shadow={"md"} padding={5} align="stretch" spacing={4}>
<HStack>
{/*TODO: Do we need this? does it make sense to only display the price from the order?*/}
@ -176,7 +178,6 @@ export default function App() {
BUY
</Button>}
</VStack>
</GridItem>
</Grid>
<Tabs>
@ -188,13 +189,13 @@ export default function App() {
<TabPanels>
<TabPanel>
<CfdTable data={running}/>
<CfdTable data={running} />
</TabPanel>
<TabPanel>
<CfdTable data={closed}/>
<CfdTable data={closed} />
</TabPanel>
<TabPanel>
<CfdTable data={unsorted}/>
<CfdTable data={unsorted} />
</TabPanel>
</TabPanels>
</Tabs>

138
frontend/src/components/CfdTile.tsx

@ -1,138 +0,0 @@
import { Box, Button, HStack, SimpleGrid, Text, useToast, VStack } from "@chakra-ui/react";
import React from "react";
import { useAsync } from "react-async";
import { postAcceptOrder, postRejectOrder } from "../MakerClient";
import { Cfd, unixTimestampToDate } from "./Types";
interface CfdTileProps {
index: number;
cfd: Cfd;
}
export default function CfdTile(
{
index,
cfd,
}: CfdTileProps,
) {
const toast = useToast();
let { run: acceptOrder, isLoading: isAccepting } = useAsync({
deferFn: async ([args]: any[]) => {
try {
let payload = {
order_id: args.order_id,
};
await postAcceptOrder(payload);
} catch (e) {
const description = typeof e === "string" ? e : JSON.stringify(e);
toast({
title: "Error",
description,
status: "error",
duration: 9000,
isClosable: true,
});
}
},
});
let { run: rejectOrder, isLoading: isRejecting } = useAsync({
deferFn: async ([args]: any[]) => {
try {
let payload = {
order_id: args.order_id,
};
await postRejectOrder(payload);
} catch (e) {
const description = typeof e === "string" ? e : JSON.stringify(e);
toast({
title: "Error",
description,
status: "error",
duration: 9000,
isClosable: true,
});
}
},
});
let actionButtons;
if (cfd.state === "Open") {
actionButtons = <Box paddingBottom={5}>
<Button colorScheme="blue" variant="solid">
Close
</Button>
</Box>;
} else if (cfd.state == "Requested") {
actionButtons = (
<HStack>
<Box paddingBottom={5}>
<Button
colorScheme="blue"
variant="solid"
onClick={async () => acceptOrder(cfd)}
isLoading={isAccepting}
>
Accept
</Button>
</Box>
<Box paddingBottom={5}>
<Button
colorScheme="blue"
variant="solid"
onClick={async () => rejectOrder(cfd)}
isLoading={isRejecting}
>
Reject
</Button>
</Box>
</HStack>
);
}
return (
<Box borderRadius={"md"} borderColor={"blue.800"} borderWidth={2} bg={"gray.50"}>
<VStack>
<Box bg="blue.800" w="100%">
<Text padding={2} color={"white"} fontWeight={"bold"}>CFD #{index}</Text>
</Box>
<SimpleGrid padding={5} columns={2} spacing={5}>
<Text>Trading Pair</Text>
<Text>{cfd.trading_pair}</Text>
<Text>Position</Text>
<Text>{cfd.position}</Text>
<Text>CFD Price</Text>
<Text>{cfd.initial_price}</Text>
<Text>Leverage</Text>
<Text>{cfd.leverage}</Text>
<Text>Quantity</Text>
<Text>{cfd.quantity_usd}</Text>
<Text>Margin</Text>
<Text>{cfd.margin}</Text>
<Text>Liquidation Price</Text>
<Text
overflow="hidden"
textOverflow="ellipsis"
whiteSpace="nowrap"
_hover={{ overflow: "visible" }}
>
{cfd.liquidation_price}
</Text>
<Text>Profit</Text>
<Text>{cfd.profit_usd}</Text>
<Text>Open since</Text>
{/* TODO: Format date in a more compact way */}
<Text>
{unixTimestampToDate(cfd.state_transition_timestamp).toString()}
</Text>
<Text>Status</Text>
<Text>{cfd.state}</Text>
</SimpleGrid>
{actionButtons}
</VStack>
</Box>
);
}
Loading…
Cancel
Save