x/erc20
module from cosmos/evm enables bidirectional conversion between Cosmos SDK coins and ERC20 tokens within the EVM runtime.
For conceptual understanding of Single Token Representation v2, see Single Token Representation.
Parameters
The module parameters control token conversion and registration (source):Parameter | Type | Default | Description |
---|---|---|---|
enable_erc20 | bool | true | Enable token conversions globally |
permissionless_registration | bool | false | Allow anyone to register ERC20 tokens |
Parameter Details
State
The module maintains token pair mappings and allowances (source):Object | Key | Value | Description |
---|---|---|---|
TokenPair | 0x01 + ID | TokenPair | Token pair configuration |
TokenPairByERC20 | 0x02 + address | ID | Lookup by ERC20 address |
TokenPairByDenom | 0x03 + denom | ID | Lookup by denomination |
Allowance | 0x04 + hash | Allowance | ERC20 allowances |
NativePrecompiles | 0x05 + address | bool | Native precompile registry |
DynamicPrecompiles | 0x06 + address | bool | Dynamic precompile registry |
Token Pair Structure
Token Pair Registration
Registration Methods
-
Automatic Registration (IBC Tokens)
- IBC tokens (denoms starting with “ibc/”) are automatically registered on first receipt
- No governance proposal or user action required
- Creates ERC20 precompile at deterministic address
-
Permissionless Registration (ERC20 Contracts)
- When
permissionless_registration
parameter istrue
- Any user can register existing ERC20 contracts via
MsgRegisterERC20
- Useful for integrating existing ERC20 tokens
- When
-
Governance Registration
- Always available regardless of parameter settings
- Can register any ERC20 contract or create new token pairs
- Required when
permissionless_registration
isfalse
Messages
MsgRegisterERC20
Register existing ERC20 contracts for conversion (source):permissionless_registration
enabled OR sender is governance authority- Valid ERC20 contract at address
- Contract not already registered
- Contract implements standard ERC20 interface
MsgConvertCoin
Convert Cosmos coins to ERC20 tokens:- Token pair exists and enabled
- Sender has sufficient balance
- Valid receiver address
MsgConvertERC20
Convert ERC20 tokens to Cosmos coins:- Token pair exists and enabled
- Sender has sufficient ERC20 balance
- Valid receiver address
MsgToggleConversion
Enable/disable conversions for a token pair (governance only):MsgUpdateParams
Update module parameters (governance only):Conversion Flows
Native Coin → ERC20
Steps:- Validate token pair enabled
- Transfer coins to module account
- Mint equivalent ERC20 to receiver
- Emit conversion event
ERC20 → Native Coin
Steps:- Validate token pair enabled
- For module-owned: burn ERC20
- For external: transfer to module
- Release native coins from escrow
- Emit conversion event
Precompile System
Native Precompiles
Automatically created for Cosmos coins at deterministic addresses:Dynamic Precompiles (WERC20)
Optional wrapped interface for registered tokens:IBC Integration
IBC Middleware v1
Standard IBC transfer integration (source):- IBC tokens (denoms starting with “ibc/”) are automatically registered on first receipt
- Factory tokens (denoms starting with “factory/”) are skipped
- Native chain tokens require explicit registration
IBC Middleware v2
Enhanced IBC v2 support (source):- New packet format support
- Improved error handling
- Multi-hop awareness
- Backward compatibility
Events
Registration Events
Event | Attributes | When Emitted |
---|---|---|
register_erc20 | erc20_address , cosmos_coin | Token pair registered |
toggle_token_conversion | erc20_address , cosmos_coin | Conversion toggled |
Conversion Events
Event | Attributes | When Emitted |
---|---|---|
convert_coin | sender , receiver , amount , cosmos_coin , erc20_token | Coin → ERC20 |
convert_erc20 | sender , receiver , amount , cosmos_coin , erc20_token | ERC20 → Coin |
IBC Events
Event | Attributes | When Emitted |
---|---|---|
ibc_transfer_conversion | sender , receiver , denom , amount | Auto-conversion during IBC |
Queries
gRPC
CLI
Integration Examples
DeFi Protocol Integration
Automatic IBC Conversion
Manual Conversion Flow
Best Practices
Chain Integration
-
Token Registration Review
- Audit contracts before registration
- Verify standard compliance
- Check for malicious behavior
-
Precompile Configuration
- Enable precompiles for frequently used tokens
- Monitor gas consumption
- Set appropriate gas costs
-
IBC Setup
- Configure middleware stack correctly
- Test auto-conversion flows
- Monitor conversion events
Security Considerations
-
Contract Validation
-
Event Monitoring
- Track conversion events
- Monitor for unusual patterns
- Alert on large conversions
-
Emergency Response
- Disable conversions via governance
- Toggle specific token pairs
- Have incident response plan
Troubleshooting
Common Issues
Issue | Cause | Solution |
---|---|---|
”token pair not found” | Token not registered | Register via governance |
”token pair disabled” | Conversions toggled off | Enable via governance |
”insufficient balance” | Low balance for conversion | Check balance in correct format |
”invalid recipient” | Wrong address format | Use hex for EVM, bech32 for Cosmos |
”module disabled” | enable_erc20 = false | Enable via governance |
Debug Commands
References
Source Code
- ERC20 Module - Module implementation
- Precompiles - ERC20 precompile contracts
- IBC Middleware - IBC integration
- Proto Definitions - API specifications
Related Documentation
- Single Token Representation - Conceptual overview
- VM Module - EVM execution environment
- Bank Module - Native token management