Skip to main content

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

Project description

token-network

Resolve blockchain networks and tokens by name and get config (contract addresses, decimals, confirmations). All returned objects are Pydantic models (TokenDto, NetworkConfigDto, TokenOnNetworkDto, NetworkDataDto).

Install

pip install token-network

From source:

pip install -e .

Examples

Get a network and its settings

from token_network import network

# By name (callable)
bitcoin = network("bitcoin")

bitcoin.decimal               # 8  (base token decimals)
bitcoin.confirmation_number    # 2  (required confirmations)
bitcoin.wallet_address_regex   # wallet validation regex for this network
bitcoin.approximate_confirmation_time # 1200  (approx. seconds to confirm)
bitcoin.config                # NetworkConfigDto (Pydantic)
bitcoin.__dict__()            # Full data: {"config": {...}, "tokens": [...]}

Wallet address validation

Each network includes a wallet_address_regex pattern in its config. Use it to validate addresses for that chain:

from token_network import network, get_network, validate_wallet_address

# Direct property (same style as .decimal / .confirmation_number)
network("ethereum").wallet_address_regex
# ^(0x)[0-9A-Fa-f]{40}$

# Via config
network("ethereum").config.wallet_address_regex
get_network("bitcoin").config.wallet_address_regex

# In full network dict
network("ethereum").to_dict()["config"]["wallet_address_regex"]

# Validate an address
validate_wallet_address("ethereum", "0xdAC17F958D2ee523a2206206994597C13D831ec7")  # True
validate_wallet_address("ethereum", "0x123")  # False

List tokens on a network

from token_network import network

ethereum = network("ethereum")
tokens = ethereum.tokens      # list of TokenOnNetworkDto

for t in tokens:
    print(t.token.symbol, t.contract_address or "native", t.decimal)
# ETH None 18
# USDT 0xdAC17F958D2ee523a2206206994597C13D831ec7 6
# ...

Get one token on a network

from token_network import get_token_on_network

usdt = get_token_on_network(network="ethereum", token_abbr="USDT")

usdt.contract_address   # "0xdAC17F958D2ee523a2206206994597C13D831ec7"
usdt.decimal            # 6
usdt.native             # False
usdt.token.symbol       # "USDT"
usdt.__dict__()         # All fields as dict (or use .model_dump())

Same via attribute access

from token_network import network

# network.ethereum.usdt is the same as get_token_on_network(network="ethereum", token_abbr="USDT")
usdt = network.ethereum.usdt
assert usdt.contract_address == "0xdAC17F958D2ee523a2206206994597C13D831ec7"

Get token or network by identifier (Pydantic models)

from token_network import get_token, get_network, get_token_network

# Token by symbol, slug, or name (case-insensitive)
token = get_token("USDT")       # TokenDto
print(token.symbol, token.precision)   # USDT 6

# Network with config + tokens
net = get_network("bitcoin")    # NetworkDataDto
print(net.config.base_token)           # BTC
print(len(net.tokens))                 # 1 (e.g. BTC)

# Token on a specific network
ton = get_token_network("USDT", "bsc")  # TokenOnNetworkDto
print(ton.contract_address)

List all networks and tokens

from token_network import get_networks, get_tokens

get_networks()   # ['bitcoin', 'bsc', 'dogecoin', 'ethereum', 'ripple', 'solana', 'tron']
get_tokens()     # ['AAVE', 'BNB', 'BTC', 'ETH', 'USDT', ...]

Testnet config

from token_network import NetworkAccessor

testnet = NetworkAccessor(testnet=True)
bsc = testnet("bsc")
usdt_test = testnet.bsc.usdt   # USDT on BSC from token_networks_testnet.yaml

Error handling

from token_network import network, get_token_on_network, TokenNetworkError

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

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

API summary

What you want How
Network by name network("bitcoin") or network.bitcoin
Base decimals / confirmations network("bitcoin").decimal, .confirmation_number, .approximate_confirmation_time
Wallet address regex network("ethereum").wallet_address_regex or .config.wallet_address_regex
Validate wallet address validate_wallet_address("ethereum", "0x...")True / False
Full network data as dict network("bitcoin").__dict__() or .to_dict()
Tokens on a network network("ethereum").tokens → list of TokenOnNetworkDto
One token on a network get_token_on_network(network="ethereum", token_abbr="USDT") or network.ethereum.usdt
Token info get_token("USDT")TokenDto
Network + tokens get_network("bitcoin")NetworkDataDto
Token on network by ids get_token_network("USDT", "bsc")TokenOnNetworkDto

All identifiers are case-insensitive. Unknown network/token raises TokenNetworkError.

Pydantic models

  • TokenDto — Token definition (slug, symbol, name, precision, factor). From get_token().
  • NetworkConfigDto — Chain config (slug, name, network_type, base_token_decimal, confirmation_number, wallet_address_regex, …). From network("x").config and inside TokenOnNetworkDto.network.
  • TokenOnNetworkDto — Token on a network (network, token, contract_address, decimal, native, type). From get_token_on_network(), get_token_network(), network.ethereum.usdt.
  • NetworkDataDto — Result of get_network(): config + tokens (list of TokenOnNetworkDto).

Use .model_dump() to export any model as a dict.

Data sources

YAML under token_network/data/:

  • networks.yaml — chain config (including wallet_address_regex per network)
  • tokens.yaml — token definitions
  • token_networks.yaml — mainnet bindings (token + network → contract, decimals)
  • token_networks_testnet.yaml — testnet bindings (used with NetworkAccessor(testnet=True))

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.4.6.tar.gz (14.7 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.4.6-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for token_network-0.4.6.tar.gz
Algorithm Hash digest
SHA256 d42dff7bae072ae3c9cadf83292b9b09edd9b2db0706559933eeb509b0d35855
MD5 0271cf5f9a7243087a40f408736014d4
BLAKE2b-256 03dcc3bff79d12c652c452670ee70de68a0ac6b1f66aabc9041b88caa5e558d2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for token_network-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7f3c4861a4ea32b12ae02dc7136e8fa52ce6ff6cc8ee3feb8640b3ec4162fa2d
MD5 f2a1341e8d502f5e7c58346c3e45939d
BLAKE2b-256 0fa7e62526524cfb6e6d2cb76b70cdab29e919b98880acb4b7120c69ec67cc32

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