From 3670c7c5d46c6701debd815c2b4038943d705b21 Mon Sep 17 00:00:00 2001 From: David Uhlmann Date: Sun, 1 Oct 2023 09:33:37 +1000 Subject: [PATCH] WIP --- src/components/cards/LimitOrderCard.tsx | 53 +++++++---- src/components/cards/LimitOrderContext.tsx | 106 +++++++++++++++++++++ src/pages/Orderbook/OrderbookChart.tsx | 2 +- src/pages/Orderbook/OrderbookFooter.tsx | 13 +-- 4 files changed, 140 insertions(+), 34 deletions(-) create mode 100644 src/components/cards/LimitOrderContext.tsx diff --git a/src/components/cards/LimitOrderCard.tsx b/src/components/cards/LimitOrderCard.tsx index 1eadfdc07..27faf0db3 100644 --- a/src/components/cards/LimitOrderCard.tsx +++ b/src/components/cards/LimitOrderCard.tsx @@ -1,6 +1,14 @@ import Long from 'long'; import BigNumber from 'bignumber.js'; -import { useCallback, useMemo, useRef, useState } from 'react'; +import { + createContext, + useCallback, + useContext, + useMemo, + useRef, + useState, +} from 'react'; +import { LimitOrderType } from '@duality-labs/dualityjs/types/codegen/dualitylabs/duality/dex/tx'; import TabsCard from './TabsCard'; import Tabs from '../Tabs'; @@ -28,6 +36,7 @@ import { useOrderedTokenPair } from '../../lib/web3/hooks/useTokenPairs'; import { useTokenPairTickLiquidity } from '../../lib/web3/hooks/useTickLiquidity'; import { useBankBalanceDisplayAmount } from '../../lib/web3/hooks/useUserBankBalances'; import RangeListSliderInput from '../inputs/RangeInput/RangeListSliderInput'; +import { LimitOrderContextProvider } from './LimitOrderContext'; export default function LimitOrderCard({ tokenA, @@ -37,29 +46,31 @@ export default function LimitOrderCard({ tokenB?: Token; }) { return ( - { - return [ - { - nav: 'Buy', - Tab: () => , - }, - { - nav: 'Sell', - Tab: () => , - }, - ]; - }, [tokenA, tokenB])} - /> + + { + return [ + { + nav: 'Buy', + Tab: () => , + }, + { + nav: 'Sell', + Tab: () => , + }, + ]; + }, [tokenA, tokenB])} + /> + ); } -function LimitOrderType({ +function LimitOrderNav({ tokenA, tokenB, sell = false, diff --git a/src/components/cards/LimitOrderContext.tsx b/src/components/cards/LimitOrderContext.tsx new file mode 100644 index 000000000..8900a0d52 --- /dev/null +++ b/src/components/cards/LimitOrderContext.tsx @@ -0,0 +1,106 @@ +import { + Dispatch, + ReactNode, + SetStateAction, + createContext, + useMemo, + useState, +} from 'react'; +import { LimitOrderType } from '@duality-labs/dualityjs/types/codegen/dualitylabs/duality/dex/tx'; + +export const orderTypeTextMap: { + [key in keyof typeof LimitOrderType]: string; +} = { + IMMEDIATE_OR_CANCEL: 'Market', + FILL_OR_KILL: 'Fill-Kill', + GOOD_TIL_CANCELLED: 'Limit', + GOOD_TIL_TIME: 'Stop Order', + JUST_IN_TIME: 'JIT', + UNRECOGNIZED: 'Unknown', +}; + +export interface FormState { + amount: string; + limitPrice: string; + triggerPrice: string; + timeAmount: string; + timePeriod: 'seconds' | 'minutes' | 'hours' | 'days' | undefined; + execution: keyof typeof LimitOrderType | undefined; + slippage: string; +} +interface FormSetState { + setAmount: Dispatch>; + setLimitPrice: Dispatch>; + setTriggerPrice: Dispatch>; + setTimeAmount: Dispatch>; + setTimePeriod: Dispatch>; + setExecution: Dispatch>; + setSlippage: Dispatch>; +} + +export const LimitOrderFormContext = createContext>({}); +export const LimitOrderFormSetContext = createContext>( + {} +); + +export function LimitOrderContextProvider({ + children, +}: { + children: ReactNode; +}) { + const [amount, setAmount] = useState(''); + const [limitPrice, setLimitPrice] = useState(''); + const [triggerPrice, setTriggerPrice] = useState(''); + const [timeAmount, setTimeAmount] = useState(''); + const [timePeriod, setTimePeriod] = useState(); + const [execution, setExecution] = useState(); + const [slippage, setSlippage] = useState(''); + + const state = useMemo(() => { + return { + amount, + limitPrice, + triggerPrice, + timeAmount, + timePeriod, + execution, + slippage, + }; + }, [ + amount, + limitPrice, + triggerPrice, + timeAmount, + timePeriod, + execution, + slippage, + ]); + + const setState = useMemo(() => { + return { + setAmount, + setLimitPrice, + setTriggerPrice, + setTimeAmount, + setTimePeriod, + setExecution, + setSlippage, + }; + }, [ + setAmount, + setLimitPrice, + setTriggerPrice, + setTimeAmount, + setTimePeriod, + setExecution, + setSlippage, + ]); + + return ( + + + {children} + + + ); +} diff --git a/src/pages/Orderbook/OrderbookChart.tsx b/src/pages/Orderbook/OrderbookChart.tsx index cfbf365ce..412d0d24d 100644 --- a/src/pages/Orderbook/OrderbookChart.tsx +++ b/src/pages/Orderbook/OrderbookChart.tsx @@ -77,7 +77,7 @@ export default function OrderBookChart({ liquidity.push(...data); // fetch again if necessary next = pagination['next_key']; - } while (next); + } while (false); timeout = setTimeout(poller.poll, 25000); setChartSeries([ diff --git a/src/pages/Orderbook/OrderbookFooter.tsx b/src/pages/Orderbook/OrderbookFooter.tsx index 34e542e05..e4f96e189 100644 --- a/src/pages/Orderbook/OrderbookFooter.tsx +++ b/src/pages/Orderbook/OrderbookFooter.tsx @@ -1,6 +1,6 @@ import BigNumber from 'bignumber.js'; -import { LimitOrderType } from '@duality-labs/dualityjs/types/codegen/dualitylabs/duality/dex/tx'; +import { orderTypeTextMap } from '../../components/cards/LimitOrderContext'; import Table from '../../components/Table/Table'; import { formatDateTime } from '../../lib/utils/time'; import { useWeb3 } from '../../lib/web3/useWeb3'; @@ -129,17 +129,6 @@ function findPlaceLimitOrderActionEvent({ ); } -const orderTypeTextMap: { - [key in keyof typeof LimitOrderType]: string; -} = { - IMMEDIATE_OR_CANCEL: 'Market', - FILL_OR_KILL: 'Fill-Kill', - GOOD_TIL_CANCELLED: 'Limit', - GOOD_TIL_TIME: 'Stop Order', - JUST_IN_TIME: 'JIT', - UNRECOGNIZED: 'Unknown', -}; - function TypeColumn({ row: tx }: { row: Tx }) { const event = tx.tx_result.events.find(findPlaceLimitOrderActionEvent); if (event) {