Prevents silent decimal mismatch bugs across EVM chains. Covers runtime decimal queries, chain-aware caching, cross-chain bridge precision drift, and safe normalization for bots, dashboards, and DeFi tools.
How this skill is triggered — by the user, by Claude, or both
Slash command
/everything-claude-code:evm-token-decimalsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
静默的小数位不匹配是导致余额或美元价值偏差几个数量级但不抛出错误的最简单方式之一。
静默的小数位不匹配是导致余额或美元价值偏差几个数量级但不抛出错误的最简单方式之一。
永远不要假设稳定币在所有地方使用相同的小数位。在运行时查询 decimals(),按 (chain_id, token_address) 缓存,并使用小数安全的数学进行价值计算。
from decimal import Decimal
from web3 import Web3
ERC20_ABI = [
{"name": "decimals", "type": "function", "inputs": [],
"outputs": [{"type": "uint8"}], "stateMutability": "view"},
{"name": "balanceOf", "type": "function",
"inputs": [{"name": "account", "type": "address"}],
"outputs": [{"type": "uint256"}], "stateMutability": "view"},
]
def get_token_balance(w3: Web3, token_address: str, wallet: str) -> Decimal:
contract = w3.eth.contract(
address=Web3.to_checksum_address(token_address),
abi=ERC20_ABI,
)
decimals = contract.functions.decimals().call()
raw = contract.functions.balanceOf(Web3.to_checksum_address(wallet)).call()
return Decimal(raw) / Decimal(10 ** decimals)
不要硬编码 1_000_000,因为一个代币符号通常在其他地方有 6 位小数。
from functools import lru_cache
@lru_cache(maxsize=512)
def get_decimals(chain_id: int, token_address: str) -> int:
w3 = get_web3_for_chain(chain_id)
contract = w3.eth.contract(
address=Web3.to_checksum_address(token_address),
abi=ERC20_ABI,
)
return contract.functions.decimals().call()
try:
decimals = contract.functions.decimals().call()
except Exception:
logging.warning(
"decimals() 在 %s(链 %s)上回退,默认使用 18",
token_address,
chain_id,
)
decimals = 18
记录回退并保持可见。旧的或非标准代币仍然存在。
interface IERC20Metadata {
function decimals() external view returns (uint8);
}
function normalizeToWad(address token, uint256 amount) internal view returns (uint256) {
uint8 d = IERC20Metadata(token).decimals();
if (d == 18) return amount;
if (d < 18) return amount * 10 ** (18 - d);
return amount / 10 ** (d - 18);
}
import { Contract, formatUnits } from 'ethers';
const ERC20_ABI = [
'function decimals() view returns (uint8)',
'function balanceOf(address) view returns (uint256)',
];
async function getBalance(provider: any, tokenAddress: string, wallet: string): Promise<string> {
const token = new Contract(tokenAddress, ERC20_ABI, provider);
const [decimals, raw] = await Promise.all([
token.decimals(),
token.balanceOf(wallet),
]);
return formatUnits(raw, decimals);
}
cast call <token_address> "decimals()(uint8)" --rpc-url <rpc>
decimals()Decimal、BigInt 或等效的精确数学运算,而非浮点数npx claudepluginhub aaione/everything-claude-code-zhPrevents silent decimal mismatch bugs across EVM chains with runtime lookup, chain-aware caching, and safe normalization for bots, dashboards, and DeFi tools.
Queries EVM blockchain data on Ethereum, Polygon, Arbitrum: transactions, address balances/tokens/history, blocks via Python CLI with Etherscan APIs.
Fetches NFT metadata, token balances, and ERC-20 transfer history using QuickNode APIs and SDK. For Web3 apps building NFT/token features or checking wallet data.