chainalysis.sql package

Module contents

class chainalysis.sql.Sql(api_key: str)[source]

Bases: object

The SQL class is used to query Database tables using SQL queries.

property transactional_query: TransactionalQuery

Call the transactional_query property to query transactional tables.

property analytical_query: AnalyticalQuery

Call the analytical_query property to query analytical tables.

property transactional: TransactionalQuery

The transactional property is deprecated. Use transactional_query instead.

property analytical: AnalyticalQuery

The analytical property is deprecated. Use analytical_query instead.

class chainalysis.sql.AnalyticalQuery(api_key: str)[source]

Bases: object

This AnalyticalQuery class provides methods to execute SQL queries on Data Solutions analytical tables. It supports fetching results as JSON or a pandas DataFrame, and provides query execution statistics.

__call__(query: str, parameters: dict[str, str] = {}, polling_interval_sec: int = 5, autopaginate: bool = True) AnalyticalQuery[source]

Execute a SQL query asynchronously using the provided parameters and polling interval.

Autopagination is enabled by default. If autopagination is enabled, the method will fetch all pages of results and return a single AnalyticalQuery object with all results.

Otherwise, the method will return an AnalyticalQuery object with the first page of results. Use the next_page() method to fetch the next page of results and the stats() method to get statistics that can help you determine the approach to take to fetch all results.

Parameters:
  • query (str) – The SQL query to be executed.

  • parameters (dict[str, str], optional) – A dictionary of parameters to be used in the query.

  • polling_interval_sec (int, optional) – The interval in seconds between status checks. The minimum value is 5 seconds.

  • autopaginate (bool, optional) – Whether to automatically retrieve full results instead of individual pages.

Returns:

An instance of the Analytical class with query results.

Return type:

Analytical

Raises:
  • DataSolutionsAPIException – Raises an exception if the query ID is not returned.

  • DataSolutionsSDKException – Raises an exception if an error occurs during query execution.

  • Exception – Raises an exception if an unexpected error occurs.

next_page() AnalyticalQuery[source]

Fetch the next page of analytical query results.

Returns:

An instance of the Analytical Query class with the next page of results.

Return type:

Analytical Query

Raises:

BadRequest – Raises an exception if there is no next page available.

json() dict[source]

Return results as a JSON.

Returns:

Results of the SQL query as a JSON.

Return type:

dict

Raises:

Exception – Raises an exception if the query resulted in an error.

df() DataFrame[source]

Convert query results into a pandas DataFrame.

Returns:

DataFrame containing the results of the SQL query.

Return type:

pd.DataFrame

Raises:

Exception – Raises an exception if the query resulted in an error.

stats() dict[source]

Get the statistics of the executed query.

Returns:

Statistics of the query execution.

Return type:

dict

Raises:

Exception – Raises an exception if the query resulted in an error.

status_code() int[source]

Get the HTTP status code of the response.

Returns:

HTTP status code.

Return type:

int

was_successful() bool[source]

Determine if the query executed successfully.

Returns:

True if the query was successful, False otherwise.

Return type:

bool

total_pages() int[source]

Return total number of pages.

Returns:

Number of pages.

Return type:

int

has_next() bool[source]

Return if the next page exists.

Returns:

Whether next page exists.

Return type:

bool

class chainalysis.sql.TransactionalQuery(api_key: str)[source]

Bases: object

The TransactionalQuery class provides methods to handle transactional queries. It supports fetching results as JSON or pandas DataFrame, and provides query execution statistics.

__call__(query: str, parameters: dict[str, str] = {}, options: dict[str, str] = {}) TransactionalQuery[source]

Execute a SQL query using the provided parameters and options.

Parameters:
  • query (str) – SQL query template with placeholders for parameters.

  • parameters (dict[str, str], optional) – Parameters to format the SQL query.

  • options (dict[str, str], optional) – Additional options for query execution.

Returns:

An instance of the Transactional class with query results.

Return type:

Transactional

Raises:
  • UnauthorizedException – Raises an exception if the API key is invalid.

  • ForbiddenException – Raises an exception if the API key does not have access to the resource.

  • NotFoundException – Raises an exception if the resource is not found.

  • DataSolutionsSDKException – Raises an exception if the query execution results in an error.

  • UnhandledException – Raises an exception if an unhandled exception occurs.

json() dict[source]

Return the JSON data of the results.

Returns:

JSON results of the SQL query.

Return type:

dict

Raises:

Exception – Raises an exception if the query resulted in an error.

df() DataFrame[source]

Convert query results into a pandas DataFrame.

Returns:

DataFrame containing the results of the SQL query.

Return type:

pd.DataFrame

Raises:

Exception – Raises an exception if the query resulted in an error.

stats() dict[source]

Get the statistics of the executed query.

Returns:

Statistics of the query execution.

Return type:

dict

Raises:

Exception – Raises an exception if the query resulted in an error.

was_successful() bool[source]

Determine if the query executed successfully.

Returns:

True if the query was successful, False otherwise.

Return type:

bool

status_code() int[source]

Return the status code of the query.

Returns:

The status code of the query.

Return type:

int