forked from danKHUU/bitcoin-co-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·216 lines (178 loc) · 5.53 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var querystring = require("querystring"),
_ = require('underscore'),
request = require('request'),
VError = require('verror'),
crypto = require("crypto");
var Bitcoincoid = function(key, secret) {
this.key = key;
this.secret = secret;
this.tapi = 'https://vip.bitcoin.co.id/tapi';
this.api = 'https://vip.bitcoin.co.id/api/';
this.timeout = 5000;
this._strictSSL = true;
}
Bitcoincoid.prototype._get = function(pair, method, callback) {
this._request({
url: this.api + pair + '/' + method
}, callback);
};
Bitcoincoid.prototype._post = function(method, params, callback) {
var functionName = 'Bitcoincoid._post()',
self = this;
if(!self.key || !self.secret) {
return callback(new VError("%s must provide key and secret to make a private API request.", functionName))
}
params.nonce = self._generateNonce();
var postData = {};
for (var x in params) {
postData[x] = params[x];
}
postData.method = method;
var post = querystring.stringify(postData);
var sign = crypto.createHmac('sha512', self.secret).update(new Buffer(post)).digest('hex').toString();
var options = {
url: self.tapi,
method: 'POST',
form: post,
headers: {
"User-Agent": "Bitcoin.co.id node.js client",
Sign: sign,
Key: self.key
}
};
self._request(options, callback);
};
Bitcoincoid.prototype._request = function (options, callback) {
var functionName = 'Bitcoincoid._request()',
self = this,
requestOptions = {
timeout: self.timeout,
agent: self.agent,
strictSSL: self._strictSSL
};
for (var key in options) {
requestOptions[key] = options[key];
}
request(requestOptions, function(err, response, body) {
var error = null;
if(err)
{
return callback(new VError('%s %s', functionName, err));
}
else if (response.statusCode < 200 || response.statusCode >= 300)
{
return callback(new VError('%s HTTP error: %s. Error message: %s', functionName, response.statusCode, response.statusMessage));
}
var result;
try {
result = JSON.parse(body);
} catch(error) {
return callback(new VError('%s Request error: %s', functionName, error.message));
}
if(result.error) {
return callback(new VError('%s API error: %s', functionName, result.error));
}
callback(null, result);
});
};
Bitcoincoid.prototype._generateNonce = function() {
var now = new Date().getTime();
if(now !== this.last)
this.nonceIncr = -1;
this.last = now;
this.nonceIncr++;
// add padding to nonce incr
// @link https://stackoverflow.com/questions/6823592/numbers-in-the-form-of-001
var padding =
this.nonceIncr < 10 ? '000' :
this.nonceIncr < 100 ? '00' :
this.nonceIncr < 1000 ? '0' : '';
return now + padding + this.nonceIncr;
}
//
// Public API
//
Bitcoincoid.prototype.getTicker = function(pair, callback) {
this._get(pair,'ticker', callback);
}
Bitcoincoid.prototype.getTrades = function(pair, callback) {
this._get(pair, 'trades', callback);
}
Bitcoincoid.prototype.depth = function(pair, callback) {
this._get(pair, 'depth', callback);
}
//
// Private API
//
//This method gives user balances and server's timestamp
Bitcoincoid.prototype.getAccountBalances = function(callback) {
this._post('getInfo', {}, callback);
};
//This method gives list of deposits and withdrawals of all currencies.
Bitcoincoid.prototype.getTransactionsHistory = function(callback) {
this._post('transHistory', {}, callback);
};
//This method is for opening a new order.
Bitcoincoid.prototype.createOrders = function(pair, type, price, amount, callback) {
const [asset, currency] = pair.split('_');
var functionName = 'Bitcoincoid.createOrder()',
pair = [asset, currency].join('_'),
params = {
pair: pair,
type: type,
price: price
};
if (type === 'buy')
{
params = _.extend({[currency]: amount}, params);
}
else if (type === 'sell')
{
params = _.extend({[asset]: amount}, params);
}
else
{
return callback(new VError('%s Parameter error: type "%s" needs to be either "buy" or "sell"', functionName, type));
}
this._post('trade', params, callback);
};
//This method gives information about transaction in buying and selling history
Bitcoincoid.prototype.getTradeHistory = function(pair, callback) {
this._post('tradeHistory', {
'pair': pair
}, callback);
};
//This method gives the list of current open orders (buy and sell).
Bitcoincoid.prototype.getActiveOrders = function(pair, callback) {
if (!callback) {
callback = pair;
pair = null;
}
this._post('openOrders', {
pair: pair},
callback);
};
//This method gives the list of order history (buy and sell).
Bitcoincoid.prototype.getOrderHistory = function(pair, limit, since, callback) {
this._post('orderHistory', {
'pair': pair,
'count': limit,
'from': since
}, callback);
};
//This method gives specific order details.
Bitcoincoid.prototype.getOrderDetails = function(pair, orderId, callback) {
this._post('getOrder', {
'pair': pair,
'order_id': orderId
}, callback);
};
//This method is for cancelling existing open order.
Bitcoincoid.prototype.cancelOrder = function(pair, orderId, type, callback) {
this._post('cancelOrder', {
'pair': pair,
'order_id': orderId,
'type': type
}, callback);
};
module.exports = Bitcoincoid;