bfx-api-node-models
Advanced tools
Comparing version 1.0.3 to 1.0.5
@@ -204,10 +204,2 @@ 'use strict' | ||
} | ||
// snapshots may not be sorted | ||
const priceI = this.raw | ||
? snapshot[0].length === 4 ? 2 : 1 | ||
: 0 | ||
this.bids.sort((a, b) => b[priceI] - a[priceI]) | ||
this.asks.sort((a, b) => a[priceI] - b[priceI]) | ||
} | ||
@@ -427,2 +419,6 @@ | ||
static updateArrayOBWith (ob, entry, raw = false) { | ||
if (entry.length === 0) { | ||
return false | ||
} | ||
const priceI = raw | ||
@@ -429,0 +425,0 @@ ? entry.length === 4 ? 2 : 1 |
'use strict' | ||
const _isArray = require('lodash/isArray') | ||
const _isObject = require('lodash/isObject') | ||
const Model = require('./model') | ||
const BOOL_FIELDS = [] | ||
const FIELDS = { | ||
const TRADING_FIELDS = { | ||
id: 0, | ||
@@ -12,11 +14,38 @@ mts: 1, | ||
const FIELD_KEYS = Object.keys(FIELDS) | ||
const FUNDING_FIELDS = { | ||
id: 0, | ||
mts: 1, | ||
amount: 2, | ||
rate: 3, | ||
period: 4 | ||
} | ||
const TRADING_FIELD_KEYS = Object.keys(TRADING_FIELDS) | ||
const FUNDING_FIELD_KEYS = Object.keys(FUNDING_FIELDS) | ||
class PublicTrade extends Model { | ||
constructor (data = {}) { | ||
super(data, FIELDS, BOOL_FIELDS, FIELD_KEYS) | ||
if (_isArray(data)) { | ||
if (data.length === 5) { | ||
super(data, FUNDING_FIELDS, BOOL_FIELDS, FUNDING_FIELD_KEYS) | ||
} else { | ||
super(data, TRADING_FIELDS, BOOL_FIELDS, TRADING_FIELD_KEYS) | ||
} | ||
} else if (_isObject(data)) { | ||
if (data.rate) { | ||
super(data, FUNDING_FIELDS, BOOL_FIELDS, FUNDING_FIELD_KEYS) | ||
} else { | ||
super(data, TRADING_FIELDS, BOOL_FIELDS, TRADING_FIELD_KEYS) | ||
} | ||
} else { | ||
throw new Error('unknown data type') | ||
} | ||
} | ||
static unserialize (arr) { | ||
return super.unserialize(arr, FIELDS, BOOL_FIELDS, FIELD_KEYS) | ||
if ((_isArray(arr[0]) && arr[0].length === 5) || (arr.length === 5)) { | ||
return super.unserialize(arr, FUNDING_FIELDS, BOOL_FIELDS, FUNDING_FIELD_KEYS) | ||
} else { | ||
return super.unserialize(arr, TRADING_FIELDS, BOOL_FIELDS, TRADING_FIELD_KEYS) | ||
} | ||
} | ||
@@ -26,1 +55,3 @@ } | ||
module.exports = PublicTrade | ||
module.exports.TRADING_FIELDS = TRADING_FIELDS | ||
module.exports.FUNDING_FIELDS = FUNDING_FIELDS |
{ | ||
"name": "bfx-api-node-models", | ||
"version": "1.0.3", | ||
"version": "1.0.5", | ||
"description": "Object models for usage with the Bitfinex node API", | ||
@@ -36,2 +36,3 @@ "engines": { | ||
"devDependencies": { | ||
"bfx-api-node-rest": "^1.0.0", | ||
"chai": "^3.4.1", | ||
@@ -38,0 +39,0 @@ "debug": "^3.1.0", |
/* eslint-env mocha */ | ||
'use strict' | ||
const assert = require('assert') | ||
const { RESTv2 } = require('bfx-api-node-rest') | ||
const { Candle } = require('../../../lib') | ||
@@ -12,2 +14,18 @@ const testModel = require('../../helpers/test_model') | ||
}) | ||
it('unserializes live data correctly', async () => { | ||
const rest = new RESTv2() | ||
const arr = await rest.candles('tBTCUSD') | ||
arr.forEach(candle => { | ||
const obj = Candle.unserialize(candle) | ||
assert.equal(obj.mts, candle[0]) | ||
assert.equal(obj.open, candle[1]) | ||
assert.equal(obj.close, candle[2]) | ||
assert.equal(obj.high, candle[3]) | ||
assert.equal(obj.low, candle[4]) | ||
assert.equal(obj.volume, candle[5]) | ||
}) | ||
}) | ||
}) |
@@ -5,2 +5,3 @@ /* eslint-env mocha */ | ||
const assert = require('assert') | ||
const { RESTv2 } = require('bfx-api-node-rest') | ||
const { FundingTicker } = require('../../../lib') | ||
@@ -67,2 +68,22 @@ | ||
}) | ||
it('unserializes live data correctly', async () => { | ||
const rest = new RESTv2() | ||
const arr = await rest.ticker('fUSD') | ||
const obj = FundingTicker.unserialize(arr) | ||
assert.equal(obj.frr, arr[0]) | ||
assert.equal(obj.bid, arr[1]) | ||
assert.equal(obj.bidPeriod, arr[2]) | ||
assert.equal(obj.bidSize, arr[3]) | ||
assert.equal(obj.ask, arr[4]) | ||
assert.equal(obj.askPeriod, arr[5]) | ||
assert.equal(obj.askSize, arr[6]) | ||
assert.equal(obj.dailyChange, arr[7]) | ||
assert.equal(obj.dailyChangePerc, arr[8]) | ||
assert.equal(obj.lastPrice, arr[9]) | ||
assert.equal(obj.volume, arr[10]) | ||
assert.equal(obj.high, arr[11]) | ||
assert.equal(obj.low, arr[12]) | ||
}) | ||
}) |
@@ -6,2 +6,3 @@ /* eslint-env mocha */ | ||
const CRC = require('crc-32') | ||
const { RESTv2 } = require('bfx-api-node-rest') | ||
const { OrderBook } = require('../../../lib') | ||
@@ -24,6 +25,6 @@ | ||
const ob = new OrderBook([ | ||
[149, 1, 10], | ||
[148, 1, 10], | ||
[145, 1, 10], | ||
[140, 1, 10], | ||
[145, 1, 10], | ||
[148, 1, 10], | ||
[149, 1, 10], | ||
[151, 1, -10], | ||
@@ -41,6 +42,6 @@ [152, 1, -10], | ||
const ob = new OrderBook([ | ||
[149, 1, 10], | ||
[148, 1, 10], | ||
[145, 1, 10], | ||
[140, 1, 10], | ||
[145, 1, 10], | ||
[148, 1, 10], | ||
[149, 1, 10], | ||
[151, 1, -10], | ||
@@ -748,2 +749,50 @@ [152, 1, -10], | ||
}) | ||
it('unserializes live trading data correctly', async () => { | ||
const rest = new RESTv2() | ||
const book = await rest.orderBook('tBTCUSD', 'P0') | ||
const obj = new OrderBook(book) | ||
let firstAsk = -1 | ||
book.forEach((entry, i) => { | ||
if (entry[2] < 0) { | ||
if (firstAsk === -1) { | ||
firstAsk = i | ||
} | ||
assert.equal(obj.asks[i - firstAsk][0], entry[0]) | ||
assert.equal(obj.asks[i - firstAsk][1], entry[1]) | ||
assert.equal(obj.asks[i - firstAsk][2], entry[2]) | ||
} else { | ||
assert.equal(obj.bids[i][0], entry[0]) | ||
assert.equal(obj.bids[i][1], entry[1]) | ||
assert.equal(obj.bids[i][2], entry[2]) | ||
} | ||
}) | ||
}) | ||
it('unserializes live funding data correctly', async () => { | ||
const rest = new RESTv2() | ||
const book = await rest.orderBook('fUSD', 'P0') | ||
const obj = new OrderBook(book) | ||
let firstAsk = -1 | ||
book.forEach((entry, i) => { | ||
if (entry[3] > 0) { | ||
if (firstAsk === -1) { | ||
firstAsk = i | ||
} | ||
assert.equal(obj.asks[i - firstAsk][0], entry[0]) | ||
assert.equal(obj.asks[i - firstAsk][1], entry[1]) | ||
assert.equal(obj.asks[i - firstAsk][2], entry[2]) | ||
assert.equal(obj.asks[i - firstAsk][3], entry[3]) | ||
} else { | ||
assert.equal(obj.bids[i][0], entry[0]) | ||
assert.equal(obj.bids[i][1], entry[1]) | ||
assert.equal(obj.bids[i][2], entry[2]) | ||
assert.equal(obj.bids[i][3], entry[3]) | ||
} | ||
}) | ||
}) | ||
}) |
/* eslint-env mocha */ | ||
'use strict' | ||
const assert = require('assert') | ||
const { RESTv2 } = require('bfx-api-node-rest') | ||
const { PublicTrade } = require('../../../lib') | ||
@@ -14,2 +16,28 @@ const testModel = require('../../helpers/test_model') | ||
}) | ||
it('unserializes live trading data correctly', async () => { | ||
const rest = new RESTv2() | ||
const arr = await rest.trades('tBTCUSD') | ||
arr.forEach(trade => { | ||
const obj = PublicTrade.unserialize(trade) | ||
Object.keys(PublicTrade.TRADING_FIELDS).forEach(field => { | ||
assert.equal(obj[field], trade[PublicTrade.TRADING_FIELDS[field]]) | ||
}) | ||
}) | ||
}) | ||
it('unserializes live funding data correctly', async () => { | ||
const rest = new RESTv2() | ||
const arr = await rest.trades('fUSD') | ||
arr.forEach(trade => { | ||
const obj = PublicTrade.unserialize(trade) | ||
Object.keys(PublicTrade.FUNDING_FIELDS).forEach(field => { | ||
assert.equal(obj[field], trade[PublicTrade.FUNDING_FIELDS[field]]) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -5,2 +5,3 @@ /* eslint-env mocha */ | ||
const assert = require('assert') | ||
const { RESTv2 } = require('bfx-api-node-rest') | ||
const { TradingTicker } = require('../../../lib') | ||
@@ -58,2 +59,19 @@ | ||
}) | ||
it('unserializes live data correctly', async () => { | ||
const rest = new RESTv2() | ||
const arr = await rest.ticker('tBTCUSD') | ||
const obj = TradingTicker.unserialize(arr) | ||
assert.equal(obj.bid, arr[0]) | ||
assert.equal(obj.bidSize, arr[1]) | ||
assert.equal(obj.ask, arr[2]) | ||
assert.equal(obj.askSize, arr[3]) | ||
assert.equal(obj.dailyChange, arr[4]) | ||
assert.equal(obj.dailyChangePerc, arr[5]) | ||
assert.equal(obj.lastPrice, arr[6]) | ||
assert.equal(obj.volume, arr[7]) | ||
assert.equal(obj.high, arr[8]) | ||
assert.equal(obj.low, arr[9]) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92574
3108
5