forked from blockworks-foundation/mango-ui-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
314 lines (281 loc) · 7.06 KB
/
index.ts
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
import { I80F48 } from '@blockworks-foundation/mango-client/lib/src/fixednum'
import { TOKEN_MINTS } from '@project-serum/serum'
import { PublicKey } from '@solana/web3.js'
import BN from 'bn.js'
import { TRIGGER_ORDER_TYPES } from '../components/trade_form/AdvancedTradeForm'
import { Orderbook } from '../stores/useMangoStore'
import { MarketKind } from '@blockworks-foundation/mango-client'
export async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
export const percentFormat = new Intl.NumberFormat(undefined, {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
export function floorToDecimal(
value: number,
decimals: number | undefined | null
) {
return decimals
? Math.floor(value * 10 ** decimals) / 10 ** decimals
: Math.floor(value)
}
export function ceilToDecimal(
value: number,
decimals: number | undefined | null
) {
return decimals
? Math.ceil(value * 10 ** decimals) / 10 ** decimals
: Math.ceil(value)
}
export function roundToDecimal(
value: number,
decimals: number | undefined | null
) {
return decimals ? Math.round(value * 10 ** decimals) / 10 ** decimals : value
}
export function getDecimalCount(value): number {
if (
!isNaN(value) &&
Math.floor(value) !== value &&
value.toString().includes('.')
)
return value.toString().split('.')[1].length || 0
if (
!isNaN(value) &&
Math.floor(value) !== value &&
value.toString().includes('e')
)
return parseInt(value.toString().split('e-')[1] || '0')
return 0
}
export function getTokenMultiplierFromDecimals(decimals: number): BN {
return new BN(10).pow(new BN(decimals))
}
export function abbreviateAddress(address: PublicKey, size = 5) {
const base58 = address.toBase58()
return base58.slice(0, size) + '…' + base58.slice(-size)
}
export function isEqual(obj1, obj2, keys) {
if (!keys && Object.keys(obj1).length !== Object.keys(obj2).length) {
return false
}
keys = keys || Object.keys(obj1)
for (const k of keys) {
if (obj1[k] !== obj2[k]) {
// shallow comparison
return false
}
}
return true
}
export function groupBy(list, keyGetter) {
const map = new Map()
list.forEach((item) => {
const key = keyGetter(item)
const collection = map.get(key)
if (!collection) {
map.set(key, [item])
} else {
collection.push(item)
}
})
return map
}
export function isDefined<T>(argument: T | undefined): argument is T {
return argument !== undefined
}
export function calculateTradePrice(
kind: MarketKind,
tradeType: string,
orderBook: Orderbook,
baseSize: number,
side: 'buy' | 'sell',
price: string | number,
triggerPrice?: string | number
): number {
if (tradeType === 'Market' && kind === 'spot') {
return calculateMarketPrice(orderBook, baseSize, side)
} else if (TRIGGER_ORDER_TYPES.includes(tradeType)) {
if (tradeType === 'Take Profit Limit' || tradeType === 'Stop Limit') {
return Number(price)
} else {
return Number(triggerPrice)
}
}
return Number(price)
}
export const calculateMarketPrice = (
orderBook: Orderbook,
size: number,
side: 'buy' | 'sell'
): number => {
const orders = side === 'buy' ? orderBook.asks : orderBook.bids
let acc = 0
let selectedOrder
for (const order of orders) {
acc += order[1]
if (acc >= size) {
selectedOrder = order
break
}
}
if (!selectedOrder) {
console.error('Orderbook empty no market price available')
return
}
if (side === 'buy') {
return selectedOrder[0] * 1.05
} else {
return selectedOrder[0] * 0.95
}
}
// Precision for our mango group token
export const tokenPrecision = {
BTC: 4,
ETH: 3,
MNGO: 2,
SOL: 2,
SRM: 2,
RAY: 3,
COPE: 2,
FTT: 3,
ADA: 2,
MSOL: 2,
BNB: 3,
AVAX: 2,
USDC: 2,
USDT: 2,
}
// Precision for 1 perp contract. -log10(baseLotSize) + baseDecimals
export const perpContractPrecision = {
BTC: 4,
ETH: 3,
MNGO: 0,
SOL: 2,
SRM: 1,
RAY: 1,
FTT: 1,
ADA: 0,
BNB: 3,
AVAX: 2,
LUNA: 2,
}
const tokenPricePrecision = {
BTC: 1,
ETH: 1,
MNGO: 4,
SOL: 2,
SRM: 3,
RAY: 3,
COPE: 3,
FTT: 3,
ADA: 4,
MSOL: 2,
BNB: 1,
AVAX: 2,
USDC: 2,
USDT: 2,
}
export const getSymbolForTokenMintAddress = (address: string): string => {
if (address && address.length) {
return TOKEN_MINTS.find((m) => m.address.toString() === address)?.name || ''
} else {
return ''
}
}
export const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
export function* chunks(arr, n) {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n)
}
}
export function zipDict<K extends string | number | symbol, V>(
keys: K[],
values: V[]
) {
const result: Partial<Record<K, V>> = {}
keys.forEach((key, index) => {
result[key] = values[index]
})
return result
}
export const copyToClipboard = (copyThis) => {
const el = document.createElement('textarea')
el.value = copyThis.toString()
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
}
// Truncate decimals without rounding
export const trimDecimals = (n, digits) => {
const step = Math.pow(10, digits || 0)
const temp = Math.trunc(step * n)
return temp / step
}
export const i80f48ToPercent = (value: I80F48) =>
value.mul(I80F48.fromNumber(100))
export const usdFormatter = (value, decimals = 2, currency = true) => {
if (decimals === 0) {
value = Math.abs(value)
}
const config = currency ? { style: 'currency', currency: 'USD' } : {}
return new Intl.NumberFormat('en-US', {
...config,
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
}).format(value)
}
export const formatUsdValue = (value: number, symbol?: string) => {
const precision = symbol
? tokenPricePrecision[symbol]
: value >= 1 || value <= -1
? 2
: value === 0 ||
(value > 0 && value < 0.001) ||
(value < 0 && value > -0.001)
? 0
: 4
return usdFormatter(value, precision)
}
export const countLeadingZeros = (x) => {
if (x % 1 == 0) {
return 0
} else {
return -1 - Math.floor(Math.log10(x % 1))
}
}
export function getBrowserVisibilityProp() {
if (typeof document.hidden !== 'undefined') {
// Opera 12.10 and Firefox 18 and later support
return 'visibilitychange'
// @ts-ignore
} else if (typeof document.msHidden !== 'undefined') {
return 'msvisibilitychange'
// @ts-ignore
} else if (typeof document.webkitHidden !== 'undefined') {
return 'webkitvisibilitychange'
}
}
export function getBrowserDocumentHiddenProp() {
if (typeof document.hidden !== 'undefined') {
return 'hidden'
// @ts-ignore
} else if (typeof document.msHidden !== 'undefined') {
return 'msHidden'
// @ts-ignore
} else if (typeof document.webkitHidden !== 'undefined') {
return 'webkitHidden'
}
}
export function getIsDocumentHidden() {
return !document[getBrowserDocumentHiddenProp()]
}
export const numberCompactFormatter = Intl.NumberFormat('en', {
notation: 'compact',
})