UNPKG

@rarimo/shared

Version:

Utility functions, types and constants shared across Rarimo packages.

82 lines (81 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "getDestinationTx", { enumerable: true, get: function() { return getDestinationTx; } }); const _jac = require(/*#__PURE__*/ "@distributedlab/jac"); const _entities = require("../entities"); const _helpers = require("../helpers"); const _api = require("./api"); function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _async_to_generator(fn) { return function() { var self = this, args = arguments; return new Promise(function(resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } const DESTINATION_TX_PULL_INTERVAL = 2000; const DESTINATION_TX_PULL_MAX_RETRIES = 20; const pullError = new _entities.RuntimeError('Failed to fetch destination transaction chain'); const getDestinationTx = function() { var _ref = _async_to_generator(function*(sourceChain, sourceTxHash) { let retries = 0; let transaction = null; while(!transaction){ retries += 1; if (retries > DESTINATION_TX_PULL_MAX_RETRIES) throw pullError; transaction = yield fetchDestinationTx(sourceChain.name, sourceTxHash); if (!transaction) yield (0, _helpers.sleep)(DESTINATION_TX_PULL_INTERVAL); } return { hash: transaction.id, status: transaction.status }; }); return function getDestinationTx(sourceChain, sourceTxHash) { return _ref.apply(this, arguments); }; }(); const fetchDestinationTx = function() { var _ref = _async_to_generator(function*(chainId, txHash) { try { const { data } = yield _api.coreApi.get(`/v1/chains/${chainId}/transactions/${txHash}`); return data; } catch (e) { const status = e.httpStatus; if (status === _jac.HTTP_STATUS_CODES.NOT_FOUND) return null; throw e; } }); return function fetchDestinationTx(chainId, txHash) { return _ref.apply(this, arguments); }; }(); //# sourceMappingURL=core.js.map