@walletpack/core
Version:
> TODO: description
426 lines (360 loc) • 13.2 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var Actions = _interopRequireWildcard(require("../../store/constants"));
var _BalanceService = _interopRequireDefault(require("../blockchain/BalanceService"));
var _BackendApiService = require("./BackendApiService");
var _Token = _interopRequireDefault(require("../../models/Token"));
var _StoreService = _interopRequireDefault(require("../utility/StoreService"));
var _Framework = _interopRequireDefault(require("../utility/Framework"));
var timeout = function timeout(rq) {
var caughtValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
return Promise.race([new Promise(function (resolve) {
return setTimeout(function () {
return resolve(caughtValue);
}, 10000);
}), rq["catch"](function () {
return caughtValue;
})]);
};
var watchers = [];
var watchTimeout;
var ExchangeService =
/*#__PURE__*/
function () {
function ExchangeService() {
(0, _classCallCheck2["default"])(this, ExchangeService);
}
(0, _createClass2["default"])(ExchangeService, null, [{
key: "pairs",
value: function () {
var _pairs = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee(token) {
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", timeout((0, _BackendApiService.POST)('exchange/pairs', {
token: token
})).then(function (pairs) {
if (!pairs) return [];
Object.keys(pairs).map(function (key) {
return pairs[key].map(function (x) {
return x.token = _Token["default"].fromJson(x.token);
});
});
return pairs;
}));
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function pairs(_x) {
return _pairs.apply(this, arguments);
}
return pairs;
}()
}, {
key: "rate",
value: function () {
var _rate = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee2(token, other, service) {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt("return", timeout((0, _BackendApiService.POST)('exchange/rate', {
token: token,
other: other,
service: service
})));
case 1:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
function rate(_x2, _x3, _x4) {
return _rate.apply(this, arguments);
}
return rate;
}()
}, {
key: "order",
value: function () {
var _order = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee3(service, token, other, amount, from, to) {
var returnsErrors,
_args3 = arguments;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
returnsErrors = _args3.length > 6 && _args3[6] !== undefined ? _args3[6] : true;
return _context3.abrupt("return", timeout((0, _BackendApiService.POST)('exchange/order', {
service: service,
token: token,
other: other,
amount: amount,
from: from,
to: to,
returnsErrors: returnsErrors
})));
case 2:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
function order(_x5, _x6, _x7, _x8, _x9, _x10) {
return _order.apply(this, arguments);
}
return order;
}()
}, {
key: "accepted",
value: function () {
var _accepted = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee4(id) {
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt("return", timeout((0, _BackendApiService.GET)("exchange/accepted/".concat(id))));
case 1:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
function accepted(_x11) {
return _accepted.apply(this, arguments);
}
return accepted;
}()
}, {
key: "cancelled",
value: function () {
var _cancelled = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee5(id) {
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
return _context5.abrupt("return", timeout((0, _BackendApiService.GET)("exchange/cancelled/".concat(id))));
case 1:
case "end":
return _context5.stop();
}
}
}, _callee5);
}));
function cancelled(_x12) {
return _cancelled.apply(this, arguments);
}
return cancelled;
}()
}, {
key: "orderStatus",
value: function () {
var _orderStatus = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee6(id) {
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
return _context6.abrupt("return", timeout((0, _BackendApiService.GET)("exchange/order/".concat(id)).then(function (res) {
return res.updated.status;
})));
case 1:
case "end":
return _context6.stop();
}
}
}, _callee6);
}));
function orderStatus(_x13) {
return _orderStatus.apply(this, arguments);
}
return orderStatus;
}()
}, {
key: "stablePaths",
value: function () {
var _stablePaths = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee7() {
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
return _context7.abrupt("return", timeout((0, _BackendApiService.GET)("exchange/stabilize/paths"), []));
case 1:
case "end":
return _context7.stop();
}
}
}, _callee7);
}));
function stablePaths() {
return _stablePaths.apply(this, arguments);
}
return stablePaths;
}()
}, {
key: "pairable",
value: function () {
var _pairable = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee8() {
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
return _context8.abrupt("return", timeout((0, _BackendApiService.GET)("exchange/pairable"), []));
case 1:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
function pairable() {
return _pairable.apply(this, arguments);
}
return pairable;
}()
}, {
key: "watch",
value: function watch(history) {
watchers.push(history);
this.checkExchanges();
return true;
}
}, {
key: "checkExchanges",
value: function () {
var _checkExchanges = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee9() {
var _this = this;
var _loop, i;
return _regenerator["default"].wrap(function _callee9$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
clearTimeout(watchTimeout);
if (watchers.length) {
_context10.next = 3;
break;
}
return _context10.abrupt("return");
case 3:
_loop =
/*#__PURE__*/
_regenerator["default"].mark(function _loop(i) {
var history, status, accounts, n;
return _regenerator["default"].wrap(function _loop$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
history = watchers[i];
_context9.next = 3;
return _this.orderStatus(history.orderDetails.id);
case 3:
status = _context9.sent;
if (!(status !== history.status)) {
_context9.next = 20;
break;
}
history.status = status;
_context9.next = 8;
return _StoreService["default"].get().dispatch(Actions.UPDATE_HISTORY, history);
case 8:
if (!(status === 'complete')) {
_context9.next = 20;
break;
}
// TODO: need to solve this with an injected sound service
//SoundService.ding();
_Framework["default"].pushNotification('Exchange Complete', "Your token exchange has just completed.");
watchers = watchers.filter(function (x) {
return x.id !== history.id;
});
accounts = _StoreService["default"].get().state.scatter.keychain.accounts.filter(function (x) {
return x.sendable() === history.to;
});
if (!accounts.length) {
_context9.next = 20;
break;
}
n = 0;
case 14:
if (!(n < accounts.length)) {
_context9.next = 20;
break;
}
_context9.next = 17;
return _BalanceService["default"].loadBalancesFor(accounts[n]);
case 17:
n++;
_context9.next = 14;
break;
case 20:
case "end":
return _context9.stop();
}
}
}, _loop);
});
i = 0;
case 5:
if (!(i < watchers.length)) {
_context10.next = 10;
break;
}
return _context10.delegateYield(_loop(i), "t0", 7);
case 7:
i++;
_context10.next = 5;
break;
case 10:
setTimeout(function () {
return _this.checkExchanges();
}, 1000 * 30);
case 11:
case "end":
return _context10.stop();
}
}
}, _callee9);
}));
function checkExchanges() {
return _checkExchanges.apply(this, arguments);
}
return checkExchanges;
}()
}]);
return ExchangeService;
}();
exports["default"] = ExchangeService;