@fioprotocol/fiojs
Version:
FioJS is a Utility SDK for packing, signing and encryption functionality for the FIO blockchain. It is used by the FIO TypeScript SDK
429 lines • 20.7 kB
JavaScript
"use strict";
/**
* @module JSON-RPC
*/
// copyright defined in fiojs/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 (g && (g = 0, op[0] && (_ = 0)), _) 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 chain_numeric_1 = require("./chain-numeric");
var chain_rpcerror_1 = require("./chain-rpcerror");
function arrayToHex(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;
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 chain_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 chain_rpcerror_1.RpcError(json);
}
return [2 /*return*/, json];
}
});
});
};
/** 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_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` */
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 })];
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", { account: account, code: code, 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, limit: limit, lower_bound: lowerBound })];
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()];
}
});
});
};
/** Raw call to `/v1/chain/get_raw_code_and_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()];
}
});
});
};
/** calls `/v1/chain/get_raw_code_and_abi` and pulls out unneeded raw wasm code */
// TODO: use `/v1/chain/get_raw_abi` directly when it becomes available
JsonRpc.prototype.getRawAbi = function (accountName) {
return __awaiter(this, void 0, void 0, function () {
var rawCodeAndAbi, abi;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.get_raw_code_and_abi(accountName)];
case 1:
rawCodeAndAbi = _a.sent();
abi = (0, chain_numeric_1.base64ToBinary)(rawCodeAndAbi.abi);
return [2 /*return*/, { accountName: rawCodeAndAbi.account_name, abi: abi }];
}
});
});
};
/** 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, limit: limit, lower_bound: lowerBound })];
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.table_key, table_key = _c === void 0 ? "" : _c, _d = _a.lower_bound, lower_bound = _d === void 0 ? "" : _d, _e = _a.upper_bound, upper_bound = _e === void 0 ? "" : _e, _f = _a.index_position, index_position = _f === void 0 ? 1 : _f, _g = _a.key_type, key_type = _g === void 0 ? "" : _g, _h = _a.limit, limit = _h === void 0 ? 10 : _h, _j = _a.reverse, reverse = _j === void 0 ? false : _j, _k = _a.show_payer, show_payer = _k === void 0 ? false : _k;
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_l) {
switch (_l.label) {
case 0: return [4 /*yield*/, this.fetch("/v1/chain/get_table_rows", {
code: code,
index_position: index_position,
json: json,
key_type: key_type,
limit: limit,
lower_bound: lower_bound,
reverse: reverse,
scope: scope,
show_payer: show_payer,
table: table,
table_key: table_key,
upper_bound: upper_bound,
})];
case 1: return [2 /*return*/, _l.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,
limit: limit,
lower_bound: lower_bound,
table: table,
upper_bound: upper_bound,
})];
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 = chain_numeric_1.convertLegacyPublicKeys;
return [4 /*yield*/, this.fetch("/v1/chain/get_required_keys", {
available_keys: args.availableKeys,
transaction: args.transaction,
})];
case 1: return [2 /*return*/, _a.apply(void 0, [(_b.sent()).required_keys])];
}
});
});
};
/** Push a serialized transaction */
JsonRpc.prototype.push_transaction = function (_a) {
var signatures = _a.signatures, serializedTransaction = _a.serializedTransaction, serializedContextFreeData = _a.serializedContextFreeData;
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.fetch("/v1/chain/push_transaction", {
compression: 0,
packed_context_free_data: arrayToHex(serializedContextFreeData || new Uint8Array(0)),
packed_trx: arrayToHex(serializedTransaction),
signatures: signatures,
})];
case 1: return [2 /*return*/, _b.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/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, offset: offset, pos: pos })];
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", { block_num_hint: blockNumHint, id: id })];
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=chain-jsonrpc.js.map