-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitstamp_test.js
92 lines (69 loc) · 1.97 KB
/
bitstamp_test.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
var http = require('http');
var https = require('https');
var crypto = require('crypto');
var querystring = require("querystring");
var _ = require('underscore');
/*
https.get('https://www.bitstamp.net/api/v2/order_book/btceur/', function(res){
var size = 0;
var chunks = [];
res.on('data', function(chunk){
size += chunk.length;
chunks.push(chunk);
})
res.on('end', function(){
var data = Buffer.concat(chunks, size);
console.log(data.toString())
});
}).on('error', function(e){
console.log('Got error: ' + e.message);
})
*/
var nonce = '' + new Date().getTime();
var key = '7SpjaGY1qg90szIgUF7TC09yfek7rrzo';
var id = '031601';
var message = nonce + id + key;
var secret = '4DRwsmPB7cueLYWHBHR0pAZQePnKPJQe'
var signer = crypto.createHmac('sha256', new Buffer(secret, 'utf8'));
var signature = signer.update(message).digest('hex').toUpperCase();
//var data = querystring.stringify(args);
var postData = querystring.stringify({
'key' : key,
'signature': signature,
'nonce': nonce
});
console.log(postData)
//console.log(args)
var options = {
hostname: 'www.bitstamp.net',
path: '/api/v2/balance/btceur/',
method: 'POST',
headers: {
'User-Agent': 'Bitstamp Javascript API client',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
},
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
var buffer = '';
res.on('data', function(data) {
buffer += data;
});
res.on('end', function() {
var target = "btc_available"
var reg = eval("/\""+target+"\": "+"\"[0-9]+([.]{1}[0-9]+){0,1}\"/gi")
var num_reg = /[0-9]+([.]{1}[0-9]+){0,1}/g
var value = parseFloat(buffer.match(reg).toString().match(num_reg))
console.log(reg)
console.log(value)
console.log(buffer);
console.log(typeof JSON.parse(buffer))
});
});
req.on('error', function(err) {
console.log(err);
});
// write data to request body
req.write(postData);
req.end();