forked from nothingisdead/npm-cryptsy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptsy.js
More file actions
152 lines (128 loc) · 4.23 KB
/
cryptsy.js
File metadata and controls
152 lines (128 loc) · 4.23 KB
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
var stringify = require("querystring").stringify,
hmac = require("crypto").createHmac,
request = require("request"),
publicMethods = ['marketdata','marketdatav2','orderdata','singleorderdata','singlemarketdata'];
function CryptsyClient(key, secret) {
var self = this;
self.key = key;
self.secret = secret;
function api_query(method, callback, args)
{
var args_tmp = {};
for(i in args)
if(args[i])
args_tmp[i] = args[i];
args = args_tmp;
var options = {
uri: 'https://www.cryptsy.com/api',
agent: false,
method: 'POST',
headers: {
"User-Agent": "Mozilla/4.0 (compatible; Cryptsy API node client)",
"Content-type": "application/x-www-form-urlencoded"
}
};
args.method = method;
if(publicMethods.indexOf(method) > -1)
{
options.method = 'GET';
options.uri = 'http://pubapi.cryptsy.com/api.php?' + stringify(args);
}
else
{
if (!self.key || !self.secret) {
throw new Error("Must provide key and secret to make this API request.");
}
else
{
args.nonce = new Date().getTime();
var message = stringify(args);
var signed_message = new hmac("sha512", self.secret);
signed_message.update(message);
options.headers.Key = self.key;
options.headers.Sign = signed_message.digest('hex');
options.body = message;
}
}
request(options, function(err, res, body) {
var response = JSON.parse(body);
if(parseInt(response.success) === 1 && typeof callback == typeof Function)
callback(response.return);
else if(response.error)
throw new Error(response.error);
});
}
// This function gets the market id for a market in the format 'LTCBTC'
self.getmarketid = function(marketname, callback) {
if(!self.markets || !self.markets.length)
{
self.getmarkets(function() {
callback(self.markets[marketname]);
});
}
else
callback(self.markets[marketname]);
};
// Old API method
self.marketdata = function(callback) {
api_query('marketdata', callback);
}
// New API method
self.marketdatav2 = function(callback) {
api_query('marketdatav2', callback);
}
self.singlemarketdata = function(marketid, callback) {
api_query('singlemarketdata', callback, { marketid: marketid });
}
self.orderdata = function(callback) {
api_query('orderdata', callback);
}
self.singleorderdata = function(marketid, callback) {
api_query('singleorderdata', callback, { marketid: marketid });
}
self.getinfo = function(callback) {
api_query("getinfo", callback);
}
self.getmarkets = function(callback) {
callback2 = function(markets) {
self.markets = {};
for(var i in markets)
{
self.markets[markets[i].primary_currency_code + markets[i].secondary_currency_code] = markets[i].marketid;
}
callback(markets);
}
api_query('getmarkets', callback2);
}
self.mytransactions = function(callback) {
api_query('mytransactions', callback);
}
self.markettrades = function(marketid, callback) {
api_query('markettrades', callback, { marketid: marketid });
}
self.marketorders = function(marketid, callback) {
api_query('marketorders', callback, { marketid: marketid });
}
self.mytrades = function(marketid, limit, callback) {
api_query('mytrades', callback, { marketid: marketid, limit: limit })
}
self.allmytrades = function(callback) {
api_query('allmytrades', callback);
}
self.myorders = function(marketid, callback) {
api_query('myorders', callback, { marketid: marketid });
}
self.allmyorders = function(callback) {
api_query('allmyorders', callback);
}
self.createorder = function(marketid, ordertype, quantity, price, callback) {
api_query('createorder', callback, { marketid: marketid, ordertype: ordertype, quantity: quantity, price: price });
}
self.cancelorder = function(orderid, callback) {
api_query('cancelorder', callback, { orderid: orderid });
}
self.calculatefees = function(ordertype, quantity, price, callback) {
api_query('calculatefees', callback, { ordertype: ordertype, quantity: quantity, price: price });
}
}
module.exports = CryptsyClient;