Skip to main content

A Python library for managing and organizing AI language model chains

Project description

Chain Index

Chain Index is a Python package that provides easy access to information about various blockchain networks. It allows developers to retrieve details such as native currencies, RPC URLs, common tokens, and more for different chains.

Features

  • Retrieve chain information by chain ID or name
  • Access details like native currency, RPC URLs, and block explorers
  • Get token information including contract addresses and decimals
  • Access commonly used blockchain constants (event topics, function signatures)
  • Handle multiple chain identifiers (ID, name, alias)
  • Robust error handling for non-existent chains

Installation

You can install Chain Index using pip:

pip install chain-index

Usage

Chain Information

from chain_index import get_chain_info, ChainNotFoundError

try:
    # Get chain info by ID
    ethereum = get_chain_info(1)
    print(f"Ethereum native currency: {ethereum.nativeCurrency.symbol}")

    # Get chain info by name
    polygon = get_chain_info("Polygon Mainnet")
    print(f"Polygon chain ID: {polygon.chainId}")

    # Error handling
    non_existent = get_chain_info(999999)
except ChainNotFoundError as e:
    print(f"Error: {e}")

Token Information

from chain_index import get_token_info, get_chain_tokens, get_all_chain_tokens, TokenNotFoundError

# Get specific token information
usdt = get_token_info(1, "USDT")
print(f"USDT contract on Ethereum: {usdt.contract}")
print(f"USDT decimals: {usdt.decimals}")

# Get all common tokens on a chain
ethereum_tokens = get_chain_tokens(1)
for symbol, token in ethereum_tokens.items():
    print(f"{symbol}: {token.contract}")

# Get all tokens including native and wrapped tokens
all_tokens = get_all_chain_tokens(1)
print(f"Native token: {all_tokens.native_token.symbol}")
print(f"Wrapped native token: {all_tokens.wrapped_native.symbol}")

Blockchain Constants

The package provides organized blockchain constants in two formats:

Method 1: Direct access

from chain_index import constants

# Direct access to constants
print(f"Transfer Topic: {constants.TRANSFER_EVENT_TOPIC}")
print(f"Null Address: {constants.ETH_NULL_ADDRESS}")
print(f"ETH Block Time: {constants.ETHEREUM_AVERAGE_BLOCK_TIME}")

# Use in web3.py
logs = web3.eth.get_logs({
    'fromBlock': 'latest',
    'toBlock': 'latest',
    'topics': [constants.TRANSFER_EVENT_TOPIC]
})

Method 2: Categorized access (recommended)

from chain_index import constants

# Access via categories (more organized)
print(f"Transfer Topic: {constants.EventTopics.TRANSFER}")
print(f"Approval Topic: {constants.EventTopics.APPROVAL}")

# Nested categories for better organization
print(f"ERC20 Transfer: {constants.EventTopics.ERC20.TRANSFER}")
print(f"Uniswap V2 Swap: {constants.EventTopics.Uniswap.V2_SWAP}")

# Function signatures
print(f"Transfer: {constants.FunctionSignatures.TRANSFER}")

# Addresses
print(f"Null Address: {constants.Addresses.NULL_ADDRESS}")
print(f"Uniswap V2 Factory: {constants.Addresses.Uniswap.V2_FACTORY}")

# Wrapped tokens across different chains
print(f"WETH on Ethereum: {constants.WrappedToken.ETHEREUM}")
print(f"WMATIC on Polygon: {constants.WrappedToken.POLYGON}")
print(f"WBNB on BSC: {constants.WrappedToken.BSC}")

# Gas limits
print(f"ERC20 Transfer Gas: {constants.GasLimits.ERC20_TRANSFER}")

# Block times
print(f"ETH Block Time: {constants.BlockTime.ETHEREUM}")
print(f"Polygon Block Time: {constants.BlockTime.POLYGON}")

API Reference

Chain Information

  • get_chain_info(chain_identifier: Union[int, str]) -> ChainInfo
    • Retrieves chain information based on the provided identifier
    • chain_identifier: Can be an integer (chain ID) or a string (chain name or alias)
    • Returns a ChainInfo object containing chain details
    • Raises ChainNotFoundError if the chain is not found

Token Information

  • get_token_info(chain_id: int, symbol: str) -> TokenInfo

    • Retrieves token information for a specific chain and symbol
    • Returns a TokenInfo object containing token details
    • Raises TokenNotFoundError if the token is not found
  • get_chain_tokens(chain_id: int) -> dict[str, TokenInfo]

    • Retrieves all common tokens for a specific chain
    • Returns a dictionary mapping token symbols to TokenInfo objects
  • get_all_chain_tokens(chain_id: int) -> ChainTokens

    • Retrieves all tokens for a chain including native, wrapped native, and common tokens
    • Returns a ChainTokens object with properties:
      • native_token: The chain's native token (e.g., ETH on Ethereum)
      • wrapped_native: The chain's wrapped native token (e.g., WETH on Ethereum)
      • common_tokens: Dictionary of common tokens on the chain

Constants Module

The package provides a constants module with various blockchain-related constants, organized into categories:

  • EventTopics: Event signature topics (keccak hash of event signatures)

    • TRANSFER, APPROVAL, UNISWAP_V2_SWAP, etc.
    • Nested categories: ERC20, ERC721, Uniswap
  • FunctionSignatures: Function signature selectors (first 4 bytes of keccak hash)

    • TRANSFER, APPROVE, BALANCE_OF, etc.
    • Nested categories: ERC20
  • Addresses: Common contract addresses

    • NULL_ADDRESS
    • Nested categories: Uniswap, Sushiswap
  • GasLimits: Standard gas limits for operations

    • ERC20_TRANSFER, ERC20_APPROVE, SWAP
  • BlockTime: Average block times in seconds

    • ETHEREUM, BSC, POLYGON, ARBITRUM, etc.
  • WrappedToken: Wrapped token information

    • ETHEREUM, POLYGON, BSC, etc.

Direct access to all constants is also available at the module level for convenience.

Development

To set up the development environment:

  1. Clone the repository
  2. Install development dependencies: pip install -e ".[dev]"
  3. Run tests: pytest

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

This project uses data from the chainid.network project, which provides a comprehensive list of EVM-compatible chains.

Disclaimer

This package is provided as-is, and while we strive for accuracy, we cannot guarantee the correctness of all chain information. Users should verify critical information independently.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chain-index-0.3.0.tar.gz (161.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chain_index-0.3.0-py3-none-any.whl (164.1 kB view details)

Uploaded Python 3

File details

Details for the file chain-index-0.3.0.tar.gz.

File metadata

  • Download URL: chain-index-0.3.0.tar.gz
  • Upload date:
  • Size: 161.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for chain-index-0.3.0.tar.gz
Algorithm Hash digest
SHA256 03a2559ed8e2812dda8fb44c5903820e722541d5b27f4083ab14729bfb7f99c5
MD5 5c7d22052de2fa422e85773a18f1058e
BLAKE2b-256 7980dc11480413aee067c4abde307d5fc995903d842339a68771e77399c6733d

See more details on using hashes here.

File details

Details for the file chain_index-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: chain_index-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 164.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for chain_index-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffd28d880a5711283e4f1bfb2c731ec9b11305a281d4e8dedca2a0960875f426
MD5 8ab4df973b565064670053e256968477
BLAKE2b-256 26f2a4cc4a46daf446cfa34bec9e1822e6e23530e823e0fe36398446efe4033d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page