affaan-m/ECC

evm-token-decimals

防止跨EVM链的静默小数不匹配错误。涵盖运行时小数查找、链感知缓存、桥接代币精度漂移以及面向机器人、仪表盘和DeFi工具的安全归一化。

63Collecting
See how to use itView GitHub source
npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/evm-token-decimals"
Automated source guide

Source checked Jul 28, 2026·Refresh due Oct 26, 2026

Reorganized from the pinned upstream SKILL.md

Turn evm-token-decimals's source instructions into a guide you can follow

According to the pinned SKILL.md from affaan-m/ECC: 静默的精度不匹配是导致余额或美元价值出现数量级偏差且不抛出错误的最常见原因之一。

npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/evm-token-decimals"
Check the pinned source

Best fit

  • 防止跨EVM链的静默小数不匹配错误。涵盖运行时小数查找、链感知缓存、桥接代币精度漂移以及面向机器人、仪表盘和DeFi工具的安全归一化。

Bring this context

  • A concrete task that matches the documented purpose of evm-token-decimals.
  • The files, examples, or context the task depends on.
  • Your constraints, target environment, and definition of done.

Expected outputs

  • A result that follows the pinned evm-token-decimals instructions.
  • A concise record of assumptions, inputs used, and unresolved questions.
  • A final check against the source workflow and relevant permission signals.

Key source sections

Read evm-token-decimals through these 5 source sections

Sections are extracted automatically from the pinned SKILL.md and link back to the source.

01

适用场景

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

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

工作原理

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

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

示例

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

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

运行时查询精度

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

SKILL.md · 运行时查询精度
不要硬编码 1000000,因为同名代币在其他链上通常有 6 位小数。
05

按链和代币缓存

Review the “按链和代币缓存” section in the pinned source before continuing.

SKILL.md · 按链和代币缓存
Review and apply the “按链和代币缓存” source section.

SkillSignal prompt templates

Provide the task, context, and acceptance criteria

These prompts were written by SkillSignal from the source structure; they are not upstream text.

Task-start prompt

Confirm source fit, inputs, and outputs before acting.

Use evm-token-decimals to help me with: [specific task]. Context: [files, data, or background]. Constraints: [environment, scope, and prohibited actions]. Before acting, check the pinned SKILL.md and explain which sections apply, what inputs are still missing, and what you will deliver.

Source-guided execution

Make the Agent explicitly follow the key extracted sections.

Apply the pinned evm-token-decimals source to [task]. Pay particular attention to these source sections: “适用场景”, “工作原理”, “示例”, “运行时查询精度”, “按链和代币缓存”. Preserve the important decision at each step. Mark facts not covered by the source as “needs confirmation” instead of inventing them. Then verify the result against my acceptance criteria: [criteria].

Result-review prompt

Check omissions, permissions, and source drift before delivery.

Review the current evm-token-decimals result: (1) does it satisfy the original task; (2) were any applicable steps or limits in the pinned SKILL.md missed; (3) did it perform any unauthorized file, command, network, or data action; and (4) which conclusions remain unverified? List issues first, then fix only what the source or user authorization supports.

Output checklist

Verify each item before delivery

The task matches the purpose documented in the SKILL.md.

The source section “适用场景” has been checked.

The source section “工作原理” has been checked.

The source section “示例” has been checked.

The source section “运行时查询精度” has been checked.

Inputs, constraints, and acceptance criteria are explicit.

Unverified facts, compatibility, and outcome claims are clearly marked.

Any file, command, network, or data action has been reviewed.

Choose a different workflow

When another Skill is the better fit

FAQ

What does evm-token-decimals do?

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

How do I start using evm-token-decimals?

The catalog detected this source-specific install command: npx skills add https://github.com/affaan-m/ECC --skill "docs/zh-CN/skills/evm-token-decimals". Inspect the command and pinned source before running it.

Which Agent platforms does it declare?

No dedicated Agent platform is declared in the pinned source record.

Repository stars
234,327
Repository forks
35,711
Quality
63/100
Source repository last pushed

Quality breakdown

Based on traceable docs and repository signals; stars are not treated as quality.

63/100
Documentation20/30
Specificity11/25
Maintenance20/20
Trust signals12/25

Compare before choosing

Related Agent Skills and source variants

These links are selected from shared tasks, functions, stacks, platforms, and same-name variants. Compare the source owner, documentation, permissions, and maintenance signals.

evm-token-decimals by affaan-m

Prevent silent decimal mismatch bugs across EVM chains. Covers runtime decimal lookup, chain-aware caching, bridged-token precision drift, and safe normalization for bots, dashboards, and DeFi tools.

evm-token-decimals by affaan-m

EVMチェーン全体でサイレントな小数点不一致バグを防ぐ。ランタイムでの小数点照会、チェーン対応キャッシング、ブリッジドトークンの精度ドリフト、ボット・ダッシュボード・DeFiツール向けの安全な正規化をカバーします。

ab-testing by coreyhaines31

When the user wants to plan, design, or implement an A/B test or experiment, or build a growth experimentation program. Also use when the user mentions "A/B test," "split test," "experiment," "test this change," "variant copy," "multivariate test," "hypothesis," "should I test this," "which version is better," "test two versions," "statistical significance," "how long should I run this test," "growth experiments," "experiment velocity," "experiment backlog," "ICE score," "experimentation program

churn-prevention by coreyhaines31

When the user wants to reduce churn, build cancellation flows, set up save offers, recover failed payments, or implement retention strategies. Also use when the user mentions 'churn,' 'cancel flow,' 'offboarding,' 'save offer,' 'dunning,' 'failed payment recovery,' 'win-back,' 'retention,' 'exit survey,' 'pause subscription,' 'involuntary churn,' 'people keep canceling,' 'churn rate is too high,' 'how do I keep users,' or 'customers are leaving.' Use this whenever someone is losing subscribers o

design-intelligence by event4u-app

Grounded design brief from the adopted corpus — style, WCAG-checked color tokens, typography, layout pattern, anti-patterns. Use on ui-design-brief or any which-style/palette/font/chart decision.

View original Skill.mdThis page is parsed directly from the repository SKILL.md without editorial rewriting. Collected: Jul 28, 2026 · about 1 min

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 或等效的精确数学运算,避免使用浮点数
  • 在跨链桥接或代币包装变更后重新查询精度
  • 在比较或定价前,始终将内部记账归一化为一致精度
Source repo
affaan-m/ECC
Skill path
docs/zh-CN/skills/evm-token-decimals/SKILL.md
Commit SHA
4e973d3eaf92
Repository license
MIT
Data collected