@rsksmart/rsk-cli
Version:
CLI tool for Rootstock network using Viem
82 lines (81 loc) • 2.4 kB
JavaScript
import { encodeFunctionData, erc20Abi } from "viem";
import { TOKENS } from "../constants/tokenAdress.js";
export function resolveTokenAddress(token, testnet) {
return TOKENS[token][testnet ? "testnet" : "mainnet"].toLowerCase();
}
export async function getTokenInfo(client, tokenAddress, holderAddress) {
const [balance, decimals, name, symbol] = await Promise.all([
client.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [holderAddress],
}),
client.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "decimals",
}),
client.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "name",
}),
client.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "symbol",
}),
]);
return {
balance: balance,
decimals: decimals,
name: name,
symbol: symbol,
};
}
export async function isERC20Contract(client, address) {
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;
}
}