UNPKG

evtjs

Version:

Javascript API Bindings for the everiToken blockchain.

1,651 lines (1,308 loc) 101 kB
"use strict"; var _promise = require("babel-runtime/core-js/promise"); var _promise2 = _interopRequireDefault(_promise); var _assign = require("babel-runtime/core-js/object/assign"); var _assign2 = _interopRequireDefault(_assign); var _isInteger = require("babel-runtime/core-js/number/is-integer"); var _isInteger2 = _interopRequireDefault(_isInteger); var _getIterator2 = require("babel-runtime/core-js/get-iterator"); var _getIterator3 = _interopRequireDefault(_getIterator2); var _stringify = require("babel-runtime/core-js/json/stringify"); var _stringify2 = _interopRequireDefault(_stringify); var _regenerator = require("babel-runtime/regenerator"); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _typeof2 = require("babel-runtime/helpers/typeof"); var _typeof3 = _interopRequireDefault(_typeof2); var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require("babel-runtime/helpers/createClass"); var _createClass3 = _interopRequireDefault(_createClass2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var ecc = require("./ecc/index"); var signHash = ecc.signHash; var EvtConfig = require("./evtConfig"); var _require = require("./fetch"), fetch = _require.fetch; var ByteBuffer = require("bytebuffer"); var _require2 = require("./action"), EvtAction = _require2.EvtAction; var Logger = require("./logger"); var EvtKey = require("./key"); var EvtLink = require("./evtLink"); var Structs = require("./structs"); var Fcbuffer = require("evt-fcbuffer"); var structs = Structs({}); /** * APICaller for everiToken */ var APICaller = function () { /** * Creates a new APICaller. * @param {EvtConfig} config */ function APICaller(config) { (0, _classCallCheck3.default)(this, APICaller); if (!config) { config = new EvtConfig(); } else if ((typeof config === "undefined" ? "undefined" : (0, _typeof3.default)(config)) == "object" && config != null && !(config instanceof EvtConfig)) { config = new EvtConfig(config); } /** @member {EvtConfig} evtConfig */ this.config = config; if (!this.config.signProvider) { this.config.signProvider = defaultSignProvider(this, this.config); } this.getNodeTimestamp(); } /** * Get the time of the server. */ (0, _createClass3.default)(APICaller, [{ key: "getNodeTimestamp", value: function () { var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() { return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(this.timeDiff == null && this.config.syncNodeTime)) { _context.next = 9; break; } _context.prev = 1; _context.next = 4; return this.getInfo({ timeout: 2000 }); case 4: _context.next = 9; break; case 6: _context.prev = 6; _context.t0 = _context["catch"](1); console.log(_context.t0); case 9: if (!(this.timeDiff == null)) { _context.next = 11; break; } return _context.abrupt("return", new Date().getTime()); case 11: return _context.abrupt("return", new Date().getTime() + this.timeDiff); case 12: case "end": return _context.stop(); } } }, _callee, this, [[1, 6]]); })); function getNodeTimestamp() { return _ref.apply(this, arguments); } return getNodeTimestamp; }() /** * Call everiToken APIs directly, not suggested to use by user * @param {*} request */ }, { key: "__callAPI", value: function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(request) { var url, requestOptions, res, ret; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: url = this.config.endpoint.protocol + "://" + this.config.endpoint.host + ":" + this.config.endpoint.port + request.url; Logger.verbose("[fetch] begin sending request: " + url + ": " + (0, _stringify2.default)(request, null, 4)); requestOptions = { method: request.method, body: request.body ? (0, _stringify2.default)(request.body) : undefined, headers: { "Content-Type": "application/json" }, networkTimeout: request.networkTimeout || this.config.networkTimeout }; res = void 0, ret = void 0; if (!this.config.httpRequestHook) { _context2.next = 11; break; } _context2.next = 7; return this.config.httpRequestHook(url, requestOptions); case 7: res = _context2.sent; if (typeof res == "string") { ret = JSON.parse(res); } else { ret = res; } _context2.next = 17; break; case 11: _context2.next = 13; return fetch(url, requestOptions); case 13: res = _context2.sent; _context2.next = 16; return res.json(); case 16: ret = _context2.sent; case 17: if (ret && ret.code && ret.message && ret.error) { this.__throwServerResponseError(ret); } if (ret) { _context2.next = 20; break; } throw new Error("No response or not a valid json from http server"); case 20: return _context2.abrupt("return", ret); case 21: case "end": return _context2.stop(); } } }, _callee2, this); })); function __callAPI(_x) { return _ref2.apply(this, arguments); } return __callAPI; }() /** * get information from everiToken chain node */ }, { key: "getInfo", value: function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(options) { var info; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: if (!(this.__lastTimeGetInfo && this.__cachedInfo)) { _context3.next = 3; break; } if (!(new Date().valueOf() - this.__lastTimeGetInfo < 5000)) { _context3.next = 3; break; } return _context3.abrupt("return", this.__cachedInfo); case 3: this.__lastTimeGetInfo = new Date().valueOf(); options = options || {}; _context3.next = 7; return this.__callAPI({ url: "/v1/chain/get_info", method: "GET", networkTimeout: options.timeout || this.config.networkTimeout }); case 7: info = _context3.sent; // do not cache the result because it may expire at any time this.__cachedInfo = info; // check version of remote net if (!(!info.evt_api_version.startsWith("2.") && !info.evt_api_version.startsWith("3.") && !info.evt_api_version.startsWith("4."))) { _context3.next = 11; break; } throw new Error("[Fatal] The API version of remote net (" + info.evt_api_version + ") is not compatible with current evtjs's version. Please upgrade your evtjs's version."); case 11: this.__node_evt_api_version = info.evt_api_version; if (info.enabled_plugins && !info.enabled_plugins.includes("evt::history_plugin")) { this.__node_history_plugin_enabled = false; } else { this.__node_history_plugin_enabled = true; } if (info.head_block_time.length == 19) { info.head_block_time += "Z"; } this.timeDiff = new Date(info.head_block_time).getTime() + 70 - new Date().getTime(); // sync time to global if (global || window) { (global || window).__evtjs_timeDiff = this.timeDiff; } return _context3.abrupt("return", info); case 17: case "end": return _context3.stop(); } } }, _callee3, this); })); function getInfo(_x2) { return _ref3.apply(this, arguments); } return getInfo; }() /** * get domain list a user created, make sure you have history_plugin enabled on the chain node * @param {*} publicKeys a array or a single value which represents public keys you want to query */ }, { key: "getCreatedDomains", value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(publicKeys) { var res; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return this.__callAPI({ url: "/v1/history/get_domains", method: "POST", body: { keys: Array.isArray(publicKeys) ? publicKeys : [publicKeys] }, sign: false // no need to sign }); case 2: res = _context4.sent; if (!Array.isArray(res)) { _context4.next = 7; break; } return _context4.abrupt("return", res.map(function (x) { return { name: x }; })); case 7: this.__throwServerResponseError(res); case 8: case "end": return _context4.stop(); } } }, _callee4, this); })); function getCreatedDomains(_x3) { return _ref4.apply(this, arguments); } return getCreatedDomains; }() /** * Provide all the public keys its has and this API will response with all the symbol ids of the fungibles that account create. * @param {*} publicKeys a array or a single value which represents public keys you want to query */ }, { key: "getCreatedFungibles", value: function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(publicKeys) { var res; return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.__callAPI({ url: "/v1/history/get_fungibles", method: "POST", body: { keys: Array.isArray(publicKeys) ? publicKeys : [publicKeys] }, sign: false // no need to sign }); case 2: res = _context5.sent; if (!Array.isArray(res)) { _context5.next = 7; break; } return _context5.abrupt("return", { ids: res }); case 7: this.__throwServerResponseError(res); case 8: case "end": return _context5.stop(); } } }, _callee5, this); })); function getCreatedFungibles(_x4) { return _ref5.apply(this, arguments); } return getCreatedFungibles; }() /** * Get required keys for suspended transactions * @param {string} proposalName The proposal name you want to sign * @param {string[]} availableKeys array of public keys you own */ }, { key: "getRequiredKeysForSuspendedTransaction", value: function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(proposalName, availableKeys) { var res; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: if (!(!proposalName || typeof proposalName !== "string")) { _context6.next = 2; break; } throw new Error("invalid proposalName"); case 2: if (!(!availableKeys || !Array.isArray(availableKeys))) { _context6.next = 4; break; } throw new Error("invalid availableKeys"); case 4: _context6.next = 6; return this.__callAPI({ url: "/v1/chain/get_suspend_required_keys", method: "POST", body: { name: proposalName, available_keys: availableKeys }, sign: false // no need to sign }); case 6: res = _context6.sent; if (!(res && res.required_keys && Array.isArray(res.required_keys))) { _context6.next = 11; break; } return _context6.abrupt("return", res.required_keys); case 11: this.__throwServerResponseError(res); case 12: case "end": return _context6.stop(); } } }, _callee6, this); })); function getRequiredKeysForSuspendedTransaction(_x5, _x6) { return _ref6.apply(this, arguments); } return getRequiredKeysForSuspendedTransaction; }() /** * Get detail information of a suspended transaction * @param {string} proposalName The proposal name you want to query */ }, { key: "getSuspendedTransactionDetail", value: function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(proposalName) { var res; return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: if (!(!proposalName || typeof proposalName !== "string")) { _context7.next = 2; break; } throw new Error("invalid proposalName"); case 2: _context7.next = 4; return this.__callAPI({ url: "/v1/evt/get_suspend", method: "POST", body: { name: proposalName }, sign: false // no need to sign }); case 4: res = _context7.sent; if (!(res && res.name && res.proposer)) { _context7.next = 9; break; } return _context7.abrupt("return", res); case 9: this.__throwServerResponseError(res); case 10: case "end": return _context7.stop(); } } }, _callee7, this); })); function getSuspendedTransactionDetail(_x7) { return _ref7.apply(this, arguments); } return getSuspendedTransactionDetail; }() /** * get a list of groups, each group in it must has a group key which is contained by provided public keys. Make sure you have history_plugin enabled on the chain node * @param {*} publicKeys a array or a single value which represents public keys you want to query */ }, { key: "getManagedGroups", value: function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(publicKeys) { var res; return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return this.__callAPI({ url: "/v1/history/get_groups", method: "POST", body: { keys: Array.isArray(publicKeys) ? publicKeys : [publicKeys] }, sign: false // no need to sign }); case 2: res = _context8.sent; if (!Array.isArray(res)) { _context8.next = 7; break; } return _context8.abrupt("return", res.map(function (x) { return { name: x }; })); case 7: this.__throwServerResponseError(res); case 8: case "end": return _context8.stop(); } } }, _callee8, this); })); function getManagedGroups(_x8) { return _ref8.apply(this, arguments); } return getManagedGroups; }() /** * get owned token list for accounts. Make sure you have history_plugin enabled on the chain node * @param {*} publicKeys a array or a single value which represents public keys you want to query * @param {boolean} groupByDomain whether group the returned values by domain, only avaiable for chain version >= 3 */ }, { key: "getOwnedTokens", value: function () { var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(publicKeys) { var groupByDomain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var res, ret, key, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, value; return _regenerator2.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.next = 2; return this.__callAPI({ url: "/v1/history/get_tokens", method: "POST", body: { keys: Array.isArray(publicKeys) ? publicKeys : [publicKeys] }, sign: false // no need to sign }); case 2: res = _context9.sent; if (!Array.isArray(res)) { _context9.next = 9; break; } if (!groupByDomain) { _context9.next = 6; break; } throw new Error("chain version < 3 not support group by domain"); case 6: return _context9.abrupt("return", res.map(function (x) { return { name: x.substr(x.lastIndexOf("-") + 1), domain: x.substr(0, x.lastIndexOf("-")) }; })); case 9: if (!res.error) { _context9.next = 13; break; } this.__throwServerResponseError(res); _context9.next = 41; break; case 13: if (!groupByDomain) { _context9.next = 15; break; } return _context9.abrupt("return", res); case 15: // chain version >= 3 ret = []; _context9.t0 = _regenerator2.default.keys(res); case 17: if ((_context9.t1 = _context9.t0()).done) { _context9.next = 40; break; } key = _context9.t1.value; _iteratorNormalCompletion = true; _didIteratorError = false; _iteratorError = undefined; _context9.prev = 22; for (_iterator = (0, _getIterator3.default)(res[key]); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { value = _step.value; ret.push({ name: value, domain: key }); } _context9.next = 30; break; case 26: _context9.prev = 26; _context9.t2 = _context9["catch"](22); _didIteratorError = true; _iteratorError = _context9.t2; case 30: _context9.prev = 30; _context9.prev = 31; if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } case 33: _context9.prev = 33; if (!_didIteratorError) { _context9.next = 36; break; } throw _iteratorError; case 36: return _context9.finish(33); case 37: return _context9.finish(30); case 38: _context9.next = 17; break; case 40: return _context9.abrupt("return", ret); case 41: case "end": return _context9.stop(); } } }, _callee9, this, [[22, 26, 30, 38], [31,, 33, 37]]); })); function getOwnedTokens(_x10) { return _ref9.apply(this, arguments); } return getOwnedTokens; }() /** * get specific token's detail information. * @param {string} domain the domain to query * @param {string} name the name to query */ }, { key: "getToken", value: function () { var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(domain, name) { var res; return _regenerator2.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: _context10.next = 2; return this.__callAPI({ url: "/v1/evt/get_token", method: "POST", body: { domain: domain, name: name }, sign: false // no need to sign }); case 2: res = _context10.sent; if (!(!res.code || !res.error)) { _context10.next = 7; break; } return _context10.abrupt("return", res); case 7: this.__throwServerResponseError(res); case 8: case "end": return _context10.stop(); } } }, _callee10, this); })); function getToken(_x11, _x12) { return _ref10.apply(this, arguments); } return getToken; }() /** * batch get tokens from one domain, the max allowed to get in one request to get is 100. * @param {string} domain the domain to query * @param {number} skip the offset of request data * @param {number} take the amount of data to get */ }, { key: "getTokens", value: function () { var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(domain, skip, take) { var res; return _regenerator2.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: if (!(typeof domain !== "string" || !(0, _isInteger2.default)(skip) || !(0, _isInteger2.default)(take))) { _context11.next = 2; break; } throw new Error("please check your datatype of params"); case 2: _context11.next = 4; return this.__callAPI({ url: "/v1/evt/get_tokens", method: "POST", body: { domain: domain, skip: skip, take: take }, sign: false // no need to sign }); case 4: res = _context11.sent; if (!(!res.code || !res.error)) { _context11.next = 9; break; } return _context11.abrupt("return", res); case 9: this.__throwServerResponseError(res); case 10: case "end": return _context11.stop(); } } }, _callee11, this); })); function getTokens(_x13, _x14, _x15) { return _ref11.apply(this, arguments); } return getTokens; }() /** * Query actions by domain, key and action names. Make sure you have history_plugin enabled on the chain node * @param {*} params */ }, { key: "getActions", value: function () { var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(params) { var res; return _regenerator2.default.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: if (!((typeof params === "undefined" ? "undefined" : (0, _typeof3.default)(params)) !== "object")) { _context12.next = 2; break; } throw new Error("invalid params"); case 2: if (!(!params || !params.domain)) { _context12.next = 4; break; } throw new Error("invalid params: domain is required"); case 4: _context12.next = 6; return this.__callAPI({ url: "/v1/history/get_actions", method: "POST", body: params, sign: false // no need to sign }); case 6: res = _context12.sent; if (!Array.isArray(res)) { _context12.next = 11; break; } return _context12.abrupt("return", res); case 11: this.__throwServerResponseError(res); case 12: case "end": return _context12.stop(); } } }, _callee12, this); })); function getActions(_x16) { return _ref12.apply(this, arguments); } return getActions; }() /** * get detail information about a transaction by its id. * @param {string} id the id of the transaction. * @param {string} blockNum (optional) the block num of the transaction. If not provided, the system will find it for you. * @param {object} options (optional) addtional options. available field: usingHistoryPlugin - (default true) whether to use history plugin. If you want to query transactions on a node that doesn't have history plugin, please set it to false. But in this case you can't query old transactions. */ }, { key: "getTransactionDetailById", value: function () { var _ref13 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13(id) { var blockNum = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined; var _options, res; return _regenerator2.default.wrap(function _callee13$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: if (!(typeof id !== "string" || !id)) { _context13.next = 2; break; } throw new Error("invalid transaction id"); case 2: _options = { usingHistoryPlugin: true }; if (options) { (0, _assign2.default)(_options, options); } _context13.next = 6; return this.__callAPI({ url: _options.usingHistoryPlugin == false ? "/v1/chain/get_transaction" : "/v1/history/get_transaction", method: "POST", body: { id: id, block_num: blockNum }, sign: false // no need to sign }); case 6: res = _context13.sent; if (!(res && res.id && res.transaction)) { _context13.next = 11; break; } return _context13.abrupt("return", res); case 11: this.__throwServerResponseError(res); case 12: case "end": return _context13.stop(); } } }, _callee13, this); })); function getTransactionDetailById(_x19) { return _ref13.apply(this, arguments); } return getTransactionDetailById; }() /** * query all the actions of one transaction by its id. Different from `getTransactionDetailById`, this function will return all the actions including actions generated by the chain. * @param {string} id the id of the transaction. */ }, { key: "getTransactionActions", value: function () { var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee14(id) { var res; return _regenerator2.default.wrap(function _callee14$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: if (!(typeof id !== "string" || !id)) { _context14.next = 2; break; } throw new Error("invalid transaction id"); case 2: _context14.next = 4; return this.__callAPI({ url: "/v1/history/get_transaction_actions", method: "POST", body: { id: id }, sign: false // no need to sign }); case 4: res = _context14.sent; if (res) { _context14.next = 7; break; } return _context14.abrupt("return", []); case 7: return _context14.abrupt("return", res); case 8: case "end": return _context14.stop(); } } }, _callee14, this); })); function getTransactionActions(_x20) { return _ref14.apply(this, arguments); } return getTransactionActions; }() /** * query block information by num or id * @param {string} blockNumOrId id or num of the block * @returns {object} information */ }, { key: "getBlockDetail", value: function () { var _ref15 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee15(blockNumOrId) { var res; return _regenerator2.default.wrap(function _callee15$(_context15) { while (1) { switch (_context15.prev = _context15.next) { case 0: if (!(typeof blockNumOrId !== "string" || !blockNumOrId)) { _context15.next = 2; break; } throw new Error("invalid query key, it can be either block_num or block_id"); case 2: _context15.next = 4; return this.__callAPI({ url: "/v1/chain/get_block", method: "POST", body: { block_num_or_id: blockNumOrId }, sign: false // no need to sign }); case 4: res = _context15.sent; if (!(res && res.id && res.block_num)) { _context15.next = 9; break; } return _context15.abrupt("return", res); case 9: this.__throwServerResponseError(res); case 10: case "end": return _context15.stop(); } } }, _callee15, this); })); function getBlockDetail(_x21) { return _ref15.apply(this, arguments); } return getBlockDetail; }() /** * query block header information by num or id * @param {string} key id or num of the block * @returns {object} header state */ /*async getBlockHeaderState(key) { if (typeof key !== "string" || !key) throw new Error("invalid query key, it can be either block_num or block_id"); let res = await this.__callAPI({ url: "/v1/chain/get_block_header_state", method: "POST", body: { block_num_or_id: key }, sign: false // no need to sign }); if (!res.error) { return res; } else { this.__throwServerResponseError(res); } }*/ /** * (deprecated) get transaction id for a linkId * @param {string} id the linkId * @deprecated */ }, { key: "getTransactionIdForLinkId", value: function () { var _ref16 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee16(id) { var res; return _regenerator2.default.wrap(function _callee16$(_context16) { while (1) { switch (_context16.prev = _context16.next) { case 0: console.warn("[warn] getTransactionIdForLinkId is deprecated, please use getStatusForLinkId instead."); if (!(typeof id !== "string" || !id)) { _context16.next = 3; break; } throw new Error("invalid link id"); case 3: _context16.next = 5; return this.__callAPI({ url: "/v1/chain/get_trx_id_for_link_id", method: "POST", body: { link_id: id }, sign: false // no need to sign }); case 5: res = _context16.sent; if (!(res && res.trx_id)) { _context16.next = 10; break; } return _context16.abrupt("return", res); case 10: this.__throwServerResponseError(res); case 11: case "end": return _context16.stop(); } } }, _callee16, this); })); function getTransactionIdForLinkId(_x22) { return _ref16.apply(this, arguments); } return getTransactionIdForLinkId; }() /** * get transaction status of an evtLink. * Note: please check the `linkId` property of the return value. If it is not the same one as your expectation, just drop it. It is possible if you use a same callback for two calls. * @param {string} linkId the linkId * @param {object} options * @returns {object} the status of an evtLink */ }, { key: "getStatusOfEvtLink", value: function () { var _ref17 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee17(options) { var url, res, currentTime, lastTimeInternet, lastError; return _regenerator2.default.wrap(function _callee17$(_context17) { while (1) { switch (_context17.prev = _context17.next) { case 0: if (!((typeof options === "undefined" ? "undefined" : (0, _typeof3.default)(options)) !== "object" || !options)) { _context17.next = 2; break; } throw new Error("invalid options"); case 2: if (!(!options.linkId || typeof options.linkId !== "string")) { _context17.next = 4; break; } throw new Error("invalid linkId"); case 4: options = (0, _assign2.default)({ block: true, throwException: false }, options); url = void 0; if (options.block) { url = "/v1/evt_link/get_trx_id_for_link_id"; } else { url = "/v1/chain/get_trx_id_for_link_id"; } res = void 0, currentTime = new Date().valueOf(); lastTimeInternet = false, lastError = null; case 9: lastTimeInternet = false; res = undefined; _context17.prev = 11; _context17.next = 14; return this.__callAPI({ url: url, method: "POST", body: { link_id: options.linkId }, sign: false // no need to sign }); case 14: res = _context17.sent; _context17.next = 25; break; case 17: _context17.prev = 17; _context17.t0 = _context17["catch"](11); if (!(!options.block && options.throwException)) { _context17.next = 23; break; } throw _context17.t0; case 23: if (_context17.t0.isServerError) { lastTimeInternet = true; } lastError = _context17.t0; case 25: if (!(res && res.trx_id)) { _context17.next = 30; break; } lastTimeInternet = true; return _context17.abrupt("return", { pending: false, successful: true, transactionId: res.trx_id, blockNum: res.block_num }); case 30: if (!(res && res.error)) { _context17.next = 46; break; } lastTimeInternet = true; if (!(options.throwException && !options.block)) { _context17.next = 36; break; } this.__throwServerResponseError(res); _context17.next = 46; break; case 36: _context17.prev = 36; this.__throwServerResponseError(res); _context17.next = 46; break; case 40: _context17.prev = 40; _context17.t1 = _context17["catch"](36); if (_context17.t1.isServerError) { lastTimeInternet = true; } if (options.block) { _context17.next = 45; break; } return _context17.abrupt("return", { pending: true, successful: false, exception: _context17.t1 }); case 45: lastError = _context17.t1; case 46: if (new Date().valueOf() - currentTime < 15000 && options.block) { _context17.next = 9; break; } case 47: if (!lastTimeInternet) { _context17.next = 51; break; } return _context17.abrupt("return", { pending: false, successful: false, exception: lastError || new Error("everiPay timeOut or network issue") }); case 51: return _context17.abrupt("return", { pending: true, successful: false, exception: new Error("No available network connection") }); case 52: case "end": return _context17.stop(); } } }, _callee17, this, [[11, 17], [36, 40]]); })); function getStatusOfEvtLink(_x23) { return _ref17.apply(this, arguments); } return getStatusOfEvtLink; }() /** * Get a raw transaction but does NOT push it to the blockchain */ }, { key: "generateUnsignedTransaction", value: function () { var _ref18 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee18() { var args, p, _args18 = arguments; return _regenerator2.default.wrap(function _callee18$(_context18) { while (1) { switch (_context18.prev = _context18.next) { case 0: /** @type Array */ args = [].slice.call(_args18); if (!(args.length == 0)) { _context18.next = 3; break; } throw new Error("invalid arguments"); case 3: if (args[0] instanceof EvtAction) { args = [{}].concat(args); } args[0].__estimateCharge = true; _context18.next = 7; return this.pushTransaction.apply(this, args); case 7: p = _context18.sent; return _context18.abrupt("return", { transaction: p.body.transaction }); case 9: case "end": return _context18.stop(); } } }, _callee18, this); })); function generateUnsignedTransaction() { return _ref18.apply(this, arguments); } return generateUnsignedTransaction; }() /** * Get estimated charge for a transaction */ }, { key: "getEstimatedChargeForTransaction", value: function () { var _ref19 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee19() { var args, p, apiRes, required_keys, res, _args19 = arguments; return _regenerator2.default.wrap(function _callee19$(_context19) { while (1) { switch (_context19.prev = _context19.next) { case 0: /** @type Array */ args = [].slice.call(_args19); if (!(args.length == 0)) { _context19.next = 3; break; } throw new Error("invalid arguments"); case 3: if (args[0] instanceof EvtAction) { args = [{}].concat(args); } args[0].__estimateCharge = true; _context19.next = 7; return this.pushTransaction.apply(this, args); case 7: p = _context19.sent; // Get required keys Logger.verbose("[getEstimatedChargeForTransaction] get required_keys, available keys: " + (0, _stringify2.default)(p.publicKeys, null, 4)); _context19.next = 11; return this.__chainGetRequiredKeys({ transaction: p.body.transaction, available_keys: p.publicKeys }); case 11: apiRes = _context19.sent; required_keys = apiRes.required_keys; Logger.verbose("[getEstimatedChargeForTransaction] got required_keys: " + (0, _stringify2.default)(required_keys, null, 4)); _context19.next = 16; return this.__callAPI({ url: "/v1/chain/get_charge", method: "POST", body: { transaction: p.body.transaction, sigs_num: required_keys.length }, sign: false // no need to sign }); case 16: res = _context19.sent; if (!(res && res.charge !== undefined)) { _context19.next = 21; break; } return _context19.abrupt("return", res); case 21: this.__throwServerResponseError(res); case 22: case "end": return _context19.stop(); } } }, _callee19, this); })); function getEstimatedChargeForTransaction() { return _ref19.apply(this, arguments); } return getEstimatedChargeForTransaction; }() /** * get detail information about a domain by its name. Make sure you have history_plugin enabled on the chain node * @param {*} name the name of the domain */ }, { key: "getDomainDetail", value: function () { var _ref20 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee20(name) { var res; return _regenerator2.default.wrap(function _callee20$(_context20) { while (1) { switch (_context20.prev = _context20.next) { case 0: if (!(typeof name !== "string" || !name)) { _context20.next = 2; break; } throw new Error("invalid domain name"); case 2: _context20.next = 4; return this.__callAPI({ url: "/v1/evt/get_domain", method: "POST", body: { name: name }, sign: false // no need to sign }); case 4: res = _context20.sent; if (!(res && res.name && res.creator)) { _context20.next = 9; break; } return _context20.abrupt("return", res); case 9: this.__throwServerResponseError(res); case 10: case "end": return _context20.stop(); } } }, _callee20, this); })); function getDomainDetail(_x24) { return _ref20.apply(this, arguments); } return getDomainDetail; }() /** * Fetch all the transaction ids in one block * @param {string} blockId the id of the block */ }, { key: "getTransactionIdsInBlock", value: function () { var _ref21 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee21(blockId) { var res; return _regenerator2.default.wrap(function _callee21$(_context21) { while (1) { switch (_context21.prev = _context21.next) { case 0: if (!(typeof blockId !== "string" || !blockId)) { _context21.next = 2; break; } throw new Error("invalid blockId"); case 2: if (!(blockId.length < 64)) { _context21.next = 4; break; } throw new Error("invalid blockId. Note: block id is not the same as block num."); case 4: _context21.next = 6; return this.__callAPI({ url: "/v1/chain/get_transaction_ids_for_block", method: "POST", body: { block_id: blockId }, sign: false // no need to sign }); case 6: res = _context21.s