@logosnetwork/logos-rpc-client
Version:
Promise-based client for interacting and building services on top of the Logos network.
322 lines • 13 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, 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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var converter_1 = require("./util/converter");
function createAPI(rpcClient) {
return function callRPC(rpc_action, body) {
return __awaiter(this, void 0, void 0, function () {
var params;
return __generator(this, function (_a) {
params = Object.assign({}, body || {}, { rpc_action: rpc_action });
return [2 /*return*/, rpcClient(params)];
});
});
};
}
function createAxiosClient(baseURL, targetURL) {
if (baseURL === void 0) { baseURL = 'https://pla.bs'; }
var headers = {};
var rpc = axios_1.default.create({
baseURL: baseURL,
headers: headers
});
return function (params) {
return __awaiter(this, void 0, void 0, function () {
var data, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!targetURL) return [3 /*break*/, 2];
return [4 /*yield*/, rpc.post('/', params)];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data];
case 2:
params.targetURL = targetURL;
return [4 /*yield*/, rpc.post('/rpc', params)];
case 3:
data = (_a.sent()).data;
return [2 /*return*/, data];
}
});
});
};
}
exports.createAxiosClient = createAxiosClient;
exports.convert = {
toReason: function (amount, denomination) {
return converter_1.default.unit(amount, denomination, 'reason');
},
fromReason: function (amount, denomination) {
return converter_1.default.unit(amount, 'reason', denomination);
},
fromTo: function (amount, currentDec, preferedDec) {
return converter_1.default.unit(amount, currentDec, preferedDec);
}
};
var Logos = /** @class */ (function () {
function Logos(options) {
if (options === void 0) { options = {}; }
this.rpc = createAPI(null);
this.debug = !!options.debug;
this._log = this._log.bind(this);
if (options.rpcClient) {
this.rpc = createAPI(options.rpcClient);
}
else if (options.proxyURL) {
var rpcClient = createAxiosClient(options.proxyURL, options.url);
this.rpc = createAPI(rpcClient);
}
else {
var rpcClient = createAxiosClient(options.url);
this.rpc = createAPI(rpcClient);
}
}
Logos.prototype.changeServer = function (baseURL, targetURL) {
if (targetURL) {
var rpcClient = createAxiosClient(baseURL, targetURL);
this.rpc = createAPI(rpcClient);
}
else {
var rpcClient = createAxiosClient(baseURL);
this.rpc = createAPI(rpcClient);
}
};
Logos.prototype._log = function (message) {
if (this.debug) {
console.log(message);
}
};
Object.defineProperty(Logos.prototype, "accounts", {
//General account methods
get: function () {
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
toAddress: function (publicKey) {
return rpc('account_from_key', { key: publicKey });
},
reasonBalance: function (account) {
return rpc('account_balance', { account: account });
},
logosBalance: function (account) {
return rpc('account_balance', { account: account }).then(function (balance) {
return converter_1.default.unit(balance.balance, 'reason', 'LOGOS');
});
},
balances: function (accounts) {
return rpc('accounts_balances', { accounts: accounts });
},
blockCount: function (account) {
return rpc('account_block_count', { account: account });
},
history: function (account, count, details, head) {
return rpc('account_history', {
account: account,
count: count || 1000,
raw: details || false,
head: head
}).then(function (res) { return res.history; });
},
info: function (account) {
return rpc('account_info', { account: account }).then(function (account) {
_log("(ACCOUNT) balance: " + account.balance);
_log("(ACCOUNT) latest hash: " + account.frontier);
return account;
});
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "requests", {
get: function () {
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
info: function (hash) {
return rpc('block', {
hash: hash
}).then(function (res) { return res; });
},
publish: function (request) {
return rpc('process', { request: request }).then(function (res) {
_log("(REQUEST) Published: " + res.hash);
return res;
});
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "epochs", {
get: function () {
var rpc = this.rpc;
return {
delegateIPs: function (time) {
if (time === void 0) { time = 'current'; }
return rpc('epoch_delegates', {
epoch: time
}).then(function (res) { return res; });
},
history: function (count, hash) {
return rpc('epochs_latest', {
count: count || 1000,
head: hash
}).then(function (res) { return res; });
},
get: function (hashes) {
return rpc('epochs', {
hashes: hashes
}).then(function (res) { return res; });
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "microEpochs", {
get: function () {
var rpc = this.rpc;
return {
history: function (count, hash) {
return rpc('micro_blocks_latest', {
count: count || 1000,
head: hash
}).then(function (res) { return res; });
},
get: function (hashes) {
return rpc('micro_blocks', {
hashes: hashes
}).then(function (res) { return res; });
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "requestBlocks", {
get: function () {
var rpc = this.rpc;
return {
history: function (count, delegateIndex, hash) {
return rpc('request_blocks_latest', {
count: count || 1000,
delegate_id: delegateIndex || '0',
head: hash
}).then(function (res) { return res; });
},
get: function (hashes) {
return rpc('request_blocks', {
hashes: hashes
}).then(function (res) { return res; });
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "convert", {
get: function () {
return exports.convert;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "key", {
get: function () {
var rpc = this.rpc;
// The word 'private' is reserved in JS so we use this function
// to get around that, and to make 'address' more clear
function convertKeyObj(keyObj) {
return {
privateKey: keyObj.private,
publicKey: keyObj.public,
address: keyObj.account
};
}
return {
create: function () {
return rpc('key_create').then(convertKeyObj);
},
expand: function (privateKey) {
return rpc('key_expand', { key: privateKey }).then(convertKeyObj);
}
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(Logos.prototype, "work", {
//Generate and get work
get: function () {
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
generate: function (hash) {
return rpc('work_generate', { hash: hash }).then(function (result) {
_log("(WORK) generated PoW: " + result.work);
return result;
});
},
validate: function (work, hash) {
return rpc('work_validate', { work: work, hash: hash });
}
};
},
enumerable: true,
configurable: true
});
Logos.prototype.available = function () {
return this.rpc('available_supply').then(function (res) { return res.available; });
};
Logos.prototype.generateMicroBlock = function (last) {
return this.rpc('generate_microblock', {
last: last
}).then(function (res) { return res; });
};
Logos.prototype.deterministicKey = function (seed, index) {
return this.rpc('deterministic_key', {
seed: seed,
index: index
});
};
Logos.convert = exports.convert;
return Logos;
}());
exports.default = Logos;
//# sourceMappingURL=index.js.map