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: Add option to check any ERC20 token balance #25

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
13 changes: 11 additions & 2 deletions src/commands/balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ViemProvider from "../utils/viemProvider.js";
import chalk from "chalk";
import inquirer from "inquirer";
import { getTokenInfo, resolveTokenAddress } from "../utils/tokenHelper.js";
import {
getTokenInfo,
isERC20Contract,
resolveTokenAddress,
} from "../utils/tokenHelper.js";
import ora from "ora";
import { console } from "inspector";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the line with import:

import { console } from "inspector";

as it is not necessary. Just use default console.log function

import {
Expand Down Expand Up @@ -78,7 +82,10 @@ export async function balanceCommand(
return "🚫 Invalid contract address";
}
if (!(await isValidContract(client, formattedContractAddress))) {
return " 🚫 Invalid contract address or contract not found";
return "🚫 Invalid contract address or contract not found";
}
if (!(await isERC20Contract(client, formattedContractAddress))) {
return "🚫 Invalid contract address, only ERC20 tokens are supported";
}
return true;
} catch {
Expand Down Expand Up @@ -127,5 +134,7 @@ export async function balanceCommand(
} else {
console.error(chalk.red("🚨 An unknown error occurred."));
}
} finally {
spinner.stop();
}
}
7 changes: 7 additions & 0 deletions src/constants/erc20ABI.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this file and use erc20abi constant that can be imported from viem.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export const erc20ABI = [
outputs: [{ name: "balance", type: "uint256" }],
type: "function",
},
{
constant: true,
inputs: [],
name: "totalSupply",
outputs: [{ name: "", type: "uint256" }],
type: "function",
},
{
constant: false,
inputs: [
Expand Down
6 changes: 3 additions & 3 deletions src/constants/tokenAdress.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokens must be: RIF, USDRIF (instead of rUSDT) and DoC (instead of rDoc). Here you can find all these token's addresses in mainnet and testnet

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Address } from "viem";
export const TOKENS: Record<string, Record<string, Address>> = {
RIF: {
mainnet: "0x2acc95758f8b5F583470ba265eb685a8f45fc9d5",
testnet: "0xC370cD19517b5A8a9f6dF0958679e8cd4874C048",
testnet: "0x19f64674d8a5b4e652319f5e239efd3bc969a1fe",
},
rUSDT: {
mainnet: "0xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96",
testnet: "0x31974a4970BAda0ca9bCdE2e2EE6FC15922c5334",
testnet: "0x2694785f9c3006edf88df3a66ba7adf106dbd6a0",
},
rDoc: {
mainnet: "0x2d919f19D4892381d58EdEbEcA66D5642ceF1A1F",
testnet: "0x7fb303D9806a72563C46aAd8D874B301419c374b",
testnet: "0x7fb303d9806a72563c46aad8d874b301419c374b",
},
};
3 changes: 3 additions & 0 deletions src/utils/index.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecesary blank spaces

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export async function isValidContract(
}
}




export function getAddress(address?: Address): Address | undefined {
if (address) {
return validateAndFormatAddress(address);
Expand Down
54 changes: 53 additions & 1 deletion src/utils/tokenHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, PublicClient } from "viem";
import { Address, encodeFunctionData, PublicClient } from "viem";
import { erc20ABI } from "../constants/erc20ABI.js";
import { TOKENS } from "../constants/tokenAdress";

Expand Down Expand Up @@ -48,3 +48,55 @@ export async function getTokenInfo(
symbol: symbol,
};
}

export async function isERC20Contract(
client: PublicClient,
address: Address
): Promise<boolean> {
try {
const checks = await Promise.all([
client
.call({
to: address,
data: encodeFunctionData({
abi: erc20ABI,
functionName: "totalSupply",
}),
})
.then(() => true)
.catch(() => false),
client
.call({
to: address,
data: encodeFunctionData({
abi: erc20ABI,
functionName: "decimals",
}),
})
.then(() => true)
.catch(() => false),

client
.call({
to: address,
data: encodeFunctionData({
abi: erc20ABI,
functionName: "symbol",
}),
})
.then(() => true)
.catch(() => false),
]);

const isERC20 = checks.every((check) => check === true);

if (!isERC20) {
return false;
}

return true;
} catch (error) {
console.error("Error checking ERC20 contract:", error);
return false;
}
}