btc-api-node 1.12.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of btc-api-node might be problematic. Click here for more details.
- package/.istanbul.yml +53 -0
- package/.travis.yml +5 -0
- package/CHANGELOG +33 -0
- package/LICENSE.md +21 -0
- package/README.md +211 -0
- package/doc/order.md +160 -0
- package/doc/rest2.md +573 -0
- package/doc/ws2.md +925 -0
- package/examples/bfx.js +26 -0
- package/examples/rest2/order_history.js +29 -0
- package/examples/rest2/symbols.js +15 -0
- package/examples/rest2/tickers.js +24 -0
- package/examples/rest2/trade_history.js +28 -0
- package/examples/ws2/auth.js +31 -0
- package/examples/ws2/calc.js +33 -0
- package/examples/ws2/cancel_all.js +35 -0
- package/examples/ws2/cancel_all_buf.js +39 -0
- package/examples/ws2/candles.js +36 -0
- package/examples/ws2/info_events.js +40 -0
- package/examples/ws2/oc_multi.js +50 -0
- package/examples/ws2/order_books.js +37 -0
- package/examples/ws2/orders.js +67 -0
- package/examples/ws2/ox_multi.js +61 -0
- package/examples/ws2/sequencing.js +23 -0
- package/examples/ws2/ticker.js +20 -0
- package/examples/ws2/trades.js +27 -0
- package/index.js +24 -0
- package/lib/model.js +25 -0
- package/lib/models/alert.js +25 -0
- package/lib/models/balance_info.js +21 -0
- package/lib/models/candle.js +33 -0
- package/lib/models/funding_credit.js +61 -0
- package/lib/models/funding_info.js +16 -0
- package/lib/models/funding_loan.js +64 -0
- package/lib/models/funding_offer.js +60 -0
- package/lib/models/funding_trade.js +33 -0
- package/lib/models/index.js +23 -0
- package/lib/models/margin_info.js +29 -0
- package/lib/models/notification.js +31 -0
- package/lib/models/order.js +288 -0
- package/lib/models/order_book.js +214 -0
- package/lib/models/position.js +43 -0
- package/lib/models/tick.js +83 -0
- package/lib/models/trade.js +43 -0
- package/lib/models/trade_tick.js +29 -0
- package/lib/models/wallet.js +34 -0
- package/lib/transports/rest.js +391 -0
- package/lib/transports/rest2.js +597 -0
- package/lib/transports/ws.js +323 -0
- package/lib/transports/ws2.js +1729 -0
- package/lib/util/gen_auth_sig.js +23 -0
- package/lib/util/index.js +11 -0
- package/lib/util/is_snapshot.js +5 -0
- package/lib/util/nonce.js +5 -0
- package/package.json +39 -0
- package/test/fixtures/response-ticker-funding.json +1 -0
- package/test/fixtures/response-ticker-pairs.json +1 -0
- package/test/fixtures/response-trades-funding.json +1 -0
- package/test/fixtures/response-trades-pairs.json +1 -0
- package/test/fixtures/response-ws-1-orderbook-R0.json +51 -0
- package/test/fixtures/response-ws2-server-order-book-P0.json +1 -0
- package/test/fixtures/response-ws2-server-order-book-P1.json +1 -0
- package/test/fixtures/response-ws2-server-order-book-R0.json +1 -0
- package/test/fixtures/response-ws2-server-ticker-funding.json +1 -0
- package/test/fixtures/response-ws2-server-trades.json +1 -0
- package/test/helpers/test_model.js +71 -0
- package/test/index.js +131 -0
- package/test/lib/models/alert.js +12 -0
- package/test/lib/models/balance_info.js +12 -0
- package/test/lib/models/candle.js +12 -0
- package/test/lib/models/funding_credit.js +17 -0
- package/test/lib/models/funding_info.js +7 -0
- package/test/lib/models/funding_loan.js +17 -0
- package/test/lib/models/funding_offer.js +17 -0
- package/test/lib/models/funding_trade.js +15 -0
- package/test/lib/models/margin_info.js +15 -0
- package/test/lib/models/notification.js +14 -0
- package/test/lib/models/order.js +395 -0
- package/test/lib/models/order_book.js +188 -0
- package/test/lib/models/position.js +15 -0
- package/test/lib/models/tick.js +34 -0
- package/test/lib/models/trade.js +16 -0
- package/test/lib/models/trade_tick.js +14 -0
- package/test/lib/models/wallet.js +14 -0
- package/test/lib/transports/rest-1-integration.js +131 -0
- package/test/lib/transports/rest-2-integration.js +80 -0
- package/test/lib/transports/rest-2-issue-80-argument-length.js +61 -0
- package/test/lib/transports/rest-2-smoke-test.js +49 -0
- package/test/lib/transports/rest-2-unit.js +26 -0
- package/test/lib/transports/rest1.js +152 -0
- package/test/lib/transports/ws-1-handle-channel.js +83 -0
- package/test/lib/transports/ws-1-parsing.js +40 -0
- package/test/lib/transports/ws-1-test.js +275 -0
- package/test/lib/transports/ws2-integration.js +259 -0
- package/test/lib/transports/ws2-unit.js +1295 -0
- package/test/lib/util/is_snapshot.js +20 -0
- package/test/lib/util/nonce.js +20 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class Alert extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.key,
|
|
9
|
+
this.type,
|
|
10
|
+
this.symbol,
|
|
11
|
+
this.price
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static unserialize (arr) {
|
|
16
|
+
return {
|
|
17
|
+
key: arr[0],
|
|
18
|
+
type: arr[1],
|
|
19
|
+
symbol: arr[2],
|
|
20
|
+
price: arr[3]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = Alert
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class BalanceInfo extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.amount,
|
|
9
|
+
this.amountNet
|
|
10
|
+
]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static unserialize (arr) {
|
|
14
|
+
return {
|
|
15
|
+
amount: arr[0],
|
|
16
|
+
amountNet: arr[1]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = BalanceInfo
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class Candle extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.mts,
|
|
9
|
+
this.open,
|
|
10
|
+
this.close,
|
|
11
|
+
this.high,
|
|
12
|
+
this.low,
|
|
13
|
+
this.volume
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static unserialize (arr) {
|
|
18
|
+
if (Array.isArray(arr[0])) {
|
|
19
|
+
return arr.map(candle => Candle.unserialize(candle))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
mts: arr[0],
|
|
24
|
+
open: arr[1],
|
|
25
|
+
close: arr[2],
|
|
26
|
+
high: arr[3],
|
|
27
|
+
low: arr[4],
|
|
28
|
+
volume: arr[5]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = Candle
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class FundingCredit extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.id,
|
|
9
|
+
this.symbol,
|
|
10
|
+
this.side,
|
|
11
|
+
this.mtsCreate,
|
|
12
|
+
this.mtsUpdate,
|
|
13
|
+
this.amount,
|
|
14
|
+
this.flags,
|
|
15
|
+
this.status,
|
|
16
|
+
this.rate,
|
|
17
|
+
this.period,
|
|
18
|
+
this.mtsOpening,
|
|
19
|
+
this.mtsLastPayout,
|
|
20
|
+
this.notify ? 1 : 0,
|
|
21
|
+
this.hidden ? 1 : 0,
|
|
22
|
+
this.insure ? 1 : 0,
|
|
23
|
+
this.renew ? 1 : 0,
|
|
24
|
+
this.rateReal,
|
|
25
|
+
this.noClose ? 1 : 0,
|
|
26
|
+
this.positionPair
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static unserialize (arr) {
|
|
31
|
+
return {
|
|
32
|
+
id: arr[0],
|
|
33
|
+
symbol: arr[1],
|
|
34
|
+
side: arr[2],
|
|
35
|
+
mtsCreate: arr[3],
|
|
36
|
+
mtsUpdate: arr[4],
|
|
37
|
+
amount: arr[5],
|
|
38
|
+
flags: arr[6],
|
|
39
|
+
status: arr[7],
|
|
40
|
+
rate: arr[8],
|
|
41
|
+
period: arr[9],
|
|
42
|
+
mtsOpening: arr[10],
|
|
43
|
+
mtsLastPayout: arr[11],
|
|
44
|
+
notify: arr[12] === 1,
|
|
45
|
+
hidden: arr[13] === 1,
|
|
46
|
+
insure: arr[14] === 1,
|
|
47
|
+
renew: arr[15] === 1,
|
|
48
|
+
rateReal: arr[16],
|
|
49
|
+
noClose: arr[17] === 1,
|
|
50
|
+
positionPair: arr[18]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
FundingCredit.status = {}
|
|
56
|
+
const statuses = ['ACTIVE', 'EXECUTED', 'PARTIALLY FILLED', 'CANCELLED']
|
|
57
|
+
statuses.forEach((s) => {
|
|
58
|
+
FundingCredit.status[s.split(' ').join('_')] = s
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
module.exports = FundingCredit
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
// TODO: documentation isn't clear, check it on the wire
|
|
6
|
+
class FundingInfo extends Model {
|
|
7
|
+
serialize () {
|
|
8
|
+
throw new Error('unimplemented')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static unserialize (arr) {
|
|
12
|
+
throw new Error('unimplemented')
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = FundingInfo
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class FundingLoan extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.id,
|
|
9
|
+
this.symbol,
|
|
10
|
+
this.side,
|
|
11
|
+
this.mtsCreate,
|
|
12
|
+
this.mtsUpdate,
|
|
13
|
+
this.amount,
|
|
14
|
+
this.flags,
|
|
15
|
+
this.status,
|
|
16
|
+
this.rate,
|
|
17
|
+
this.period,
|
|
18
|
+
this.mtsOpening,
|
|
19
|
+
this.mtsLastPayout,
|
|
20
|
+
this.notify ? 1 : 0,
|
|
21
|
+
this.hidden ? 1 : 0,
|
|
22
|
+
this.insure ? 1 : 0,
|
|
23
|
+
this.renew ? 1 : 0,
|
|
24
|
+
this.rateReal,
|
|
25
|
+
this.noClose ? 1 : 0
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static unserialize (arr) {
|
|
30
|
+
return {
|
|
31
|
+
id: arr[0],
|
|
32
|
+
symbol: arr[1],
|
|
33
|
+
side: arr[2],
|
|
34
|
+
mtsCreate: arr[3],
|
|
35
|
+
mtsUpdate: arr[4],
|
|
36
|
+
amount: arr[5],
|
|
37
|
+
flags: arr[6],
|
|
38
|
+
status: arr[7],
|
|
39
|
+
rate: arr[8],
|
|
40
|
+
period: arr[9],
|
|
41
|
+
mtsOpening: arr[10],
|
|
42
|
+
mtsLastPayout: arr[11],
|
|
43
|
+
notify: arr[12] === 1,
|
|
44
|
+
hidden: arr[13] === 1,
|
|
45
|
+
insure: arr[14] === 1,
|
|
46
|
+
renew: arr[15] === 1,
|
|
47
|
+
rateReal: arr[16],
|
|
48
|
+
noClose: arr[17] === 1
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
FundingLoan.status = {}
|
|
54
|
+
FundingLoan.side = { // TODO: enquire about case sensitivity
|
|
55
|
+
LEND: 'Lend',
|
|
56
|
+
LOAN: 'Loan'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const statuses = ['ACTIVE', 'EXECUTED', 'PARTIALLY FILLED', 'CANCELLED']
|
|
60
|
+
statuses.forEach((s) => {
|
|
61
|
+
FundingLoan.status[s.split(' ').join('_')] = s
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
module.exports = FundingLoan
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class FundingOffer extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.id,
|
|
9
|
+
this.symbol,
|
|
10
|
+
this.mtsCreate,
|
|
11
|
+
this.mtsUpdate,
|
|
12
|
+
this.amount,
|
|
13
|
+
this.amountOrig,
|
|
14
|
+
this.type,
|
|
15
|
+
this.flags,
|
|
16
|
+
this.status,
|
|
17
|
+
this.rate,
|
|
18
|
+
this.period,
|
|
19
|
+
this.notify ? 1 : 0,
|
|
20
|
+
this.hidden ? 1 : 0,
|
|
21
|
+
this.insure ? 1 : 0,
|
|
22
|
+
this.renew ? 1 : 0,
|
|
23
|
+
this.rateReal
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static unserialize (arr) {
|
|
28
|
+
return {
|
|
29
|
+
id: arr[0],
|
|
30
|
+
symbol: arr[1],
|
|
31
|
+
mtsCreate: arr[2],
|
|
32
|
+
mtsUpdate: arr[3],
|
|
33
|
+
amount: arr[4],
|
|
34
|
+
amountOrig: arr[5],
|
|
35
|
+
type: arr[6],
|
|
36
|
+
flags: arr[7],
|
|
37
|
+
status: arr[8],
|
|
38
|
+
rate: arr[9],
|
|
39
|
+
period: arr[10],
|
|
40
|
+
notify: arr[11] === 1,
|
|
41
|
+
hidden: arr[12] === 1,
|
|
42
|
+
insure: arr[13] === 1,
|
|
43
|
+
renew: arr[14] === 1,
|
|
44
|
+
rateReal: arr[15]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
FundingOffer.status = {}
|
|
50
|
+
FundingOffer.type = { // TODO: enquire about case sensitivity
|
|
51
|
+
LEND: 'lend',
|
|
52
|
+
LOAN: 'loan'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const statuses = ['ACTIVE', 'EXECUTED', 'PARTIALLY FILLED', 'CANCELLED']
|
|
56
|
+
statuses.forEach((s) => {
|
|
57
|
+
FundingOffer.status[s.split(' ').join('_')] = s
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
module.exports = FundingOffer
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class FundingTrade extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.id,
|
|
9
|
+
this.symbol,
|
|
10
|
+
this.mtsCreate,
|
|
11
|
+
this.offerID,
|
|
12
|
+
this.amount,
|
|
13
|
+
this.rate,
|
|
14
|
+
this.period,
|
|
15
|
+
this.maker // TODO: {0,1} or something else?
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static unserialize (arr) {
|
|
20
|
+
return {
|
|
21
|
+
id: arr[0],
|
|
22
|
+
symbol: arr[1],
|
|
23
|
+
mtsCreate: arr[2],
|
|
24
|
+
offerID: arr[3],
|
|
25
|
+
amount: arr[4],
|
|
26
|
+
rate: arr[5],
|
|
27
|
+
period: arr[6],
|
|
28
|
+
maker: arr[7]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = FundingTrade
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const models = {
|
|
4
|
+
OrderBook: require('./order_book'),
|
|
5
|
+
BalanceInfo: require('./balance_info'),
|
|
6
|
+
FundingCredit: require('./funding_credit'),
|
|
7
|
+
FundingInfo: require('./funding_info'),
|
|
8
|
+
FundingLoan: require('./funding_loan'),
|
|
9
|
+
FundingOffer: require('./funding_offer'),
|
|
10
|
+
FundingTrade: require('./funding_trade'),
|
|
11
|
+
MarginInfo: require('./margin_info'),
|
|
12
|
+
Notification: require('./notification'),
|
|
13
|
+
Order: require('./order'),
|
|
14
|
+
Position: require('./position'),
|
|
15
|
+
Trade: require('./trade'),
|
|
16
|
+
Wallet: require('./wallet'),
|
|
17
|
+
Alert: require('./alert'),
|
|
18
|
+
Tick: require('./tick'),
|
|
19
|
+
TradeTick: require('./trade_tick'),
|
|
20
|
+
Candle: require('./candle')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = models
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class MarginInfo extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.userPL,
|
|
9
|
+
this.userSwaps,
|
|
10
|
+
this.symbol,
|
|
11
|
+
this.tradeableBalance,
|
|
12
|
+
this.marginBalance,
|
|
13
|
+
this.marginNet
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static unserialize (arr) {
|
|
18
|
+
return {
|
|
19
|
+
userPL: arr[0],
|
|
20
|
+
userSwaps: arr[1],
|
|
21
|
+
symbol: arr[2],
|
|
22
|
+
tradeableBalance: arr[3],
|
|
23
|
+
marginBalance: arr[4],
|
|
24
|
+
marginNet: arr[5]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = MarginInfo
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Model = require('../model')
|
|
4
|
+
|
|
5
|
+
class Notification extends Model {
|
|
6
|
+
serialize () {
|
|
7
|
+
return [
|
|
8
|
+
this.mts,
|
|
9
|
+
this.type,
|
|
10
|
+
this.messageID,
|
|
11
|
+
this.notifyInfo,
|
|
12
|
+
this.code,
|
|
13
|
+
this.status,
|
|
14
|
+
this.text
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static unserialize (arr) {
|
|
19
|
+
return {
|
|
20
|
+
mts: arr[0],
|
|
21
|
+
type: arr[1],
|
|
22
|
+
messageID: arr[2],
|
|
23
|
+
notifyInfo: arr[3],
|
|
24
|
+
code: arr[4],
|
|
25
|
+
status: arr[5],
|
|
26
|
+
text: arr[6]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = Notification
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Promise = require('bluebird')
|
|
4
|
+
const Model = require('../model')
|
|
5
|
+
let lastCID = Date.now()
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* High level order model; provides methods for execution & can stay updated via
|
|
9
|
+
* a WSv2 connection
|
|
10
|
+
*/
|
|
11
|
+
class Order extends Model {
|
|
12
|
+
/**
|
|
13
|
+
* @param {Object|Array} data - either a map of order fields or a raw array
|
|
14
|
+
* @param {WSv2} ws - optional, saved for a later call to registerListeners()
|
|
15
|
+
*/
|
|
16
|
+
constructor (data, ws) {
|
|
17
|
+
super(data)
|
|
18
|
+
|
|
19
|
+
this._ws = ws
|
|
20
|
+
this._lastAmount = this.amount
|
|
21
|
+
|
|
22
|
+
this._onWSOrderNew = this._onWSOrderNew.bind(this)
|
|
23
|
+
this._onWSOrderUpdate = this._onWSOrderUpdate.bind(this)
|
|
24
|
+
this._onWSOrderClose = this._onWSOrderClose.bind(this)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Registers for updates/persistence on the specified ws2 instance
|
|
29
|
+
*
|
|
30
|
+
* @param {WSv2} ws - optional, defaults to internal ws
|
|
31
|
+
*/
|
|
32
|
+
registerListeners (ws = this._ws) {
|
|
33
|
+
if (!ws) return
|
|
34
|
+
|
|
35
|
+
const chanData = {
|
|
36
|
+
symbol: this.symbol,
|
|
37
|
+
cid: this.cid || null,
|
|
38
|
+
cbGID: this.cbGID()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ws.onOrderNew(chanData, this._onWSOrderNew)
|
|
42
|
+
ws.onOrderUpdate(chanData, this._onWSOrderUpdate)
|
|
43
|
+
ws.onOrderClose(chanData, this._onWSOrderClose)
|
|
44
|
+
|
|
45
|
+
this._ws = ws
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Removes update listeners from the specified ws2 instance
|
|
50
|
+
*
|
|
51
|
+
* @param {WSv2} ws - optional, defaults to internal ws
|
|
52
|
+
*/
|
|
53
|
+
removeListeners (ws = this._ws) {
|
|
54
|
+
if (ws) ws.removeListeners(this.cbGID())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @return {string} cbGID
|
|
59
|
+
*/
|
|
60
|
+
cbGID () {
|
|
61
|
+
return `${this.gid}.${this.cid}`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {WSv2} ws - optional, defaults to internal ws
|
|
66
|
+
* @return {Promise} p
|
|
67
|
+
*/
|
|
68
|
+
submit (ws = this._ws) {
|
|
69
|
+
if (!ws) return Promise.reject(new Error('no ws connection'))
|
|
70
|
+
|
|
71
|
+
return ws.submitOrder(this).then((orderArr) => {
|
|
72
|
+
Object.assign(this, Order.unserialize(orderArr))
|
|
73
|
+
|
|
74
|
+
return this
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {WSv2} ws - optional, defaults to internal ws
|
|
80
|
+
* @return {Promise} p
|
|
81
|
+
*/
|
|
82
|
+
cancel (ws = this._ws) {
|
|
83
|
+
if (!ws) return Promise.reject(new Error('no ws connection'))
|
|
84
|
+
if (!this.id) return Promise.reject(new Error('order has no ID'))
|
|
85
|
+
|
|
86
|
+
return ws.cancelOrder(this.id)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Equivalent to calling cancel() followed by submit()
|
|
91
|
+
*
|
|
92
|
+
* @param {WSv2} ws - optional, defaults to internal ws
|
|
93
|
+
* @return {Promise} p
|
|
94
|
+
*/
|
|
95
|
+
recreate (ws = this._ws) {
|
|
96
|
+
if (!ws) return Promise.reject(new Error('no ws connection'))
|
|
97
|
+
if (!this.id) return Promise.reject(new Error('order has no ID'))
|
|
98
|
+
|
|
99
|
+
return this.cancel(ws).then(() => {
|
|
100
|
+
this.id = null
|
|
101
|
+
|
|
102
|
+
return this.submit(ws)
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Query the amount that was filled on the last order update
|
|
108
|
+
*
|
|
109
|
+
* @return {number} amount
|
|
110
|
+
*/
|
|
111
|
+
getLastFillAmount () {
|
|
112
|
+
return this._lastAmount - this.amount
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @return {string} currency
|
|
117
|
+
*/
|
|
118
|
+
getBaseCurrency () {
|
|
119
|
+
return this.symbol.substring(1, 4)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @return {string} currency
|
|
124
|
+
*/
|
|
125
|
+
getQuoteCurrency () {
|
|
126
|
+
return this.symbol.substring(4)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @return {number} value
|
|
131
|
+
*/
|
|
132
|
+
getNotionalValue () {
|
|
133
|
+
return Math.abs(this.amount * this.price)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @param {Array} order
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
_onWSOrderUpdate (order) {
|
|
141
|
+
this._lastAmount = this.amount
|
|
142
|
+
Object.assign(this, Order.unserialize(order))
|
|
143
|
+
|
|
144
|
+
this.emit('update')
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {Array} order
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
_onWSOrderClose (order) {
|
|
152
|
+
this._lastAmount = this.amount
|
|
153
|
+
Object.assign(this, Order.unserialize(order))
|
|
154
|
+
|
|
155
|
+
this.emit('close')
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {Array} order
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
_onWSOrderNew (order) {
|
|
163
|
+
this._lastAmount = this.amount
|
|
164
|
+
Object.assign(this, Order.unserialize(order))
|
|
165
|
+
|
|
166
|
+
this.emit('update')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @return {Array} o
|
|
171
|
+
*/
|
|
172
|
+
serialize () {
|
|
173
|
+
return [
|
|
174
|
+
this.id,
|
|
175
|
+
this.gid,
|
|
176
|
+
this.cid,
|
|
177
|
+
this.symbol,
|
|
178
|
+
this.mtsCreate,
|
|
179
|
+
this.mtsUpdate,
|
|
180
|
+
this.amount,
|
|
181
|
+
this.amountOrig,
|
|
182
|
+
this.type,
|
|
183
|
+
this.typePrev,
|
|
184
|
+
null,
|
|
185
|
+
null,
|
|
186
|
+
this.flags,
|
|
187
|
+
this.status,
|
|
188
|
+
null,
|
|
189
|
+
null,
|
|
190
|
+
this.price,
|
|
191
|
+
this.priceAvg,
|
|
192
|
+
this.priceTrailing,
|
|
193
|
+
this.priceAuxLimit,
|
|
194
|
+
null,
|
|
195
|
+
null,
|
|
196
|
+
null,
|
|
197
|
+
this.notify ? 1 : 0,
|
|
198
|
+
this.hidden ? 1 : 0,
|
|
199
|
+
this.placedId
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Creates an order map that can be passed to the `on` command.
|
|
205
|
+
*
|
|
206
|
+
* @return {Object} o
|
|
207
|
+
*/
|
|
208
|
+
toNewOrderPacket () {
|
|
209
|
+
return {
|
|
210
|
+
gid: this.gid,
|
|
211
|
+
cid: isNaN(this.cid) ? lastCID++ : this.cid,
|
|
212
|
+
symbol: this.symbol,
|
|
213
|
+
type: this.type,
|
|
214
|
+
price_trailing: `${this.priceTrailing || ''}`,
|
|
215
|
+
price_aux_limit: `${this.priceAuxLimit || ''}`,
|
|
216
|
+
price: `${this.price}`,
|
|
217
|
+
amount: `${this.amount}`,
|
|
218
|
+
hidden: this.hidden ? 1 : 0,
|
|
219
|
+
postonly: this.postonly ? 1 : 0
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* @param {Array} arr
|
|
225
|
+
* @return {Object} order
|
|
226
|
+
*/
|
|
227
|
+
static unserialize (arr) {
|
|
228
|
+
return {
|
|
229
|
+
id: arr[0],
|
|
230
|
+
gid: arr[1],
|
|
231
|
+
cid: arr[2],
|
|
232
|
+
symbol: arr[3],
|
|
233
|
+
mtsCreate: arr[4],
|
|
234
|
+
mtsUpdate: arr[5],
|
|
235
|
+
amount: arr[6],
|
|
236
|
+
amountOrig: arr[7],
|
|
237
|
+
type: arr[8],
|
|
238
|
+
typePrev: arr[9],
|
|
239
|
+
flags: arr[12],
|
|
240
|
+
status: arr[13],
|
|
241
|
+
price: arr[16],
|
|
242
|
+
priceAvg: arr[17],
|
|
243
|
+
priceTrailing: arr[18],
|
|
244
|
+
priceAuxLimit: arr[19],
|
|
245
|
+
notify: arr[23] === 1,
|
|
246
|
+
hidden: arr[24] === 1,
|
|
247
|
+
placedId: arr[25]
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {Array} arr - order in ws2 array format
|
|
253
|
+
* @return {string} currency - base currency from symbol
|
|
254
|
+
*/
|
|
255
|
+
static getBaseCurrency (arr = []) {
|
|
256
|
+
return (arr[3] || '').substring(1, 4).toUpperCase()
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @param {Array} arr - order in ws2 array format
|
|
261
|
+
* @return {string} currency - quote currency from symbol
|
|
262
|
+
*/
|
|
263
|
+
static getQuoteCurrency (arr = []) {
|
|
264
|
+
return (arr[3] || '').substring(4).toUpperCase()
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
Order.type = {}
|
|
269
|
+
Order.status = {}
|
|
270
|
+
|
|
271
|
+
const statuses = ['ACTIVE', 'EXECUTED', 'PARTIALLY FILLED', 'CANCELLED']
|
|
272
|
+
const types = [
|
|
273
|
+
'MARKET', 'EXCHANGE MARKET', 'LIMIT', 'EXCHANGE LIMIT', 'STOP',
|
|
274
|
+
'EXCHANGE STOP', 'TRAILING STOP', 'EXCHANGE TRAILING STOP', 'FOK',
|
|
275
|
+
'EXCHANGE FOK', 'STOP LIMIT', 'EXCHANGE STOP LIMIT'
|
|
276
|
+
]
|
|
277
|
+
|
|
278
|
+
statuses.forEach((s) => {
|
|
279
|
+
Order.type[s] = s
|
|
280
|
+
Order.status[s.split(' ').join('_')] = s
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
types.forEach((t) => {
|
|
284
|
+
Order.type[t] = t
|
|
285
|
+
Order.type[t.split(' ').join('_')] = t
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
module.exports = Order
|