From bb5bfc822fe9092e164783b075fe73f85894ee58 Mon Sep 17 00:00:00 2001 From: Wim Devos Date: Tue, 8 Dec 2020 20:32:08 +0100 Subject: [PATCH 1/4] Extend the Derivatives Status Message to contain all values --- lib/status_messages_deriv.js | 12 ++++++++++++ test/lib/models/status_messages_deriv.js | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/status_messages_deriv.js b/lib/status_messages_deriv.js index 21ffb49..c375e53 100644 --- a/lib/status_messages_deriv.js +++ b/lib/status_messages_deriv.js @@ -10,8 +10,12 @@ const fields = { price: 3, priceSpot: 4, fundBal: 6, + fundingEventTimestamp: 8, fundingAccrued: 9, fundingStep: 10, + currentFunding: 12, + markprice: 15, + openInterest: 18, clampMin: 22, clampMax: 23 } @@ -27,8 +31,12 @@ class StatusMessagesDeriv extends Model { * @param {string} data.price - price * @param {string} data.priceSpot - spot price * @param {string} data.fundBal - funding balance + * @param {number} data.fundingEventTimestamp - timestamp * @param {string} data.fundingAccrued - accrued funding * @param {string} data.fundingStep - funding step + * @param {number} data.currentFunding - funding applied in the current 8h period, + * @param {number} data.markprice - markprice, + * @param {number} data.openInterest - total number of outstanding derivative contracts, * @param {number} data.clampMin - min clamp * @param {number} data.clampMax - max clamp */ @@ -60,8 +68,12 @@ class StatusMessagesDeriv extends Model { price: priceValidator, priceSpot: priceValidator, fundBal: priceValidator, + fundingEventTimestamp: stringValidator, fundingAccrued: priceValidator, fundingStep: priceValidator, + currentFunding: priceValidator, + markprice: priceValidator, + openInterest: priceValidator, clampMin: priceValidator, clampMax: priceValidator } diff --git a/test/lib/models/status_messages_deriv.js b/test/lib/models/status_messages_deriv.js index ea56435..34a9b03 100644 --- a/test/lib/models/status_messages_deriv.js +++ b/test/lib/models/status_messages_deriv.js @@ -13,8 +13,9 @@ describe('Derivatives Status Message model', () => { model: StatusMessagesDeriv, orderedFields: [ 'key', 'timestamp', null, 'price', 'priceSpot', null, 'fundBal', null, - null, 'fundingAccrued', 'fundingStep', null, null, null, null, null, - null, null, null, null, null, null, 'clampMin', 'clampMax' + 'fundingEventTimestamp', 'fundingAccrued', 'fundingStep', null, + 'currentFunding', null, null, 'markprice', null, null, 'openInterest', + null, null, null, 'clampMin', 'clampMax' ] }) @@ -26,8 +27,12 @@ describe('Derivatives Status Message model', () => { price: new Array(5).fill().map(Math.random), priceSpot: new Array(5).fill().map(Math.random), fundBal: new Array(5).fill().map(Math.random), + fundingEventTimestamp: VALID_STRINGS, fundingAccrued: new Array(5).fill().map(Math.random), fundingStep: new Array(5).fill().map(Math.random), + currentFunding: new Array(5).fill().map(Math.random), + markprice: new Array(5).fill().map(Math.random), + openInterest: new Array(5).fill().map(Math.random), clampMin: new Array(5).fill().map(Math.random), clampMax: new Array(5).fill().map(Math.random) } From b5ad2d5cc424d554a19c82b9b3ebb19bf7913533 Mon Sep 17 00:00:00 2001 From: Wim Devos Date: Tue, 8 Dec 2020 20:41:15 +0100 Subject: [PATCH 2/4] Bump version to 1.5.1 and update Changelog --- CHANGELOG | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 23532b0..1cf879d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +# 1.5.1 +- feature: add fundingEventTimestamp to StatusMessagesDeriv model +- feature: add currentFunding to StatusMessagesDeriv model +- feature: add markprice to StatusMessagesDeriv model +- feature: add openInterest to StatusMessagesDeriv model + # 1.5.0 - feature: added isPaperTradeEnabled info in user info model diff --git a/package.json b/package.json index 9421717..aed2091 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bfx-api-node-models", - "version": "1.5.0", + "version": "1.5.1", "description": "Object models for usage with the Bitfinex node API", "engines": { "node": ">=8.3.0" From df94517ef005d10f52d5d6698c7d7463ca39a6ac Mon Sep 17 00:00:00 2001 From: Wim Devos Date: Sun, 16 Jan 2022 00:57:10 +0100 Subject: [PATCH 3/4] Extend liquidation with liquidationprice --- lib/liquidations.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/liquidations.js b/lib/liquidations.js index b496960..631678e 100644 --- a/lib/liquidations.js +++ b/lib/liquidations.js @@ -16,7 +16,8 @@ const fields = { amount: 5, basePrice: 6, isMatch: 8, - isMarketSold: 9 + isMarketSold: 9, + liquidationPrice: 11 } /** @@ -32,6 +33,7 @@ class Liquidations extends Model { * @param {number} data.basePrice - base price * @param {number|boolean} data.isMatch - matched flag * @param {number|boolean} data.isMarketSold - sold flag + * @param {number} data.liquidationPrice - Price at which the liquidation was triggered */ constructor (data = {}) { super({ data, fields }) @@ -79,7 +81,8 @@ class Liquidations extends Model { amount: amountValidator, basePrice: priceValidator, isMatch: boolValidator, - isMarketSold: boolValidator + isMarketSold: boolValidator, + liquidationPrice: numberValidator } }) } From ae92ef3c7bf072a6a34f655b1b22e9f2ee8310c2 Mon Sep 17 00:00:00 2001 From: Wim Devos Date: Sun, 16 Jan 2022 01:29:30 +0100 Subject: [PATCH 4/4] liquidation, add status_message_deriv for WS --- lib/index.js | 1 + lib/liquidations.js | 6 ++- lib/ws_status_messages_deriv.js | 81 +++++++++++++++++++++++++++++++++ test/lib/models/liquidations.js | 9 ++-- 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 lib/ws_status_messages_deriv.js diff --git a/lib/index.js b/lib/index.js index 3a3507d..7f1bbda 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,6 +28,7 @@ module.exports = { UserInfo: require('./user_info'), Currency: require('./currency'), StatusMessagesDeriv: require('./status_messages_deriv'), + StatusMessagesDerivWS: require('./ws_status_messages_deriv'), Login: require('./login'), ChangeLog: require('./change_log'), PublicPulseProfile: require('./public_pulse_profile'), diff --git a/lib/liquidations.js b/lib/liquidations.js index 631678e..f5a8c2c 100644 --- a/lib/liquidations.js +++ b/lib/liquidations.js @@ -1,5 +1,6 @@ 'use strict' +const { preparePrice } = require('bfx-api-node-util') const numberValidator = require('./validators/number') const dateValidator = require('./validators/date') const amountValidator = require('./validators/amount') @@ -52,7 +53,7 @@ class Liquidations extends Model { */ toString () { const { - mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold + mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold, liquidationPrice } = this return _compact(_flatten([ @@ -60,7 +61,8 @@ class Liquidations extends Model { symbol, [amount, '@', basePrice], isMatch && 'matched', - isMarketSold && 'sold' + isMarketSold && 'sold', + ['liq', preparePrice(liquidationPrice),] ])).join(' ') } diff --git a/lib/ws_status_messages_deriv.js b/lib/ws_status_messages_deriv.js new file mode 100644 index 0000000..c6613d2 --- /dev/null +++ b/lib/ws_status_messages_deriv.js @@ -0,0 +1,81 @@ +'use strict' + +const stringValidator = require('./validators/string') +const priceValidator = require('./validators/price') +const Model = require('./model') + +const fields = { + timestamp: 0, + price: 2, + priceSpot: 3, + fundBal: 5, + fundingEventTimestamp: 7, + fundingAccrued: 8, + fundingStep: 9, + currentFunding: 11, + markprice: 14, + openInterest: 17, + clampMin: 21, + clampMax: 22 +} + +/** + * Derivatives Status Message model used by the websocket + */ +class StatusMessagesDerivWS extends Model { + /** + * @param {object|Array} data - derivatives status message data + * @param {number} data.timestamp - timestamp + * @param {string} data.price - price + * @param {string} data.priceSpot - spot price + * @param {string} data.fundBal - funding balance + * @param {number} data.fundingEventTimestamp - timestamp + * @param {string} data.fundingAccrued - accrued funding + * @param {string} data.fundingStep - funding step + * @param {number} data.currentFunding - funding applied in the current 8h period, + * @param {number} data.markprice - markprice, + * @param {number} data.openInterest - total number of outstanding derivative contracts, + * @param {number} data.clampMin - min clamp + * @param {number} data.clampMax - max clamp + */ + constructor (data = {}) { + super({ data, fields }) + } + + /** + * @param {object[]|object|Array[]|Array} data - data to convert to POJO + * @returns {object} pojo + */ + static unserialize (data) { + return super.unserialize({ data, fields }) + } + + /** + * Validates a given public trade instance + * + * @param {object[]|object|PublicTrade[]|PublicTrade|Array} data - instance to validate + * @returns {string} error - null if instance is valid + */ + static validate (data) { + return super.validate({ + data, + fields, + validators: { + timestamp: stringValidator, + price: priceValidator, + priceSpot: priceValidator, + fundBal: priceValidator, + fundingEventTimestamp: stringValidator, + fundingAccrued: priceValidator, + fundingStep: priceValidator, + currentFunding: priceValidator, + markprice: priceValidator, + openInterest: priceValidator, + clampMin: priceValidator, + clampMax: priceValidator + } + }) + } +} + +module.exports = StatusMessagesDerivWS diff --git a/test/lib/models/liquidations.js b/test/lib/models/liquidations.js index b622d39..d6292dc 100644 --- a/test/lib/models/liquidations.js +++ b/test/lib/models/liquidations.js @@ -14,7 +14,7 @@ describe('Liquidations entry model', () => { testModel({ model: Liquidations, orderedFields: [ - null, 'posId', 'mtsUpdated', null, 'symbol', 'amount', 'basePrice', null, 'isMatch', 'isMarketSold' + null, 'posId', 'mtsUpdated', null, 'symbol', 'amount', 'basePrice', null, 'isMatch', 'isMarketSold', null, 'liquidationPrice' ] }) @@ -27,7 +27,8 @@ describe('Liquidations entry model', () => { amount: new Array(...(new Array(5))).map(() => Math.random()), basePrice: new Array(...(new Array(5))).map(() => Math.random()), isMatch: new Array(...(new Array(5))).map(() => Math.random() > 0.5), - isMarketSold: new Array(...(new Array(5))).map(() => Math.random() > 0.5) + isMarketSold: new Array(...(new Array(5))).map(() => Math.random() > 0.5), + liquidationPrice: new Array(...(new Array(5))).map(() => Math.random()) } }) @@ -36,13 +37,15 @@ describe('Liquidations entry model', () => { const l = new Liquidations({ symbol: 'tBTCUSD', amount: 42, - basePrice: 0.1 + basePrice: 0.1, + liquidationPrice: 33 }) const str = l.toString() assert.ok(/BTCUSD/.test(str), 'symbol missing') assert.ok(_includes(str, '42'), 'amount missing') assert.ok(_includes(str, '0.1'), 'rate missing') + assert.ok(_includes(str, '33'), 'liquidationPrice missing') }) }) })