-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
145 lines (126 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var debug = require('debug')('mtgox')
, crypto = require('crypto')
, request = require('request')
, qs = require('querystring')
, util = require('util')
, num = require('num')
, MtGox = module.exports = function(options) {
this.options = options || {}
this.options.url || (this.options.url = 'https://data.mtgox.com/api')
this.nonce = require('nonce')()
}
MtGox.prototype.query = function(path, payload, cb) {
if (!cb) {
cb = payload
payload = {}
}
payload.nonce = this.nonce()
var post = qs.stringify(payload)
var hmac = crypto.createHmac('sha512', new Buffer(this.options.secret, 'base64'))
hmac.update(post)
var r = {
url: this.options.url + path,
method: 'POST',
json: true,
body: post,
headers: {
'Rest-Key': this.options.key,
'Rest-Sign': hmac.digest('base64'),
'User-Agent': 'Mozilla/4.0 (compatible MtGox node.js client)',
'Content-Type': 'application/x-www-form-urlencoded'
}
}
debug(util.inspect(r))
request(r, function(err, res, body) {
if (err) return cb(err)
if (!body) return cb(new Error('failed to parse body'))
if (body.error) return cb(new Error(body.error))
if (res.statusCode < 200 || res.statusCode >= 300) return cb(new Error('status code ' + res.statusCode))
cb(null, body['return'])
})
}
MtGox.prototype.orders = function(cb) {
this.query('/1/generic/private/orders', function(err, res) {
if (err) return cb(err)
cb(null, res.map(function(o) {
debug(util.inspect(o))
return {
id: o.oid,
side: o.type,
market: o.item + o.currency,
volume: (+num(o.amount.value)).toString(),
price: (+num(o.price.value)).toString()
}
}))
})
}
MtGox.prototype.depth = function(market, cb) {
request({
url: this.options.url + '/1/' + market + '/depth',
json: true,
headers: {
'User-Agent': 'hello'
}
}, function(err, res, data) {
if (err) return cb(err)
if (data.result != 'success') {
return cb(new Error('error from api: ' + data.error))
}
var m = function(depth) {
return {
price: depth.price + '',
volume: depth.amount + ''
}
}
cb(null, {
bids: data['return'].bids.map(m),
asks: data['return'].asks.map(m).reverse()
})
})
}
MtGox.prototype.market = function(market, cb) {
var that = this
request({
url: this.options.url + '/1/' + market + '/ticker',
json: true,
headers: {
'User-Agent': 'hello'
}
}, function(err, res, data) {
err = err || that.error(data)
if (err) return cb(err)
var result = data['return']
cb(null, {
bid: result.buy.value,
ask: result.sell.value,
last: result.last.value,
high: result.high.value,
low: result.low.value,
volume: result.vol.value,
average: result.avg.value,
timestamp: +result.now
})
})
}
MtGox.prototype.error = function(result) {
if (result.result == 'success') return
return new Error('API error: ' + (result.error || 'Unspecified'))
}
MtGox.prototype.order = function(order, cb) {
this.query('/1/' + order.market + '/private/order/add', {
type: order.side,
price_int: +num(order.price).mul(1e5),
amount_int: +num(order.volume).mul(1e8)
}, function(err, res) {
if (err) return cb(err)
cb(null, res)
})
}
MtGox.prototype.cancel = function(id, cb) {
this.query('/1/BTCUSD/private/order/cancel', {
oid: id
}, function(err, res) {
if (err) return cb(err)
cb(null, res)
})
}