bfx-api-node-models
Advanced tools
Comparing version 1.0.13 to 1.1.0
@@ -5,2 +5,3 @@ 'use strict' | ||
const BOOL_FIELDS = ['notify', 'hidden', 'renew'] | ||
const { prepareAmount } = require('bfx-api-node-util') | ||
const FIELDS = { | ||
@@ -27,4 +28,9 @@ id: 0, | ||
class FundingOffer extends Model { | ||
constructor (data = {}) { | ||
/** | ||
* @param {Object|Array} data - either a map of order fields or a raw array | ||
* @param {Object} _apiInterface - optional, rest or websocket object capable of submitting funding offers | ||
*/ | ||
constructor (data = {}, apiInterface) { | ||
super(data, FIELDS, BOOL_FIELDS, FIELD_KEYS) | ||
this._apiInterface = apiInterface | ||
} | ||
@@ -35,2 +41,58 @@ | ||
} | ||
/** | ||
* Creates an order map that can be used in either the websocket `on` | ||
* command or a rest request body | ||
* | ||
* @return {Object} o | ||
*/ | ||
toNewOfferPacket () { | ||
return { | ||
type: this.type, | ||
symbol: this.symbol, | ||
amount: prepareAmount(+this.amount), | ||
rate: prepareAmount(+this.rate), | ||
period: this.period, | ||
flags: this.flags | ||
} | ||
} | ||
/** | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
submit (apiInterface = this._apiInterface ) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
return apiInterface.submitFundingOffer(this).then((offerArray) => { | ||
Object.assign(this, FundingOffer.unserialize(offerArray)) | ||
return this | ||
}) | ||
} | ||
/** | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
cancel (apiInterface = this._apiInterface ) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
return apiInterface.cancelFundingOffer(this.id).then((offerArray) => { | ||
Object.assign(this, FundingOffer.unserialize(offerArray)) | ||
return this | ||
}) | ||
} | ||
/** | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
close (apiInterface = this._apiInterface ) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
return apiInterface.closeFunding({ id: this.id, type: this.type }).then((offerArray) => { | ||
Object.assign(this, FundingOffer.unserialize(offerArray)) | ||
return this | ||
}) | ||
} | ||
} | ||
@@ -37,0 +99,0 @@ |
@@ -38,3 +38,3 @@ 'use strict' | ||
* High level order model; provides methods for execution & can stay updated via | ||
* a WSv2 connection | ||
* a WSv2 connection or used to execute as a rest payload | ||
*/ | ||
@@ -44,5 +44,5 @@ class Order extends Model { | ||
* @param {Object|Array} data - either a map of order fields or a raw array | ||
* @param {WSv2} ws - optional, saved for a later call to registerListeners() | ||
* @param {Object} apiInterface - optional, saved for a later call to registerListeners() | ||
*/ | ||
constructor (data = {}, ws) { | ||
constructor (data = {}, apiInterface) { | ||
super(data, FIELDS, BOOL_FIELDS, FIELD_KEYS) | ||
@@ -56,3 +56,3 @@ | ||
this._ws = ws | ||
this._apiInterface = apiInterface | ||
@@ -176,6 +176,6 @@ this._onWSOrderNew = this._onWSOrderNew.bind(this) | ||
* @param {Object} changes | ||
* @param {WSv2} ws - optional, defaults to internal instance | ||
* @return {Promise} p - resolves on ws2 confirmation, or rejects if no ws2 | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal instance | ||
* @return {Promise} p - resolves on ws2 confirmation or rest response | ||
*/ | ||
update (changes = {}, ws = this._ws) { | ||
update (changes = {}, apiInterface = this._apiInterface) { | ||
const keys = Object.keys(changes) | ||
@@ -216,4 +216,4 @@ | ||
return ws | ||
? ws.updateOrder(changes) | ||
return apiInterface | ||
? apiInterface.updateOrder(changes) | ||
: Promise.reject(new Error('no ws client available')) | ||
@@ -241,6 +241,6 @@ } | ||
* | ||
* @param {WSv2} ws - optional, defaults to internal ws | ||
* @param {Object} apiInterface - optional, defaults to internal ws | ||
*/ | ||
registerListeners (ws = this._ws) { | ||
if (!ws) return | ||
registerListeners (apiInterface = this._apiInterface) { | ||
if (!apiInterface) return | ||
@@ -256,16 +256,17 @@ const chanData = { | ||
ws.onOrderNew(chanData, this._onWSOrderNew) | ||
ws.onOrderUpdate(chanData, this._onWSOrderUpdate) | ||
ws.onOrderClose(chanData, this._onWSOrderClose) | ||
apiInterface.onOrderNew(chanData, this._onWSOrderNew) | ||
apiInterface.onOrderUpdate(chanData, this._onWSOrderUpdate) | ||
apiInterface.onOrderClose(chanData, this._onWSOrderClose) | ||
this._ws = ws | ||
this._apiInterface = apiInterface | ||
} | ||
/** | ||
* Removes update listeners from the specified ws2 instance | ||
* Removes update listeners from the specified ws2 instance. | ||
* Will fail if rest interface is provided. | ||
* | ||
* @param {WSv2} ws - optional, defaults to internal ws | ||
* @param {WSv2|Rest2} apiInterface - optional ws defaults to internal ws | ||
*/ | ||
removeListeners (ws = this._ws) { | ||
if (ws) ws.removeListeners(this.cbGID()) | ||
removeListeners (apiInterface = this._apiInterface) { | ||
if (apiInterface) apiInterface.removeListeners(this.cbGID()) | ||
} | ||
@@ -281,9 +282,9 @@ | ||
/** | ||
* @param {WSv2} ws - optional, defaults to internal ws | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
submit (ws = this._ws) { | ||
if (!ws) return Promise.reject(new Error('no ws connection')) | ||
submit (apiInterface = this._apiInterface) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
return ws.submitOrder(this).then((orderArr) => { | ||
return apiInterface.submitOrder(this).then((orderArr) => { | ||
Object.assign(this, Order.unserialize(orderArr)) | ||
@@ -295,10 +296,10 @@ return this | ||
/** | ||
* @param {WSv2} ws - optional, defaults to internal ws | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
cancel (ws = this._ws) { | ||
if (!ws) return Promise.reject(new Error('no ws connection')) | ||
cancel (apiInterface = this._apiInterface) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
if (!this.id) return Promise.reject(new Error('order has no ID')) | ||
return ws.cancelOrder(this.id) | ||
return apiInterface.cancelOrder(this.id) | ||
} | ||
@@ -309,13 +310,13 @@ | ||
* | ||
* @param {WSv2} ws - optional, defaults to internal ws | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
recreate (ws = this._ws) { | ||
if (!ws) return Promise.reject(new Error('no ws connection')) | ||
recreate (apiInterface = this._apiInterface) { | ||
if (!apiInterface) return Promise.reject(new Error('no API interface provided')) | ||
if (!this.id) return Promise.reject(new Error('order has no ID')) | ||
return this.cancel(ws).then(() => { | ||
return this.cancel(apiInterface).then(() => { | ||
this.id = null | ||
return this.submit(ws) | ||
return this.submit(apiInterface) | ||
}) | ||
@@ -322,0 +323,0 @@ } |
@@ -29,4 +29,9 @@ 'use strict' | ||
class Position extends Model { | ||
constructor (data = {}) { | ||
/** | ||
* @param {Object} data - either a map of order fields or a raw array | ||
* @param {WSv2|Rest2} apiInterface - optional, rest or websocket object thats capable of submitting position changes | ||
*/ | ||
constructor (data = {}, apiInterface) { | ||
super(data, FIELDS, BOOL_FIELDS, FIELD_KEYS) | ||
this._apiInterface = apiInterface | ||
} | ||
@@ -37,2 +42,16 @@ | ||
} | ||
/** | ||
* @param {WSv2|Rest2} apiInterface - optional ws or rest, defaults to internal ws | ||
* @return {Promise} p | ||
*/ | ||
claim (apiInterface = this._apiInterface) { | ||
if (!apiInterface) return Promise.reject(new Error('no claim handler')) | ||
apiInterface.claimPosition(this.id) | ||
.then((positionArray) => { | ||
Object.assign(this, Position.unserialize(positionArray)) | ||
return this | ||
}) | ||
} | ||
} | ||
@@ -39,0 +58,0 @@ |
{ | ||
"name": "bfx-api-node-models", | ||
"version": "1.0.13", | ||
"version": "1.1.0", | ||
"description": "Object models for usage with the Bitfinex node API", | ||
@@ -5,0 +5,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
109584
3548
0