From f1925a75a45440584e1193dbb43ea7cf53db3455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20M=C3=B6llers?= Date: Fri, 16 Feb 2024 15:53:09 +0100 Subject: [PATCH 1/2] feat: add fees endpoint --- example/testApi.js | 25 ++++++++++++++++++++++++- lib/index.js | 28 +++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/example/testApi.js b/example/testApi.js index 127a6d6..3d839d0 100644 --- a/example/testApi.js +++ b/example/testApi.js @@ -6,7 +6,7 @@ * Bitvavo: https://bitvavo.com * README: https://github.com/bitvavo/node-bitvavo-api */ -const bitvavo = require('bitvavo')().options({ +const bitvavo = require('../lib')().options({ APIKEY: '', APISECRET: '', ACCESSWINDOW: 10000, @@ -388,6 +388,23 @@ let testAccount = async () => { } } +let testFees = async () => { + bitvavo.fees((error, response) => { + if (error == null) { + console.log(response) + } else { + console.log(error) + } + }) + + try { + let response = await bitvavo.fees() + console.log(response) + } catch (error) { + console.log(error) + } +} + let testBalance = async () => { bitvavo.balance({}, (error, response) => { if (error == null) { @@ -509,6 +526,7 @@ let testRestApi = async () => { // testTrades() // testAccount() + // testFees() // testBalance() // testDepositAssets() // testWithdrawAssets() @@ -573,6 +591,9 @@ let websocketSetListeners = async () => { emitter.on('account', (response) => { console.log('AccountResponse', response) }) + emitter.on('fees', (response) => { + console.log('FeesResponse', response) + }) emitter.on('balance', (response) => { console.log('BalanceResponse', response) }) @@ -616,6 +637,8 @@ let testWebSockets = async () => { // bitvavo.websocket.trades('BTC-EUR', {}) // bitvavo.websocket.account() + // bitvavo.websocket.fees() + // bitvavo.websocket.fees('BTC-EUR') // bitvavo.websocket.balance({}) // bitvavo.websocket.depositAssets('BTC') // bitvavo.websocket.withdrawAssets('BTC', '1', 'BitcoinAddress', {}) diff --git a/lib/index.js b/lib/index.js index 47f43e2..e4a9f18 100644 --- a/lib/index.js +++ b/lib/index.js @@ -234,6 +234,9 @@ let api = function Bitvavo() { case 'privateGetAccount': emitter.emit('account', response.response) break + case 'privateGetFees': + emitter.emit('fees', response.response) + break case 'privateGetBalance': emitter.emit('balance', response.response) break @@ -619,6 +622,20 @@ let api = function Bitvavo() { return request(callback, 'GET', '/account') }, + fees: function (market = '', callback = false) { + if (typeof market === 'function') { + callback = market + market = '' + } + + const query = {} + if (market) { + query.market = market + } + + return request(callback, 'GET', '/account/fees', query) + }, + // options: symbol balance: function (options = {}, callback = false) { return request(callback, 'GET', '/balance', options) @@ -828,7 +845,16 @@ let api = function Bitvavo() { account: async function () { await this.checkSocket() - options = { action: 'privateGetAccount' } + const options = { action: 'privateGetAccount' } + doSendPrivate(this.websocket, JSON.stringify(options)) + }, + + fees: async function (market = '') { + await this.checkSocket() + const options = { action: 'privateGetFees' } + if (market) { + options.market = market + } doSendPrivate(this.websocket, JSON.stringify(options)) }, From 8042c27f351da8c1e7c020576e1bb4871c47a9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20M=C3=B6llers?= Date: Fri, 16 Feb 2024 16:10:22 +0100 Subject: [PATCH 2/2] fix: wait for authentication on WebSocket --- lib/index.js | 53 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/lib/index.js b/lib/index.js index e4a9f18..ceb413a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,12 +51,9 @@ let api = function Bitvavo() { } const waitForSocketPrivate = function (socket, callback) { - let authentication = this.authenticated setTimeout(() => { - if (authentication) { - if (callback !== null) { - callback() - } + if (socket.authenticated) { + callback() } else { waitForSocketPrivate(socket, callback) } @@ -68,18 +65,18 @@ let api = function Bitvavo() { ws.send(message) } - const doSendPrivate = function (ws, message) { + const doSendPrivate = function (message) { if (apiKey === '') { errorToConsole('You did not set the API key, but requested a private function.') return } if (this.authenticated) { debugToConsole('SENT: ' + message) - ws.send(message) + this.websocket.send(message) } else { - waitForSocketPrivate(ws, () => { + waitForSocketPrivate(this, () => { debugToConsole('SENT: ' + message) - ws.send(message) + this.websocket.send(message) }) } } @@ -775,7 +772,7 @@ let api = function Bitvavo() { body.market = market body.side = side body.orderType = orderType - doSendPrivate(this.websocket, JSON.stringify(body)) + doSendPrivate.call(this, JSON.stringify(body)) }, getOrder: async function (market = '', options = {}) { @@ -791,7 +788,7 @@ let api = function Bitvavo() { orderId: options.orderId, clientOrderId: options.clientOrderId, } - doSendPrivate(this.websocket, JSON.stringify(message)) + doSendPrivate.call(this, JSON.stringify(message)) }, // Optional body parameters: limit:(amount, amountRemaining, price, timeInForce, selfTradePrevention, postOnly) @@ -802,7 +799,7 @@ let api = function Bitvavo() { body.action = 'privateUpdateOrder' body.market = market body.orderId = orderId - doSendPrivate(this.websocket, JSON.stringify(body)) + doSendPrivate.call(this, JSON.stringify(body)) }, cancelOrder: async function (market = '', orderId = '') { @@ -810,7 +807,7 @@ let api = function Bitvavo() { let options = { 'action': 'privateCancelOrder' } options.market = market options.orderId = orderId - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: limit, start, end, orderIdFrom, orderIdTo @@ -818,21 +815,21 @@ let api = function Bitvavo() { await this.checkSocket() options.action = 'privateGetOrders' options.market = market - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: market cancelOrders: async function (options = {}) { await this.checkSocket() options.action = 'privateCancelOrders' - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: market ordersOpen: async function (options = {}) { await this.checkSocket() options.action = 'privateGetOrdersOpen' - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: limit, start, end, tradeIdFrom, tradeIdTo @@ -840,13 +837,13 @@ let api = function Bitvavo() { await this.checkSocket() options.action = 'privateGetTrades' options.market = market - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, account: async function () { await this.checkSocket() const options = { action: 'privateGetAccount' } - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, fees: async function (market = '') { @@ -855,20 +852,20 @@ let api = function Bitvavo() { if (market) { options.market = market } - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: symbol balance: async function (options = {}) { await this.checkSocket() options.action = 'privateGetBalance' - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, depositAssets: async function (symbol = '') { await this.checkSocket() let options = { 'action': 'privateDepositAssets', 'symbol': symbol } - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // Optional body parameters: paymentId, internal, addWithdrawalFee @@ -878,21 +875,21 @@ let api = function Bitvavo() { body.symbol = symbol body.amount = amount body.address = address - doSendPrivate(this.websocket, JSON.stringify(body)) + doSendPrivate.call(this, JSON.stringify(body)) }, // options: symbol, limit, start, end depositHistory: async function (options = {}) { await this.checkSocket() options.action = 'privateGetDepositHistory' - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, // options: symbol, limit, start, end withdrawalHistory: async function (options = {}) { await this.checkSocket() options.action = 'privateGetWithdrawalHistory' - doSendPrivate(this.websocket, JSON.stringify(options)) + doSendPrivate.call(this, JSON.stringify(options)) }, subscriptionTicker: async function (market = '', callback) { @@ -919,8 +916,8 @@ let api = function Bitvavo() { if (typeof subscriptionAccountCallback === 'undefined') subscriptionAccountCallback = {} subscriptionAccountCallback[market] = callback await this.checkSocket() - doSendPrivate( - this.websocket, + doSendPrivate.call( + this, JSON.stringify({ 'action': 'subscribe', 'channels': [{ 'name': 'account', 'markets': [market] }] }), ) }, @@ -979,8 +976,8 @@ let api = function Bitvavo() { 'Specify which channel you want to unsubscribe from (such as: ticker, ticker24h, candles, trades, account and book.)', ) await this.checkSocket() - doSendPrivate( - this.websocket, + doSendPrivate.call( + this, JSON.stringify({ 'action': 'unsubscribe', 'channels': [{ 'name': name, 'markets': [market] }] }), ) },