-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitcoincoid.js
143 lines (114 loc) · 3.37 KB
/
bitcoincoid.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
var querystring = require("querystring"),
VError = require('verror'),
crypto = require("crypto"),
https =require('https'),
url = require('url');
var self;
var bitcoinCoId = function bitcoinCoId(settings)
{
self = this;
this.key = settings.key;
this.secret = settings.secret;
this.url = settings.url || "https://vip.bitcoin.co.id/api";
this.tradeUrl = settings.tradeUrl || "https://vip.bitcoin.co.id/tapi/";
};
function makePublicRequest( path, args, callback)
{
var functionName = 'bitcoinCoIdbitcoinCoId.makePublicRequest()';
var options = url.parse(self.url+path);
options.method = "GET";
executeRequest(options, new Buffer({}, 'utf8'), callback);
};
function makePrivateRequest(method, args, callback)
{
var functionName = 'bitcoinCoIdbitcoinCoId.makePrivateRequest()';
var uri = self.tradeUrl;
var nonce = (new Date()).getTime();
args.nonce = nonce;
if (!self.key || !self.secret)
{
return callback(new VError("%s must provide key and secret to make a private API request.", functionName));
}
postData = JSON.stringify(args);
var content_data = querystring.stringify(args);
var signature = crypto
.createHmac('sha512', new Buffer(self.secret, 'utf8'))
.update(new Buffer(content_data, 'utf8'))
.digest('hex');
var options = url.parse(self.tradeUrl);
options.method = 'POST'
options.headers = {
'Key': self.key,
'Sign': signature,
'content-type': 'application/x-www-form-urlencoded',
'content-length': content_data.length,
}
executeRequest(options, content_data, callback);
};
function executeRequest(options,content, callback){
var functionName = 'bitcoincoid.executeRequest()';
var req = https.request(options, function(res) {
var data = ''
res.setEncoding('utf8')
res.on('data', function (chunk) {
data+= chunk
})
res.on('end', function() {
try{
data = JSON.parse(data);
callback(null,data);
}
catch(e){
return callback(new VError("%s Error in Parsing Data to JSON", functionName));
}
})
});
req.on('error', function(err) {
return callback(new VError("%s Error in Parsing request", functionName));
})
if(options.method == 'POST'){
req.write(content);
}
req.end();
}
bitcoinCoId.prototype.getTicker = function(tickerSymbol, callback)
{
makePublicRequest( "/"+tickerSymbol+"/ticker", {}, callback);
};
bitcoinCoId.prototype.getTrades = function(tickerSymbol, callback)
{
makePublicRequest( "/"+tickerSymbol+"/trades", {}, callback);
};
bitcoinCoId.prototype.getDepth = function(tickerSymbol, callback)
{
makePublicRequest( "/"+tickerSymbol+"/depth", {}, callback);
};
bitcoinCoId.prototype.getInfo = function(callback)
{
makePrivateRequest("POST", {"method":"getInfo"}, callback);
};
bitcoinCoId.prototype.transHistory = function(callback)
{
makePrivateRequest("POST", {"method":"transHistory"}, callback);
};
bitcoinCoId.prototype.tradeHistory = function(params, callback)
{
params.method = "tradeHistory";
makePrivateRequest("POST", params, callback);
};
bitcoinCoId.prototype.openOrders = function(params, callback)
{
params.method = "openOrders";
makePrivateRequest("POST", params, callback);
};
bitcoinCoId.prototype.trade = function(params, callback)
{
params.method = "trade";
makePrivateRequest("POST", params, callback);
};
bitcoinCoId.prototype.cancelOrder = function(params, callback)
{
params.method = "cancelOrder";
makePrivateRequest("POST", params, callback);
};
module.exports = bitcoinCoId;