beeline-cli
Version:
A terminal wallet for the Hive blockchain - type, sign, rule the chain
250 lines • 11.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const neon_js_1 = require("../utils/neon.js");
const crypto_js_1 = require("../utils/crypto.js");
const hive_js_1 = require("../utils/hive.js");
class RC extends core_1.Command {
async run() {
const { args, flags } = await this.parse(RC);
const keyManager = new crypto_js_1.KeyManager();
await keyManager.initialize();
let account = args.account;
// Clean @ prefix if provided
if (account?.startsWith('@')) {
account = account.substring(1);
}
// Use default account if no account specified
if (!account) {
account = keyManager.getDefaultAccount();
if (!account) {
console.log(neon_js_1.neonChalk.warning(`${neon_js_1.neonSymbols.cross} No account specified and no default account set`));
console.log(neon_js_1.neonChalk.info('Specify an account or import a key first'));
return;
}
}
if (flags.watch) {
return this.watchRC(account, flags.node, flags.threshold);
}
await this.checkRC(account, flags.node, flags.format, flags.threshold);
}
async checkRC(account, nodeUrl, format = 'table', threshold = 20) {
console.log(neon_js_1.neonChalk.glow(`${neon_js_1.neonSymbols.diamond} Checking Resource Credits...`));
console.log('');
const spinner = (0, neon_js_1.neonSpinner)('Fetching RC data from Hive blockchain');
try {
const keyManager = new crypto_js_1.KeyManager();
await keyManager.initialize();
const hiveClient = new hive_js_1.HiveClient(keyManager, nodeUrl);
const rcData = await hiveClient.getResourceCredits(account);
clearInterval(spinner);
process.stdout.write('\r' + ' '.repeat(80) + '\r');
if (format === 'json') {
console.log(JSON.stringify({
account,
current_rc: rcData.current,
max_rc: rcData.max,
percentage: rcData.percentage,
status: this.getRCStatus(rcData.percentage, threshold)
}, null, 2));
return;
}
// Display RC information
const rcStatus = this.getRCStatus(rcData.percentage, threshold);
const statusColor = this.getRCStatusColor(rcData.percentage, threshold);
const progressBar = this.createProgressBar(rcData.percentage);
const rcDetails = [
`${neon_js_1.neonChalk.darkCyan('Account: @' + account)}`,
``,
`${neon_js_1.neonChalk.orange('Current RC:')} ${neon_js_1.neonChalk.white(this.formatNumber(rcData.current))}`,
`${neon_js_1.neonChalk.cyan('Maximum RC:')} ${neon_js_1.neonChalk.white(this.formatNumber(rcData.max))}`,
`${neon_js_1.neonChalk.electric('Percentage:')} ${statusColor(rcData.percentage.toFixed(2) + '%')}`,
``,
`${neon_js_1.neonChalk.white('RC Level:')} ${progressBar}`,
`${neon_js_1.neonChalk.magenta('Status:')} ${statusColor(rcStatus)}`,
``,
this.getRCAdvice(rcData.percentage, threshold)
].join('\n');
console.log((0, neon_js_1.createNeonBox)(rcDetails, `${neon_js_1.neonSymbols.star} RESOURCE CREDITS STATUS ${neon_js_1.neonSymbols.star}`));
// Show transaction capacity estimate
console.log('');
console.log(neon_js_1.neonChalk.info('💡 Transaction Capacity Estimates:'));
console.log(this.getTransactionEstimates(rcData.current));
}
catch (error) {
clearInterval(spinner);
process.stdout.write('\r' + ' '.repeat(80) + '\r');
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} RC check failed: ${error instanceof Error ? error.message : 'Unknown error'}`));
console.log('');
console.log(neon_js_1.neonChalk.info('Possible causes:'));
console.log(neon_js_1.neonChalk.darkCyan('• Invalid account name'));
console.log(neon_js_1.neonChalk.darkCyan('• Network connectivity issues'));
console.log(neon_js_1.neonChalk.darkCyan('• RC data not available for account'));
}
}
async watchRC(account, nodeUrl, threshold = 20) {
console.log(neon_js_1.neonChalk.glow(`${neon_js_1.neonSymbols.diamond} Watching Resource Credits for @${account}...`));
console.log(neon_js_1.neonChalk.info('💡 Updates every 30 seconds. Press Ctrl+C to stop.'));
console.log('');
const checkRCContinuously = async () => {
try {
const keyManager = new crypto_js_1.KeyManager();
await keyManager.initialize();
const hiveClient = new hive_js_1.HiveClient(keyManager, nodeUrl);
const rcData = await hiveClient.getResourceCredits(account);
// Clear screen and show timestamp
console.clear();
console.log(neon_js_1.neonChalk.glow(`${neon_js_1.neonSymbols.diamond} RC Watch Mode - @${account}`));
console.log(neon_js_1.neonChalk.darkCyan(`Last updated: ${new Date().toLocaleTimeString()}`));
console.log('');
const rcStatus = this.getRCStatus(rcData.percentage, threshold);
const statusColor = this.getRCStatusColor(rcData.percentage, threshold);
const progressBar = this.createProgressBar(rcData.percentage);
const rcDetails = [
`${neon_js_1.neonChalk.orange('Current RC:')} ${neon_js_1.neonChalk.white(this.formatNumber(rcData.current))}`,
`${neon_js_1.neonChalk.cyan('Maximum RC:')} ${neon_js_1.neonChalk.white(this.formatNumber(rcData.max))}`,
`${neon_js_1.neonChalk.electric('Percentage:')} ${statusColor(rcData.percentage.toFixed(2) + '%')}`,
``,
`${neon_js_1.neonChalk.white('RC Level:')} ${progressBar}`,
`${neon_js_1.neonChalk.magenta('Status:')} ${statusColor(rcStatus)}`,
``,
this.getRCAdvice(rcData.percentage, threshold)
].join('\n');
console.log((0, neon_js_1.createNeonBox)(rcDetails, `${neon_js_1.neonSymbols.star} LIVE RC STATUS ${neon_js_1.neonSymbols.star}`));
console.log('');
console.log(neon_js_1.neonChalk.info('Press Ctrl+C to exit watch mode'));
}
catch (error) {
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} RC update failed: ${error instanceof Error ? error.message : 'Unknown error'}`));
}
};
// Initial check
await checkRCContinuously();
// Set up interval for continuous monitoring
const interval = setInterval(checkRCContinuously, 30000);
// Handle Ctrl+C to exit gracefully
process.on('SIGINT', () => {
clearInterval(interval);
console.log('');
console.log(neon_js_1.neonChalk.info('RC watch mode stopped'));
process.exit(0);
});
}
getRCStatus(percentage, threshold) {
if (percentage >= 80)
return 'EXCELLENT';
if (percentage >= 50)
return 'GOOD';
if (percentage >= threshold)
return 'LOW';
return 'CRITICAL';
}
getRCStatusColor(percentage, threshold) {
if (percentage >= 80)
return neon_js_1.neonChalk.green;
if (percentage >= 50)
return neon_js_1.neonChalk.cyan;
if (percentage >= threshold)
return neon_js_1.neonChalk.orange;
return neon_js_1.neonChalk.error;
}
createProgressBar(percentage) {
const barLength = 20;
const filled = Math.round((percentage / 100) * barLength);
const empty = barLength - filled;
let bar = '';
// Use different colors based on percentage
if (percentage >= 80) {
bar = neon_js_1.neonChalk.green('█'.repeat(filled)) + neon_js_1.neonChalk.darkCyan('░'.repeat(empty));
}
else if (percentage >= 50) {
bar = neon_js_1.neonChalk.cyan('█'.repeat(filled)) + neon_js_1.neonChalk.darkCyan('░'.repeat(empty));
}
else if (percentage >= 20) {
bar = neon_js_1.neonChalk.orange('█'.repeat(filled)) + neon_js_1.neonChalk.darkCyan('░'.repeat(empty));
}
else {
bar = neon_js_1.neonChalk.error('█'.repeat(filled)) + neon_js_1.neonChalk.darkCyan('░'.repeat(empty));
}
return `[${bar}] ${percentage.toFixed(1)}%`;
}
getRCAdvice(percentage, threshold) {
if (percentage >= 80) {
return neon_js_1.neonChalk.green('✨ Excellent! You can perform many transactions');
}
else if (percentage >= 50) {
return neon_js_1.neonChalk.cyan('👍 Good RC levels, normal transaction activity');
}
else if (percentage >= threshold) {
return neon_js_1.neonChalk.orange('⚠️ Low RC - consider reducing transaction frequency');
}
else {
return neon_js_1.neonChalk.error('🚨 Critical! Very limited transaction capacity');
}
}
formatNumber(num) {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(2) + 'B';
}
else if (num >= 1000000) {
return (num / 1000000).toFixed(2) + 'M';
}
else if (num >= 1000) {
return (num / 1000).toFixed(2) + 'K';
}
return num.toFixed(0);
}
getTransactionEstimates(currentRC) {
// Rough estimates based on typical RC costs
const transferCost = 13000000; // Approximate RC cost for a transfer
const commentCost = 200000000; // Approximate RC cost for a comment
const voteCost = 100000000; // Approximate RC cost for a vote
const transfers = Math.floor(currentRC / transferCost);
const comments = Math.floor(currentRC / commentCost);
const votes = Math.floor(currentRC / voteCost);
return [
`${neon_js_1.neonChalk.cyan('Transfers:')} ~${neon_js_1.neonChalk.white(transfers.toString())} remaining`,
`${neon_js_1.neonChalk.magenta('Comments:')} ~${neon_js_1.neonChalk.white(comments.toString())} remaining`,
`${neon_js_1.neonChalk.electric('Votes:')} ~${neon_js_1.neonChalk.white(votes.toString())} remaining`,
'',
neon_js_1.neonChalk.darkCyan('💡 RC regenerates over 5 days - 20% per day')
].join('\n');
}
}
RC.description = 'Monitor Resource Credits (RC) with cyberpunk style - transaction power meter';
RC.examples = [
`$ beeline rc`,
`$ beeline rc alice`,
`$ beeline rc alice --format json`,
`$ beeline rc alice --watch`
];
RC.flags = {
node: core_1.Flags.string({
char: 'n',
description: 'RPC node to use'
}),
format: core_1.Flags.string({
char: 'f',
description: 'output format',
options: ['table', 'json'],
default: 'table'
}),
watch: core_1.Flags.boolean({
char: 'w',
description: 'watch RC levels continuously (updates every 30 seconds)',
default: false
}),
threshold: core_1.Flags.integer({
char: 't',
description: 'warning threshold percentage (default: 20%)',
default: 20
})
};
RC.args = {
account: core_1.Args.string({
description: 'account to check RC for',
required: false
})
};
exports.default = RC;
//# sourceMappingURL=rc.js.map