UNPKG

hamal-btc38

Version:
145 lines (116 loc) 4.89 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _request = require('./request'); var _request2 = _interopRequireDefault(_request); var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var DOMAIN = 'http://api.btc38.com'; var _class = function () { function _class(_ref) { var key = _ref.key, secret = _ref.secret, userId = _ref.userId, _ref$version = _ref.version, version = _ref$version === undefined ? 'v1' : _ref$version; _classCallCheck(this, _class); this._hashes = [key, userId, secret]; this._version = version; this._secret = secret; this._generatePublicApi(); } // Private api _createClass(_class, [{ key: 'account', value: function account() { var api = this._makeApi('getMyBalance'); return this._post(api); } }, { key: 'buy', value: function buy(asset, price, amount, currency) { return this._submitOrder(1, { price: price, coinname: asset, amount: amount, mk_type: currency }); } }, { key: 'sell', value: function sell(asset, price, amount, currency) { return this._submitOrder(2, { price: price, coinname: asset, amount: amount, mk_type: currency }); } }, { key: 'orderList', value: function orderList(asset) { var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cny'; var api = this._makeApi('getOrderList'); return this._post(api, { coinname: asset, mk_type: currency }); } }, { key: 'tradeList', value: function tradeList(asset) { var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cny'; var api = this._makeApi('getMyTradeList'); return this._post(api, { coinname: asset, mk_type: currency }); } }, { key: 'cancelOrder', value: function cancelOrder(orderId) { var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cny'; var api = this._makeApi('cancelOrder'); return this._post(api, { order_id: orderId, mk_type: currency }); } }, { key: '_submitOrder', value: function _submitOrder(type, _ref2) { var price = _ref2.price, amount = _ref2.amount, coinname = _ref2.coinname, _ref2$mk_type = _ref2.mk_type, mk_type = _ref2$mk_type === undefined ? 'cny' : _ref2$mk_type; var api = this._makeApi('submitOrder'); return this._post(api, { type: type, price: price, amount: amount, coinname: coinname, mk_type: mk_type }); } // Utils }, { key: '_generatePublicApi', value: function _generatePublicApi() { var _this = this; ['ticker', 'trades', 'depth'].forEach(function (method) { _this[method] = function () { var asset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'all'; var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cny'; var api = _this._makeApi(method); return _this._get(api, { c: asset, mk_type: currency }); }; }); } }, { key: '_makeApi', value: function _makeApi(name) { return '/' + this._version + '/' + name + '.php'; } }, { key: '_get', value: function _get(api) { var qs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var uri = DOMAIN + api; return _request2.default.get({ uri: uri, qs: qs }); } }, { key: '_post', value: function _post(api) { var form = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; form.time = new Date().getTime(); form.key = this._hashes[0]; var hashes = this._hashes.slice(0); hashes.push(form.time); form.md5 = _crypto2.default.createHash('md5').update(hashes.join('_')).digest('hex'); var uri = DOMAIN + api; return _request2.default.post({ uri: uri, form: form }); } }]); return _class; }(); exports.default = _class;