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

Feat: add fees endpoint #37

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion example/testApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<APIKEY>',
APISECRET: '<APISECRET>',
ACCESSWINDOW: 10000,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -509,6 +526,7 @@ let testRestApi = async () => {
// testTrades()

// testAccount()
// testFees()
// testBalance()
// testDepositAssets()
// testWithdrawAssets()
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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', {})
Expand Down
79 changes: 51 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
})
}
}
Expand Down Expand Up @@ -234,6 +231,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
Expand Down Expand Up @@ -619,6 +619,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)
Expand Down Expand Up @@ -758,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 = {}) {
Expand All @@ -774,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)
Expand All @@ -785,64 +799,73 @@ 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 = '') {
await this.checkSocket()
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
getOrders: async function (market = '', options = {}) {
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
trades: async function (market = '', options = {}) {
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()
options = { action: 'privateGetAccount' }
doSendPrivate(this.websocket, JSON.stringify(options))
const options = { action: 'privateGetAccount' }
doSendPrivate.call(this, JSON.stringify(options))
},

fees: async function (market = '') {
await this.checkSocket()
const options = { action: 'privateGetFees' }
if (market) {
options.market = market
}
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
Expand All @@ -852,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) {
Expand All @@ -893,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] }] }),
)
},
Expand Down Expand Up @@ -953,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] }] }),
)
},
Expand Down