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

Feature/ws2 new funding functions #543

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.0.15
- WSv2: submitFundingOffer function
- WSv2: cancelFundingOffer function
- WSv2: closeFundingLoan function
- WSv2: closeFundingCredit function

4.0.14
- fix: README docs reference

Expand Down
4 changes: 2 additions & 2 deletions docs/WS2Manager.html

Large diffs are not rendered by default.

3,898 changes: 2,671 additions & 1,227 deletions docs/WSv2.html

Large diffs are not rendered by default.

240 changes: 238 additions & 2 deletions docs/global.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/index.html

Large diffs are not rendered by default.

174 changes: 163 additions & 11 deletions docs/transports_ws2.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/util_precision.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/util_ws2.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/ws2_manager.js.html

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions examples/ws2/cancel_funding_offer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { WSv2 } = require('../..')
// from 'bitfinex-api-node' module
// const { WSv2 } = require('bitfinex-api-node')

;(async () => {
const ws2 = new WSv2({
apiKey: '<your-api-key>',
apiSecret: '<your-api-secret>'
// you can obtain your keys from https://www.bitfinex.com/api
})

try {
await ws2.open()
await ws2.auth()

ws2.onFundingOfferClose({}, (resp) => {
console.log('onFundingOfferClose: ', resp)
})

ws2.cancelFundingOffer('<insert-your-funding-offer-id>')
} catch (error) {
console.log('error: ', error)
}
})()
24 changes: 24 additions & 0 deletions examples/ws2/close_funding_credit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { WSv2 } = require('../..')
// from 'bitfinex-api-node' module
// const { WSv2 } = require('bitfinex-api-node')

;(async () => {
const ws2 = new WSv2({
apiKey: '<your-api-key>',
apiSecret: '<your-api-secret>'
// you can obtain your keys from https://www.bitfinex.com/api
})

try {
await ws2.open()
await ws2.auth()

ws2.onFundingCreditClose({}, (resp) => {
console.log('onFundingCreditClose: ', resp)
})

ws2.closeFundingCredit('<insert-your-funding-credit-id>')
} catch (error) {
console.log('error: ', error)
}
})()
24 changes: 24 additions & 0 deletions examples/ws2/close_funding_loan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { WSv2 } = require('../..')
// from 'bitfinex-api-node' module
// const { WSv2 } = require('bitfinex-api-node')

;(async () => {
const ws2 = new WSv2({
apiKey: '<your-api-key>',
apiSecret: '<your-api-secret>'
// you can obtain your keys from https://www.bitfinex.com/api
})

try {
await ws2.open()
await ws2.auth()

ws2.onFundingLoanClose({}, (resp) => {
console.log('onFundingLoanClose: ', resp)
})

ws2.closeFundingLoan('<insert-your-funding-loan-id>')
} catch (error) {
console.log('error: ', error)
}
})()
32 changes: 32 additions & 0 deletions examples/ws2/submit_funding_offer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { FundingOffer } = require('bfx-api-node-models')
const { WSv2 } = require('../..')
// from 'bitfinex-api-node' module
// const { WSv2 } = require('bitfinex-api-node')

;(async () => {
const ws2 = new WSv2({
apiKey: '<your-api-key>',
apiSecret: '<your-api-secret>'
// you can obtain your keys from https://www.bitfinex.com/api
})

try {
await ws2.open()
await ws2.auth()

ws2.onFundingOfferNew({ symbol: 'fUSD' }, (resp) => {
console.log('onFundingOfferNew:', resp)
})

ws2.submitFundingOffer(new FundingOffer({
type: 'LIMIT',
symbol: 'fUSD',
amount: '-50',
rate: '0.001',
period: 2,
flags: 0
}))
} catch (error) {
console.log('error: ', error)
}
})()
158 changes: 158 additions & 0 deletions lib/transports/ws2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,164 @@ class WSv2 extends EventEmitter {
return this._getEventPromise(`order-new-${packet.cid}`)
}

/**
* @typedef {object} FundingOffer
* @property {string} type - offer type i.e. LIMIT, FRRDELTAVAR
* @property {string} symbol - symbol i.e. fUSD, fBTC
* @property {string} amount - positive for buy, negative for sell i.e. '50'
* @property {string} rate - rate of the offer
* @property {number} period - Time period of offer. Min:2, Max:30 days
* @property {number} flags - flags
*/

/**
* Sends a new funding offer to the server. Emits an error if not
* authenticated. The funding offer can be either key/value pair object
* literal, an instance of FundingOffer class or an array
*
* @see WSv2#cancelFundingOffer
* @see WSv2#closeFundingCredit
* @see WSv2#closeFundingLoan
*
* @param {FundingOffer|Array} payload - funding offer object model or array
* @throws an error if not authenticated
* @example
* const fo = new FundingOffer({
* type: 'LIMIT',
* symbol: 'fUSD',
* amount: '50',
* rate: '0.001',
* period: 2,
* flags: 0
* }, ws)
*
* ws.submitFundingOffer(fo)
*
* ws.onFundingOfferNew({ symbol: 'fUSD' }, (resp) => {
* console.log('Funding offer status: ', resp.status)
* })
*/
async submitFundingOffer (payload) {
if (!this._isAuthenticated) {
throw new Error('not authenticated')
}

const packet = (
payload instanceof FundingOffer
? payload
: new FundingOffer(payload)
).toNewOfferPacket()

if (this._affCode) {
packet.meta.aff_code = packet.meta.aff_code || this._affCode
}

this.send([0, 'fon', null, packet])
}

/**
* Cancels funding offer by ID. Emits an error if not authenticated.
* The ID can be passed as a number, key/value pair object literal,
* an instance of FundingOffer class or an array
*
* @see WSv2#submitFundingOffer
* @see WSv2#closeFundingCredit
* @see WSv2#closeFundingLoan
*
* @param {FundingOffer|object|Array|number} payload - funding offer model,
* array or ID to cancel
* @throws an error if not authenticated
* @example
* ws.cancelFundingOffer({ id: 1234 })
*
* ws.onFundingOfferClose({ symbol: 'fUSD' }, (resp) => {
* console.log('Funding offer status: ', resp.status)
* })
*/
cancelFundingOffer (payload) {
if (!this._isAuthenticated) {
throw new Error('not authenticated')
}

const id = _isNumber(payload)
? payload
: Array.isArray(payload)
? payload[0]
: payload.id

this.send([0, 'foc', null, { id }])
}

/**
* Cancels funding loan by ID. Emits an error if not authenticated.
* The ID can be passed as a number, key/value pair object literal,
* an instance of FundingLoan class or an array
*
* @see WSv2#submitFundingOffer
* @see WSv2#cancelFundingOffer
* @see WSv2#closeFundingCredit
*
* @param {FundingLoan|object|Array|number} payload - funding loan class,
* object literal, array or ID to cancel
* @throws an error if not authenticated
* @example
* ws.closeFundingLoan({ id: 1234 })
*
* ws.onFundingLoanClose({ symbol: 'fUSD' }, (resp) => {
* console.log('Funding loan status: ', resp.status)
* })
*/
closeFundingLoan (payload) {
throw new Error('coming soon') // TODO: remove after socket input is implemented
// eslint-disable-next-line
if (!this._isAuthenticated) {
throw new Error('not authenticated')
}

const id = _isNumber(payload)
? payload
: Array.isArray(payload)
? payload[0]
: payload.id

this.send([0, 'flc', null, { id }])
}

/**
* Cancels funding credit by ID. Emits an error if not authenticated.
* The ID can be passed as a number, key/value pair object literal,
* an instance of FundingCredit class or an array
*
* @see WSv2#submitFundingOffer
* @see WSv2#cancelFundingOffer
* @see WSv2#closeFundingLoan
*
* @param {FundingCredit|object|Array|number} payload - funding credit class,
* object literal, array or ID to cancel
* @throws an error if not authenticated
* @example
* ws.closeFundingCredit({ id: 1234 })
*
* ws.onFundingCreditClose({ symbol: 'fUSD' }, (resp) => {
* console.log('Funding credit status: ', resp.status)
* })
*/
closeFundingCredit (payload) {
throw new Error('coming soon') // TODO: remove after socket input is implemented
// eslint-disable-next-line
if (!this._isAuthenticated) {
throw new Error('not authenticated')
}

const id = _isNumber(payload)
? payload
: Array.isArray(payload)
? payload[0]
: payload.id

this.send([0, 'fcc', null, { id }])
}

/**
* Send a changeset to update an order in-place while maintaining position in
* the price queue. The changeset must contain the order ID, and supports a
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": "bitfinex-api-node",
"version": "4.0.14",
"version": "4.0.15",
"description": "Node reference library for Bitfinex API",
"engines": {
"node": ">=8.3.0"
Expand Down
Loading