Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:dynamic wallet selection #27

Merged
merged 25 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ece3293
feat: add external wallet balance option
Pratham-19 Nov 24, 2024
2e1b9e7
feat: revet changes
Pratham-19 Nov 24, 2024
ef6988d
feat: add check token balance
Pratham-19 Nov 24, 2024
c4fa69b
feat:dynamic wallet selection
SarveshLimaye Nov 28, 2024
e5998db
fix:fail tx if not valid wallet name
SarveshLimaye Nov 30, 2024
6b94d22
feat: add wallet backup
chandrabosep Dec 3, 2024
48e50a4
feat: remove token option
Pratham-19 Dec 6, 2024
0703fa4
Merge branch 'rsksmart:main' into main
Pratham-19 Dec 6, 2024
44e5d8b
feat: resolve merge conflicts
Pratham-19 Dec 6, 2024
73d21a6
chore: u[date error comment
Pratham-19 Dec 6, 2024
7cc11a5
fix: wallet address fetch
Pratham-19 Dec 6, 2024
7ba62af
fix: add erc20 contract check
Pratham-19 Dec 6, 2024
42b69cb
feat:show transaction history for current wallet
SarveshLimaye Dec 6, 2024
920bd62
refactor: streamline wallet data handling and improve backup process
chandrabosep Dec 7, 2024
25e8cdd
chore: update token address
Pratham-19 Dec 10, 2024
7749ce2
chore: rmv debug console
Pratham-19 Dec 10, 2024
ff2e1fd
chore:history command enhancements
SarveshLimaye Dec 16, 2024
5b85028
chore: remove blank space
Pratham-19 Dec 17, 2024
9627a62
chore: change token address
Pratham-19 Dec 17, 2024
83a7c43
chore: rmv console import
Pratham-19 Dec 17, 2024
d97a9b9
feat: add viem erc20Abi
Pratham-19 Dec 17, 2024
ce53f72
chore:history command enhancements
SarveshLimaye Dec 18, 2024
7a7bdcf
Merge branch 'feat-backup' into pr-merge
chrisarevalodev Jan 9, 2025
895f596
merge with PR 41
chrisarevalodev Jan 9, 2025
a32c2d4
chore:conflicts resolved
SarveshLimaye Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: remove token option
Pratham-19 committed Dec 6, 2024
commit 48e50a4ff0372001e319b997794e4f5e4355f90b
38 changes: 2 additions & 36 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import { deployCommand } from "../src/commands/deploy.js";
import { verifyCommand } from "../src/commands/verify.js";
import { ReadContract } from "../src/commands/contract.js";
import { Address } from "viem";
import { checkTokenBalance, transferToken } from "../src/commands/token.js";

interface CommandOptions {
testnet?: boolean;
@@ -58,8 +57,9 @@ program
.command("balance")
.description("Check the balance of the saved wallet")
.option("-t, --testnet", "Check the balance on the testnet")
.option("-a ,--address <address>", "Token holder address")
.action(async (options: CommandOptions) => {
await balanceCommand(!!options.testnet);
await balanceCommand(!!options.testnet, options.address);
});

program
@@ -145,38 +145,4 @@ program
await ReadContract(options.address! as `0x${string}`, !!options.testnet);
});

const token = program
.command("token")
.description("Interact with ERC20 tokens");

token
.command("balance")
.description("Check token balance")
.requiredOption("-c,--contract <address>", "Token contract address")
.option("-a ,--address <address>", "Token holder address")
.option("-t, --testnet", "Use testnet")
.action(async (options: CommandOptions) => {
await checkTokenBalance(
!!options.testnet,
options.contract as Address,
options.address
);
});

token
.command("transfer")
.description("Transfer tokens")
.requiredOption("-c,--contract <address>", "Token contract address")
.requiredOption("-a,--address <address>", "Recipient address")
.requiredOption("-v,--value <amount>", "Amount to transfer")
.option("-t, --testnet", "Use testnet")
.action(async (options: CommandOptions) => {
await transferToken(
!!options.testnet,
options.contract as Address,
options.address as Address,
options.value!
);
});

program.parse(process.argv);
121 changes: 98 additions & 23 deletions src/commands/balance.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,118 @@
import ViemProvider from "../utils/viemProvider.js";
import fs from "fs";
import path from "path";
import chalk from "chalk";
import inquirer from "inquirer";
import { getTokenInfo, resolveTokenAddress } from "../utils/tokenHelper.js";
import ora from "ora";
import { console } from "inspector";
import {
getAddress,
isValidContract,
validateAndFormatAddress,
} from "../utils/index.js";
import { Address, formatUnits } from "viem";
import { TOKENS } from "../constants/tokenAdress.js";

const walletFilePath = path.join(process.cwd(), "rootstock-wallet.json");

export async function balanceCommand(testnet: boolean) {
export async function balanceCommand(
testnet: boolean,
holderAddress?: Address
) {
const spinner = ora();
try {
if (!fs.existsSync(walletFilePath)) {
console.log(
chalk.red("🚫 No saved wallet found. Please create a wallet first.")
);
const targetAddress = getAddress(holderAddress);

if (!targetAddress) {
console.log(chalk.red("⚠️ No valid aholder ddress found"));
return;
}
const provider = new ViemProvider(testnet);
const client = await provider.getPublicClient();

const walletData = JSON.parse(fs.readFileSync(walletFilePath, "utf8"));
const { address } = walletData;
const { token } = await inquirer.prompt({
type: "list",
name: "token",
message: "Select token to check balance:",
choices: ["rBTC", ...Object.keys(TOKENS), "Custom Token"],
});

if (!address) {
console.log(chalk.red("⚠️ No valid address found in the saved wallet."));
if (token === "rBTC") {
spinner.start(chalk.white("🔍 Checking balance..."));
const balance = await client.getBalance({ address: targetAddress });
const rbtcBalance = formatUnits(balance, 18);

spinner.succeed(chalk.green("Balance retrieved successfully"));

console.log(
chalk.white(`📄 Wallet Address:`),
chalk.green(targetAddress)
);
console.log(
chalk.white(`🌐 Network:`),
chalk.green(testnet ? "Rootstock Testnet" : "Rootstock Mainnet")
);
console.log(
chalk.white(`💰 Current Balance:`),
chalk.green(`${rbtcBalance} RBTC`)
);
console.log(
chalk.blue(
`🔗 Ensure that transactions are being conducted on the correct network.`
)
);
return;
}

const provider = new ViemProvider(testnet);
const client = await provider.getPublicClient();
let tokenAddress: Address;

const balance = await client.getBalance({ address });
if (token === "Custom Token") {
spinner.stop();
const { address } = await inquirer.prompt({
type: "input",
name: "address",
message: "Enter the token address:",
validate: async (input: string) => {
try {
const address = input as Address;
const formattedContractAddress = validateAndFormatAddress(address);
if (!formattedContractAddress) {
console.log(chalk.red());
return "🚫 Invalid contract address";
}
if (!(await isValidContract(client, formattedContractAddress))) {
return " 🚫 Invalid contract address or contract not found";
}
return true;
} catch {
return false;
}
},
});
tokenAddress = address.toLowerCase() as Address;
} else {
tokenAddress = resolveTokenAddress(token, testnet);
}

const rbtcBalance = Number(balance) / 10 ** 18;
spinner.start(chalk.white("🔍 Checking balance..."));

console.log(chalk.white(`📄 Wallet Address:`), chalk.green(address));
console.log(
chalk.white(`🌐 Network:`),
chalk.green(testnet ? "Rootstock Testnet" : "Rootstock Mainnet")
const { balance, decimals, name, symbol } = await getTokenInfo(
client,
tokenAddress,
targetAddress
);
const formattedBalance = formatUnits(balance, decimals);

spinner.succeed(chalk.green("Balance retrieved successfully"));

console.log(
chalk.white(`💰 Current Balance:`),
chalk.green(`${rbtcBalance} RBTC`)
chalk.white(`📄 Token Information:
Name: ${chalk.green(name)}
Contract: ${chalk.green(tokenAddress)}
👤 Holder Address: ${chalk.green(targetAddress)}
💰 Balance: ${chalk.green(`${formattedBalance} ${symbol}`)}
🌐 Network: ${chalk.green(
testnet ? "Rootstock Testnet" : "Rootstock Mainnet"
)}`)
);

console.log(
chalk.blue(
`🔗 Ensure that transactions are being conducted on the correct network.`
Loading