Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the StatusMessagesDeriv model with all the available values #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
13 changes: 9 additions & 4 deletions lib/liquidations.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -16,7 +17,8 @@ const fields = {
amount: 5,
basePrice: 6,
isMatch: 8,
isMarketSold: 9
isMarketSold: 9,
liquidationPrice: 11
}

/**
Expand All @@ -32,6 +34,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 })
Expand All @@ -50,15 +53,16 @@ class Liquidations extends Model {
*/
toString () {
const {
mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold
mtsUpdated, symbol, amount, basePrice, isMatch, isMarketSold, liquidationPrice
} = this

return _compact(_flatten([
new Date(mtsUpdated).toLocaleString(),
symbol,
[amount, '@', basePrice],
isMatch && 'matched',
isMarketSold && 'sold'
isMarketSold && 'sold',
['liq', preparePrice(liquidationPrice),]
])).join(' ')
}

Expand All @@ -79,7 +83,8 @@ class Liquidations extends Model {
amount: amountValidator,
basePrice: priceValidator,
isMatch: boolValidator,
isMarketSold: boolValidator
isMarketSold: boolValidator,
liquidationPrice: numberValidator
}
})
}
Expand Down
12 changes: 12 additions & 0 deletions lib/status_messages_deriv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
}
Expand Down
81 changes: 81 additions & 0 deletions lib/ws_status_messages_deriv.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 6 additions & 3 deletions test/lib/models/liquidations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
})

Expand All @@ -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())
}
})

Expand All @@ -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')
})
})
})
9 changes: 7 additions & 2 deletions test/lib/models/status_messages_deriv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
})

Expand All @@ -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)
}
Expand Down