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 SDK x/bank module.
Description: Queries all native token balances for a specific account address and returns an array of Balance (ERC-20 contract address & amount) structures.
Copy
Ask AI
import { ethers } from "ethers";// Connect to the networkconst provider = new ethers.JsonRpcProvider("<RPC_URL>");// Precompile address and ABIconst precompileAddress = "0x0000000000000000000000000000000000000804";const precompileAbi = [ "function balances(address account) view returns (tuple(address contractAddress, uint256 amount)[])"];// Create a contract instanceconst contract = new ethers.Contract(precompileAddress, precompileAbi, provider);// Address to queryconst accountAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; // Placeholderasync function getBalances() { try { const balances = await contract.balances(accountAddress); console.log("Balances:", balances); } catch (error) { console.error("Error fetching balances:", error); }}getBalances();
Parameters:
account (address): The account address to query balances for
Returns: Array of Balance structures with contractAddress and amount fields
Description: Queries the total supply of all native tokens in the system. Returns comprehensive supply information for every token registered in the system.
Copy
Ask AI
import { ethers } from "ethers";// Connect to the networkconst provider = new ethers.JsonRpcProvider("<RPC_URL>");// Precompile address and ABIconst precompileAddress = "0x0000000000000000000000000000000000000804";const precompileAbi = [ "function totalSupply() view returns (tuple(address contractAddress, uint256 amount)[])"];// Create a contract instanceconst contract = new ethers.Contract(precompileAddress, precompileAbi, provider);async function getTotalSupply() { try { const supply = await contract.totalSupply(); console.log("Total Supply:", supply); } catch (error) { console.error("Error fetching total supply:", error); }}getTotalSupply();
Parameters: None
Returns: Array of Balance structures showing total supply for each token
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.
Copy
Ask AI
import { ethers } from "ethers";// Connect to the networkconst provider = new ethers.JsonRpcProvider("<RPC_URL>");// Precompile address and ABIconst precompileAddress = "0x0000000000000000000000000000000000000804";const precompileAbi = [ "function supplyOf(address erc20Address) view returns (uint256)"];// Create a contract instanceconst contract = new ethers.Contract(precompileAddress, precompileAbi, provider);// ERC20 address to queryconst erc20Address = "0xdAC17F958D2ee523a2206206994597C13D831ec7"; // Placeholder for a token contractasync function getSupplyOf() { try { const supply = await contract.supplyOf(erc20Address); console.log("Supply of", erc20Address, ":", supply.toString()); } catch (error) { console.error("Error fetching supply:", error); }}getSupplyOf();
Parameters:
erc20Address (address): The ERC20 contract address of the token
Returns: Total supply of the specified token as uint256
// SPDX-License-Identifier: LGPL-3.0-onlypragma solidity >=0.8.18;/// @dev The IBank contract's address.address constant IBANK_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000804;/// @dev The IBank contract's instance.IBank constant IBANK_CONTRACT = IBank(IBANK_PRECOMPILE_ADDRESS);/// @dev Balance specifies the ERC20 contract address and the amount of tokens.struct Balance { /// contractAddress defines the ERC20 contract address. address contractAddress; /// amount of tokens uint256 amount;}/** * @author Evmos Team * @title Bank Interface * @dev Interface for querying balances and supply from the Bank module. */interface IBank { /// @dev balances defines a method for retrieving all the native token balances /// for a given account. /// @param account the address of the account to query balances for. /// @return balances the array of native token balances. function balances( address account ) external view returns (Balance[] memory balances); /// @dev totalSupply defines a method for retrieving the total supply of all /// native tokens. /// @return totalSupply the supply as an array of native token balances function totalSupply() external view returns (Balance[] memory totalSupply); /// @dev supplyOf defines a method for retrieving the total supply of a particular native coin. /// @return totalSupply the supply as a uint256 function supplyOf( address erc20Address ) external view returns (uint256 totalSupply);}