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

API updates (getTransactions, getFee, getTradesList) #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions lib/BitX.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ BitX.prototype.getTrades = function (options, callback) {
options = null
}
var defaults = {
pair: this.pair
pair: this.pair,
since: 0
}
this._request('GET', 'trades', extend(defaults, options), callback)
}
Expand Down Expand Up @@ -218,17 +219,16 @@ BitX.prototype.createFundingAddress = function (asset, callback) {
this._request('POST', 'funding_address', {asset: asset}, callback)
}

BitX.prototype.getTransactions = function (asset, options, callback) {
BitX.prototype.getTransactions = function (id, options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}
var defaults = {
asset: asset,
offset: 0,
limit: 10
min_row: 1,
max_row: 1000
}
this._request('GET', 'transactions', extend(defaults, options), callback)
this._request('GET', 'accounts/' + id + '/transactions', extend(defaults, options), callback)
}

BitX.prototype.getWithdrawals = function (callback) {
Expand All @@ -251,4 +251,28 @@ BitX.prototype.cancelWithdrawal = function (id, callback) {
this._request('DELETE', 'withdrawals/' + id, null, callback)
}

BitX.prototype.getFee = function (options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}
var defaults = {
pair: this.pair
}
this._request('GET', 'fee_info', extend(defaults, options), callback)
}

BitX.prototype.getTradesList = function (since, limit, callback) {
if (typeof limit === 'function') {
callback = limit
limit = null
}
var defaults = {
pair: this.pair,
since: since,
limit: limit || 100
}
this._request('GET', 'listtrades', defaults, callback)
}

module.exports = BitX
112 changes: 106 additions & 6 deletions test/IntegrationTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ tap.test('getTrades should return the expected trades', function (t) {

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/trades?pair=XBTZAR')
t.equal(req.url, '/api/1/trades?pair=XBTZAR&since=0')
res.end(JSON.stringify(expectedTrades))
})

Expand Down Expand Up @@ -587,7 +587,7 @@ tap.test('getTransactions should return the transactions', function (t) {

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/transactions?asset=XBT&offset=0&limit=10')
t.equal(req.url, '/api/1/accounts/XBT/transactions?min_row=1&max_row=1000')
res.end(JSON.stringify(expectedTransactions))
})

Expand Down Expand Up @@ -625,15 +625,15 @@ tap.test('getTransactions should send options and return the transactions', func

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/transactions?asset=XBT&offset=5&limit=5')
t.equal(req.url, '/api/1/accounts/99999999/transactions?min_row=1&max_row=5')
res.end(JSON.stringify(expectedTransactions))
})

var options = {
offset: 5,
limit: 5
min_row: 1,
max_row: 5
}
bitx.getTransactions('XBT', options, function (err, transactions) {
bitx.getTransactions('99999999', options, function (err, transactions) {
t.ifErr(err)
t.deepEqual(transactions, expectedTransactions)
t.end()
Expand Down Expand Up @@ -732,3 +732,103 @@ tap.test('cancelWithdrawal should delete the specified withdrawal', function (t)
t.end()
})
})

tap.test('getFee should return the fee', function (t) {
var expectedFee = {
thirty_day_volume: '0.00',
maker_fee: '0.00',
taker_fee: '0.01'
}

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/fee_info?pair=XBTZAR')
res.end(JSON.stringify(expectedFee))
})

bitx.getFee(function (err, res) {
t.ifErr(err)
t.deepEqual(res, expectedFee)
t.end()
})
})

tap.test('getFee should send options and return the fee', function (t) {
var expectedFee = {
thirty_day_volume: '0.00',
maker_fee: '0.00',
taker_fee: '0.01'
}

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/fee_info?pair=XBTZAR')
res.end(JSON.stringify(expectedFee))
})

var options = {
pair: 'XBTZAR'
}

bitx.getFee(options, function (err, res) {
t.ifErr(err)
t.deepEqual(res, expectedFee)
t.end()
})
})

tap.test('getTradesList should return the trades list', function (t) {
var expectedTradesList = {
pair: 'XBTZAR',
order_id: 'BXKDDK2ZVABCDEF',
type: 'ASK',
timestamp: 1511115163537,
price: '34500.00',
volume: '0.0666',
base: '0.0666',
counter: '2000.00',
fee_base: '0.00',
fee_counter: '0.00',
is_buy: true
}

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/listtrades?pair=XBTZAR&since=0&limit=100')
res.end(JSON.stringify(expectedTradesList))
})

bitx.getTradesList(0, function (err, res) {
t.ifErr(err)
t.deepEqual(res, expectedTradesList)
t.end()
})
})

tap.test('getTradesList should send options and return the trades list', function (t) {
var expectedTradesList = {
pair: 'XBTZAR',
order_id: 'BXKDDK2ZVABCDEF',
type: 'ASK',
timestamp: 1511115163537,
price: '34500.00',
volume: '0.0666',
base: '0.0666',
counter: '2000.00',
fee_base: '0.00',
fee_counter: '0.00',
is_buy: true
}

server.on('request', function (req, res) {
t.equal(req.method, 'GET')
t.equal(req.url, '/api/1/listtrades?pair=XBTZAR&since=1000&limit=10')
res.end(JSON.stringify(expectedTradesList))
})

bitx.getTradesList(1000, 10, function (err, res) {
t.ifErr(err)
t.deepEqual(res, expectedTradesList)
t.end()
})
})
4 changes: 2 additions & 2 deletions test/UnitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ tap.test('External', {autoend: true}, function (t) {
})

t.test('getTrades should call _request with the correct parameters', function (tt) {
mock.expects('_request').once().withArgs('GET', 'trades', {pair: 'XBTZAR'}, callback)
mock.expects('_request').once().withArgs('GET', 'trades', {pair: 'XBTZAR', since: 0}, callback)
bitx.getTrades(callback)
mock.verify()
tt.end()
})

t.test('getTrades should accept a pair option', function (tt) {
mock.expects('_request').once().withArgs('GET', 'trades', {pair: 'XBTMYR'}, callback)
mock.expects('_request').once().withArgs('GET', 'trades', {pair: 'XBTMYR', since: 0}, callback)
bitx.getTrades({pair: 'XBTMYR'}, callback)
mock.verify()
tt.end()
Expand Down