chainalysis.orm.transactional_table - Data Solutions Python SDK 0.7.0 documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
Toggle table of contents sidebar

# Source code for chainalysis.orm.transactional_table

    from sqlalchemy import Column, MetaData, Table

    from chainalysis.constants import BASE_URL, TRANSACTIONAL_ENDPOINTS
    from chainalysis.exceptions import ValueException
    from chainalysis.orm._select import Select
    from chainalysis.sql.transactional import Transactional
    from chainalysis.types import type_mapping
    from chainalysis.util_functions.requests import issue_request

    class TransactionalTable(Select):
        """
        The TransactionalTable class implements the Select base class
        for querying a chain-specific table within the Data Solutions system.

        It provides the structure for constructing and executing SQL queries
        using Common Table Expressions.
        """

        def get_table(self, chain, table) -> None:
            """
            Return a SQLAlchemy Table object for the given chain and table name.

            :param chain: The chain name.
            :type chain: str
            :param table: The table name.
            :type table: str
            :return: A SQLAlchemy Table object.
            :rtype: Table
            :raises ValueException: If the chain or table does not exist in the database.
            """
            get_schemas_url = (
                f"{BASE_URL['base_url']}/{TRANSACTIONAL_ENDPOINTS['get_schemas']}"
            )
            self.schemas: dict = issue_request(get_schemas_url, self.api_key)["schema"]

            chain_schema = self.schemas.get(chain)
            if not chain_schema:
                raise ValueException(
                    f"Chain '{chain}' does not exist in the database. Check your input. "
                    f"If you believe the inputted chain should exist, contact Data Solutions."
                )

            # If the chain exists, get the schema for the table
            for layer_tables in chain_schema.values():
                table_data = next(
                    (_table for _table in layer_tables if table in _table), None
                )
                if not table_data:
                    raise ValueException(
                        f"Table '{table}' does not exist in the database for chain '{chain}'. Check your input. "
                        f"If you believe the inputted table should exist, contact Data Solutions."
                    )

                # If the table exists, return the table
                columns = table_data[table]["schema"]

                table_columns = [
                    Column(
                        col["column"],
                        type_mapping[col["type"]],
                        comment=col["description"],
                    )
                    for col in columns
                ]

                return Table(f"{chain}.{table}", MetaData(), *table_columns)

        def execute(
            self,
            options: dict = {},
        ) -> Transactional:
            """
            Execute the query and return a Transactional object.

            :param options: The options for the query.
            :type options: dict
            :return: Transactional object.
            :rtype: Transactional
            """
            return Transactional(self.api_key).__call__(
                self.sql().replace('"', ""),
                options=options,
            )