mango-ui-v3/components/TradeForm.tsx

357 lines
10 KiB
TypeScript
Raw Normal View History

2021-04-02 11:26:21 -07:00
import { useState, useEffect, useRef } from 'react'
2021-04-18 03:34:37 -07:00
import styled from '@emotion/styled'
2021-04-06 15:11:42 -07:00
import useMarket from '../hooks/useMarket'
2021-04-02 11:26:21 -07:00
import useIpAddress from '../hooks/useIpAddress'
import useConnection from '../hooks/useConnection'
import { PublicKey } from '@solana/web3.js'
import { IDS } from '@blockworks-foundation/mango-client'
import { notify } from '../utils/notifications'
import { placeAndSettle } from '../utils/mango'
import { calculateMarketPrice, getDecimalCount } from '../utils'
import FloatingElement from './FloatingElement'
import { roundToDecimal } from '../utils/index'
import useMangoStore from '../stores/useMangoStore'
2021-04-10 12:21:02 -07:00
import Button from './Button'
import TradeType from './TradeType'
2021-04-13 16:41:04 -07:00
import Input from './Input'
import Switch from './Switch'
2021-04-02 11:26:21 -07:00
2021-04-18 03:34:37 -07:00
const StyledRightInput = styled(Input)`
border-left: 1px solid transparent;
`
2021-04-12 20:39:08 -07:00
export default function TradeForm() {
2021-04-10 14:12:15 -07:00
const { baseCurrency, quoteCurrency, market, marketAddress } = useMarket()
const set = useMangoStore((s) => s.set)
2021-04-13 22:23:50 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
2021-04-12 20:39:08 -07:00
const actions = useMangoStore((s) => s.actions)
2021-04-02 11:26:21 -07:00
const { connection, cluster } = useConnection()
const { side, baseSize, quoteSize, price, tradeType } = useMangoStore(
(s) => s.tradeForm
)
const { ipAllowed } = useIpAddress()
const [postOnly, setPostOnly] = useState(false)
const [ioc, setIoc] = useState(false)
const [submitting, setSubmitting] = useState(false)
2021-04-02 11:26:21 -07:00
const orderBookRef = useRef(useMangoStore.getState().market.orderBook)
const orderbook = orderBookRef.current[0]
useEffect(
() =>
useMangoStore.subscribe(
(orderBook) => (orderBookRef.current = orderBook as any[]),
(state) => state.market.orderBook
),
[]
)
const setSide = (side) =>
set((s) => {
s.tradeForm.side = side
})
const setBaseSize = (baseSize) =>
set((s) => {
s.tradeForm.baseSize = parseFloat(baseSize)
})
const setQuoteSize = (quoteSize) =>
set((s) => {
s.tradeForm.quoteSize = parseFloat(quoteSize)
})
const setPrice = (price) =>
set((s) => {
s.tradeForm.price = parseFloat(price)
})
const setTradeType = (type) =>
set((s) => {
s.tradeForm.tradeType = type
})
2021-04-02 11:26:21 -07:00
const markPriceRef = useRef(useMangoStore.getState().market.markPrice)
const markPrice = markPriceRef.current
useEffect(
() =>
useMangoStore.subscribe(
(markPrice) => (markPriceRef.current = markPrice as number),
(state) => state.market.markPrice
),
[]
)
const sizeDecimalCount =
market?.minOrderSize && getDecimalCount(market.minOrderSize)
2021-04-12 20:39:08 -07:00
// const priceDecimalCount = market?.tickSize && getDecimalCount(market.tickSize)
2021-04-02 11:26:21 -07:00
2021-04-15 14:36:55 -07:00
const onSetPrice = (price: number | '') => {
setPrice(price)
if (!price) return
if (baseSize) {
onSetBaseSize(baseSize)
}
}
2021-04-02 11:26:21 -07:00
2021-04-12 20:39:08 -07:00
const onSetBaseSize = (baseSize: number | '') => {
2021-04-02 11:26:21 -07:00
setBaseSize(baseSize)
if (!baseSize) {
2021-04-12 20:39:08 -07:00
setQuoteSize('')
2021-04-02 11:26:21 -07:00
return
}
2021-04-12 20:39:08 -07:00
const usePrice = Number(price) || markPrice
2021-04-02 11:26:21 -07:00
if (!usePrice) {
2021-04-12 20:39:08 -07:00
setQuoteSize('')
2021-04-02 11:26:21 -07:00
return
}
const rawQuoteSize = baseSize * usePrice
const quoteSize = baseSize && roundToDecimal(rawQuoteSize, sizeDecimalCount)
setQuoteSize(quoteSize)
}
2021-04-12 20:39:08 -07:00
const onSetQuoteSize = (quoteSize: number | '') => {
2021-04-02 11:26:21 -07:00
setQuoteSize(quoteSize)
if (!quoteSize) {
2021-04-12 20:39:08 -07:00
setBaseSize('')
2021-04-02 11:26:21 -07:00
return
}
2021-04-12 20:39:08 -07:00
2021-04-15 14:36:55 -07:00
if (!Number(price) && tradeType === 'Limit') {
2021-04-12 20:39:08 -07:00
setBaseSize('')
2021-04-02 11:26:21 -07:00
return
}
2021-04-15 14:36:55 -07:00
const usePrice = Number(price) || markPrice
2021-04-02 11:26:21 -07:00
const rawBaseSize = quoteSize / usePrice
const baseSize = quoteSize && roundToDecimal(rawBaseSize, sizeDecimalCount)
setBaseSize(baseSize)
}
const postOnChange = (checked) => {
if (checked) {
setIoc(false)
}
setPostOnly(checked)
}
const iocOnChange = (checked) => {
if (checked) {
setPostOnly(false)
}
setIoc(checked)
}
async function onSubmit() {
if (!price && tradeType === 'Limit') {
console.warn('Missing price')
notify({
message: 'Missing price',
type: 'error',
})
return
} else if (!baseSize) {
console.warn('Missing size')
notify({
message: 'Missing size',
type: 'error',
})
return
}
2021-04-10 14:12:15 -07:00
const marginAccount = useMangoStore.getState().selectedMarginAccount.current
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
if (!mangoGroup || !marketAddress || !marginAccount || !market) return
2021-04-02 11:26:21 -07:00
setSubmitting(true)
try {
let calculatedPrice
if (tradeType === 'Market') {
calculatedPrice =
side === 'buy'
? calculateMarketPrice(orderbook.asks, baseSize, side)
: calculateMarketPrice(orderbook.bids, baseSize, side)
2021-04-02 11:26:21 -07:00
}
await placeAndSettle(
connection,
new PublicKey(IDS[cluster].mango_program_id),
mangoGroup,
marginAccount,
market,
wallet,
side,
calculatedPrice ?? price,
baseSize,
ioc ? 'ioc' : postOnly ? 'postOnly' : 'limit'
)
2021-04-10 14:12:15 -07:00
console.log('Successfully placed trade!')
2021-04-02 11:26:21 -07:00
2021-04-12 20:39:08 -07:00
setPrice('')
onSetBaseSize('')
2021-04-14 15:46:36 -07:00
actions.fetchMarginAccounts()
2021-04-02 11:26:21 -07:00
} catch (e) {
2021-04-11 21:17:23 -07:00
notify({
message: 'Error placing order',
description: e.message,
type: 'error',
})
2021-04-02 11:26:21 -07:00
} finally {
setSubmitting(false)
}
}
const handleTradeTypeChange = (tradeType) => {
setTradeType(tradeType)
if (tradeType === 'Market') {
setIoc(true)
setPrice('')
2021-04-02 11:26:21 -07:00
} else {
const limitPrice =
side === 'buy' ? orderbook.asks[0][0] : orderbook.bids[0][0]
setPrice(limitPrice)
setIoc(false)
}
}
const disabledTradeButton =
(!price && tradeType === 'Limit') || !baseSize || !connected || submitting
2021-04-02 11:26:21 -07:00
return (
<FloatingElement>
<div>
<div className={`flex text-base text-th-fgd-4`}>
<button
onClick={() => setSide('buy')}
className={`flex-1 outline-none focus:outline-none`}
2021-04-02 11:26:21 -07:00
>
<div
className={`hover:text-th-green pb-1 transition-colors duration-500
${
side === 'buy' &&
`text-th-green hover:text-th-green border-b-2 border-th-green`
}`}
>
Buy
</div>
</button>
<button
onClick={() => setSide('sell')}
className={`flex-1 outline-none focus:outline-none`}
2021-04-02 11:26:21 -07:00
>
<div
className={`hover:text-th-red pb-1 transition-colors duration-500
${
side === 'sell' &&
`text-th-red hover:text-th-red border-b-2 border-th-red`
}
`}
>
Sell
</div>
</button>
</div>
2021-04-13 16:41:04 -07:00
<Input.Group className="mt-4">
<Input
2021-04-02 11:26:21 -07:00
type="number"
step={market?.tickSize || 1}
2021-04-15 14:36:55 -07:00
onChange={(e) => onSetPrice(parseFloat(e.target.value))}
value={price}
2021-04-02 11:26:21 -07:00
disabled={tradeType === 'Market'}
prefix={'Price'}
suffix={quoteCurrency}
2021-04-18 03:34:37 -07:00
className="rounded-none"
wrapperClassName="w-3/5"
2021-04-02 11:26:21 -07:00
/>
<TradeType
2021-04-02 11:26:21 -07:00
onChange={handleTradeTypeChange}
value={tradeType}
2021-04-18 03:34:37 -07:00
className="hover:border-th-primary flex-grow"
/>
2021-04-13 16:41:04 -07:00
</Input.Group>
2021-04-13 16:41:04 -07:00
<Input.Group className="mt-4">
<Input
2021-04-02 11:26:21 -07:00
type="number"
step={market?.minOrderSize || 1}
onChange={(e) => onSetBaseSize(parseFloat(e.target.value))}
value={baseSize}
2021-04-18 03:34:37 -07:00
className="rounded-none"
wrapperClassName="w-3/5"
prefix={'Size'}
suffix={baseCurrency}
2021-04-02 11:26:21 -07:00
/>
2021-04-18 03:34:37 -07:00
<StyledRightInput
2021-04-02 11:26:21 -07:00
type="number"
step={market?.minOrderSize || 1}
onChange={(e) => onSetQuoteSize(parseFloat(e.target.value))}
value={quoteSize}
2021-04-18 03:34:37 -07:00
className="rounded-l-none"
wrapperClassName="w-2/5"
suffix={quoteCurrency}
2021-04-02 11:26:21 -07:00
/>
2021-04-13 16:41:04 -07:00
</Input.Group>
2021-04-02 11:26:21 -07:00
{tradeType !== 'Market' ? (
2021-04-18 03:34:37 -07:00
<div className="flex items-center mt-4">
2021-04-13 16:41:04 -07:00
<Switch checked={postOnly} onChange={postOnChange}>
POST
2021-04-13 16:41:04 -07:00
</Switch>
<div className="ml-4">
2021-04-13 16:41:04 -07:00
<Switch checked={ioc} onChange={iocOnChange}>
IOC
2021-04-13 16:41:04 -07:00
</Switch>
</div>
2021-04-02 11:26:21 -07:00
</div>
) : null}
</div>
<div className={`flex pt-6`}>
{ipAllowed ? (
connected ? (
side === 'buy' ? (
<Button
disabled={disabledTradeButton}
onClick={onsubmit}
className={`rounded ${
!disabledTradeButton &&
'border-th-green hover:border-th-green-dark'
} text-th-green hover:text-th-fgd-1 hover:bg-th-green-dark flex-grow`}
>
{`Buy ${baseSize > 0 ? baseSize : ''} ${baseCurrency}`}
</Button>
) : (
<Button
disabled={disabledTradeButton}
onClick={onSubmit}
className={`rounded ${
!disabledTradeButton &&
'border-th-red hover:border-th-red-dark'
} text-th-red hover:text-th-fgd-1 hover:bg-th-red-dark flex-grow`}
>
{`Sell ${baseSize > 0 ? baseSize : ''} ${baseCurrency}`}
</Button>
)
) : (
2021-04-19 09:15:49 -07:00
<>
{/* <div className="flex justify-between border border-th-fgd-4 rounded-md w-full">
<Button
onClick={() => wallet.connect()}
className={`rounded-r-none flex flex-grow items-center justify-center border-none`}
>
<WalletIcon className="fill-current h-4 w-4 mr-2" />
Connect Wallet
</Button>
<div className="relative h-full">
<WalletSelect />
</div>
</div> */}
</>
)
2021-04-02 11:26:21 -07:00
) : (
<Button disabled className="flex-grow">
<span className="text-lg font-light">Country Not Allowed</span>
2021-04-11 17:09:47 -07:00
</Button>
)}
</div>
2021-04-02 11:26:21 -07:00
</FloatingElement>
)
}