概览
Bank 预编译提供了对原生 Cosmos SDK 代币的 ERC20 风格访问能力,使智能合约能够通过标准化接口查询余额和代币总供应量。它充当 Cosmos SDKx/bank 模块的 Solidity 封装。
地址:0x0000000000000000000000000000000000000804
相关模块:x/bank
Gas 成本
- balances:2,851 + (2,851 × (n-1)),其中 n = 返回的代币数量
- totalSupply:2,477 + (2,477 × (n-1)),其中 n = 返回的代币数量
- supplyOf:2,477
主要方法
balances
签名:balances(address account) → Balance[] memory
说明:查询指定账户地址的所有原生代币余额,并返回由 Balance(ERC-20 合约地址和数量)结构组成的数组。
account(address):要查询余额的账户地址
contractAddress 和 amount 字段的 Balance 结构体数组
totalSupply
签名:totalSupply() → Balance[] memory
说明:查询系统中所有原生代币的总供应量。返回系统中每个已注册代币的完整供应信息。
supplyOf
签名:supplyOf(address erc20Address) → uint256
说明:通过提供特定代币的 ERC20 合约地址来查询其总供应量。当你只需要单个代币的供应信息时,这种方式效率更高。
erc20Address(address):该代币的 ERC20 合约地址
数据结构 ->
Balance
Balance 结构体表示一个代币余额及其关联的 ERC20 合约地址:contractAddress(address):表示该原生代币的 ERC20 合约地址amount(uint256):以最小计量单位表示的代币数量
完整接口与 ABI
Bank Solidity 接口
Bank ABI
实现细节
代币解析
预编译会通过x/erc20 模块的代币对注册表,将原生 Cosmos SDK denom 解析为对应的 ERC-20 合约地址。只有已注册代币对的代币才会在查询结果中返回。
小数精度
所有返回的数量都会保留x/bank 模块中存储的原始小数精度。预编译不会执行任何小数转换。
Gas 计量
该预编译通过以下方式实现高效的 Gas 计量:- 对首个结果收取基础 Gas
- 在批量查询中,对每个额外结果逐步增加收费
- 在返回结果前消耗 Gas,以防止 DoS 攻击向量
错误处理
supplyOf中的无效代币地址会返回 0,而不是回退- 对没有余额的账户进行查询时,会返回空数组
- 所有方法都是只读的,无法修改状态
Overview
The Bank precompile provides ERC20-style access to native Cosmos SDK tokens, enabling smart contracts to query balances and token supplies through standardized interfaces. It serves as a Solidity wrapper around the Cosmos SDKx/bank module.
Address: 0x0000000000000000000000000000000000000804
Related Module: x/bank
Gas Costs
- balances: 2,851 + (2,851 × (n-1)) where n = number of tokens returned
- totalSupply: 2,477 + (2,477 × (n-1)) where n = number of tokens returned
- supplyOf: 2,477
Primary Methods
balances
Signature:balances(address account) → Balance[] memory
Description: Queries all native token balances for a specific account address and returns an array of Balance (ERC-20 contract address & amount) structures.
account(address): The account address to query balances for
totalSupply
Signature:totalSupply() → Balance[] memory
Description: Queries the total supply of all native tokens in the system. Returns comprehensive supply information for every token registered in the system.
supplyOf
Signature:supplyOf(address erc20Address) → uint256
Description: Queries the total supply of a specific token by providing its ERC20 contract address. More efficient when you need supply information for a single token.
erc20Address(address): The ERC20 contract address of the token
Data Structures ->
Balance
The Balance struct represents a token balance with its associated ERC20 contract address:contractAddress(address): The ERC20 contract address representing the native tokenamount(uint256): Token amount in the smallest denomination
Full Interface & ABI
Bank Solidity Interface
Bank ABI
Implementation Details
Token Resolution
The precompile resolves native Cosmos SDK denominations to their corresponding ERC-20 contract addresses through thex/erc20 module’s token pair registry. Only tokens with registered token pairs are returned in query results.
Decimal Precision
All amounts returned preserve the original decimal precision stored in thex/bank module. No decimal conversion is performed by the precompile.
Gas Metering
The precompile implements efficient gas metering by:- Charging base gas for the first result
- Incrementally charging for each additional result in batch queries
- Consuming gas before returning results to prevent DoS vectors
Error Handling
- Invalid token addresses in
supplyOfreturn 0 rather than reverting - Queries for accounts with no balances return empty arrays
- All methods are read-only and cannot modify state