-
Notifications
You must be signed in to change notification settings - Fork 0
/
getObjects.mjs
193 lines (177 loc) · 6.22 KB
/
getObjects.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
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
import golos from 'golos-lib-js'
import { Asset, } from 'golos-lib-js/lib/utils/index.js'
import { OTYPES, golosifyId, ungolosifyId } from './ids.mjs'
import { convertAcc, } from './accounts.mjs'
import { convertAsset, lookupAssetSymbols, } from './assets.mjs'
// Used for order_delete market event
// and in convertOrder() below
export const convertOrderHeader = async (header) => {
const obj = {}
obj.id = await ungolosifyId(OTYPES.limit_order, header.seller + '|' + header.orderid.toString())
obj.seller = await ungolosifyId(OTYPES.account, header.seller)
const { sell_price } = header
let base = await Asset(sell_price.base)
let quote = await Asset(sell_price.quote);
// required at least for case with "my own orders"
obj.sell_price = {
base: await convertAsset(base),
quote: await convertAsset(quote),
}
obj._orig_id = header.orderid
return obj
}
export const convertOrder = async (order) => {
const obj = await convertOrderHeader(order)
// order_create_operation has asset-string,
// but getOrders returns order with number OR number-string if it is long
obj.for_sale = (order.for_sale.includes && order.for_sale.includes(' ')) ?
(await convertAsset(order.for_sale)).amount :
order.for_sale
obj.expiration = order.expiration
obj.deferred_fee = 0
obj.deferred_paid_fee = { amount: 0, asset_id: '1.3.0' }
obj._orig_id = order.orderid
return obj
}
export async function getObjects(args) {
const [ ids ] = args
const names = []
const nameIdx = {}
const orderids = []
const orderIdx = {}
const syms = []
const symIdx = {}
for (const id of ids) {
if (id.startsWith(OTYPES.account)) {
const res = await golosifyId(id)
if (res.golos_id) {
names.push(res.golos_id)
nameIdx[id] = names.length - 1
} else {
console.warn('Account not found', res)
}
} else if (id.startsWith(OTYPES.limit_order)) {
const res = await golosifyId(id)
if (res.golos_id) {
orderids.push(res.golos_id)
orderIdx[id] = orderids.length - 1
} else {
console.warn('Limit order not found', res)
}
} else if (id.startsWith(OTYPES.asset)) {
const res = await golosifyId(id)
if (res.golos_id) {
syms.push(res.golos_id)
symIdx[id] = syms.length - 1
} else {
console.warn('Asset not found', res)
}
} else if (id.startsWith(OTYPES.global_property)) {
// nothing to do here
} else if (id.startsWith(OTYPES.block_sumamry)) {
// nothing to do here
} else {
throw new Error('get_objects currently not supports ' + id)
}
}
let accs = []
if (names.length) {
accs = await golos.api.lookupAccountNamesAsync(names, true)
}
let orders = []
if (orderids.length) {
orders = await golos.api.getOrdersAsync(orderids)
console.log(orders)
}
let assets = []
if (syms.length) {
assets = await lookupAssetSymbols([syms])
}
const res = []
for (const id of ids) {
if (id.startsWith(OTYPES.account)) {
const idx = nameIdx[id]
if (accs[idx]) {
res.push(await convertAcc(accs[idx]))
} else {
res.push(null)
}
} else if (id.startsWith(OTYPES.limit_order)) {
const idx = orderIdx[id]
if (orders[idx] && orders[idx].for_sale) { // if exists
res.push(await convertOrder(orders[idx]))
} else {
res.push(null)
// And it is correct for not-exist orders.
// TODO: but what about fully filled orders?
}
} else if (id.startsWith(OTYPES.asset)) {
const idx = symIdx[id]
if (assets[idx]) {
res.push(assets[idx])
} else {
res.push(null)
}
} else if (id.startsWith(OTYPES.global_property)) {
res.push({
'id': '2.0.0',
'parameters': {
'current_fees': {
'parameters': [
[0, {
'fee': 0,
}],
[1, {
'fee': 0,
}],
[2, {
'fee': 0,
'price_per_kbyte': 0
}],
[3, {
'fee': 0,
}],
[4, {
'fee': 0,
}],
[5, {
'fee': 0,
}],
[6, {
'fee': 0,
}]
],
'scale': 10000
},
'block_interval': 3,
}
})
} else if (id.startsWith(OTYPES.block_sumamry)) {
const parts = id.split('.')
let num = parts[parts.length - 1]
num = parseInt(num) + 1
const obj = { id }
let block = await golos.api.getBlockAsync(num)
if (!block) {
block = await golos.api.getBlockAsync(num - 1)
if (!block) {
res.push(null)
continue
}
await new Promise(resolve => setTimeout(resolve, 3500))
block = await golos.api.getBlockAsync(num)
if (block) {
obj.block_id = block.previous
} else {
console.warn('WARNING - cannot find block ' + num)
res.push(null)
continue
}
} else {
obj.block_id = block.previous
}
res.push(obj)
}
}
return res
}