@rsksmart/rsk-cli
Version:
CLI tool for Rootstock network using Viem
61 lines (60 loc) • 2.39 kB
JavaScript
import ViemProvider from "../utils/viemProvider.js";
import chalk from "chalk";
import Table from "cli-table3";
function logMessage(params, message, color = chalk.white) {
if (!params.isExternal) {
console.log(color(message));
}
}
function logError(params, message) {
logMessage(params, `❌ ${message}`, chalk.red);
}
export async function txCommand(params) {
try {
const formattedTxId = params.txid.startsWith("0x") ? params.txid : `0x${params.txid}`;
const txidWithCorrectType = formattedTxId;
const provider = new ViemProvider(params.testnet);
const client = await provider.getPublicClient();
const txReceipt = await client.getTransactionReceipt({
hash: txidWithCorrectType,
});
if (!txReceipt) {
const errorMessage = "Transaction not found. Please check the transaction ID and try again.";
logError(params, errorMessage);
return {
error: errorMessage,
success: false,
};
}
const txData = {
txId: txidWithCorrectType,
blockHash: txReceipt.blockHash,
blockNumber: txReceipt.blockNumber.toString(),
gasUsed: txReceipt.gasUsed.toString(),
status: txReceipt.status ? "Success" : "Failed",
from: txReceipt.from,
to: txReceipt.to,
network: params.testnet ? "Rootstock Testnet" : "Rootstock Mainnet",
};
if (!params.isExternal) {
const table = new Table({
head: ["🔍", "Details"],
colWidths: [20, 68],
});
table.push({ "🔑 Tx ID": txidWithCorrectType }, { "🔗 Block Hash": txReceipt.blockHash }, { "🧱 Block No.": txReceipt.blockNumber.toString() }, { "⛽ Gas Used": txReceipt.gasUsed.toString() }, { "✅ Status": txReceipt.status ? "Success" : "Failed" }, { "📤 From": txReceipt.from }, { "📥 To": txReceipt.to });
console.log(table.toString());
}
return {
success: true,
data: txData,
};
}
catch (error) {
const errorMessage = "Error checking transaction status, please check the transaction ID.";
logError(params, errorMessage);
return {
error: errorMessage,
success: false,
};
}
}