get_pusd_balance
Check PUSD token balance across multiple blockchain networks including Ethereum, Arbitrum, and Polygon to monitor cross-chain trading positions.
Instructions
Get PUSD token balance on specified chain.
Args:
chain_id: Chain ID (1, 10, 56, 100, 137, 8453, 42161)
Returns:
JSON string with PUSD balance information.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chain_id | Yes |
Input Schema (JSON Schema)
{
"properties": {
"chain_id": {
"title": "Chain Id",
"type": "string"
}
},
"required": [
"chain_id"
],
"type": "object"
}
Implementation Reference
- padex.py:552-598 (handler)The main handler function for the 'get_pusd_balance' tool. It is registered via the @mcp.tool() decorator and implements the logic to query the PUSD ERC20 token balance on the specified EVM chain using Web3.py, returning formatted JSON with balance details.@mcp.tool() async def get_pusd_balance(ctx: Context, chain_id: str) -> str: """Get PUSD token balance on specified chain. Args: chain_id: Chain ID (1, 10, 56, 100, 137, 8453, 42161) Returns: JSON string with PUSD balance information. """ try: paloma_ctx = ctx.request_context.lifespan_context if chain_id not in CHAIN_CONFIGS: return f"Error: Unsupported chain ID {chain_id}" config = CHAIN_CONFIGS[chain_id] if not config.pusd_token: return f"Error: PUSD token address not configured for {config.name}" if chain_id not in paloma_ctx.web3_clients: return f"Error: Web3 client not available for {config.name}" web3 = paloma_ctx.web3_clients[chain_id] pusd_contract = web3.eth.contract( address=config.pusd_token, abi=ERC20_ABI ) balance_wei = pusd_contract.functions.balanceOf(paloma_ctx.address).call() decimals = pusd_contract.functions.decimals().call() balance = balance_wei / (10 ** decimals) balance_info = { "chain": config.name, "chain_id": config.chain_id, "token_address": config.pusd_token, "balance": str(balance), "symbol": "PUSD", "decimals": decimals } return json.dumps(balance_info, indent=2) except Exception as e: logger.error(f"Error getting PUSD balance: {e}") return f"Error getting PUSD balance: {str(e)}"
- padex.py:552-552 (registration)The @mcp.tool() decorator registers the get_pusd_balance function as an MCP tool.@mcp.tool()
- padex.py:197-240 (helper)ERC20_ABI contract ABI used by get_pusd_balance to interact with the PUSD token contract for balanceOf and decimals calls.# ERC-20 ABI (minimal) ERC20_ABI = [ { "constant": True, "inputs": [{"name": "_owner", "type": "address"}], "name": "balanceOf", "outputs": [{"name": "balance", "type": "uint256"}], "type": "function" }, { "constant": True, "inputs": [], "name": "decimals", "outputs": [{"name": "", "type": "uint8"}], "type": "function" }, { "constant": True, "inputs": [], "name": "symbol", "outputs": [{"name": "", "type": "string"}], "type": "function" }, { "constant": False, "inputs": [ {"name": "spender", "type": "address"}, {"name": "amount", "type": "uint256"} ], "name": "approve", "outputs": [{"name": "", "type": "bool"}], "type": "function" }, { "constant": True, "inputs": [ {"name": "owner", "type": "address"}, {"name": "spender", "type": "address"} ], "name": "allowance", "outputs": [{"name": "", "type": "uint256"}], "type": "function" } ]
- padex.py:60-131 (helper)CHAIN_CONFIGS dictionary providing PUSD token addresses and chain configurations used by get_pusd_balance to locate the correct PUSD contract per chain.CHAIN_CONFIGS = { ChainID.ETHEREUM_MAIN: ChainConfig( chain_id=1, name="Ethereum", rpc_url="https://eth.llamarpc.com", pusd_token=os.getenv("PUSD_TOKEN_ETH", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_ETH", ""), etf_connector=os.getenv("ETF_CONNECTOR_ETH", ""), explorer_url="https://etherscan.io", gas_price_gwei=30 ), ChainID.ARBITRUM_MAIN: ChainConfig( chain_id=42161, name="Arbitrum One", rpc_url="https://arb1.arbitrum.io/rpc", pusd_token=os.getenv("PUSD_TOKEN_ARB", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_ARB", ""), etf_connector=os.getenv("ETF_CONNECTOR_ARB", ""), explorer_url="https://arbiscan.io", gas_price_gwei=1 ), ChainID.OPTIMISM_MAIN: ChainConfig( chain_id=10, name="Optimism", rpc_url="https://mainnet.optimism.io", pusd_token=os.getenv("PUSD_TOKEN_OP", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_OP", ""), etf_connector=os.getenv("ETF_CONNECTOR_OP", ""), explorer_url="https://optimistic.etherscan.io", gas_price_gwei=1 ), ChainID.BASE_MAIN: ChainConfig( chain_id=8453, name="Base", rpc_url="https://mainnet.base.org", pusd_token=os.getenv("PUSD_TOKEN_BASE", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_BASE", ""), etf_connector=os.getenv("ETF_CONNECTOR_BASE", ""), explorer_url="https://basescan.org", gas_price_gwei=1 ), ChainID.BSC_MAIN: ChainConfig( chain_id=56, name="BNB Smart Chain", rpc_url="https://bsc-dataseed1.binance.org", pusd_token=os.getenv("PUSD_TOKEN_BSC", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_BSC", ""), etf_connector=os.getenv("ETF_CONNECTOR_BSC", ""), explorer_url="https://bscscan.com", gas_price_gwei=5 ), ChainID.POLYGON_MAIN: ChainConfig( chain_id=137, name="Polygon", rpc_url="https://polygon-rpc.com", pusd_token=os.getenv("PUSD_TOKEN_MATIC", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_MATIC", ""), etf_connector=os.getenv("ETF_CONNECTOR_MATIC", ""), explorer_url="https://polygonscan.com", gas_price_gwei=30 ), ChainID.GNOSIS_MAIN: ChainConfig( chain_id=100, name="Gnosis Chain", rpc_url="https://rpc.gnosischain.com", pusd_token=os.getenv("PUSD_TOKEN_GNOSIS", ""), pusd_connector=os.getenv("PUSD_CONNECTOR_GNOSIS", ""), etf_connector=os.getenv("ETF_CONNECTOR_GNOSIS", ""), explorer_url="https://gnosisscan.io", gas_price_gwei=2 ) }