UNPKG

eosjs

Version:
593 lines 29.6 kB
"use strict"; /** * @module JSON-RPC */ // copyright defined in eosjs/LICENSE.txt var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonRpc = void 0; var eosjs_numeric_1 = require("./eosjs-numeric"); var eosjs_rpcerror_1 = require("./eosjs-rpcerror"); var arrayToHex = function (data) { var e_1, _a; var result = ''; try { for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) { var x = data_1_1.value; result += ('00' + x.toString(16)).slice(-2); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1); } finally { if (e_1) throw e_1.error; } } return result; }; /** Make RPC calls */ var JsonRpc = /** @class */ (function () { /** * @param args * `fetch`: * browsers: leave `null` or `undefined` * node: provide an implementation */ function JsonRpc(endpoint, args) { if (args === void 0) { args = {}; } this.endpoint = endpoint.replace(/\/$/, ''); if (args.fetch) { this.fetchBuiltin = args.fetch; } else { this.fetchBuiltin = global.fetch; } } /** Post `body` to `endpoint + path`. Throws detailed error information in `RpcError` when available. */ JsonRpc.prototype.fetch = function (path, body) { return __awaiter(this, void 0, void 0, function () { var response, json, f, e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); f = this.fetchBuiltin; return [4 /*yield*/, f(this.endpoint + path, { body: JSON.stringify(body), method: 'POST', })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: json = _a.sent(); if (json.processed && json.processed.except) { throw new eosjs_rpcerror_1.RpcError(json); } else if (json.result && json.result.except) { throw new eosjs_rpcerror_1.RpcError(json); } return [3 /*break*/, 4]; case 3: e_2 = _a.sent(); e_2.isFetchError = true; throw e_2; case 4: if (!response.ok) { throw new eosjs_rpcerror_1.RpcError(json); } return [2 /*return*/, json]; } }); }); }; JsonRpc.prototype.abi_bin_to_json = function (code, action, binargs) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/abi_bin_to_json', { code: code, action: action, binargs: binargs })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; JsonRpc.prototype.abi_json_to_bin = function (code, action, args) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/abi_json_to_bin', { code: code, action: action, args: args })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_abi` */ JsonRpc.prototype.get_abi = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_abi', { account_name: accountName })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_account` */ JsonRpc.prototype.get_account = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_account', { account_name: accountName })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_accounts_by_authorizers` */ JsonRpc.prototype.get_accounts_by_authorizers = function (accounts, keys) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_accounts_by_authorizers', { accounts: accounts, keys: keys })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `get_activated_protocol_features` */ JsonRpc.prototype.get_activated_protocol_features = function (_a) { var _b = _a.limit, limit = _b === void 0 ? 10 : _b, _c = _a.search_by_block_num, search_by_block_num = _c === void 0 ? false : _c, _d = _a.reverse, reverse = _d === void 0 ? false : _d, _e = _a.lower_bound, lower_bound = _e === void 0 ? null : _e, _f = _a.upper_bound, upper_bound = _f === void 0 ? null : _f; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_g) { switch (_g.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_activated_protocol_features', { lower_bound: lower_bound, upper_bound: upper_bound, limit: limit, search_by_block_num: search_by_block_num, reverse: reverse })]; case 1: return [2 /*return*/, _g.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_block_header_state` */ JsonRpc.prototype.get_block_header_state = function (blockNumOrId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_block_header_state', { block_num_or_id: blockNumOrId })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_block_info` */ JsonRpc.prototype.get_block_info = function (blockNum) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_block_info', { block_num: blockNum })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_block` */ JsonRpc.prototype.get_block = function (blockNumOrId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_block', { block_num_or_id: blockNumOrId })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_code` */ JsonRpc.prototype.get_code = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_code', { account_name: accountName, code_as_wasm: true })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_code_hash` */ JsonRpc.prototype.get_code_hash = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_code_hash', { account_name: accountName })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_currency_balance` */ JsonRpc.prototype.get_currency_balance = function (code, account, symbol) { if (symbol === void 0) { symbol = null; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_currency_balance', { code: code, account: account, symbol: symbol })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_currency_stats` */ JsonRpc.prototype.get_currency_stats = function (code, symbol) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_currency_stats', { code: code, symbol: symbol })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_info` */ JsonRpc.prototype.get_info = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_info', {})]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_producer_schedule` */ JsonRpc.prototype.get_producer_schedule = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_producer_schedule', {})]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_producers` */ JsonRpc.prototype.get_producers = function (json, lowerBound, limit) { if (json === void 0) { json = true; } if (lowerBound === void 0) { lowerBound = ''; } if (limit === void 0) { limit = 50; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_producers', { json: json, lower_bound: lowerBound, limit: limit })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_raw_code_and_abi` */ JsonRpc.prototype.get_raw_code_and_abi = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_raw_code_and_abi', { account_name: accountName })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** calls `/v1/chain/get_raw_code_and_abi` and pulls out unneeded raw wasm code */ JsonRpc.prototype.getRawAbi = function (accountName) { return __awaiter(this, void 0, void 0, function () { var rawAbi, abi; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.get_raw_abi(accountName)]; case 1: rawAbi = _a.sent(); abi = eosjs_numeric_1.base64ToBinary(rawAbi.abi); return [2 /*return*/, { accountName: rawAbi.account_name, abi: abi }]; } }); }); }; /** Raw call to `/v1/chain/get_raw_abi` */ JsonRpc.prototype.get_raw_abi = function (accountName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_raw_abi', { account_name: accountName })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_scheduled_transactions` */ JsonRpc.prototype.get_scheduled_transactions = function (json, lowerBound, limit) { if (json === void 0) { json = true; } if (lowerBound === void 0) { lowerBound = ''; } if (limit === void 0) { limit = 50; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_scheduled_transactions', { json: json, lower_bound: lowerBound, limit: limit })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_table_rows` */ JsonRpc.prototype.get_table_rows = function (_a) { var _b = _a.json, json = _b === void 0 ? true : _b, code = _a.code, scope = _a.scope, table = _a.table, _c = _a.lower_bound, lower_bound = _c === void 0 ? '' : _c, _d = _a.upper_bound, upper_bound = _d === void 0 ? '' : _d, _e = _a.index_position, index_position = _e === void 0 ? 1 : _e, _f = _a.key_type, key_type = _f === void 0 ? '' : _f, _g = _a.limit, limit = _g === void 0 ? 10 : _g, _h = _a.reverse, reverse = _h === void 0 ? false : _h, _j = _a.show_payer, show_payer = _j === void 0 ? false : _j; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_k) { switch (_k.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_table_rows', { json: json, code: code, scope: scope, table: table, lower_bound: lower_bound, upper_bound: upper_bound, index_position: index_position, key_type: key_type, limit: limit, reverse: reverse, show_payer: show_payer, })]; case 1: return [2 /*return*/, _k.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_kv_table_rows` */ JsonRpc.prototype.get_kv_table_rows = function (_a) { var _b = _a.json, json = _b === void 0 ? true : _b, code = _a.code, table = _a.table, index_name = _a.index_name, _c = _a.encode_type, encode_type = _c === void 0 ? 'bytes' : _c, index_value = _a.index_value, lower_bound = _a.lower_bound, upper_bound = _a.upper_bound, _d = _a.limit, limit = _d === void 0 ? 10 : _d, _e = _a.reverse, reverse = _e === void 0 ? false : _e, _f = _a.show_payer, show_payer = _f === void 0 ? false : _f; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_g) { switch (_g.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_kv_table_rows', { json: json, code: code, table: table, index_name: index_name, encode_type: encode_type, index_value: index_value, lower_bound: lower_bound, upper_bound: upper_bound, limit: limit, reverse: reverse, show_payer: show_payer, })]; case 1: return [2 /*return*/, _g.sent()]; } }); }); }; /** Raw call to `/v1/chain/get_table_by_scope` */ JsonRpc.prototype.get_table_by_scope = function (_a) { var code = _a.code, table = _a.table, _b = _a.lower_bound, lower_bound = _b === void 0 ? '' : _b, _c = _a.upper_bound, upper_bound = _c === void 0 ? '' : _c, _d = _a.limit, limit = _d === void 0 ? 10 : _d; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_e) { switch (_e.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/get_table_by_scope', { code: code, table: table, lower_bound: lower_bound, upper_bound: upper_bound, limit: limit, })]; case 1: return [2 /*return*/, _e.sent()]; } }); }); }; /** Get subset of `availableKeys` needed to meet authorities in `transaction`. Implements `AuthorityProvider` */ JsonRpc.prototype.getRequiredKeys = function (args) { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = eosjs_numeric_1.convertLegacyPublicKeys; return [4 /*yield*/, this.fetch('/v1/chain/get_required_keys', { transaction: args.transaction, available_keys: args.availableKeys, })]; case 1: return [2 /*return*/, _a.apply(void 0, [(_b.sent()).required_keys])]; } }); }); }; /** Push a serialized transaction (replaced by send_transaction, but returned format has changed) */ JsonRpc.prototype.push_transaction = function (_a) { var signatures = _a.signatures, _b = _a.compression, compression = _b === void 0 ? 0 : _b, serializedTransaction = _a.serializedTransaction, serializedContextFreeData = _a.serializedContextFreeData; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/push_transaction', { signatures: signatures, compression: compression, packed_context_free_data: arrayToHex(serializedContextFreeData || new Uint8Array(0)), packed_trx: arrayToHex(serializedTransaction), })]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }; /** Raw call to `/v1/chain/push_ro_transaction */ JsonRpc.prototype.push_ro_transaction = function (_a, returnFailureTraces) { var signatures = _a.signatures, _b = _a.compression, compression = _b === void 0 ? 0 : _b, serializedTransaction = _a.serializedTransaction; if (returnFailureTraces === void 0) { returnFailureTraces = false; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/push_ro_transaction', { transaction: { signatures: signatures, compression: compression, packed_context_free_data: arrayToHex(new Uint8Array(0)), packed_trx: arrayToHex(serializedTransaction), }, return_failure_traces: returnFailureTraces, })]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }; JsonRpc.prototype.push_transactions = function (transactions) { return __awaiter(this, void 0, void 0, function () { var packedTrxs; return __generator(this, function (_a) { switch (_a.label) { case 0: packedTrxs = transactions.map(function (_a) { var signatures = _a.signatures, _b = _a.compression, compression = _b === void 0 ? 0 : _b, serializedTransaction = _a.serializedTransaction, serializedContextFreeData = _a.serializedContextFreeData; return { signatures: signatures, compression: compression, packed_context_free_data: arrayToHex(serializedContextFreeData || new Uint8Array(0)), packed_trx: arrayToHex(serializedTransaction), }; }); return [4 /*yield*/, this.fetch('/v1/chain/push_transactions', packedTrxs)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Send a serialized transaction */ JsonRpc.prototype.send_transaction = function (_a) { var signatures = _a.signatures, _b = _a.compression, compression = _b === void 0 ? 0 : _b, serializedTransaction = _a.serializedTransaction, serializedContextFreeData = _a.serializedContextFreeData; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.fetch('/v1/chain/send_transaction', { signatures: signatures, compression: compression, packed_context_free_data: arrayToHex(serializedContextFreeData || new Uint8Array(0)), packed_trx: arrayToHex(serializedTransaction), })]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }; /** Raw call to `/v1/db_size/get` */ JsonRpc.prototype.db_size_get = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/db_size/get', {})]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/trace_api/get_block` */ JsonRpc.prototype.trace_get_block = function (block_num) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/trace_api/get_block', { block_num: block_num })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/history/get_actions` */ JsonRpc.prototype.history_get_actions = function (accountName, pos, offset) { if (pos === void 0) { pos = null; } if (offset === void 0) { offset = null; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/history/get_actions', { account_name: accountName, pos: pos, offset: offset })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/history/get_transaction` */ JsonRpc.prototype.history_get_transaction = function (id, blockNumHint) { if (blockNumHint === void 0) { blockNumHint = null; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/history/get_transaction', { id: id, block_num_hint: blockNumHint })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/history/get_key_accounts` */ JsonRpc.prototype.history_get_key_accounts = function (publicKey) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/history/get_key_accounts', { public_key: publicKey })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** Raw call to `/v1/history/get_controlled_accounts` */ JsonRpc.prototype.history_get_controlled_accounts = function (controllingAccount) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.fetch('/v1/history/get_controlled_accounts', { controlling_account: controllingAccount })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; return JsonRpc; }()); // JsonRpc exports.JsonRpc = JsonRpc; //# sourceMappingURL=eosjs-jsonrpc.js.map