Skip to content

Commit

Permalink
chore:history command enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
SarveshLimaye committed Dec 18, 2024
1 parent ff2e1fd commit ce53f72
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,28 @@ The history command allows you to fetch the transaction history for a wallet on

#### Mainnet

With arguments:

```bash
rsk-cli history --apiKey <apiKey> --number <number>
```

Without arguments:

```bash
rsk-cli history
```

#### Testnet

With arguments:

```bash
rsk-cli history --testnet --apiKey <apiKey> --number <number>
```

Without arguments

```bash
rsk-cli history --testnet
```
Expand All @@ -357,27 +373,14 @@ Output example:

```
? 🔒 Enter Alchemy API key to fetch history: ********************************
🔍 Fetching transaction history for 0x19661D036D4e590948b9c00eef3807b88fBfA8e1 ...
✅ Transaction history fetched successfully: [
{
"blockNum": "0x57bdd9",
"uniqueId": "0xde678614cd9e20fe5891c25069afef680174456b104f31c9078eb486abd95a64:external",
"hash": "0xde678614cd9e20fe5891c25069afef680174456b104f31c9078eb486abd95a64",
"from": "0x19661d036d4e590948b9c00eef3807b88fbfa8e1",
"to": "0xb45805aead9407f5c7860ff8eccaedd4d0ab36a6",
"value": 0.000003,
"erc721TokenId": null,
"erc1155Metadata": null,
"tokenId": null,
"asset": "ETH",
"category": "external",
"rawContract": {
"value": "0x2ba7def3000",
"address": null,
"decimal": "0x12"
}
}
]
🔍 Fetching transaction history on Rootstack Testnet for 0x19661D036D4e590948b9c00eef3807b88fBfA8e1 ...
✅ Transfer:
From: 0x19661d036d4e590948b9c00eef3807b88fbfa8e1
To: 0xb45805aead9407f5c7860ff8eccaedd4d0ab36a6
Token: ETH
Value: 0.000003
Tx Hash: 0xde678614cd9e20fe5891c25069afef680174456b104f31c9078eb486abd95a64
```

## Contributing
Expand Down
4 changes: 3 additions & 1 deletion bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface CommandOptions {
json?: any;
name?: string;
decodedArgs?: any;
number?: string;
}

const orange = chalk.rgb(255, 165, 0);
Expand Down Expand Up @@ -157,9 +158,10 @@ program
.command("history")
.description("Fetch history for current wallet")
.option("--apiKey <apiKey", "Alchemy API key")
.option("--number <number>", "Number of transactions to fetch")
.option("-t, --testnet", "History of wallet on the testnet")
.action(async (options: CommandOptions) => {
await historyCommand(!!options.testnet, options.apiKey!);
await historyCommand(!!options.testnet, options.apiKey!, options.number!);
});

program.parse(process.argv);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"wallet": "pnpm run build && node dist/bin/index.js wallet",
"balance": "pnpm run build && node dist/bin/index.js balance",
"transfer": "pnpm run build && node dist/bin/index.js transfer --testnet --address 0xa5f45f5bddefC810C48aCC1D5CdA5e5a4c6BC59E --value 0.001",
"tx-status": "pnpm run build && node dist/bin/index.js tx --testnet --txid 0x876a0a9b167889350c41930a4204e5d9acf5704a7f201447a337094189af961c4"
"tx-status": "pnpm run build && node dist/bin/index.js tx --testnet --txid 0x876a0a9b167889350c41930a4204e5d9acf5704a7f201447a337094189af961c4",
"history": "pnpm run build && node dist/bin/index.js history"
},
"keywords": [
"rootstock",
Expand Down
29 changes: 18 additions & 11 deletions src/commands/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import chalk from "chalk";
import fs from "fs";
import { walletFilePath } from "../utils/constants.js";

export async function historyCommand(testnet: boolean, apiKey: string) {
export async function historyCommand(
testnet: boolean,
apiKey: string,
number: string
) {
try {
// Check if API key exists in storage or passed as argument
let storedApiKey = getApiKeyFromStorage();
Expand Down Expand Up @@ -41,7 +45,11 @@ export async function historyCommand(testnet: boolean, apiKey: string) {
const { address: walletAddress } = wallet;

console.log(
chalk.blue(`🔍 Fetching transaction history for ${walletAddress} ... `)
chalk.blue(
`🔍 Fetching transaction history on Rootstack ${
testnet ? "Testnet" : "Mainnet"
} for ${walletAddress} ... `
)
);

const data = JSON.stringify({
Expand All @@ -53,6 +61,7 @@ export async function historyCommand(testnet: boolean, apiKey: string) {
fromBlock: "0x0",
fromAddress: walletAddress,
category: ["external", "erc20", "erc721", "erc1155"],
withMetadata: true,
},
],
});
Expand Down Expand Up @@ -84,18 +93,24 @@ export async function historyCommand(testnet: boolean, apiKey: string) {
return;
}

const transfers = result.result?.transfers;
let transfers = result.result?.transfers;

if (!transfers || transfers.length === 0) {
console.log(chalk.yellow("⚠️ No transactions found."));
return;
}

if (number) {
transfers = transfers.slice(0, parseInt(number));
}
for (const transfer of transfers) {
console.log(chalk.green(`✅ Transfer:`));
console.log(` From: ${transfer.from}`);
console.log(` To: ${transfer.to}`);
console.log(` Token: ${transfer.asset || "N/A"}`);
console.log(` Value: ${transfer.value || "N/A"}`);
console.log(` Tx Hash: ${transfer.hash}`);
console.log(` Time: ${new Date(transfer.metadata.blockTimestamp)}`);
}
} catch (error: any) {
console.error(
Expand All @@ -106,14 +121,6 @@ export async function historyCommand(testnet: boolean, apiKey: string) {

async function writeApiKey(apiKey: string) {
try {
// Check if wallet file exists
if (!fs.existsSync(walletFilePath)) {
console.error(
chalk.red("🚫 Wallet file not found. Please create a wallet first.")
);
return;
}

// Read the existing wallet file
const walletsData = JSON.parse(fs.readFileSync(walletFilePath, "utf8"));

Expand Down

0 comments on commit ce53f72

Please sign in to comment.