Skip to main content

Validate input and return token network config (e.g. network.bitcoin, network.bsc.usdt)

Project description

token-network

Python library that validates input and returns token network config. Resolve networks and tokens by name/symbol and get merged config (e.g. USDT on BSC with contract address and decimals).

Install

pip install token-network

From source (development):

pip install -e .

Quick start

from token_network import network, get_network, get_token, get_token_network, TokenNetworkError

# Attribute access
network.bitcoin.config          # Bitcoin network config
network.bsc.usdt               # USDT on BSC (contract, decimal, etc.)

# By name/symbol
get_network("bitcoin")         # Full network data
get_token("USDT")              # Token info by symbol, slug, or name
get_token_network("USDT", "bsc")  # Token-on-network config

API reference

All identifiers (network name, token symbol/slug/name) are case-insensitive. Unknown network or token raises TokenNetworkError.

Attribute access: network

Use network.<network> and optionally network.<network>.<token> for direct access.

Usage Returns
network.bitcoin Network node (see below)
network.bitcoin.config Network config dict
network.bitcoin.tokens List of token bindings on this network
network.bitcoin.to_dict() {"config": {...}, "tokens": [...]}
network.bsc.usdt Token-on-network dict (network + token + contract_address, decimal, native)

Example

network.bitcoin.config
# {'network_type': 'UTXO', 'token_standard': 'BTC', 'base_token': 'BTC', 'base_token_decimal': 8, ...}

network.bsc.usdt
# {'network': {...}, 'token': {...}, 'contract_address': '0x55d398326f99059fF775485246999027B3197955', 'decimal': 18, 'native': False}

get_networks()

Returns a sorted list of all network ids.

Returns: list[str] — e.g. ['bitcoin', 'bsc', 'dogecoin', 'ethereum', 'ripple', 'solana', 'tron']

Example

from token_network import get_networks
get_networks()
# ['bitcoin', 'bsc', 'dogecoin', 'ethereum', 'ripple', 'solana', 'tron']

Also available as network.get_networks().


get_tokens()

Returns a sorted list of all token symbols.

Returns: list[str] — e.g. ['AAVE', 'BNB', 'BTC', 'ETH', 'USDT', ...]

Example

from token_network import get_tokens
get_tokens()
# ['AAVE', 'BNB', 'BTC', 'DOGE', 'ETH', 'LINK', 'SHIB', 'SOL', 'TRX', 'UNI', 'USDC', 'USDT', 'XRP']

Also available as network.get_tokens().


get_token(identifier)

Get token object by symbol, slug, or name (case-insensitive).

Parameters

  • identifier — Token symbol (e.g. USDT), slug (e.g. usdt), or name (e.g. tether).

Returns: dict — Token info: slug, symbol, standard_symbol, name, precision, factor.

Raises: TokenNetworkError if no token matches.

Example

from token_network import get_token
get_token("USDT")
# {'slug': 'usdt', 'symbol': 'USDT', 'standard_symbol': 'USDT', 'name': 'tether', 'precision': 6, 'factor': '1e6'}
get_token("tether")   # same
get_token("btc")      # Bitcoin token info

Also available as network.get_token(identifier).


get_network(identifier)

Get network object by network name/id (case-insensitive).

Parameters

  • identifier — Network name (e.g. bitcoin, bsc, ethereum).

Returns: dict with keys:

  • config — Network config (network_type, token_standard, base_token, confirmation_number, etc.).
  • tokens — List of token bindings on this network.

Raises: TokenNetworkError if network is unknown.

Example

from token_network import get_network
get_network("bitcoin")
# {'config': {'network_type': 'UTXO', 'base_token': 'BTC', ...}, 'tokens': [...]}
get_network("BSC")

Also available as network.get_network(identifier). Same shape as network.bitcoin.to_dict().


get_token_network(token_identifier, network_identifier)

Get token_network config for a token on a network (case-insensitive).

Parameters

  • token_identifier — Token symbol, slug, or name (e.g. USDT, usdt, tether).
  • network_identifier — Network name (e.g. bsc, ethereum).

Returns: dict with keys:

  • network — Network config.
  • token — Token info (slug, symbol, name, precision, factor).
  • contract_address — Contract address or None for native.
  • decimal / decimals — Decimals on this network.
  • native / type — Whether it is the chain’s native asset.

Raises: TokenNetworkError if token or network is unknown, or if the token is not on that network.

Example

from token_network import get_token_network
get_token_network("USDT", "bsc")
# {'network': {...}, 'token': {...}, 'contract_address': '0x55d398326f99059fF775485246999027B3197955', 'decimal': 18, 'native': False}
get_token_network("tether", "BSC")   # same
get_token_network("ETH", "ethereum")

Same data as network.bsc.usdt. Also available as network.get_token_network(token_identifier, network_identifier).


TokenNetworkError

Exception raised when:

  • A network name is unknown.
  • A token (symbol/slug/name) is unknown.
  • A token is requested on a network where it is not defined.

Example

from token_network import get_network, get_token, get_token_network, TokenNetworkError

try:
    get_network("unknown_chain")
except TokenNetworkError as e:
    print(e)  # Unknown network: 'unknown_chain'. Known networks: bitcoin, bsc, ...

try:
    get_token("unknown_token")
except TokenNetworkError as e:
    print(e)  # Unknown token: 'unknown_token'. Known tokens: [...]

try:
    get_token_network("SHIB", "bitcoin")
except TokenNetworkError as e:
    print(e)  # Token 'SHIB' is not on network 'bitcoin'. Available on this network: ['BTC']

Data sources

Config is loaded from YAML in the package’s data directory:

File Content
networks.yaml Chain config (network_type, token_standard, base_token, confirmation_number, …)
tokens.yaml Token definitions (symbol, slug, name, precision, factor)
token_networks.yaml Token–network bindings (contract_address, decimal, native)

To change data, edit the YAML files in token_network/data/.


Publishing to PyPI

To allow pip install token-network from PyPI:

  1. Create an account on pypi.org.
  2. pip install build twine
  3. python -m build then twine upload dist/*

For testing: twine upload --repository testpypi dist/*, then
pip install --index-url https://test.pypi.org/simple/ token-network.


Development

pip install -e ".[dev]"
pytest

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

token_network-0.1.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

token_network-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file token_network-0.1.1.tar.gz.

File metadata

  • Download URL: token_network-0.1.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for token_network-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a8f8a190c8ca4e4184e1d07a41baf7c6af77a9bfd03dc850c03c1eadb1b86ceb
MD5 4edc1d846bd4f618c2718c555a17b43a
BLAKE2b-256 8fab8bd4e3945aa2f1cc9f5c54ac8a3d66c10e2525ed1d1cfdc5bbc486a7e826

See more details on using hashes here.

File details

Details for the file token_network-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: token_network-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for token_network-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a1562842ce98941eb4fb123500a883bfe43fb66b9beffbe8de7379b5af70fe2f
MD5 ab59a87f1ec077722f7d86a801306d07
BLAKE2b-256 689d67ec5020f26eb56e6e5e83c9661931e3d4799803c5ff5f06ec7304501f3e

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