@mraicodedev/bsc-contract-security
Version:
A comprehensive library for analyzing smart contract security risks on Binance Smart Chain, specialized for meme coins and shitcoins
96 lines (82 loc) • 2.88 kB
JavaScript
const { ethers } = require('ethers');
class TokenAnalyzer {
constructor(provider) {
this.provider = provider;
this.erc20Abi = [
'function name() view returns (string)',
'function symbol() view returns (string)',
'function decimals() view returns (uint8)',
'function totalSupply() view returns (uint256)',
'function balanceOf(address) view returns (uint256)',
'function owner() view returns (address)',
'function getOwner() view returns (address)'
];
}
async analyzeToken(address) {
const contract = new ethers.Contract(address, this.erc20Abi, this.provider);
const analysis = {};
try {
// Get basic information
const [name, symbol, decimals, totalSupply] = await Promise.allSettled([
contract.name(),
contract.symbol(),
contract.decimals(),
contract.totalSupply()
]);
analysis.name = name.status === 'fulfilled' ? name.value : 'Unknown';
analysis.symbol = symbol.status === 'fulfilled' ? symbol.value : 'Unknown';
analysis.decimals = decimals.status === 'fulfilled' ? decimals.value : 18;
analysis.totalSupply = totalSupply.status === 'fulfilled' ?
ethers.formatUnits(totalSupply.value, analysis.decimals) : '0';
// Check owner
let owner = null;
try {
owner = await contract.owner();
} catch {
try {
owner = await contract.getOwner();
} catch {}
}
analysis.owner = owner;
// Analyze token risks
analysis.risks = this.analyzeTokenRisks(analysis);
} catch (error) {
analysis.error = error.message;
}
return analysis;
}
analyzeTokenRisks(tokenInfo) {
const risks = [];
// Check for suspicious token names
const suspiciousNames = ['test', 'fake', 'scam', 'rug'];
if (suspiciousNames.some(word =>
tokenInfo.name.toLowerCase().includes(word) ||
tokenInfo.symbol.toLowerCase().includes(word)
)) {
risks.push({
type: 'SUSPICIOUS_NAME',
severity: 'HIGH',
description: 'Token name contains suspicious words'
});
}
// Check for excessive total supply
const supply = parseFloat(tokenInfo.totalSupply);
if (supply > 1000000000000) {
risks.push({
type: 'LARGE_SUPPLY',
severity: 'MEDIUM',
description: 'Total supply is too large, may cause inflation'
});
}
// Check if owner is zero address (renounced)
if (tokenInfo.owner === '0x0000000000000000000000000000000000000000') {
risks.push({
type: 'RENOUNCED_OWNERSHIP',
severity: 'LOW',
description: 'Ownership has been renounced (positive)'
});
}
return risks;
}
}
module.exports = TokenAnalyzer;