概述
WERC20 预编译通过 Cosmos EVM 的 单一代币表示 架构,为原生 Cosmos 代币提供标准 ERC20 接口。 不同于传统封装代币在功能上属于两个彼此独立、各自具有唯一属性和行为的代币,Cosmos EVM 的 WERC20 逻辑让智能合约能够通过熟悉的 ERC20 方法直接访问原生 bank 模块余额。 关键概念:TEST 和 WTEST 并不是两个独立的代币,它们是存储在 bank 模块中的同一个代币的两种不同接口。 原生 Cosmos 代币(包括 TEST 和所有 IBC 代币)始终同时以封装和未封装状态存在,这使开发者可以选择最适合其用例的交互方式:- 通过 Cosmos bank send 正常使用(未封装状态)
- 像在 EVM 上正常使用 ether 或
wei一样使用它(原生 value 转账) - 使用下方合约地址将其作为 ERC20 WTEST 使用(封装状态)
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
预编译类型:动态(每个封装代币都有唯一地址)
相关模块:x/bank(通过 ERC20 模块集成)
Gas 成本
Gas 成本为近似值,可能会因代币复杂度和链配置而有所不同。
| 方法 | Gas 成本 |
|---|---|
name() | ~3,000 gas |
symbol() | ~3,000 gas |
decimals() | ~2,000 gas |
totalSupply() | ~2,500 gas |
balanceOf(address) | ~2,900 gas |
allowance(address,address) | ~3,000 gas |
transfer(address,uint256) | ~35,000 gas |
transferFrom(address,address,uint256) | ~40,000 gas |
approve(address,uint256) | ~30,000 gas |
deposit() | ~23,000 gas(无操作) |
withdraw(uint256) | ~9,000 gas(无操作) |
如需全面了解单一代币表示的工作方式,以及它相较传统封装方式的优势,请参阅单一代币表示文档。
技术实现
架构深入解析
ERC20 模块创建了一种统一的代币表示,用于在原生 Cosmos 代币与 ERC20 接口之间建立桥接:存入/提取实现细节
由于 TEST 和 WTEST 为同一个 bank 模块代币提供了不同接口,因此会保留 deposit/withdraw 函数以兼容 WETH 接口:理解 Deposit/Withdraw 模式不同于传统 WETH 实现中由合约持有封装代币:
- 传统 WETH:合约接收 ETH,并铸造由其持有的 WETH 代币
- WERC20:合约从不持有代币,所有余额始终保留在 bank 模块中
- 结果:预编译合约地址本身没有余额;代币始终由用户持有
deposit() 和 withdraw() 都是无操作函数,因为这里没有独立的封装代币状态需要管理。真实示例
方法
标准 ERC20 接口
所有标准 ERC20 方法均可用,并且都作用于底层 bank 余额:balanceOf
返回指定账户的原生代币余额(与 bank 模块余额相同)。
transfer
使用 bank 模块转移代币(与原生 Cosmos 转账完全相同)。
totalSupply
返回来自 bank 模块的总供应量。
approve / allowance / transferFrom
用于委托转账的标准 ERC20 授权机制。
name / symbol / decimals
代币元数据(例如 “Wrapped Test”、“WTEST”、18)。
WETH 兼容性方法
这些方法用于兼容 WETH 接口:deposit
WETH 兼容函数 - 处理可支付存款以实现接口兼容性。
此函数会接收
msg.value,并立即通过 bank 模块将代币发送回调用者,然后触发一个 Deposit 事件。由于 WTEST 和 TEST 底层使用的是同一个 bank 模块代币,因此不会发生实际的包装操作,你的余额只是同时可以通过原生接口和 ERC20 接口访问。withdraw
空操作函数 - 为兼容 WETH 合约接口而提供。
此函数只会触发一个 Withdrawal 事件,不会执行任何实际的代币转移。由于 WTEST 和 TEST 底层使用的是同一个 bank 模块代币,因此你的原生代币余额始终可直接访问,无需任何解包过程。
用法示例
DeFi 集成示例
跨接口余额验证
使用 IBC 代币
Solidity 接口与 ABI
WERC20 Solidity 接口
WERC20 ABI
Overview
The WERC20 precompile provides a standard ERC20 interface to native Cosmos tokens through Cosmos EVM’s Single Token Representation architecture. Unlike traditional wrapped tokens that are functionally two separate tokens with unique individual properties and behaviors, Cosmos EVM’s WERC20 logic gives smart contracts direct access to native bank module balances through familiar ERC20 methods. Key Concept: TEST and WTEST are not separate tokens—they are two different interfaces to the same token stored in the bank module. Native Cosmos tokens (including TEST and all IBC tokens) exist in both wrapped and unwrapped states at all times, allowing developers to choose the interaction method that best fits their use case:- Use it normally through Cosmos bank send (unwrapped state)
- Use it like you would normally use ether or ‘wei’ on the EVM (native value transfers)
- Use it as ERC20 WTEST with the contract address below (wrapped state)
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
Precompile Type: Dynamic (unique address per wrapped token)
Related Module: x/bank (via ERC20 module integration)
Gas Costs
Gas costs are approximated and may vary based on token complexity and chain settings.
| Method | Gas Cost |
|---|---|
name() | ~3,000 gas |
symbol() | ~3,000 gas |
decimals() | ~2,000 gas |
totalSupply() | ~2,500 gas |
balanceOf(address) | ~2,900 gas |
allowance(address,address) | ~3,000 gas |
transfer(address,uint256) | ~35,000 gas |
transferFrom(address,address,uint256) | ~40,000 gas |
approve(address,uint256) | ~30,000 gas |
deposit() | ~23,000 gas (no-op) |
withdraw(uint256) | ~9,000 gas (no-op) |
For a comprehensive understanding of how single token representation works and its benefits over traditional wrapping, see the Single Token Representation documentation.
Technical Implementation
Architecture Deep Dive
The ERC20 module creates a unified token representation that bridges native Cosmos tokens with ERC20 interfaces:Deposit/Withdraw Implementation Details
Since TEST and WTEST provide different interfaces to the same bank module token, deposit/withdraw functions exist for WETH interface compatibility:Understanding the Deposit/Withdraw PatternUnlike traditional WETH implementations where the contract holds wrapped tokens:
- Traditional WETH: Contract receives ETH and mints WETH tokens that it holds
- WERC20: Contract never holds tokens - all balances remain in the bank module
- Result: The precompile contract address has no balance; tokens stay with users
deposit() and withdraw() are no-ops - there’s no separate wrapped token state to manage.Real-World Example
Methods
Standard ERC20 Interface
All standard ERC20 methods are available and operate on the underlying bank balance:balanceOf
Returns the native token balance for a specific account (same as bank module balance).
transfer
Transfers tokens using the bank module (identical to native Cosmos transfer).
totalSupply
Returns the total supply from the bank module.
approve / allowance / transferFrom
Standard ERC20 approval mechanisms for delegated transfers.
name / symbol / decimals
Token metadata (e.g., “Wrapped Test”, “WTEST”, 18).
WETH Compatibility Methods
These methods exist for WETH interface compatibility:deposit
WETH compatibility function - Handles payable deposits for interface compatibility.
This function receives msg.value and immediately sends the coins back to the caller via the bank module, then emits a Deposit event. Since WTEST and TEST are the same underlying bank module token, no actual wrapping occurs - your balance is simply accessible through both native and ERC20 interfaces.
withdraw
No-op function - Included for interface compatibility with WETH contracts.
This function only emits a Withdrawal event but performs no actual token movement. Since WTEST and TEST are the same underlying bank module token, your native token balance is always directly accessible without any unwrapping process.
Usage Examples
DeFi Integration Example
Cross-Interface Balance Verification
Working with IBC Tokens
Solidity Interface & ABI
WERC20 Solidity Interface
WERC20 ABI