Skip to main content

Shared constants and utilities for ChainSwarm blockchain analytics projects

Project description

chainswarm-core

CI PyPI version Python 3.13+ License

Shared constants and utilities for ChainSwarm blockchain analytics projects.

Overview

chainswarm-core provides a single source of truth for:

  • Blockchain network definitions - Network types, block times, native assets
  • Address classifications - Address types, trust levels, risk mappings
  • Pattern detection constants - Pattern types, detection methods, role classifications
  • Database utilities - ClickHouse repository base class and row conversion utilities

This package eliminates code duplication across ChainSwarm projects including:

  • data-pipeline
  • chain-synthetics
  • analytics-pipeline
  • ml-pipeline
  • benchmark
  • risk-scoring

Installation

pip install chainswarm-core

For development:

pip install chainswarm-core[dev]

Quick Start

from chainswarm_core import (
    AddressTypes,
    Network,
    NetworkType,
    RiskLevels,
    TrustLevels,
)

# Check network type
if Network.get_node_type("polkadot") == NetworkType.SUBSTRATE:
    print("Polkadot is a Substrate network")

# Get block time
block_time = Network.get_block_time("bitcoin")  # Returns 600 seconds

# Check address risk
from chainswarm_core.constants import get_address_type_risk_level, is_high_risk_address_type

risk = get_address_type_risk_level(AddressTypes.MIXER)  # Returns "critical"
is_risky = is_high_risk_address_type(AddressTypes.GAMBLING)  # Returns True

Modules

chainswarm_core.constants.networks

Network type classifications and blockchain network enum.

from chainswarm_core.constants.networks import (
    NetworkType,      # SUBSTRATE, EVM, UTXO
    Network,          # Enum of supported networks
    substrate_networks,
    evm_networks,
    utxo_networks,
)

# Get native asset symbol
symbol = Network.get_native_asset_symbol("bittensor")  # Returns "TAO"

chainswarm_core.constants.addresses

Address type and trust level classifications.

from chainswarm_core.constants.addresses import (
    AddressTypes,     # EXCHANGE, DEX, MIXER, VALIDATOR, etc.
    TrustLevels,      # VERIFIED, COMMUNITY, OFFICIAL, etc.
    is_high_risk_address_type,
    is_trusted_address_type,
)

chainswarm_core.constants.risk

Risk levels, severities, and risk mappings.

from chainswarm_core.constants.risk import (
    RiskLevels,       # LOW, MEDIUM, HIGH, CRITICAL
    Severities,       # LOW, MEDIUM, HIGH, CRITICAL
    ADDRESS_TYPE_RISK_MAP,
    AddressSubtypeRiskModifiers,
    get_address_type_risk_level,
    get_subtype_risk_modifier,
)

# Get risk level for address type
risk = get_address_type_risk_level(AddressTypes.SCAM)  # Returns "critical"

# Get risk modifier for subtype
modifier = get_subtype_risk_modifier("uniswap_v3")  # Returns 0.8

chainswarm_core.constants.patterns

Pattern detection types and role classifications.

from chainswarm_core.constants.patterns import (
    PatternTypes,     # CYCLE, LAYERING_PATH, SMURFING_NETWORK, etc.
    DetectionMethods, # SCC_ANALYSIS, CYCLE_DETECTION, etc.
    PatternRoles,     # ATTACKER, MULE, HOT_WALLET, etc.
    MALICIOUS_ROLES,
    VICTIM_ROLES,
    BENIGN_ROLES,
    is_malicious_role,
    is_victim_role,
    is_benign_role,
)

chainswarm_core.db

ClickHouse database utilities.

from chainswarm_core.db import (
    BaseRepository,
    row_to_dict,
    convert_clickhouse_enum,
    clickhouse_row_to_pydantic,
    rows_to_pydantic_list,
)

# Create a repository
class MyRepository(BaseRepository):
    @classmethod
    def schema(cls) -> str:
        return "my_table.sql"
    
    @classmethod
    def table_name(cls) -> str:
        return "my_table"

# Convert rows to Pydantic models
from pydantic import BaseModel

class MyModel(BaseModel):
    id: int
    name: str

rows = [(1, "first"), (2, "second")]
columns = ["id", "name"]
models = rows_to_pydantic_list(MyModel, rows, columns)

Migration Guide

From project-local constants

Before:

from packages.storage.constants import AddressTypes, RiskLevels
from packages.storage.repositories.base_repository import BaseRepository

After:

from chainswarm_core import AddressTypes, RiskLevels, BaseRepository

From project-local repository utils

Before:

from packages.storage.repositories.utils import row_to_dict

After:

from chainswarm_core.db import row_to_dict

Development

Setup

# Clone the repository
git clone https://github.com/chainswarm/core.git
cd core

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

Running Tests

# All tests
pytest

# With coverage
pytest --cov=chainswarm_core --cov-report=html

# Specific module
pytest tests/test_constants/test_networks.py -v

CI/CD

  • CI: Runs on every push and PR to main

    • Tests on Python 3.13
  • Publish: Manual workflow dispatch to publish to PyPI

    • Requires version match in pyproject.toml
    • Creates GitHub release with tag

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

License

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

Links

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

chainswarm_core-0.1.3.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

chainswarm_core-0.1.3-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file chainswarm_core-0.1.3.tar.gz.

File metadata

  • Download URL: chainswarm_core-0.1.3.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chainswarm_core-0.1.3.tar.gz
Algorithm Hash digest
SHA256 60c4984cf6570f26b38b58859f977da03f6a6d4c8d88bc5249de1ebcb64b6337
MD5 c95a4200724e7064a9dc2d9a3a94a3aa
BLAKE2b-256 25ffcbb0abdbca049ed3e9918ff676b817de4a2d9746988b28baeeb2522fd8b9

See more details on using hashes here.

File details

Details for the file chainswarm_core-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for chainswarm_core-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 815de04b46338ee3bcc736390c922a85208d5f3e54bb8f170507fdf625e73ab3
MD5 2311ef15e73e4c7840be94e65851884a
BLAKE2b-256 379af1a2da914c8c907e578f9e69b447c2eb955afdf9fba72d6b6062b71f0918

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