Skip to content

Commit f22a2dc

Browse files
committed
liquidation, add status_message_deriv for WS
1 parent df94517 commit f22a2dc

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

lib/liquidations.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { preparePrice } = require('bfx-api-node-util')
34
const numberValidator = require('./validators/number')
45
const dateValidator = require('./validators/date')
56
const amountValidator = require('./validators/amount')
@@ -52,15 +53,16 @@ class Liquidations extends Model {
5253
*/
5354
toString () {
5455
const {
55-
mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold
56+
mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold, liquidationPrice
5657
} = this
5758

5859
return _compact(_flatten([
5960
new Date(mtsUpdated).toLocaleString(),
6061
symbol,
6162
[amount, '@', basePrice],
6263
isMatch && 'matched',
63-
isMarketSold && 'sold'
64+
isMarketSold && 'sold',
65+
['liq', preparePrice(liquidationPrice),]
6466
])).join(' ')
6567
}
6668

lib/ws_status_messages_deriv.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict'
2+
3+
const stringValidator = require('./validators/string')
4+
const priceValidator = require('./validators/price')
5+
const Model = require('./model')
6+
7+
const fields = {
8+
timestamp: 0,
9+
price: 2,
10+
priceSpot: 3,
11+
fundBal: 5,
12+
fundingEventTimestamp: 7,
13+
fundingAccrued: 8,
14+
fundingStep: 9,
15+
currentFunding: 11,
16+
markprice: 14,
17+
openInterest: 17,
18+
clampMin: 21,
19+
clampMax: 22
20+
}
21+
22+
/**
23+
* Derivatives Status Message model used by the websocket
24+
*/
25+
class StatusMessagesDerivWS extends Model {
26+
/**
27+
* @param {object|Array} data - derivatives status message data
28+
* @param {number} data.timestamp - timestamp
29+
* @param {string} data.price - price
30+
* @param {string} data.priceSpot - spot price
31+
* @param {string} data.fundBal - funding balance
32+
* @param {number} data.fundingEventTimestamp - timestamp
33+
* @param {string} data.fundingAccrued - accrued funding
34+
* @param {string} data.fundingStep - funding step
35+
* @param {number} data.currentFunding - funding applied in the current 8h period,
36+
* @param {number} data.markprice - markprice,
37+
* @param {number} data.openInterest - total number of outstanding derivative contracts,
38+
* @param {number} data.clampMin - min clamp
39+
* @param {number} data.clampMax - max clamp
40+
*/
41+
constructor (data = {}) {
42+
super({ data, fields })
43+
}
44+
45+
/**
46+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
47+
* @returns {object} pojo
48+
*/
49+
static unserialize (data) {
50+
return super.unserialize({ data, fields })
51+
}
52+
53+
/**
54+
* Validates a given public trade instance
55+
*
56+
* @param {object[]|object|PublicTrade[]|PublicTrade|Array} data - instance to validate
57+
* @returns {string} error - null if instance is valid
58+
*/
59+
static validate (data) {
60+
return super.validate({
61+
data,
62+
fields,
63+
validators: {
64+
timestamp: stringValidator,
65+
price: priceValidator,
66+
priceSpot: priceValidator,
67+
fundBal: priceValidator,
68+
fundingEventTimestamp: stringValidator,
69+
fundingAccrued: priceValidator,
70+
fundingStep: priceValidator,
71+
currentFunding: priceValidator,
72+
markprice: priceValidator,
73+
openInterest: priceValidator,
74+
clampMin: priceValidator,
75+
clampMax: priceValidator
76+
}
77+
})
78+
}
79+
}
80+
81+
module.exports = StatusMessagesDerivWS

test/lib/models/liquidations.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Liquidations entry model', () => {
1414
testModel({
1515
model: Liquidations,
1616
orderedFields: [
17-
null, 'posId', 'mtsUpdated', null, 'symbol', 'amount', 'basePrice', null, 'isMatch', 'isMarketSold'
17+
null, 'posId', 'mtsUpdated', null, 'symbol', 'amount', 'basePrice', null, 'isMatch', 'isMarketSold', null, 'liquidationPrice'
1818
]
1919
})
2020

@@ -27,7 +27,8 @@ describe('Liquidations entry model', () => {
2727
amount: new Array(...(new Array(5))).map(() => Math.random()),
2828
basePrice: new Array(...(new Array(5))).map(() => Math.random()),
2929
isMatch: new Array(...(new Array(5))).map(() => Math.random() > 0.5),
30-
isMarketSold: new Array(...(new Array(5))).map(() => Math.random() > 0.5)
30+
isMarketSold: new Array(...(new Array(5))).map(() => Math.random() > 0.5),
31+
liquidationPrice: new Array(...(new Array(5))).map(() => Math.random())
3132
}
3233
})
3334

@@ -36,13 +37,15 @@ describe('Liquidations entry model', () => {
3637
const l = new Liquidations({
3738
symbol: 'tBTCUSD',
3839
amount: 42,
39-
basePrice: 0.1
40+
basePrice: 0.1,
41+
liquidationPrice: 33
4042
})
4143

4244
const str = l.toString()
4345
assert.ok(/BTCUSD/.test(str), 'symbol missing')
4446
assert.ok(_includes(str, '42'), 'amount missing')
4547
assert.ok(_includes(str, '0.1'), 'rate missing')
48+
assert.ok(_includes(str, '33'), 'liquidationPrice missing')
4649
})
4750
})
4851
})

0 commit comments

Comments
 (0)