-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids.mjs
77 lines (70 loc) · 1.79 KB
/
ids.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
import Tarantool from './tarantool.mjs'
export function randomId() {
return '1.2.' + Math.floor(Math.random() * 500)
}
export const OTYPES = {
asset: '1.3.',
account: '1.2.',
limit_order: '1.7.',
global_property: '2.0.',
block_sumamry: '2.8.',
}
export function isId(idOrName) {
return idOrName.split('.').length === 3
}
export async function ungolosifyId(oType, golosId, extra = {}) {
let res
try {
res = await Tarantool.instance('tarantool').call(
'ungolosify_id', oType, golosId, extra
)
} catch (err) {
console.error('ungolosifyId error')
console.trace()
console.error(err)
throw err
}
res = res[0][0]
const oTypeId = res.otype + res.id
return oTypeId
}
export async function golosifyId(oTypeId) {
let [ oSpace, oObjectType, oId ] = oTypeId.split('.')
const oType = oSpace + '.' + oObjectType + '.'
oId = parseInt(oId)
if (isNaN(oId) || oId < 0) {
throw new Error('Wrong oId')
}
let res
try {
res = await Tarantool.instance('tarantool').call(
'golosify_id', oType, oId
)
} catch (err) {
console.error('golosifyId error')
console.trace()
console.error(err)
throw err
}
res = res[0][0]
return res
}
export async function idData(oType, someId) {
if (isId(someId)) {
return await golosifyId(someId)
} else {
let res
try {
res = await Tarantool.instance('tarantool').call(
'ungolosify_id', oType, someId, {}
)
} catch (err) {
console.error('idData: ungolosifyId error')
console.trace()
console.error(err)
throw err
}
res = res[0][0]
return res
}
}