-
Notifications
You must be signed in to change notification settings - Fork 6
/
mercadobitcoin.js
94 lines (71 loc) · 2.27 KB
/
mercadobitcoin.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
var unirest = require('unirest');
crypto = require('crypto'),
qs = require('querystring');
var ENDPOINT_API = 'https://www.mercadobitcoin.com.br/api/',
ENDPOINT_TRADE_API = 'https://www.mercadobitcoin.com.br/tapi/';
var MercadoBitcoin = function (config) {
this.config = {
CURRENCY: config.currency
}
}
MercadoBitcoin.prototype = {
ticker: function (success) {
this.call('ticker', success);
},
orderBook: function (success) {
this.call('orderbook', success);
},
trades: function (success) {
this.call('trades', success);
},
call: function (method, success) {
var isLitecoin = this.config.CURRENCY === 'LTC';
unirest.get(ENDPOINT_API + method + (isLitecoin ? '_litecoin' : ''))
.headers('Accept', 'application/json')
.end(function (response) {
success(JSON.parse(response.raw_body));
});
}
}
var MercadoBitcoinTrade = function (config) {
this.config = {
KEY: config.key,
SECRET: config.secret,
PIN: config.pin
}
};
MercadoBitcoinTrade.prototype = {
getInfo: function(success, error) {
this.call('getInfo', {}, success, error);
},
orderList: function (parameters, success, error) {
this.call('OrderList', parameters, success, error)
},
trade: function (parameters, success, error) {
this.call('Trade', parameters, success, error)
},
cancelOrder: function (parameters, success, error) {
this.call('CancelOrder', parameters, success, error)
},
call: function (method, parameters, success, error) {
var now = Math.round(new Date().getTime() / 1000);
var signature = crypto.createHmac('sha512', this.config.SECRET)
.update(method + ':' + this.config.PIN + ':' + now)
.digest('hex');
unirest.post(ENDPOINT_TRADE_API)
.headers({'Key': this.config.KEY})
.headers({'Sign': signature})
.send(qs.stringify({'method': method, 'tonce': now}))
.send(qs.stringify(parameters))
.end(function (response) {
if (response.body.success === 1 && success)
success(response.body.return);
else if (error)
error(response.body.error);
});
}
}
module.exports = {
MercadoBitcoin: MercadoBitcoin,
MercadoBitcoinTrade: MercadoBitcoinTrade
};