-
Notifications
You must be signed in to change notification settings - Fork 0
/
market.mjs
157 lines (135 loc) · 4.58 KB
/
market.mjs
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
import golos from 'golos-lib-js'
import { Asset, } from 'golos-lib-js/lib/utils/index.js'
import { convertAsset, getPrecision } from './assets.mjs'
import { randomId, OTYPES, ungolosifyId, golosifyId, isId, idData } from './ids.mjs'
export const reversePriceObj = (orig) => {
return {
base: orig.quote,
quote: orig.base
}
}
const convertOrder = async (order, isAsk, reversed) => {
const obj = {}
obj.id = await ungolosifyId(OTYPES.limit_order, order.seller + '|' + order.orderid.toString())
obj.seller = await ungolosifyId(OTYPES.account, order.seller)
obj.for_sale = isAsk ? order.asset1 : order.asset2
const { order_price } = order
let base = await Asset(order_price.base)
let quote = await Asset(order_price.quote);
obj.sell_price = {
base: await convertAsset(base),
quote: await convertAsset(quote),
}
if (reversed) {
obj.sell_price = reversePriceObj(obj.sell_price)
}
const expiration = new Date()
expiration.setUTCFullYear(expiration.getUTCFullYear() + 1)
obj.expiration = expiration.toISOString().split('.')[0]
obj.deferred_fee = 0
obj.deferred_paid_fee = { amount: 0, asset_id: '1.3.0' }
obj._orig_id = order.orderid
return obj
}
const parseAssetNameOrId = async (arg) => {
let sym
let data = await idData(OTYPES.asset, arg)
const { golos_id } = data
if (!golos_id) {
throw new Error('No asset', golos_id)
}
const precision = await getPrecision(data)
return { golos_id, precision }
}
const isPairReversed = (sym1, sym2) => {
return sym2 === 'GOLOS' || (sym2 < sym1 && sym1 !== 'GOLOS')
}
export async function getLimitOrders(args) {
let [ sellAsset, buyAsset, limit ] = args
limit = Math.ceil(limit / 2)
sellAsset = (await parseAssetNameOrId(sellAsset)).golos_id
buyAsset = (await parseAssetNameOrId(buyAsset)).golos_id
const book = await golos.api.getOrderBookExtendedAsync(limit, [buyAsset, sellAsset])
const reversed = isPairReversed(buyAsset, sellAsset)
const { bids, asks } = book
const res = []
for (let i = 0; i < limit; ++i) {
const bid = bids[i]
const ask = asks[i]
if (bid) {
res.push(await convertOrder(bid, false, reversed))
}
if (ask) {
res.push(await convertOrder(ask, true, reversed))
}
if (!bid && !ask) break
}
return res
}
export async function getOrderBook(args) {
let [ sellAsset, buyAsset, limit ] = args
sellAsset = await parseAssetNameOrId(sellAsset)
const sellPrec = sellAsset.precision
const sellSym = sellAsset.golos_id
buyAsset = await parseAssetNameOrId(buyAsset)
const buyPrec = buyAsset.precision
const buySym = buyAsset.golos_id
const book = await golos.api.getOrderBookExtendedAsync(limit, [buySym, sellSym])
const convertShortOrder = async (order) => {
const base = await Asset(order.asset2, sellPrec, sellSym)
const quote = await Asset(order.asset1, buyPrec, buySym)
const price = parseFloat(order.real_price).toString()
return {
price,
quote: quote.amountFloat,
base: base.amountFloat
}
}
const bids = []
for (const bid of book.bids) {
bids.push(await convertShortOrder(bid))
}
const asks = []
for (const ask of book.asks) {
asks.push(await convertShortOrder(ask))
}
const res = {
base: sellSym,
quote: buySym,
bids,
asks
}
return res
}
export async function getTicker(args) {
let [ base, quote ] = args
if (isId(base)) {
const res = await golosifyId(base)
if (res.golos_id) {
base = res.golos_id
} else {
throw new Error('getTicker base', base)
}
}
if (isId(quote)) {
const res = await golosifyId(quote)
if (res.golos_id) {
quote = res.golos_id
} else {
throw new Error('getTicker quote', quote)
}
}
const ticker = await golos.api.getTickerAsync([quote, base])
const res = {}
const dgp = await golos.api.getDynamicGlobalPropertiesAsync()
res.time = dgp.time
res.base = base
res.quote = quote
res.latest = ticker.latest1
res.lowest_ask = ticker.lowest_ask
res.highest_bid = ticker.highest_bid
res.percent_change = ticker.percent_change1 // TODO: looks wrong because of Golos-BitShares price difference
res.base_volume = new Asset(ticker.asset2_volume).amount.toString()
res.quote_volume = new Asset(ticker.asset1_volume).amount.toString()
return res
}