2021-04-02 11:26:21 -07:00
|
|
|
import { useState, useEffect, useRef } from 'react'
|
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'
|
2021-04-12 13:01:55 -07:00
|
|
|
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-12 20:39:08 -07:00
|
|
|
export default function TradeForm() {
|
2021-04-10 14:12:15 -07:00
|
|
|
const { baseCurrency, quoteCurrency, market, marketAddress } = useMarket()
|
2021-04-12 13:01:55 -07:00
|
|
|
const set = useMangoStore((s) => s.set)
|
2021-04-10 14:12:15 -07:00
|
|
|
const { connected } = useMangoStore((s) => s.wallet)
|
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()
|
2021-04-12 13:01:55 -07:00
|
|
|
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
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2021-04-12 13:01:55 -07:00
|
|
|
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
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-12 20:39:08 -07:00
|
|
|
onSetBaseSize(baseSize)
|
|
|
|
}, [price, 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
|
|
|
const usePrice = Number(price) || markPrice
|
|
|
|
|
2021-04-02 11:26:21 -07:00
|
|
|
if (!usePrice) {
|
2021-04-12 20:39:08 -07:00
|
|
|
setBaseSize('')
|
2021-04-02 11:26:21 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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'
|
2021-04-12 13:01:55 -07:00
|
|
|
? 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('')
|
|
|
|
actions.fetchMarginAcccount()
|
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)
|
2021-04-12 13:01:55 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FloatingElement>
|
|
|
|
<div>
|
2021-04-12 13:01:55 -07:00
|
|
|
<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
|
|
|
>
|
2021-04-12 13:01:55 -07:00
|
|
|
<div
|
2021-04-13 07:10:57 -07:00
|
|
|
className={`hover:text-th-fgd-1 pb-1 transition-colors duration-500
|
2021-04-12 13:01:55 -07:00
|
|
|
${
|
|
|
|
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
|
|
|
>
|
2021-04-12 13:01:55 -07:00
|
|
|
<div
|
2021-04-13 07:10:57 -07:00
|
|
|
className={`hover:text-th-fgd-1 pb-1 transition-colors duration-500
|
2021-04-12 13:01:55 -07:00
|
|
|
${
|
|
|
|
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}
|
|
|
|
onChange={(e) => setPrice(parseFloat(e.target.value))}
|
2021-04-12 13:01:55 -07:00
|
|
|
value={price}
|
2021-04-02 11:26:21 -07:00
|
|
|
disabled={tradeType === 'Market'}
|
2021-04-12 13:01:55 -07:00
|
|
|
prefix={'Price'}
|
|
|
|
suffix={quoteCurrency}
|
2021-04-13 07:10:57 -07:00
|
|
|
className="w-3/5 rounded-r-none"
|
2021-04-02 11:26:21 -07:00
|
|
|
/>
|
2021-04-12 13:01:55 -07:00
|
|
|
<TradeType
|
2021-04-02 11:26:21 -07:00
|
|
|
onChange={handleTradeTypeChange}
|
|
|
|
value={tradeType}
|
2021-04-13 16:41:04 -07:00
|
|
|
className="w-2/5 hover:border-th-primary"
|
2021-04-12 13:01:55 -07:00
|
|
|
/>
|
2021-04-13 16:41:04 -07:00
|
|
|
</Input.Group>
|
2021-04-12 13:01:55 -07:00
|
|
|
|
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))}
|
2021-04-12 13:01:55 -07:00
|
|
|
value={baseSize}
|
2021-04-13 16:41:04 -07:00
|
|
|
className="flex-grow w-3/5 rounded-r-none"
|
2021-04-12 13:01:55 -07:00
|
|
|
prefix={'Size'}
|
|
|
|
suffix={baseCurrency}
|
2021-04-02 11:26:21 -07:00
|
|
|
/>
|
2021-04-13 16:41:04 -07:00
|
|
|
<Input
|
2021-04-02 11:26:21 -07:00
|
|
|
type="number"
|
|
|
|
step={market?.minOrderSize || 1}
|
|
|
|
onChange={(e) => onSetQuoteSize(parseFloat(e.target.value))}
|
2021-04-12 13:01:55 -07:00
|
|
|
value={quoteSize}
|
2021-04-13 16:41:04 -07:00
|
|
|
className="border-l border-th-fgd-4 rounded-l-none w-2/5"
|
2021-04-12 13:01:55 -07:00
|
|
|
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-12 15:15:15 -07:00
|
|
|
<div className="flex items-center mt-4">
|
2021-04-13 16:41:04 -07:00
|
|
|
<Switch checked={postOnly} onChange={postOnChange}>
|
2021-04-12 15:15:15 -07:00
|
|
|
POST
|
2021-04-13 16:41:04 -07:00
|
|
|
</Switch>
|
2021-04-12 15:15:15 -07:00
|
|
|
<div className="ml-4">
|
2021-04-13 16:41:04 -07:00
|
|
|
<Switch checked={ioc} onChange={iocOnChange}>
|
2021-04-12 15:15:15 -07:00
|
|
|
IOC
|
2021-04-13 16:41:04 -07:00
|
|
|
</Switch>
|
2021-04-12 15:15:15 -07:00
|
|
|
</div>
|
2021-04-02 11:26:21 -07:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2021-04-12 09:49:02 -07:00
|
|
|
<div className={`flex mt-4`}>
|
2021-04-05 07:32:11 -07:00
|
|
|
{ipAllowed ? (
|
|
|
|
side === 'buy' ? (
|
2021-04-10 12:21:02 -07:00
|
|
|
<Button
|
2021-04-05 07:32:11 -07:00
|
|
|
disabled={
|
2021-04-06 15:11:42 -07:00
|
|
|
(!price && tradeType === 'Limit') ||
|
|
|
|
!baseSize ||
|
|
|
|
!connected ||
|
|
|
|
submitting
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
|
|
|
onClick={onSubmit}
|
2021-04-12 15:15:15 -07:00
|
|
|
className="rounded text-lg bg-th-green text-th-bkg-1 hover:bg-th-primary flex-grow"
|
2021-04-05 07:32:11 -07:00
|
|
|
>
|
2021-04-13 07:10:57 -07:00
|
|
|
{connected ? `Buy ${baseCurrency}` : 'Connect Wallet'}
|
2021-04-10 12:21:02 -07:00
|
|
|
</Button>
|
2021-04-05 07:32:11 -07:00
|
|
|
) : (
|
2021-04-10 12:21:02 -07:00
|
|
|
<Button
|
2021-04-05 07:32:11 -07:00
|
|
|
disabled={
|
2021-04-06 15:11:42 -07:00
|
|
|
(!price && tradeType === 'Limit') ||
|
|
|
|
!baseSize ||
|
|
|
|
!connected ||
|
|
|
|
submitting
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
|
|
|
onClick={onSubmit}
|
2021-04-12 15:15:15 -07:00
|
|
|
className="rounded text-lg bg-th-red text-white hover:bg-th-primary flex-grow"
|
2021-04-05 07:32:11 -07:00
|
|
|
>
|
2021-04-13 07:10:57 -07:00
|
|
|
{connected ? `Sell ${baseCurrency}` : 'Connect Wallet'}
|
2021-04-10 12:21:02 -07:00
|
|
|
</Button>
|
2021-04-05 07:32:11 -07:00
|
|
|
)
|
2021-04-02 11:26:21 -07:00
|
|
|
) : (
|
2021-04-12 15:15:15 -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>
|
2021-04-05 07:32:11 -07:00
|
|
|
)}
|
|
|
|
</div>
|
2021-04-02 11:26:21 -07:00
|
|
|
</FloatingElement>
|
|
|
|
)
|
|
|
|
}
|