预编译(precompile)是在固定地址部署的智能合约接口,其实现以原生 Go 代码运行,而不是 EVM 字节码。在标准以太坊中,预编译通常是无状态的,用于处理签名验证、哈希等操作。在 Cosmos EVM 中,它们也可以是有状态的,即可读取和写入 EVM 之外的 Cosmos SDK 模块状态。
这正是它们强大的原因:智能合约可以调用 staking 预编译来委托代币,调用 governance 预编译来提交提案,或调用 IBC 预编译来发送跨链代币,而且全部通过标准 Solidity 接口完成,并且都可在一笔交易中执行。
预编译充当了通往 Cosmos SDK 的窗口:当调用预编译时,执行会从 EVM 路由到对应的 Cosmos SDK 模块,并将结果返回给 EVM 以继续执行。Gas 会在这两个环境中统一计量。
所有预编译合约都应使用以太坊标准的 18 位小数精度。尽管典型的 Cosmos 链通常使用 6 位小数精度,但 EVM 链应使用 18 位小数精度。
在与预编译交互之前,务必先确认你的链所使用的小数精度。
内置预编译
| 预编译 | 地址 | 用途 | 参考 |
|---|
| P256 | 0x0000000000000000000000000000000000000100 | P-256 椭圆曲线签名验证 | 详情 |
| Bech32 | 0x0000000000000000000000000000000000000400 | 以太坊十六进制地址与 Cosmos bech32 地址格式转换 | 详情 |
| Staking | 0x0000000000000000000000000000000000000800 | 验证者操作、委托与质押奖励 | 详情 |
| Distribution | 0x0000000000000000000000000000000000000801 | 质押奖励与社区资金池管理 | 详情 |
| ICS20 | 0x0000000000000000000000000000000000000802 | 通过 IBC 进行跨链代币转移 | 详情 |
| Bank | 0x0000000000000000000000000000000000000804 | 以 ERC20 风格访问原生 Cosmos SDK 代币 | 详情 |
| Governance | 0x0000000000000000000000000000000000000805 | 链上治理提案与投票 | 详情 |
| Slashing | 0x0000000000000000000000000000000000000806 | 验证者惩罚与监禁管理 | 详情 |
| ICS02 | 0x0000000000000000000000000000000000000807 | IBC 轻客户端查询 | 源码 |
| ERC20 | 每个代币动态分配 | 原生 Cosmos 代币的标准 ERC20 接口 | 详情 |
| WERC20 | 每个代币动态分配 | 原生代币的包装功能 | 详情 |
链构建者可以控制哪些预编译处于启用状态,也可以添加自定义预编译。详情参见预编译配置指南。
Precompiles are smart contract interfaces deployed at fixed addresses where the implementation runs as native Go code rather than EVM bytecode. In standard Ethereum, precompiles are stateless and handle things like signature verification and hashing. In Cosmos EVM, they can also be stateful — reading from and writing to Cosmos SDK module state outside the EVM.
This is what makes them powerful: a smart contract can call the staking precompile to delegate tokens, the governance precompile to submit a proposal, or the IBC precompile to send tokens cross-chain — all using a standard Solidity interface, all within a single transaction.
Precompiles act as a window into the Cosmos SDK: when a precompile is invoked, execution routes from the EVM through the corresponding Cosmos SDK module and returns the result to the EVM for continued execution. Gas is metered across both environments.
All precompile contracts should use Ethereum’s standard 18 decimals.Although typical Cosmos chains use 6 decimals, EVM chains should use 18 decimals.
Always check your chain’s decimal precision before interacting with a precompile.
Built-in Precompiles
| Precompile | Address | Purpose | Reference |
|---|
| P256 | 0x0000000000000000000000000000000000000100 | P-256 elliptic curve signature verification | Details |
| Bech32 | 0x0000000000000000000000000000000000000400 | Address format conversion between Ethereum hex and Cosmos bech32 | Details |
| Staking | 0x0000000000000000000000000000000000000800 | Validator operations, delegation, and staking rewards | Details |
| Distribution | 0x0000000000000000000000000000000000000801 | Staking rewards and community pool management | Details |
| ICS20 | 0x0000000000000000000000000000000000000802 | Cross-chain token transfers via IBC | Details |
| Bank | 0x0000000000000000000000000000000000000804 | ERC20-style access to native Cosmos SDK tokens | Details |
| Governance | 0x0000000000000000000000000000000000000805 | On-chain governance proposals and voting | Details |
| Slashing | 0x0000000000000000000000000000000000000806 | Validator slashing and jail management | Details |
| ICS02 | 0x0000000000000000000000000000000000000807 | IBC light client queries | Source |
| ERC20 | Dynamic per token | Standard ERC20 interface for native Cosmos tokens | Details |
| WERC20 | Dynamic per token | Wrapped native token functionality | Details |
Chain builders can control which precompiles are active and add their own. See the Precompile Configuration guide for details.