@haechi-labs/henesis-cli
Version:
🚀 Command Line Interface tool to Utilize henesis
87 lines • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../types");
const class_transformer_1 = require("class-transformer");
const wretch_1 = require("./wretch");
const url_1 = require("./url");
const node_1 = require("../types/node");
const utils_1 = require("../utils");
class NodeRpc {
constructor(server, version) {
this.trustedNodeBaseUrl = 'https://tn.henesis.io';
this.blockchainNodes = new types_1.BlockchainNodes(['mainnet', 'ropsten', 'rinkeby'], //ethereum
['mainnet', 'baobab']);
this.server = server + '/stats/' + version;
this.rpcVersion = version;
}
async dailyUsage() {
const clientId = utils_1.getUserProperty('clientId');
const now = new Date().toISOString();
// json rpc db record is created at tomorrow. 1 day time difference exists now.
const startDate = utils_1.getSpecificDayOfMonth(2).toISOString();
const json = await wretch_1.getWretcher()
.url(`${this.server}/jsonRpcDailyStats?clientId=${clientId}&start=${startDate}&end=${now}&size=31&page=0`)
.get()
.error(404, err => {
if (err.json.error.message === 'stats does not exist') {
return {
data: [],
};
}
})
.json()
.catch((err) => {
throw err;
});
if (!Array.isArray(json['data'])) {
throw new Error(`Expected dailyUsage to return an array but it returned ${json['data']}`);
}
return class_transformer_1.plainToClass(node_1.NodeDailyStat, json['data']);
}
async hourlyUsage() {
const clientId = utils_1.getUserProperty('clientId');
const now = new Date().toISOString();
// json rpc db record is created at 1 hour later. 1 hour time difference exists now.
const startDate = utils_1.getSpecificHourOfDay(1).toISOString();
const json = await wretch_1.getWretcher()
.url(`${this.server}/jsonRpcHourlyStats?clientId=${clientId}&start=${startDate}&end=${now}&size=24&page=0`)
.get()
.error(404, err => {
if (err.json.error.message === 'stats does not exist') {
return {
data: [],
};
}
})
.json()
.catch((err) => {
throw err;
});
if (!Array.isArray(json['data'])) {
throw new Error(`Expected hourlyUsage to return an array but it returned ${json['data']}`);
}
return class_transformer_1.plainToClass(node_1.NodeHourlyStat, json['data']);
}
async status() {
const nodeStatus = [];
for (const network of this.blockchainNodes.ethereum) {
nodeStatus.push(this.statusToNodeStatus('ethereum', network));
}
for (const network of this.blockchainNodes.klaytn) {
nodeStatus.push(this.statusToNodeStatus('klaytn', network));
}
return nodeStatus;
}
statusToNodeStatus(blockchain, network) {
const result = {
platform: blockchain,
network,
endpoint: `${this.trustedNodeBaseUrl}/${blockchain}/${network}`,
};
return class_transformer_1.plainToClass(types_1.NodeStatus, result);
}
}
exports.NodeRpc = NodeRpc;
const url = url_1.baseUrl();
exports.default = new NodeRpc(url, url_1.rpcVersion);
//# sourceMappingURL=node.js.map