UNPKG

@stellar/stellar-sdk

Version:

A library for working with the Stellar network, including communication with the Horizon and Soroban RPC servers.

824 lines (823 loc) 38.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SUBMIT_TRANSACTION_TIMEOUT = exports.RpcServer = exports.LinearSleepStrategy = exports.Durability = exports.BasicSleepStrategy = void 0; var _urijs = _interopRequireDefault(require("urijs")); var _stellarBase = require("@stellar/stellar-base"); var _axios = _interopRequireDefault(require("./axios")); var jsonrpc = _interopRequireWildcard(require("./jsonrpc")); var _api = require("./api"); var _transaction = require("./transaction"); var _parsers = require("./parsers"); var _utils = require("../utils"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t5 in e) "default" !== _t5 && {}.hasOwnProperty.call(e, _t5) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t5)) && (i.get || i.set) ? o(f, _t5, i) : f[_t5] = e[_t5]); return f; })(e, t); } 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 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 _regenerator() { var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); } 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 _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 SUBMIT_TRANSACTION_TIMEOUT = exports.SUBMIT_TRANSACTION_TIMEOUT = 60 * 1000; var Durability = exports.Durability = function (Durability) { Durability["Temporary"] = "temporary"; Durability["Persistent"] = "persistent"; return Durability; }({}); var DEFAULT_GET_TRANSACTION_TIMEOUT = 30; var BasicSleepStrategy = exports.BasicSleepStrategy = function BasicSleepStrategy(_iter) { return 1000; }; var LinearSleepStrategy = exports.LinearSleepStrategy = function LinearSleepStrategy(iter) { return 1000 * iter; }; function findCreatedAccountSequenceInTransactionMeta(meta) { var _operations$flatMap$f; var operations = []; switch (meta.switch()) { case 0: operations = meta.operations(); break; case 1: case 2: case 3: case 4: operations = meta.value().operations(); break; default: throw new Error('Unexpected transaction meta switch value'); } var sequenceNumber = (_operations$flatMap$f = operations.flatMap(function (op) { return op.changes(); }).find(function (c) { return c.switch() === _stellarBase.xdr.LedgerEntryChangeType.ledgerEntryCreated() && c.created().data().switch() === _stellarBase.xdr.LedgerEntryType.account(); })) === null || _operations$flatMap$f === void 0 || (_operations$flatMap$f = _operations$flatMap$f.created()) === null || _operations$flatMap$f === void 0 || (_operations$flatMap$f = _operations$flatMap$f.data()) === null || _operations$flatMap$f === void 0 || (_operations$flatMap$f = _operations$flatMap$f.account()) === null || _operations$flatMap$f === void 0 || (_operations$flatMap$f = _operations$flatMap$f.seqNum()) === null || _operations$flatMap$f === void 0 ? void 0 : _operations$flatMap$f.toString(); if (sequenceNumber) { return sequenceNumber; } throw new Error('No account created in transaction'); } var RpcServer = exports.RpcServer = function () { function RpcServer(serverURL) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, RpcServer); this.serverURL = (0, _urijs.default)(serverURL); if (opts.headers && Object.keys(opts.headers).length !== 0) { _axios.default.interceptors.request.use(function (config) { config.headers = Object.assign(config.headers, opts.headers); return config; }); } if (this.serverURL.protocol() !== 'https' && !opts.allowHttp) { throw new Error("Cannot connect to insecure Soroban RPC server if `allowHttp` isn't set"); } } return _createClass(RpcServer, [{ key: "getAccount", value: (function () { var _getAccount = _asyncToGenerator(_regenerator().m(function _callee(address) { var ledgerKey, resp, accountEntry; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: ledgerKey = _stellarBase.xdr.LedgerKey.account(new _stellarBase.xdr.LedgerKeyAccount({ accountId: _stellarBase.Keypair.fromPublicKey(address).xdrPublicKey() })); _context.n = 1; return this.getLedgerEntries(ledgerKey); case 1: resp = _context.v; if (!(resp.entries.length === 0)) { _context.n = 2; break; } return _context.a(2, Promise.reject({ code: 404, message: "Account not found: ".concat(address) })); case 2: accountEntry = resp.entries[0].val.account(); return _context.a(2, new _stellarBase.Account(address, accountEntry.seqNum().toString())); } }, _callee, this); })); function getAccount(_x) { return _getAccount.apply(this, arguments); } return getAccount; }()) }, { key: "getHealth", value: (function () { var _getHealth = _asyncToGenerator(_regenerator().m(function _callee2() { return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: return _context2.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getHealth')); } }, _callee2, this); })); function getHealth() { return _getHealth.apply(this, arguments); } return getHealth; }()) }, { key: "getContractData", value: (function () { var _getContractData = _asyncToGenerator(_regenerator().m(function _callee3(contract, key) { var durability, scAddress, xdrDurability, contractKey, _args3 = arguments, _t; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: durability = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : Durability.Persistent; if (!(typeof contract === 'string')) { _context3.n = 1; break; } scAddress = new _stellarBase.Contract(contract).address().toScAddress(); _context3.n = 4; break; case 1: if (!(contract instanceof _stellarBase.Address)) { _context3.n = 2; break; } scAddress = contract.toScAddress(); _context3.n = 4; break; case 2: if (!(contract instanceof _stellarBase.Contract)) { _context3.n = 3; break; } scAddress = contract.address().toScAddress(); _context3.n = 4; break; case 3: throw new TypeError("unknown contract type: ".concat(contract)); case 4: _t = durability; _context3.n = _t === Durability.Temporary ? 5 : _t === Durability.Persistent ? 6 : 7; break; case 5: xdrDurability = _stellarBase.xdr.ContractDataDurability.temporary(); return _context3.a(3, 8); case 6: xdrDurability = _stellarBase.xdr.ContractDataDurability.persistent(); return _context3.a(3, 8); case 7: throw new TypeError("invalid durability: ".concat(durability)); case 8: contractKey = _stellarBase.xdr.LedgerKey.contractData(new _stellarBase.xdr.LedgerKeyContractData({ key: key, contract: scAddress, durability: xdrDurability })); return _context3.a(2, this.getLedgerEntries(contractKey).then(function (r) { if (r.entries.length === 0) { return Promise.reject({ code: 404, message: "Contract data not found. Contract: ".concat(_stellarBase.Address.fromScAddress(scAddress).toString(), ", Key: ").concat(key.toXDR('base64'), ", Durability: ").concat(durability) }); } return r.entries[0]; })); } }, _callee3, this); })); function getContractData(_x2, _x3) { return _getContractData.apply(this, arguments); } return getContractData; }()) }, { key: "getContractWasmByContractId", value: (function () { var _getContractWasmByContractId = _asyncToGenerator(_regenerator().m(function _callee4(contractId) { var _response$entries$; var contractLedgerKey, response, wasmHash; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: contractLedgerKey = new _stellarBase.Contract(contractId).getFootprint(); _context4.n = 1; return this.getLedgerEntries(contractLedgerKey); case 1: response = _context4.v; if (!(!response.entries.length || !((_response$entries$ = response.entries[0]) !== null && _response$entries$ !== void 0 && _response$entries$.val))) { _context4.n = 2; break; } return _context4.a(2, Promise.reject({ code: 404, message: "Could not obtain contract hash from server" })); case 2: wasmHash = response.entries[0].val.contractData().val().instance().executable().wasmHash(); return _context4.a(2, this.getContractWasmByHash(wasmHash)); } }, _callee4, this); })); function getContractWasmByContractId(_x4) { return _getContractWasmByContractId.apply(this, arguments); } return getContractWasmByContractId; }()) }, { key: "getContractWasmByHash", value: (function () { var _getContractWasmByHash = _asyncToGenerator(_regenerator().m(function _callee5(wasmHash) { var _responseWasm$entries; var format, wasmHashBuffer, ledgerKeyWasmHash, responseWasm, wasmBuffer, _args5 = arguments; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: format = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : undefined; wasmHashBuffer = typeof wasmHash === "string" ? Buffer.from(wasmHash, format) : wasmHash; ledgerKeyWasmHash = _stellarBase.xdr.LedgerKey.contractCode(new _stellarBase.xdr.LedgerKeyContractCode({ hash: wasmHashBuffer })); _context5.n = 1; return this.getLedgerEntries(ledgerKeyWasmHash); case 1: responseWasm = _context5.v; if (!(!responseWasm.entries.length || !((_responseWasm$entries = responseWasm.entries[0]) !== null && _responseWasm$entries !== void 0 && _responseWasm$entries.val))) { _context5.n = 2; break; } return _context5.a(2, Promise.reject({ code: 404, message: "Could not obtain contract wasm from server" })); case 2: wasmBuffer = responseWasm.entries[0].val.contractCode().code(); return _context5.a(2, wasmBuffer); } }, _callee5, this); })); function getContractWasmByHash(_x5) { return _getContractWasmByHash.apply(this, arguments); } return getContractWasmByHash; }()) }, { key: "getLedgerEntries", value: (function () { var _getLedgerEntries2 = _asyncToGenerator(_regenerator().m(function _callee6() { var _args6 = arguments; return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: return _context6.a(2, this._getLedgerEntries.apply(this, _args6).then(_parsers.parseRawLedgerEntries)); } }, _callee6, this); })); function getLedgerEntries() { return _getLedgerEntries2.apply(this, arguments); } return getLedgerEntries; }()) }, { key: "_getLedgerEntries", value: function () { var _getLedgerEntries3 = _asyncToGenerator(_regenerator().m(function _callee7() { var _len, keys, _key, _args7 = arguments; return _regenerator().w(function (_context7) { while (1) switch (_context7.n) { case 0: for (_len = _args7.length, keys = new Array(_len), _key = 0; _key < _len; _key++) { keys[_key] = _args7[_key]; } return _context7.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getLedgerEntries', { keys: keys.map(function (k) { return k.toXDR('base64'); }) })); } }, _callee7, this); })); function _getLedgerEntries() { return _getLedgerEntries3.apply(this, arguments); } return _getLedgerEntries; }() }, { key: "pollTransaction", value: (function () { var _pollTransaction = _asyncToGenerator(_regenerator().m(function _callee8(hash, opts) { var _opts$attempts, _opts$attempts2; var maxAttempts, foundInfo, attempt, _opts$sleepStrategy; return _regenerator().w(function (_context8) { while (1) switch (_context8.n) { case 0: maxAttempts = ((_opts$attempts = opts === null || opts === void 0 ? void 0 : opts.attempts) !== null && _opts$attempts !== void 0 ? _opts$attempts : 0) < 1 ? DEFAULT_GET_TRANSACTION_TIMEOUT : (_opts$attempts2 = opts === null || opts === void 0 ? void 0 : opts.attempts) !== null && _opts$attempts2 !== void 0 ? _opts$attempts2 : DEFAULT_GET_TRANSACTION_TIMEOUT; attempt = 1; case 1: if (!(attempt < maxAttempts)) { _context8.n = 5; break; } _context8.n = 2; return this.getTransaction(hash); case 2: foundInfo = _context8.v; if (!(foundInfo.status !== _api.Api.GetTransactionStatus.NOT_FOUND)) { _context8.n = 3; break; } return _context8.a(2, foundInfo); case 3: _context8.n = 4; return _utils.Utils.sleep(((_opts$sleepStrategy = opts === null || opts === void 0 ? void 0 : opts.sleepStrategy) !== null && _opts$sleepStrategy !== void 0 ? _opts$sleepStrategy : BasicSleepStrategy)(attempt)); case 4: attempt++; _context8.n = 1; break; case 5: return _context8.a(2, foundInfo); } }, _callee8, this); })); function pollTransaction(_x6, _x7) { return _pollTransaction.apply(this, arguments); } return pollTransaction; }()) }, { key: "getTransaction", value: (function () { var _getTransaction2 = _asyncToGenerator(_regenerator().m(function _callee9(hash) { return _regenerator().w(function (_context9) { while (1) switch (_context9.n) { case 0: return _context9.a(2, this._getTransaction(hash).then(function (raw) { var foundInfo = {}; if (raw.status !== _api.Api.GetTransactionStatus.NOT_FOUND) { Object.assign(foundInfo, (0, _parsers.parseTransactionInfo)(raw)); } var result = _objectSpread({ status: raw.status, txHash: hash, latestLedger: raw.latestLedger, latestLedgerCloseTime: raw.latestLedgerCloseTime, oldestLedger: raw.oldestLedger, oldestLedgerCloseTime: raw.oldestLedgerCloseTime }, foundInfo); return result; })); } }, _callee9, this); })); function getTransaction(_x8) { return _getTransaction2.apply(this, arguments); } return getTransaction; }()) }, { key: "_getTransaction", value: function () { var _getTransaction3 = _asyncToGenerator(_regenerator().m(function _callee0(hash) { return _regenerator().w(function (_context0) { while (1) switch (_context0.n) { case 0: return _context0.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getTransaction', { hash: hash })); } }, _callee0, this); })); function _getTransaction(_x9) { return _getTransaction3.apply(this, arguments); } return _getTransaction; }() }, { key: "getTransactions", value: (function () { var _getTransactions2 = _asyncToGenerator(_regenerator().m(function _callee1(request) { return _regenerator().w(function (_context1) { while (1) switch (_context1.n) { case 0: return _context1.a(2, this._getTransactions(request).then(function (raw) { var result = { transactions: (raw.transactions || []).map(_parsers.parseRawTransactions), latestLedger: raw.latestLedger, latestLedgerCloseTimestamp: raw.latestLedgerCloseTimestamp, oldestLedger: raw.oldestLedger, oldestLedgerCloseTimestamp: raw.oldestLedgerCloseTimestamp, cursor: raw.cursor }; return result; })); } }, _callee1, this); })); function getTransactions(_x0) { return _getTransactions2.apply(this, arguments); } return getTransactions; }()) }, { key: "_getTransactions", value: function () { var _getTransactions3 = _asyncToGenerator(_regenerator().m(function _callee10(request) { return _regenerator().w(function (_context10) { while (1) switch (_context10.n) { case 0: return _context10.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getTransactions', request)); } }, _callee10, this); })); function _getTransactions(_x1) { return _getTransactions3.apply(this, arguments); } return _getTransactions; }() }, { key: "getEvents", value: (function () { var _getEvents2 = _asyncToGenerator(_regenerator().m(function _callee11(request) { return _regenerator().w(function (_context11) { while (1) switch (_context11.n) { case 0: return _context11.a(2, this._getEvents(request).then(_parsers.parseRawEvents)); } }, _callee11, this); })); function getEvents(_x10) { return _getEvents2.apply(this, arguments); } return getEvents; }()) }, { key: "_getEvents", value: function () { var _getEvents3 = _asyncToGenerator(_regenerator().m(function _callee12(request) { var _request$filters; return _regenerator().w(function (_context12) { while (1) switch (_context12.n) { case 0: return _context12.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getEvents', _objectSpread(_objectSpread({ filters: (_request$filters = request.filters) !== null && _request$filters !== void 0 ? _request$filters : [], pagination: _objectSpread(_objectSpread({}, request.cursor && { cursor: request.cursor }), request.limit && { limit: request.limit }) }, request.startLedger && { startLedger: request.startLedger }), request.endLedger && { endLedger: request.endLedger }))); } }, _callee12, this); })); function _getEvents(_x11) { return _getEvents3.apply(this, arguments); } return _getEvents; }() }, { key: "getNetwork", value: (function () { var _getNetwork = _asyncToGenerator(_regenerator().m(function _callee13() { return _regenerator().w(function (_context13) { while (1) switch (_context13.n) { case 0: return _context13.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getNetwork')); } }, _callee13, this); })); function getNetwork() { return _getNetwork.apply(this, arguments); } return getNetwork; }()) }, { key: "getLatestLedger", value: (function () { var _getLatestLedger = _asyncToGenerator(_regenerator().m(function _callee14() { return _regenerator().w(function (_context14) { while (1) switch (_context14.n) { case 0: return _context14.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getLatestLedger')); } }, _callee14, this); })); function getLatestLedger() { return _getLatestLedger.apply(this, arguments); } return getLatestLedger; }()) }, { key: "simulateTransaction", value: (function () { var _simulateTransaction2 = _asyncToGenerator(_regenerator().m(function _callee15(tx, addlResources, authMode) { return _regenerator().w(function (_context15) { while (1) switch (_context15.n) { case 0: return _context15.a(2, this._simulateTransaction(tx, addlResources, authMode).then(_parsers.parseRawSimulation)); } }, _callee15, this); })); function simulateTransaction(_x12, _x13, _x14) { return _simulateTransaction2.apply(this, arguments); } return simulateTransaction; }()) }, { key: "_simulateTransaction", value: function () { var _simulateTransaction3 = _asyncToGenerator(_regenerator().m(function _callee16(transaction, addlResources, authMode) { return _regenerator().w(function (_context16) { while (1) switch (_context16.n) { case 0: return _context16.a(2, jsonrpc.postObject(this.serverURL.toString(), 'simulateTransaction', _objectSpread({ transaction: transaction.toXDR(), authMode: authMode }, addlResources !== undefined && { resourceConfig: { instructionLeeway: addlResources.cpuInstructions } }))); } }, _callee16, this); })); function _simulateTransaction(_x15, _x16, _x17) { return _simulateTransaction3.apply(this, arguments); } return _simulateTransaction; }() }, { key: "prepareTransaction", value: (function () { var _prepareTransaction = _asyncToGenerator(_regenerator().m(function _callee17(tx) { var simResponse; return _regenerator().w(function (_context17) { while (1) switch (_context17.n) { case 0: _context17.n = 1; return this.simulateTransaction(tx); case 1: simResponse = _context17.v; if (!_api.Api.isSimulationError(simResponse)) { _context17.n = 2; break; } throw new Error(simResponse.error); case 2: return _context17.a(2, (0, _transaction.assembleTransaction)(tx, simResponse).build()); } }, _callee17, this); })); function prepareTransaction(_x18) { return _prepareTransaction.apply(this, arguments); } return prepareTransaction; }()) }, { key: "sendTransaction", value: (function () { var _sendTransaction2 = _asyncToGenerator(_regenerator().m(function _callee18(transaction) { return _regenerator().w(function (_context18) { while (1) switch (_context18.n) { case 0: return _context18.a(2, this._sendTransaction(transaction).then(_parsers.parseRawSendTransaction)); } }, _callee18, this); })); function sendTransaction(_x19) { return _sendTransaction2.apply(this, arguments); } return sendTransaction; }()) }, { key: "_sendTransaction", value: function () { var _sendTransaction3 = _asyncToGenerator(_regenerator().m(function _callee19(transaction) { return _regenerator().w(function (_context19) { while (1) switch (_context19.n) { case 0: return _context19.a(2, jsonrpc.postObject(this.serverURL.toString(), 'sendTransaction', { transaction: transaction.toXDR() })); } }, _callee19, this); })); function _sendTransaction(_x20) { return _sendTransaction3.apply(this, arguments); } return _sendTransaction; }() }, { key: "requestAirdrop", value: (function () { var _requestAirdrop = _asyncToGenerator(_regenerator().m(function _callee20(address, friendbotUrl) { var account, response, meta, txMeta, sequence, _error$response, _error$response$detai, _t2, _t3; return _regenerator().w(function (_context20) { while (1) switch (_context20.p = _context20.n) { case 0: account = typeof address === 'string' ? address : address.accountId(); _t2 = friendbotUrl; if (_t2) { _context20.n = 2; break; } _context20.n = 1; return this.getNetwork(); case 1: _t2 = _context20.v.friendbotUrl; case 2: friendbotUrl = _t2; if (friendbotUrl) { _context20.n = 3; break; } throw new Error('No friendbot URL configured for current network'); case 3: _context20.p = 3; _context20.n = 4; return _axios.default.post("".concat(friendbotUrl, "?addr=").concat(encodeURIComponent(account))); case 4: response = _context20.v; if (response.data.result_meta_xdr) { _context20.n = 7; break; } _context20.n = 5; return this.getTransaction(response.data.hash); case 5: txMeta = _context20.v; if (!(txMeta.status !== _api.Api.GetTransactionStatus.SUCCESS)) { _context20.n = 6; break; } throw new Error("Funding account ".concat(address, " failed")); case 6: meta = txMeta.resultMetaXdr; _context20.n = 8; break; case 7: meta = _stellarBase.xdr.TransactionMeta.fromXDR(response.data.result_meta_xdr, 'base64'); case 8: sequence = findCreatedAccountSequenceInTransactionMeta(meta); return _context20.a(2, new _stellarBase.Account(account, sequence)); case 9: _context20.p = 9; _t3 = _context20.v; if (!(((_error$response = _t3.response) === null || _error$response === void 0 ? void 0 : _error$response.status) === 400)) { _context20.n = 10; break; } if (!((_error$response$detai = _t3.response.detail) !== null && _error$response$detai !== void 0 && _error$response$detai.includes('createAccountAlreadyExist'))) { _context20.n = 10; break; } return _context20.a(2, this.getAccount(account)); case 10: throw _t3; case 11: return _context20.a(2); } }, _callee20, this, [[3, 9]]); })); function requestAirdrop(_x21, _x22) { return _requestAirdrop.apply(this, arguments); } return requestAirdrop; }()) }, { key: "getFeeStats", value: (function () { var _getFeeStats = _asyncToGenerator(_regenerator().m(function _callee21() { return _regenerator().w(function (_context21) { while (1) switch (_context21.n) { case 0: return _context21.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getFeeStats')); } }, _callee21, this); })); function getFeeStats() { return _getFeeStats.apply(this, arguments); } return getFeeStats; }()) }, { key: "getVersionInfo", value: (function () { var _getVersionInfo = _asyncToGenerator(_regenerator().m(function _callee22() { return _regenerator().w(function (_context22) { while (1) switch (_context22.n) { case 0: return _context22.a(2, jsonrpc.postObject(this.serverURL.toString(), 'getVersionInfo')); } }, _callee22, this); })); function getVersionInfo() { return _getVersionInfo.apply(this, arguments); } return getVersionInfo; }()) }, { key: "getSACBalance", value: (function () { var _getSACBalance = _asyncToGenerator(_regenerator().m(function _callee23(contractId, sac, networkPassphrase) { var passphrase, sacId, key, ledgerKey, response, _response$entries$2, lastModifiedLedgerSeq, liveUntilLedgerSeq, val, entry, _t4; return _regenerator().w(function (_context23) { while (1) switch (_context23.n) { case 0: if (_stellarBase.StrKey.isValidContract(contractId)) { _context23.n = 1; break; } throw new TypeError("expected contract ID, got ".concat(contractId)); case 1: if (!(networkPassphrase !== null && networkPassphrase !== void 0)) { _context23.n = 2; break; } _t4 = networkPassphrase; _context23.n = 4; break; case 2: _context23.n = 3; return this.getNetwork().then(function (n) { return n.passphrase; }); case 3: _t4 = _context23.v; case 4: passphrase = _t4; sacId = sac.contractId(passphrase); key = (0, _stellarBase.nativeToScVal)(["Balance", contractId], { type: ["symbol", "address"] }); ledgerKey = _stellarBase.xdr.LedgerKey.contractData(new _stellarBase.xdr.LedgerKeyContractData({ contract: new _stellarBase.Address(sacId).toScAddress(), durability: _stellarBase.xdr.ContractDataDurability.persistent(), key: key })); _context23.n = 5; return this.getLedgerEntries(ledgerKey); case 5: response = _context23.v; if (!(response.entries.length === 0)) { _context23.n = 6; break; } return _context23.a(2, { latestLedger: response.latestLedger }); case 6: _response$entries$2 = response.entries[0], lastModifiedLedgerSeq = _response$entries$2.lastModifiedLedgerSeq, liveUntilLedgerSeq = _response$entries$2.liveUntilLedgerSeq, val = _response$entries$2.val; if (!(val.switch().value !== _stellarBase.xdr.LedgerEntryType.contractData().value)) { _context23.n = 7; break; } return _context23.a(2, { latestLedger: response.latestLedger }); case 7: entry = (0, _stellarBase.scValToNative)(val.contractData().val()); return _context23.a(2, { latestLedger: response.latestLedger, balanceEntry: { liveUntilLedgerSeq: liveUntilLedgerSeq, lastModifiedLedgerSeq: lastModifiedLedgerSeq, amount: entry.amount.toString(), authorized: entry.authorized, clawback: entry.clawback } }); } }, _callee23, this); })); function getSACBalance(_x23, _x24, _x25) { return _getSACBalance.apply(this, arguments); } return getSACBalance; }()) }]); }();