-
Notifications
You must be signed in to change notification settings - Fork 3
/
trade.js
54 lines (46 loc) · 1.47 KB
/
trade.js
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
const UInt64BE = require('int64-buffer').Uint64BE
const libUtils = require('./utils')
const libCommon = require('./common')
const MSG_TYPES = {
t_trade:
4 + // SYM
8 + // ID
8 + // PRICE
8 + // AMOUNT
1, // SIGN
f_bu:
4 + // SYM
1 + // ID
8 + // RATE
1 + // PERIOD
8 + // AMOUNT
1 // SIGN
}
const getTradeMsgSize = (symType) => {
return 8 + // SEQ
MSG_TYPES[`${symType}_trade`]
}
const fTradeEntry = (symType, symId, e, seq) => {
const msize = getTradeMsgSize(symType)
const b = Buffer.allocUnsafe(1 + 1 + msize)
b.writeUInt8(35, 0)
b.writeUInt8(msize, 1)
b.fill((new UInt64BE(seq)).toBuffer(), 2)
b.writeUInt32BE(symId, 10)
if (symType === 'f') {
b.fill((new UInt64BE(e.id)).toBuffer(), 14)
b.fill((new UInt64BE(libUtils.nBN(e.rate).times(libCommon.DEF_MULTIPLIER).dp(0).toString(16), 16)).toBuffer(), 22)
b.fill((new UInt8(e.period)).toBuffer(), 30)
b.fill((new UInt64BE(libUtils.nBN(e.amount).times(libCommon.DEF_MULTIPLIER).abs().dp(0).toString(16), 16)).toBuffer(), 31)
b.writeUInt8(+e.amount >= 0 ? 0 : 1, 39)
} else {
b.fill((new UInt64BE(e.id)).toBuffer(), 14)
b.fill((new UInt64BE(libUtils.nBN(e.price).times(libCommon.DEF_MULTIPLIER).dp(0).toString(16), 16)).toBuffer(), 22)
b.fill((new UInt64BE(libUtils.nBN(e.amount).times(libCommon.DEF_MULTIPLIER).abs().dp(0).toString(16), 16)).toBuffer(), 30)
b.writeUInt8(+e.amount >= 0 ? 0 : 1, 38)
}
return b
}
module.exports = {
fTradeEntry: fTradeEntry
}