-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathTradeForm.jsx
More file actions
358 lines (334 loc) · 10.6 KB
/
TradeForm.jsx
File metadata and controls
358 lines (334 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { Button, Input, Radio, Switch, Slider, Select } from 'antd';
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import {
useBaseCurrencyBalances,
useQuoteCurrencyBalances,
useMarket,
useMarkPrice,
useSelectedOpenOrdersAccount,
useSelectedBaseCurrencyAccount,
useSelectedQuoteCurrencyAccount,
useOrderbook,
useFeeDiscountKeys,
calculateBestPrice,
} from '../utils/markets';
import { getFeeRates } from '@project-serum/serum';
import { useWallet } from '../utils/wallet';
import { notify } from '../utils/notifications';
import {
getDecimalCount,
roundToDecimal,
floorToDecimal,
isIncrement,
} from '../utils/utils';
import { useSendConnection } from '../utils/connection';
import FloatingElement from './layout/FloatingElement';
import { placeOrder } from '../utils/send';
const DEFAULT_ORDER_TYPE = 'limit';
const ORDER_TYPES = [
{ label: 'Limit', value: 'limit' },
{ label: 'Market', value: 'market' },
];
const SellButton = styled(Button)`
margin: 20px 0px 0px 0px;
background: #f23b69;
border-color: #f23b69;
`;
const BuyButton = styled(Button)`
margin: 20px 0px 0px 0px;
background: #02bf76;
border-color: #02bf76;
`;
const sliderMarks = {
0: '0%',
25: '25%',
50: '50%',
75: '75%',
100: '100%',
};
export default function TradeForm({ style, setChangeOrderRef }) {
const { baseCurrency, quoteCurrency, market } = useMarket();
const baseCurrencyBalances = useBaseCurrencyBalances();
const quoteCurrencyBalances = useQuoteCurrencyBalances();
const baseCurrencyAccount = useSelectedBaseCurrencyAccount();
const quoteCurrencyAccount = useSelectedQuoteCurrencyAccount();
const openOrdersAccount = useSelectedOpenOrdersAccount(true);
const { wallet } = useWallet();
const sendConnection = useSendConnection();
const markPrice = useMarkPrice();
const [orderbook] = useOrderbook();
const [feeAccounts] = useFeeDiscountKeys();
const [side, setSide] = useState('buy');
const [orderType, setOrderType] = useState(DEFAULT_ORDER_TYPE);
const [postOnly, setPostOnly] = useState(false);
const [ioc, setIoc] = useState(false);
const [baseSize, setBaseSize] = useState(null);
const [quoteSize, setQuoteSize] = useState(null);
const [price, setPrice] = useState(null);
const [submitting, setSubmitting] = useState(false);
const [sizeFraction, setSizeFraction] = useState(0);
const availableQuote = openOrdersAccount
? market.quoteSplSizeToNumber(openOrdersAccount.quoteTokenFree)
: 0;
let feeRates = getFeeRates(feeAccounts && feeAccounts[0]?.feeTier);
let quoteBalance = (quoteCurrencyBalances || 0) + (availableQuote || 0);
let baseBalance = baseCurrencyBalances || 0;
let sizeDecimalCount =
market?.minOrderSize && getDecimalCount(market.minOrderSize);
let priceDecimalCount = market?.tickSize && getDecimalCount(market.tickSize);
useEffect(() => {
setChangeOrderRef && setChangeOrderRef(doChangeOrder);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [setChangeOrderRef]);
useEffect(() => {
baseSize && price && onSliderChange(sizeFraction);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [side]);
useEffect(() => {
updateSizeFraction();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [price, baseSize]);
const onSetPrice = (price) => {
const formattedPrice = isIncrement(price, market?.tickSize)
? price
: floorToDecimal(price, priceDecimalCount);
setPrice(formattedPrice);
};
const onSetBaseSize = (baseSize) => {
const formattedBaseSize = isIncrement(baseSize, market?.minOrderSize)
? baseSize
: floorToDecimal(baseSize, sizeDecimalCount);
setBaseSize(formattedBaseSize);
const rawQuoteSize = formattedBaseSize * (price || markPrice);
const quoteSize =
formattedBaseSize && roundToDecimal(rawQuoteSize, sizeDecimalCount);
setQuoteSize(quoteSize);
};
const onSetQuoteSize = (quoteSize) => {
setQuoteSize(quoteSize);
const rawBaseSize = quoteSize / price;
const baseSize = quoteSize && roundToDecimal(rawBaseSize, sizeDecimalCount);
setBaseSize(baseSize);
};
const doChangeOrder = ({ size, price }) => {
const formattedSize = size && roundToDecimal(size, sizeDecimalCount);
const formattedPrice = price && roundToDecimal(price, priceDecimalCount);
formattedSize && onSetBaseSize(formattedSize);
formattedPrice && onSetPrice(formattedPrice);
};
const updateSizeFraction = () => {
let rawMaxSize;
if (side === 'sell') {
rawMaxSize = baseBalance;
} else {
let maxQuoteBalance =
orderType === 'market' || price >= orderbook?.asks[0]?.[0]
? (1 - feeRates.taker) * quoteBalance
: quoteBalance;
rawMaxSize = maxQuoteBalance / price;
}
const maxSize = floorToDecimal(rawMaxSize, sizeDecimalCount);
const sizeFraction = Math.min((baseSize / maxSize) * 100, 100);
setSizeFraction(sizeFraction);
};
const onSliderChange = (value) => {
if (!price && markPrice) {
let formattedMarkPrice = priceDecimalCount
? markPrice.toFixed(priceDecimalCount)
: markPrice;
onSetPrice(formattedMarkPrice);
}
let newSize;
if (side === 'buy') {
if (price || markPrice) {
newSize = ((quoteBalance / (price || markPrice)) * value) / 100;
}
} else {
newSize = (baseBalance * value) / 100;
}
// round down to minOrderSize increment
let formatted = floorToDecimal(newSize, sizeDecimalCount);
onSetBaseSize(formatted);
};
const postOnChange = (checked) => {
if (checked) {
setIoc(false);
}
setPostOnly(checked);
};
const iocOnChange = (checked) => {
if (checked) {
setPostOnly(false);
}
setIoc(checked);
};
async function onSubmit() {
let parsedSize = parseFloat(baseSize);
let parsedPrice;
if (orderType === 'market') {
parsedPrice =
side === 'buy'
? calculateBestPrice(orderbook, parsedSize)
: market?.tickSize;
} else {
parsedPrice = parseFloat(price);
}
setSubmitting(true);
try {
await placeOrder({
side,
price: parsedPrice,
size: parsedSize,
orderType: ioc ? 'ioc' : postOnly ? 'postOnly' : 'limit',
market,
connection: sendConnection,
wallet,
baseCurrencyAccount: baseCurrencyAccount?.pubkey,
quoteCurrencyAccount: quoteCurrencyAccount?.pubkey,
});
onSetPrice(null);
onSetBaseSize(null);
} catch (e) {
console.warn(e);
notify({
message: 'Error placing order',
description: e.message,
type: 'error',
});
} finally {
setSubmitting(false);
}
}
return (
<FloatingElement
style={{ display: 'flex', flexDirection: 'column', ...style }}
>
<div style={{ flex: 1 }}>
<Radio.Group
onChange={(e) => setSide(e.target.value)}
value={side}
buttonStyle="solid"
style={{
marginBottom: 8,
width: '100%',
}}
>
<Radio.Button
value="buy"
style={{
width: '50%',
textAlign: 'center',
background: side === 'buy' ? '#02bf76' : '',
borderColor: side === 'buy' ? '#02bf76' : '',
}}
>
BUY
</Radio.Button>
<Radio.Button
value="sell"
style={{
width: '50%',
textAlign: 'center',
background: side === 'sell' ? '#F23B69' : '',
borderColor: side === 'sell' ? '#F23B69' : '',
}}
>
SELL
</Radio.Button>
</Radio.Group>
<div style={{ display: 'flex', paddingBottom: 8 }}>
<span
className="ant-input-group-addon"
style={{ width: '53px', display: 'flex', alignItems: 'center' }}
>
Type
</span>
<Select
style={{ flex: 1, textAlign: 'center' }}
defaultValue={DEFAULT_ORDER_TYPE}
value={orderType}
options={ORDER_TYPES}
onSelect={setOrderType}
/>
</div>
<Input
style={{ textAlign: 'right', paddingBottom: 8 }}
addonBefore={<div style={{ width: '30px' }}>Price</div>}
suffix={
<span style={{ fontSize: 10, opacity: 0.5 }}>{quoteCurrency}</span>
}
value={orderType === 'market' ? 'MARKET' : price}
type={orderType === 'market' ? 'text' : 'number'}
step={market?.tickSize || 1}
disabled={orderType === 'market'}
onChange={(e) => onSetPrice(e.target.value)}
/>
<Input.Group compact style={{ paddingBottom: 8 }}>
<Input
style={{ width: 'calc(50% + 30px)', textAlign: 'right' }}
addonBefore={<div style={{ width: '30px' }}>Size</div>}
suffix={
<span style={{ fontSize: 10, opacity: 0.5 }}>{baseCurrency}</span>
}
value={baseSize}
type="number"
step={market?.minOrderSize || 1}
onChange={(e) => onSetBaseSize(e.target.value)}
/>
<Input
style={{ width: 'calc(50% - 30px)', textAlign: 'right' }}
suffix={
<span style={{ fontSize: 10, opacity: 0.5 }}>
{quoteCurrency}
</span>
}
value={quoteSize}
type="number"
step={market?.minOrderSize || 1}
onChange={(e) => onSetQuoteSize(e.target.value)}
/>
</Input.Group>
<Slider
value={sizeFraction}
tipFormatter={(value) => `${value}%`}
marks={sliderMarks}
onChange={onSliderChange}
/>
<div style={{ paddingTop: 18 }}>
{'POST '}
<Switch
checked={postOnly}
onChange={postOnChange}
style={{ marginRight: 40 }}
/>
{'IOC '}
<Switch checked={ioc} onChange={iocOnChange} />
</div>
</div>
{side === 'buy' ? (
<BuyButton
disabled={(orderType !== 'market' && !price) || !baseSize}
onClick={onSubmit}
block
type="primary"
size="large"
loading={submitting}
>
Buy {baseCurrency}
</BuyButton>
) : (
<SellButton
disabled={(orderType !== 'market' && !price) || !baseSize}
onClick={onSubmit}
block
type="primary"
size="large"
loading={submitting}
>
Sell {baseCurrency}
</SellButton>
)}
</FloatingElement>
);
}