Skip to content

Commit

Permalink
Merge pull request #78 from vitaliystoliarovcc/fix/public-trade-snapshot
Browse files Browse the repository at this point in the history
Fix/public trade snapshot
  • Loading branch information
vigan-abd authored Feb 22, 2022
2 parents d8b5f17 + 7e720ef commit 04f8631
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#1.6.3
- fix: public trade unserializing from snapshot

#1.6.2
- feature: visible on hit option supported for hidden orders

Expand Down
2 changes: 1 addition & 1 deletion lib/public_trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PublicTrade extends Model {
* @returns {object} pojo
*/
static unserialize (data) {
if ((_isArray(data[0]) && data[0].length === 5) || (data.length === 5)) {
if ((_isArray(data[0]) && data[0].length === 5) || (_isObject(data[0]) && data[0].rate) || data.length === 5) {
return super.unserialize({ data, fields: FUNDING_FIELDS })
} else {
return super.unserialize({ data, fields: TRADING_FIELDS })
Expand Down
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.6.2",
"version": "1.6.3",
"description": "Object models for usage with the Bitfinex node API",
"engines": {
"node": ">=8.3.0"
Expand Down
39 changes: 39 additions & 0 deletions test/lib/models/public_trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const { PublicTrade } = require('../../../lib')
const testModel = require('../../helpers/test_model')
const testModelValidation = require('../../helpers/test_model_validation')

const snapshot = [
[267951933, 1645117845846, -5862.1, 0.002, 2],
[267947377, 1645115432454, -83763.1, 0.003, 3]
]

describe('Public Trade model', () => {
testModel({
model: PublicTrade,
Expand Down Expand Up @@ -64,6 +69,40 @@ describe('Public Trade model', () => {
})
}).timeout(60000)

it('unserializes snapshot', async () => {
const serialized = PublicTrade.unserialize(snapshot)

serialized.forEach((item, i) => {
Object.keys(PublicTrade.FUNDING_FIELDS).forEach(field => {
assert.strictEqual(item[field], snapshot[i][PublicTrade.FUNDING_FIELDS[field]])
})
})
}).timeout(60000)

it('creates instance from unserialized snapshot ', async () => {
const trades = new PublicTrade(PublicTrade.unserialize(snapshot))

for (let i = 0; i < trades.length; i++) {
const item = trades[i]

Object.keys(PublicTrade.FUNDING_FIELDS).forEach(field => {
assert.strictEqual(item[field], snapshot[i][PublicTrade.FUNDING_FIELDS[field]])
})
}
}).timeout(60000)

it('creates instance from snapshot', async () => {
const trades = new PublicTrade(snapshot)

for (let i = 0; i < trades.length; i++) {
const item = trades[i]

Object.keys(PublicTrade.FUNDING_FIELDS).forEach(field => {
assert.strictEqual(item[field], snapshot[i][PublicTrade.FUNDING_FIELDS[field]])
})
}
}).timeout(60000)

describe('toString', () => {
it('includes pertinent information', () => {
const t = new PublicTrade({
Expand Down

0 comments on commit 04f8631

Please sign in to comment.