Skip to main content
Glama

check_token_allowance

Verify token spending permissions by checking the approved allowance amount between an owner and spender address on supported EVM chains.

Instructions

Check token allowance for a specific owner and spender.

Args: chain_id: Chain ID (1, 10, 56, 100, 137, 8453, 42161) token_address: Address of the token owner_address: Address of the token owner spender_address: Address of the spender (typically Trader contract) Returns: JSON string with allowance information.

Input Schema

NameRequiredDescriptionDefault
chain_idYes
token_addressYes
owner_addressYes
spender_addressYes

Input Schema (JSON Schema)

{ "properties": { "chain_id": { "title": "Chain Id", "type": "string" }, "owner_address": { "title": "Owner Address", "type": "string" }, "spender_address": { "title": "Spender Address", "type": "string" }, "token_address": { "title": "Token Address", "type": "string" } }, "required": [ "chain_id", "token_address", "owner_address", "spender_address" ], "type": "object" }

Implementation Reference

  • The handler function for the check_token_allowance tool. It validates inputs, connects to the chain via Web3, queries the ERC20 token contract for allowance and balance, formats the results into JSON, and returns whether approval is needed.
    @mcp.tool() async def check_token_allowance(ctx: Context, chain_id: str, token_address: str, owner_address: str, spender_address: str) -> str: """Check token allowance for a specific owner and spender. Args: chain_id: Chain ID (1, 10, 56, 100, 137, 8453, 42161) token_address: Address of the token owner_address: Address of the token owner spender_address: Address of the spender (typically Trader contract) Returns: JSON string with allowance 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 chain_id not in paloma_ctx.web3_clients: return f"Error: Web3 client not available for {config.name}" # Validate addresses if not Web3.is_address(token_address): return f"Error: Invalid token address: {token_address}" if not Web3.is_address(owner_address): return f"Error: Invalid owner address: {owner_address}" if not Web3.is_address(spender_address): return f"Error: Invalid spender address: {spender_address}" web3 = paloma_ctx.web3_clients[chain_id] token_contract = web3.eth.contract(address=token_address, abi=ERC20_ABI) # Get allowance and token info allowance = token_contract.functions.allowance(owner_address, spender_address).call() try: token_symbol = token_contract.functions.symbol().call() token_decimals = token_contract.functions.decimals().call() balance = token_contract.functions.balanceOf(owner_address).call() except: token_symbol = "Unknown" token_decimals = 18 balance = 0 allowance_display = float(allowance) / (10 ** token_decimals) balance_display = float(balance) / (10 ** token_decimals) result = { "chain": config.name, "chain_id": config.chain_id, "token": { "address": token_address, "symbol": token_symbol, "decimals": token_decimals }, "owner_address": owner_address, "spender_address": spender_address, "allowance": { "wei": str(allowance), "display": str(allowance_display), "is_unlimited": allowance == MAX_AMOUNT }, "owner_balance": { "wei": str(balance), "display": str(balance_display) }, "needs_approval": allowance == 0, "sufficient_allowance": allowance >= balance if balance > 0 else True } return json.dumps(result, indent=2) except Exception as e: logger.error(f"Error checking token allowance: {e}") return f"Error checking token allowance: {str(e)}"
  • Minimal ERC20 ABI used by the tool, including the crucial 'allowance' function definition required for querying token allowances.
    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" } ]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/VolumeFi/mcpPADEX'

If you have feedback or need assistance with the MCP directory API, please join our Discord server