Source profileQuality 63/100

affaan-m/ECC/docs/zh-CN/skills/evm-token-decimals/SKILL.md

evm-token-decimals

Review evm-token-decimals's use cases, installation, workflow, and original source instructions.

Source repository stars
234,327
Declared platforms
0
Static risk flags
0
Last source update
2026-07-27
Source checked
2026-07-28

Decision brief

What it does—and where it fits

静默的精度不匹配是导致余额或美元价值出现数量级偏差且不抛出错误的最常见原因之一。

Best for

    Not for

    • Tasks that require unconfirmed production actions or broad system permissions.
    • Environments where the pinned source and install steps cannot be inspected.

    Compatibility matrix

    Platform support, with evidence labels

    PlatformStatusEvidenceWhat to check
    CodexNot declaredNo explicit evidencePortability before use
    Claude CodeNot declaredNo explicit evidencePortability before use
    CursorNot declaredNo explicit evidencePortability before use
    Gemini CLINot declaredNo explicit evidencePortability before use
    Open the compatibility checker

    Installation

    Inspect first. Install second.

    The source command is displayed only when detected. A safe inspection prompt is always available so your agent can explain every action before execution.

    Source-detected install commandSource
    npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/evm-token-decimals"
    Safe inspection promptEditorial

    Inspect the Agent Skill "evm-token-decimals" from https://github.com/affaan-m/ECC/blob/4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38/docs/zh-CN/skills/evm-token-decimals/SKILL.md at commit 4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38. List every install step, command, network request, credential, file read/write, external action, and rollback step. Explain whether it fits my task. Do not install or execute anything until I approve.

    Workflow

    What the source asks the agent to do

    1. 01

      适用场景

      在 Python、TypeScript 或 Solidity 中读取 ERC-20 余额 根据链上余额计算法币价值 跨多条 EVM 链比较代币数量 处理跨链桥接资产 构建投资组合追踪器、机器人或聚合器

      在 Python、TypeScript 或 Solidity 中读取 ERC-20 余额根据链上余额计算法币价值跨多条 EVM 链比较代币数量
    2. 02

      工作原理

      切勿假设稳定币在所有链上使用相同的精度。在运行时查询 decimals(),按 (chainid, tokenaddress) 进行缓存,并使用精度安全的数学运算进行价值计算。

      切勿假设稳定币在所有链上使用相同的精度。在运行时查询 decimals(),按 (chainid, tokenaddress) 进行缓存,并使用精度安全的数学运算进行价值计算。
    3. 03

      示例

      不要硬编码 1000000,因为同名代币在其他链上通常有 6 位小数。

      不要硬编码 1000000,因为同名代币在其他链上通常有 6 位小数。记录回退值并保持可见。旧版或非标准代币仍然存在。
    4. 04

      运行时查询精度

      不要硬编码 1000000,因为同名代币在其他链上通常有 6 位小数。

      不要硬编码 1000000,因为同名代币在其他链上通常有 6 位小数。

    Permission review

    Static risk signals and limitations

    No configured static risk pattern was detected

    This is not proof of safety. Runtime behavior, indirect dependencies, and hidden external systems are outside the static scan.

    Evidence record

    Why each signal appears

    EvidenceSourceComputedTestedEditorial
    SignalValueEvidence typeMeaning
    Quality score63/100ComputedDocumentation, specificity, maintenance, and trust rules
    Repository stars234,327SourceRepository attention, not individual Skill quality
    Compatibility0 platformsSourceDeclared in the catalog source record
    Usage guideautomated source guideEditorialGenerated or reviewed according to the visible evidence level

    Pinned source

    Provenance and original SKILL.md

    Repository
    affaan-m/ECC
    Skill path
    docs/zh-CN/skills/evm-token-decimals/SKILL.md
    Commit
    4e973d3eaf92d97f8d2e2d8abb39d8bdc8711b38
    License
    MIT
    Collected
    2026-07-28
    Default branch
    main
    View the original SKILL.md

    EVM 代币精度

    静默的精度不匹配是导致余额或美元价值出现数量级偏差且不抛出错误的最常见原因之一。

    适用场景

    • 在 Python、TypeScript 或 Solidity 中读取 ERC-20 余额
    • 根据链上余额计算法币价值
    • 跨多条 EVM 链比较代币数量
    • 处理跨链桥接资产
    • 构建投资组合追踪器、机器人或聚合器

    工作原理

    切勿假设稳定币在所有链上使用相同的精度。在运行时查询 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() reverted on %s (chain %s), defaulting to 18",
            token_address,
            chain_id,
        )
        decimals = 18
    

    记录回退值并保持可见。旧版或非标准代币仍然存在。

    在 Solidity 中归一化为 18 位 WAD 精度

    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);
    }
    

    使用 ethers 的 TypeScript 示例

    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()
    • 按链加代币地址进行缓存,而非按代币符号
    • 使用 DecimalBigInt 或等效的精确数学运算,避免使用浮点数
    • 在跨链桥接或代币包装变更后重新查询精度
    • 在比较或定价前,始终将内部记账归一化为一致精度

    Alternatives

    Compare before choosing