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.config                # NetworkConfigDto (Pydantic)
bitcoin.__dict__()            # Full data: {"config": {...}, "tokens": [...]}

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
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, …). 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
  • 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.4.tar.gz (12.9 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.4-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: token_network-0.4.4.tar.gz
  • Upload date:
  • Size: 12.9 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.4.4.tar.gz
Algorithm Hash digest
SHA256 c33e4dd21c565a95d56799b0f92bc33d80bea328007683ea0347d48d7ce6bf21
MD5 32a6f387c199123aef7e91e3835d55c8
BLAKE2b-256 7f6013e376d9d0a787a3641067782b01d6f0c17cfd903989c0442231b1de82ff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: token_network-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 12.5 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.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 313120873ddbf4990b23127df69d682d8337215148aa2028f15b12f18388f7c7
MD5 b3d4276458a92bec926527d92a916fc3
BLAKE2b-256 d084e8d19aca88587e4aceb5381b561c51edf984de77ef57bfbdcc71e95c08c5

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