UNPKG

@mraicodedev/bsc-contract-security

Version:

A comprehensive library for analyzing smart contract security risks on Binance Smart Chain, specialized for meme coins and shitcoins

69 lines (58 loc) 1.9 kB
const { ethers } = require('ethers'); const SecurityAnalyzer = require('./security-analyzer'); class ContractAddressAnalyzer { constructor(rpcUrl = 'https://bsc-dataseed1.binance.org/') { this.provider = new ethers.JsonRpcProvider(rpcUrl); this.securityAnalyzer = new SecurityAnalyzer(rpcUrl); } isValidAddress(address) { return ethers.isAddress(address); } async isContract(address) { if (!this.isValidAddress(address)) return false; const code = await this.provider.getCode(address); return code !== '0x'; } async getContractInfo(address) { if (!await this.isContract(address)) { throw new Error('Address is not a contract'); } const [code, balance, nonce] = await Promise.all([ this.provider.getCode(address), this.provider.getBalance(address), this.provider.getTransactionCount(address) ]); return { address, isContract: true, bytecodeSize: (code.length - 2) / 2, balance: ethers.formatEther(balance), transactionCount: nonce, bytecode: code }; } async analyzeContract(address) { const info = await this.getContractInfo(address); const security = await this.securityAnalyzer.analyzeContractSecurity(address); return { ...info, analysis: { hasCode: info.bytecodeSize > 0, isActive: info.transactionCount > 0, hasBalance: parseFloat(info.balance) > 0, complexity: this.estimateComplexity(info.bytecode) }, security }; } estimateComplexity(bytecode) { const size = (bytecode.length - 2) / 2; if (size < 1000) return 'Simple'; if (size < 5000) return 'Medium'; return 'Complex'; } checksumAddress(address) { return ethers.getAddress(address); } } module.exports = ContractAddressAnalyzer;