crowdtoken-demo-wallet
Version:
ERC20 Token wallet utilize SafeMath, DAOs & Web3
229 lines (200 loc) • 6.53 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var Bitfinex, crypto, qs, request;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
request = require('request');
crypto = require('crypto');
qs = require('querystring');
module.exports = Bitfinex = (function() {
function Bitfinex(key, secret, nonceGenerator) {
this.url = "https://api.bitfinex.com";
this.version = 'v1';
this.key = key;
this.secret = secret;
this.nonce = new Date().getTime();
this._nonce = typeof nonceGenerator === "function" ? nonceGenerator : function() {
return ++this.nonce;
};
}
Bitfinex.prototype.make_request = function(sub_path, params, cb) {
var headers, key, nonce, path, payload, signature, url, value;
if (!this.key || !this.secret) {
return cb(new Error("missing api key or secret"));
}
path = '/' + this.version + '/' + sub_path;
url = this.url + path;
nonce = JSON.stringify(this._nonce());
payload = {
request: path,
nonce: nonce
};
for (key in params) {
value = params[key];
payload[key] = value;
}
payload = new Buffer(JSON.stringify(payload)).toString('base64');
signature = crypto.createHmac("sha384", this.secret).update(payload).digest('hex');
headers = {
'X-BFX-APIKEY': this.key,
'X-BFX-PAYLOAD': payload,
'X-BFX-SIGNATURE': signature
};
return request({
url: url,
method: "POST",
headers: headers,
timeout: 15000
}, function(err, response, body) {
var error, error1, result;
if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
return cb(new Error(err != null ? err : response.statusCode));
}
try {
result = JSON.parse(body);
} catch (error1) {
error = error1;
return cb(null, {
messsage: body.toString()
});
}
if (result.message != null) {
return cb(new Error(result.message));
}
return cb(null, result);
});
};
Bitfinex.prototype.make_public_request = function(path, cb) {
var url;
url = this.url + '/v1/' + path;
return request({
url: url,
method: "GET",
timeout: 15000
}, function(err, response, body) {
var error, error1, result;
if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
return cb(new Error(err != null ? err : response.statusCode));
}
try {
result = JSON.parse(body);
} catch (error1) {
error = error1;
return cb(null, {
messsage: body.toString()
});
}
if (result.message != null) {
return cb(new Error(result.message));
}
return cb(null, result);
});
};
Bitfinex.prototype.orderbook = function(symbol, options, cb) {
var err, error1, index, option, query_string, uri, value;
index = 0;
uri = 'book/' + symbol;
if (typeof options === 'function') {
cb = options;
} else {
try {
for (option in options) {
value = options[option];
if (index++ > 0) {
query_string += '&' + option + '=' + value;
} else {
query_string = '/?' + option + '=' + value;
}
}
if (index > 0) {
uri += query_string;
}
} catch (error1) {
err = error1;
return cb(err);
}
}
return this.make_public_request(uri, cb);
};
Bitfinex.prototype.trades = function(symbol, cb) {
return this.make_public_request('trades/' + symbol, cb);
};
Bitfinex.prototype.get_symbols = function(cb) {
return this.make_public_request('symbols', cb);
};
Bitfinex.prototype.symbols_details = function(cb) {
return this.make_public_request('symbols_details', cb);
};
Bitfinex.prototype.new_order = function(symbol, amount, price, exchange, side, type, cb) {
var params;
params = {
symbol: symbol,
amount: amount,
price: price,
exchange: exchange,
side: side,
type: type
};
return this.make_request('order/new', params, cb);
};
Bitfinex.prototype.cancel_order = function(order_id, cb) {
var params;
params = {
order_id: parseInt(order_id)
};
return this.make_request('order/cancel', params, cb);
};
Bitfinex.prototype.cancel_all_orders = function(cb) {
return this.make_request('order/cancel/all', {}, cb);
};
Bitfinex.prototype.order_status = function(order_id, cb) {
var params;
params = {
order_id: order_id
};
return this.make_request('order/status', params, cb);
};
Bitfinex.prototype.active_orders = function(cb) {
return this.make_request('orders', {}, cb);
};
Bitfinex.prototype.account_infos = function(cb) {
return this.make_request('account_infos', {}, cb);
};
/*
POST /v1/withdraw
Parameters:
'withdraw_type' :string (can be "bitcoin", "litecoin" or "darkcoin" or "mastercoin")
'walletselected' :string (the origin of the wallet to withdraw from, can be "trading", "exchange", or "deposit")
'amount' :decimal (amount to withdraw)
'address' :address (destination address for withdrawal)
*/
Bitfinex.prototype.withdraw = function(withdraw_type, walletselected, amount, address, cb) {
var params;
params = {
withdraw_type: withdraw_type,
walletselected: walletselected,
amount: amount,
address: address
};
return this.make_request('withdraw', params, cb);
};
/*
POST /v1/transfer
Parameters:
‘amount’: decimal (amount to transfer)
‘currency’: string, currency of funds to transfer
‘walletfrom’: string. Wallet to transfer from
‘walletto’: string. Wallet to transfer to
*/
Bitfinex.prototype.transfer = function(amount, currency, walletfrom, walletto, cb) {
var params;
params = {
amount: amount,
currency: currency,
walletfrom: walletfrom,
walletto: walletto
};
return this.make_request('transfer', params, cb);
};
return Bitfinex;
})();
}).call(this);