@azteam/huobi-api
Version:
N/A
1,019 lines (1,018 loc) • 56.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _crypto = _interopRequireDefault(require("crypto"));
var _lodash = _interopRequireDefault(require("lodash"));
var _candlestickChart = require("@azteam/candlestick-chart");
var _httpClient = _interopRequireDefault(require("@azteam/http-client"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var SPOT_ENDPOINT = 'api-aws.huobi.pro';
var LINEAR_SWAP_ENDPOINT = 'api.btcgateway.pro';
var HuobiAPI = /*#__PURE__*/function () {
function HuobiAPI(accessKey, secretKey) {
_classCallCheck(this, HuobiAPI);
this.client = new _httpClient["default"]({
timeout: 20000
});
this.accessKey = accessKey;
this.secretKey = secretKey;
}
return _createClass(HuobiAPI, [{
key: "_generateQueryString",
value: function _generateQueryString(params) {
var data = _objectSpread(_objectSpread({}, params), {}, {
Timestamp: new Date().toISOString().replace(/\..+/, ''),
SignatureMethod: 'HmacSHA256',
SignatureVersion: 2,
AccessKeyId: this.accessKey
});
return Object.keys(data).sort(function (a, b) {
return a > b ? 1 : -1;
}).reduce(function (a, k) {
a.push("".concat(k, "=").concat(encodeURIComponent(data[k])));
return a;
}, []).join('&');
}
}, {
key: "_encodeSpotURL",
value: function _encodeSpotURL(method, version, uri) {
var params = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var queryString = this._generateQueryString(params);
var source = "".concat(method, "\n").concat(SPOT_ENDPOINT, "\n/").concat(version, "/").concat(uri, "\n").concat(queryString);
var signature = encodeURIComponent(_crypto["default"].createHmac('sha256', this.secretKey).update(String(source)).digest('base64'));
return "https://".concat(SPOT_ENDPOINT, "/").concat(version, "/").concat(uri, "?").concat(queryString, "&Signature=").concat(signature);
}
}, {
key: "_encodeLinearSwapURL",
value: function _encodeLinearSwapURL(method, version, uri) {
var params = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var queryString = this._generateQueryString(params);
var source = "".concat(method, "\n").concat(LINEAR_SWAP_ENDPOINT, "\n/linear-swap-api/").concat(version, "/").concat(uri, "\n").concat(queryString);
var signature = encodeURIComponent(_crypto["default"].createHmac('sha256', this.secretKey).update(String(source)).digest('base64'));
return "https://".concat(LINEAR_SWAP_ENDPOINT, "/linear-swap-api/").concat(version, "/").concat(uri, "?").concat(queryString, "&Signature=").concat(signature);
}
}, {
key: "transferCrossToSpot",
value: function () {
var _transferCrossToSpot = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(amount) {
var url, res;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
url = this._encodeSpotURL('POST', 'v2', "account/transfer");
_context.next = 3;
return this.client.post(url, {
amount: amount,
from: 'linear-swap',
to: 'spot',
currency: 'USDT',
'margin-account': 'USDT'
}, false);
case 3:
res = _context.sent;
if (!(res.success === true)) {
_context.next = 6;
break;
}
return _context.abrupt("return", true);
case 6:
throw new Error(res.message);
case 7:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function transferCrossToSpot(_x) {
return _transferCrossToSpot.apply(this, arguments);
}
return transferCrossToSpot;
}()
}, {
key: "transferSpotToCross",
value: function () {
var _transferSpotToCross = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(amount) {
var url, res;
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
url = this._encodeSpotURL('POST', 'v2', "account/transfer");
_context2.next = 3;
return this.client.post(url, {
amount: amount,
from: 'spot',
to: 'linear-swap',
currency: 'USDT',
'margin-account': 'USDT'
}, false);
case 3:
res = _context2.sent;
if (!(res.success === true)) {
_context2.next = 6;
break;
}
return _context2.abrupt("return", true);
case 6:
throw new Error(res.message);
case 7:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function transferSpotToCross(_x2) {
return _transferSpotToCross.apply(this, arguments);
}
return transferSpotToCross;
}()
}, {
key: "transferSpotToIsolated",
value: function () {
var _transferSpotToIsolated = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(amount, symbol) {
var url, res;
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
url = this._encodeSpotURL('POST', 'v2', "account/transfer");
_context3.next = 3;
return this.client.post(url, {
amount: amount,
from: 'spot',
to: 'linear-swap',
currency: 'USDT',
'margin-account': symbol
}, false);
case 3:
res = _context3.sent;
if (!(res.success === true)) {
_context3.next = 6;
break;
}
return _context3.abrupt("return", true);
case 6:
throw new Error(res.message);
case 7:
case "end":
return _context3.stop();
}
}, _callee3, this);
}));
function transferSpotToIsolated(_x3, _x4) {
return _transferSpotToIsolated.apply(this, arguments);
}
return transferSpotToIsolated;
}()
}, {
key: "transferIsolatedToSpot",
value: function () {
var _transferIsolatedToSpot = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(amount, symbol) {
var url, res;
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
url = this._encodeSpotURL('POST', 'v2', "account/transfer");
_context4.next = 3;
return this.client.post(url, {
amount: amount,
from: 'linear-swap',
to: 'spot',
currency: 'USDT',
'margin-account': symbol
}, false);
case 3:
res = _context4.sent;
if (!(res.success === true)) {
_context4.next = 6;
break;
}
return _context4.abrupt("return", true);
case 6:
throw new Error(res.message);
case 7:
case "end":
return _context4.stop();
}
}, _callee4, this);
}));
function transferIsolatedToSpot(_x5, _x6) {
return _transferIsolatedToSpot.apply(this, arguments);
}
return transferIsolatedToSpot;
}()
}, {
key: "getSpotAccount",
value: function () {
var _getSpotAccount = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
var url, res;
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
url = this._encodeSpotURL('GET', 'v1', 'account/accounts');
_context5.next = 3;
return this.client.get(url);
case 3:
res = _context5.sent;
if (!(res.status === 'ok')) {
_context5.next = 6;
break;
}
return _context5.abrupt("return", res.data.find(function (account) {
return account.type === 'spot';
}));
case 6:
throw new Error(res['err-msg']);
case 7:
case "end":
return _context5.stop();
}
}, _callee5, this);
}));
function getSpotAccount() {
return _getSpotAccount.apply(this, arguments);
}
return getSpotAccount;
}()
}, {
key: "getSpotBalance",
value: function () {
var _getSpotBalance = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
var currency,
spotAccount,
url,
res,
_res$data$list$find,
balance,
_args6 = arguments;
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
currency = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : 'usdt';
_context6.next = 3;
return this.getSpotAccount();
case 3:
spotAccount = _context6.sent;
url = this._encodeSpotURL('GET', 'v1', "account/accounts/".concat(spotAccount.id, "/balance"));
_context6.next = 7;
return this.client.get(url);
case 7:
res = _context6.sent;
if (!(res.status === 'ok')) {
_context6.next = 11;
break;
}
_res$data$list$find = res.data.list.find(function (coin) {
return coin.currency === currency;
}), balance = _res$data$list$find.balance;
return _context6.abrupt("return", _lodash["default"].floor(balance, 4));
case 11:
throw new Error(res.err_msg);
case 12:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function getSpotBalance() {
return _getSpotBalance.apply(this, arguments);
}
return getSpotBalance;
}()
}, {
key: "getCrossBalance",
value: function () {
var _getCrossBalance = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
var url, res, balance;
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', "swap_cross_account_info");
_context7.next = 3;
return this.client.post(url, {}, false);
case 3:
res = _context7.sent;
if (!(res.status === 'ok')) {
_context7.next = 7;
break;
}
balance = res.data[0].margin_balance;
return _context7.abrupt("return", _lodash["default"].floor(balance, 4));
case 7:
throw new Error(res.err_msg);
case 8:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function getCrossBalance() {
return _getCrossBalance.apply(this, arguments);
}
return getCrossBalance;
}()
}, {
key: "getIsolatedBalance",
value: function () {
var _getIsolatedBalance = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(symbol) {
var url, res, balance;
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', "swap_account_info");
_context8.next = 3;
return this.client.post(url, {
contract_code: symbol
}, false);
case 3:
res = _context8.sent;
if (!(res.status === 'ok')) {
_context8.next = 7;
break;
}
balance = res.data[0].margin_static;
return _context8.abrupt("return", _lodash["default"].floor(balance, 4));
case 7:
throw new Error(res.err_msg);
case 8:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function getIsolatedBalance(_x7) {
return _getIsolatedBalance.apply(this, arguments);
}
return getIsolatedBalance;
}()
}, {
key: "_setLeverRate",
value: function () {
var _setLeverRate2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(symbol, leverRate, uri) {
var url, res;
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context9.next = 3;
return this.client.post(url, {
contract_code: symbol,
lever_rate: leverRate
}, false);
case 3:
res = _context9.sent;
if (!(res.status === 'ok')) {
_context9.next = 6;
break;
}
return _context9.abrupt("return", res.data);
case 6:
throw new Error(res.err_msg);
case 7:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function _setLeverRate(_x8, _x9, _x0) {
return _setLeverRate2.apply(this, arguments);
}
return _setLeverRate;
}()
}, {
key: "setCrossLeverRate",
value: function () {
var _setCrossLeverRate = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(symbol, leverRate) {
return _regeneratorRuntime().wrap(function _callee0$(_context0) {
while (1) switch (_context0.prev = _context0.next) {
case 0:
return _context0.abrupt("return", this._setLeverRate(symbol, leverRate, 'swap_cross_switch_lever_rate'));
case 1:
case "end":
return _context0.stop();
}
}, _callee0, this);
}));
function setCrossLeverRate(_x1, _x10) {
return _setCrossLeverRate.apply(this, arguments);
}
return setCrossLeverRate;
}()
}, {
key: "setIsolatedLeverRate",
value: function () {
var _setIsolatedLeverRate = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(symbol, leverRate) {
return _regeneratorRuntime().wrap(function _callee1$(_context1) {
while (1) switch (_context1.prev = _context1.next) {
case 0:
return _context1.abrupt("return", this._setLeverRate(symbol, leverRate, 'swap_switch_lever_rate'));
case 1:
case "end":
return _context1.stop();
}
}, _callee1, this);
}));
function setIsolatedLeverRate(_x11, _x12) {
return _setIsolatedLeverRate.apply(this, arguments);
}
return setIsolatedLeverRate;
}()
}, {
key: "_setOrder",
value: function () {
var _setOrder2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(symbol, leverRate, clientOrderId, direction, volume, openPrice, takeProfitPrice, stopLossPrice, offset, uri) {
var data, url, res;
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
data = {
offset: offset,
order_price_type: 'market',
contract_code: symbol,
client_order_id: clientOrderId,
volume: volume,
direction: direction,
lever_rate: leverRate
};
if (openPrice) {
data.order_price_type = 'limit';
data.price = openPrice;
}
if (takeProfitPrice) {
data.tp_trigger_price = takeProfitPrice;
data.tp_order_price_type = 'optimal_20';
}
if (stopLossPrice) {
data.sl_trigger_price = stopLossPrice;
data.sl_order_price_type = 'optimal_20';
}
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context10.next = 7;
return this.client.parseBigInt().post(url, data, false);
case 7:
res = _context10.sent;
if (!(res.status === 'ok')) {
_context10.next = 10;
break;
}
return _context10.abrupt("return", res.data);
case 10:
throw new Error(res.err_msg);
case 11:
case "end":
return _context10.stop();
}
}, _callee10, this);
}));
function _setOrder(_x13, _x14, _x15, _x16, _x17, _x18, _x19, _x20, _x21, _x22) {
return _setOrder2.apply(this, arguments);
}
return _setOrder;
}()
}, {
key: "setCrossOpenOrder",
value: function () {
var _setCrossOpenOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(symbol, leverRate, clientOrderId, type, volume, openPrice) {
var takeProfitPrice,
stopLossPrice,
direction,
_args11 = arguments;
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
while (1) switch (_context11.prev = _context11.next) {
case 0:
takeProfitPrice = _args11.length > 6 && _args11[6] !== undefined ? _args11[6] : null;
stopLossPrice = _args11.length > 7 && _args11[7] !== undefined ? _args11[7] : null;
direction = null;
if (type === _candlestickChart.TREND.LONG) {
direction = 'buy';
} else if (type === _candlestickChart.TREND.SHORT) {
direction = 'sell';
}
return _context11.abrupt("return", this._setOrder(symbol, leverRate, clientOrderId, direction, volume, openPrice, takeProfitPrice, stopLossPrice, 'open', 'swap_cross_order'));
case 5:
case "end":
return _context11.stop();
}
}, _callee11, this);
}));
function setCrossOpenOrder(_x23, _x24, _x25, _x26, _x27, _x28) {
return _setCrossOpenOrder.apply(this, arguments);
}
return setCrossOpenOrder;
}()
}, {
key: "setIsolatedOpenOrder",
value: function () {
var _setIsolatedOpenOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(symbol, leverRate, clientOrderId, type, volume, openPrice) {
var takeProfitPrice,
stopLossPrice,
direction,
_args12 = arguments;
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
while (1) switch (_context12.prev = _context12.next) {
case 0:
takeProfitPrice = _args12.length > 6 && _args12[6] !== undefined ? _args12[6] : null;
stopLossPrice = _args12.length > 7 && _args12[7] !== undefined ? _args12[7] : null;
direction = null;
if (type === _candlestickChart.TREND.LONG) {
direction = 'buy';
} else if (type === _candlestickChart.TREND.SHORT) {
direction = 'sell';
}
return _context12.abrupt("return", this._setOrder(symbol, leverRate, clientOrderId, direction, volume, openPrice, takeProfitPrice, stopLossPrice, 'open', 'swap_order'));
case 5:
case "end":
return _context12.stop();
}
}, _callee12, this);
}));
function setIsolatedOpenOrder(_x29, _x30, _x31, _x32, _x33, _x34) {
return _setIsolatedOpenOrder.apply(this, arguments);
}
return setIsolatedOpenOrder;
}()
}, {
key: "setCrossCloseOrder",
value: function () {
var _setCrossCloseOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(symbol, leverRate, clientOrderId, type, volume, openPrice) {
var takeProfitPrice,
stopLossPrice,
direction,
_args13 = arguments;
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
while (1) switch (_context13.prev = _context13.next) {
case 0:
takeProfitPrice = _args13.length > 6 && _args13[6] !== undefined ? _args13[6] : null;
stopLossPrice = _args13.length > 7 && _args13[7] !== undefined ? _args13[7] : null;
direction = null;
if (type === _candlestickChart.TREND.LONG) {
direction = 'sell';
} else if (type === _candlestickChart.TREND.SHORT) {
direction = 'buy';
}
return _context13.abrupt("return", this._setOrder(symbol, leverRate, clientOrderId, direction, volume, openPrice, takeProfitPrice, stopLossPrice, 'close', 'swap_cross_order'));
case 5:
case "end":
return _context13.stop();
}
}, _callee13, this);
}));
function setCrossCloseOrder(_x35, _x36, _x37, _x38, _x39, _x40) {
return _setCrossCloseOrder.apply(this, arguments);
}
return setCrossCloseOrder;
}()
}, {
key: "setIsolatedCloseOrder",
value: function () {
var _setIsolatedCloseOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee14(symbol, leverRate, clientOrderId, type, volume, openPrice) {
var takeProfitPrice,
stopLossPrice,
direction,
_args14 = arguments;
return _regeneratorRuntime().wrap(function _callee14$(_context14) {
while (1) switch (_context14.prev = _context14.next) {
case 0:
takeProfitPrice = _args14.length > 6 && _args14[6] !== undefined ? _args14[6] : null;
stopLossPrice = _args14.length > 7 && _args14[7] !== undefined ? _args14[7] : null;
direction = null;
if (type === _candlestickChart.TREND.LONG) {
direction = 'sell';
} else if (type === _candlestickChart.TREND.SHORT) {
direction = 'buy';
}
return _context14.abrupt("return", this._setOrder(symbol, leverRate, clientOrderId, direction, volume, openPrice, takeProfitPrice, stopLossPrice, 'close', 'swap_order'));
case 5:
case "end":
return _context14.stop();
}
}, _callee14, this);
}));
function setIsolatedCloseOrder(_x41, _x42, _x43, _x44, _x45, _x46) {
return _setIsolatedCloseOrder.apply(this, arguments);
}
return setIsolatedCloseOrder;
}()
}, {
key: "_getOrder",
value: function () {
var _getOrder2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee15(symbol, clientOrderId, uri) {
var url, res;
return _regeneratorRuntime().wrap(function _callee15$(_context15) {
while (1) switch (_context15.prev = _context15.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context15.next = 3;
return this.client.parseBigInt().post(url, {
client_order_id: clientOrderId,
contract_code: symbol
}, false);
case 3:
res = _context15.sent;
if (!(res.status === 'ok')) {
_context15.next = 6;
break;
}
return _context15.abrupt("return", res.data);
case 6:
throw new Error(res.err_msg);
case 7:
case "end":
return _context15.stop();
}
}, _callee15, this);
}));
function _getOrder(_x47, _x48, _x49) {
return _getOrder2.apply(this, arguments);
}
return _getOrder;
}()
}, {
key: "getCrossOrder",
value: function () {
var _getCrossOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee16(symbol, clientOrderId) {
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
while (1) switch (_context16.prev = _context16.next) {
case 0:
return _context16.abrupt("return", this._getOrder(symbol, clientOrderId, 'swap_cross_order_info'));
case 1:
case "end":
return _context16.stop();
}
}, _callee16, this);
}));
function getCrossOrder(_x50, _x51) {
return _getCrossOrder.apply(this, arguments);
}
return getCrossOrder;
}()
}, {
key: "getIsolatedOrder",
value: function () {
var _getIsolatedOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee17(symbol, clientOrderId) {
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
while (1) switch (_context17.prev = _context17.next) {
case 0:
return _context17.abrupt("return", this._getOrder(symbol, clientOrderId, 'swap_order_info'));
case 1:
case "end":
return _context17.stop();
}
}, _callee17, this);
}));
function getIsolatedOrder(_x52, _x53) {
return _getIsolatedOrder.apply(this, arguments);
}
return getIsolatedOrder;
}()
}, {
key: "_setCancelOrder",
value: function () {
var _setCancelOrder2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee18(symbol, clientOrderId, uri) {
var url, res;
return _regeneratorRuntime().wrap(function _callee18$(_context18) {
while (1) switch (_context18.prev = _context18.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context18.next = 3;
return this.client.parseBigInt().post(url, {
contract_code: symbol,
client_order_id: clientOrderId
}, false);
case 3:
res = _context18.sent;
if (!(res.status === 'ok')) {
_context18.next = 6;
break;
}
return _context18.abrupt("return", res.data);
case 6:
throw new Error(res.err_msg);
case 7:
case "end":
return _context18.stop();
}
}, _callee18, this);
}));
function _setCancelOrder(_x54, _x55, _x56) {
return _setCancelOrder2.apply(this, arguments);
}
return _setCancelOrder;
}()
}, {
key: "setCrossCancelOrder",
value: function () {
var _setCrossCancelOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee19(symbol, clientOrderId) {
return _regeneratorRuntime().wrap(function _callee19$(_context19) {
while (1) switch (_context19.prev = _context19.next) {
case 0:
return _context19.abrupt("return", this._setCancelOrder(symbol, clientOrderId, 'swap_cross_cancel'));
case 1:
case "end":
return _context19.stop();
}
}, _callee19, this);
}));
function setCrossCancelOrder(_x57, _x58) {
return _setCrossCancelOrder.apply(this, arguments);
}
return setCrossCancelOrder;
}()
}, {
key: "setIsolatedCancelOrder",
value: function () {
var _setIsolatedCancelOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee20(symbol, clientOrderId) {
return _regeneratorRuntime().wrap(function _callee20$(_context20) {
while (1) switch (_context20.prev = _context20.next) {
case 0:
return _context20.abrupt("return", this._setCancelOrder(symbol, clientOrderId, 'swap_cancel'));
case 1:
case "end":
return _context20.stop();
}
}, _callee20, this);
}));
function setIsolatedCancelOrder(_x59, _x60) {
return _setIsolatedCancelOrder.apply(this, arguments);
}
return setIsolatedCancelOrder;
}()
}, {
key: "_setCancelAllOpenOrder",
value: function () {
var _setCancelAllOpenOrder2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee21(symbol, uri) {
var url, res, errorString;
return _regeneratorRuntime().wrap(function _callee21$(_context21) {
while (1) switch (_context21.prev = _context21.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context21.next = 3;
return this.client.parseBigInt().post(url, {
contract_code: symbol,
offset: 'open'
}, false);
case 3:
res = _context21.sent;
if (!(res.status === 'ok')) {
_context21.next = 6;
break;
}
return _context21.abrupt("return", res.data);
case 6:
errorString = res.err_msg.toString();
if (!errorString.includes('No cancellable orders')) {
_context21.next = 9;
break;
}
return _context21.abrupt("return", {
successes: true
});
case 9:
throw new Error(errorString);
case 10:
case "end":
return _context21.stop();
}
}, _callee21, this);
}));
function _setCancelAllOpenOrder(_x61, _x62) {
return _setCancelAllOpenOrder2.apply(this, arguments);
}
return _setCancelAllOpenOrder;
}()
}, {
key: "setCrossCancelAllOpenOrder",
value: function () {
var _setCrossCancelAllOpenOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee22(symbol) {
return _regeneratorRuntime().wrap(function _callee22$(_context22) {
while (1) switch (_context22.prev = _context22.next) {
case 0:
return _context22.abrupt("return", this._setCancelAllOpenOrder(symbol, 'swap_cross_cancelall'));
case 1:
case "end":
return _context22.stop();
}
}, _callee22, this);
}));
function setCrossCancelAllOpenOrder(_x63) {
return _setCrossCancelAllOpenOrder.apply(this, arguments);
}
return setCrossCancelAllOpenOrder;
}()
}, {
key: "setIsolatedCancelAllOpenOrder",
value: function () {
var _setIsolatedCancelAllOpenOrder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee23(symbol) {
return _regeneratorRuntime().wrap(function _callee23$(_context23) {
while (1) switch (_context23.prev = _context23.next) {
case 0:
return _context23.abrupt("return", this._setCancelAllOpenOrder(symbol, 'swap_cancelall'));
case 1:
case "end":
return _context23.stop();
}
}, _callee23, this);
}));
function setIsolatedCancelAllOpenOrder(_x64) {
return _setIsolatedCancelAllOpenOrder.apply(this, arguments);
}
return setIsolatedCancelAllOpenOrder;
}()
}, {
key: "_getPositionInfo",
value: function () {
var _getPositionInfo2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee24(symbol, uri) {
var url, res;
return _regeneratorRuntime().wrap(function _callee24$(_context24) {
while (1) switch (_context24.prev = _context24.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
_context24.next = 3;
return this.client.post(url, {
contract_code: symbol
}, false);
case 3:
res = _context24.sent;
if (!(res.status === 'ok')) {
_context24.next = 6;
break;
}
return _context24.abrupt("return", res.data);
case 6:
throw new Error(res.err_msg);
case 7:
case "end":
return _context24.stop();
}
}, _callee24, this);
}));
function _getPositionInfo(_x65, _x66) {
return _getPositionInfo2.apply(this, arguments);
}
return _getPositionInfo;
}()
}, {
key: "getCrossPositionInfo",
value: function () {
var _getCrossPositionInfo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee25(symbol) {
return _regeneratorRuntime().wrap(function _callee25$(_context25) {
while (1) switch (_context25.prev = _context25.next) {
case 0:
return _context25.abrupt("return", this._getPositionInfo(symbol, 'swap_cross_position_info'));
case 1:
case "end":
return _context25.stop();
}
}, _callee25, this);
}));
function getCrossPositionInfo(_x67) {
return _getCrossPositionInfo.apply(this, arguments);
}
return getCrossPositionInfo;
}()
}, {
key: "getIsolatedPositionInfo",
value: function () {
var _getIsolatedPositionInfo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee26(symbol) {
return _regeneratorRuntime().wrap(function _callee26$(_context26) {
while (1) switch (_context26.prev = _context26.next) {
case 0:
return _context26.abrupt("return", this._getPositionInfo(symbol, 'swap_position_info'));
case 1:
case "end":
return _context26.stop();
}
}, _callee26, this);
}));
function getIsolatedPositionInfo(_x68) {
return _getIsolatedPositionInfo.apply(this, arguments);
}
return getIsolatedPositionInfo;
}()
}, {
key: "_setLightningClose",
value: function () {
var _setLightningClose2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee27(symbol, direction, clientOrderId, uri) {
var url, data, res;
return _regeneratorRuntime().wrap(function _callee27$(_context27) {
while (1) switch (_context27.prev = _context27.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v1', uri);
data = {
contract_code: symbol,
direction: direction === 'sell' ? 'buy' : 'sell'
};
if (clientOrderId) {
data.client_order_id = clientOrderId;
}
_context27.next = 5;
return this.client.parseBigInt().post(url, data, false);
case 5:
res = _context27.sent;
if (!(res.status === 'ok')) {
_context27.next = 8;
break;
}
return _context27.abrupt("return", res.data);
case 8:
throw new Error(res.err_msg);
case 9:
case "end":
return _context27.stop();
}
}, _callee27, this);
}));
function _setLightningClose(_x69, _x70, _x71, _x72) {
return _setLightningClose2.apply(this, arguments);
}
return _setLightningClose;
}()
}, {
key: "setCrossLightningClose",
value: function () {
var _setCrossLightningClose = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee28(symbol, direction) {
var clientId,
_args28 = arguments;
return _regeneratorRuntime().wrap(function _callee28$(_context28) {
while (1) switch (_context28.prev = _context28.next) {
case 0:
clientId = _args28.length > 2 && _args28[2] !== undefined ? _args28[2] : null;
return _context28.abrupt("return", this._setLightningClose(symbol, direction, clientId, 'swap_cross_lightning_close_position'));
case 2:
case "end":
return _context28.stop();
}
}, _callee28, this);
}));
function setCrossLightningClose(_x73, _x74) {
return _setCrossLightningClose.apply(this, arguments);
}
return setCrossLightningClose;
}()
}, {
key: "setIsolatedLightningClose",
value: function () {
var _setIsolatedLightningClose = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee29(symbol, direction) {
var clientId,
_args29 = arguments;
return _regeneratorRuntime().wrap(function _callee29$(_context29) {
while (1) switch (_context29.prev = _context29.next) {
case 0:
clientId = _args29.length > 2 && _args29[2] !== undefined ? _args29[2] : null;
return _context29.abrupt("return", this._setLightningClose(symbol, direction, clientId, 'swap_lightning_close_position'));
case 2:
case "end":
return _context29.stop();
}
}, _callee29, this);
}));
function setIsolatedLightningClose(_x75, _x76) {
return _setIsolatedLightningClose.apply(this, arguments);
}
return setIsolatedLightningClose;
}()
}, {
key: "_getHistoryOrders",
value: function () {
var _getHistoryOrders2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee30(symbol, uri) {
var url, res;
return _regeneratorRuntime().wrap(function _callee30$(_context30) {
while (1) switch (_context30.prev = _context30.next) {
case 0:
url = this._encodeLinearSwapURL('POST', 'v3', uri);
_context30.next = 3;
return this.client.parseBigInt().post(url, {
contract: symbol,
trade_type: 0,
type: 1,
status: 0
}, false);
case 3:
res = _context30.sent;
if (!(res.msg === 'ok')) {
_context30.next = 6;
break;
}
return _context30.abrupt("return", res.data);
case 6:
throw new Error(res.err_msg);
case 7:
case "end":
return _context30.stop();
}
}, _callee30, this);
}));
function _getHistoryOrders(_x77, _x78) {
return _getHistoryOrders2.appl