chainalysis.orm package

Module contents

class chainalysis.orm.Orm(api_key: str)[source]

Bases: object

The Orm class is used to query Database tables using Ordinal Relational Mapping (ORM), which allows querying tables using Python methods rather than raw SQL.

TransactionalSelect(chain_table_name: str) TransactionalSelect[source]

Method to create a TransactionalTable object.

Parameters:

chain_table_name (str) – The name of the chain table.

Returns:

TransactionalTable object.

Return type:

TransactionalTable

AnalyticalSelect(chain_table_name: str) AnalyticalSelect[source]

Method to create an AnalyticalSelect object.

Parameters:

chain_table_name (str) – The name of the chain table.

Returns:

AnalyticalSelect object.

Return type:

AnalyticalSelect

class chainalysis.orm.Select(api_key: str, chain_table_name: str)[source]

Bases: Select, ABC

Select is an abstract base class that represents an interface for an Object Relational Mapper (ORM) which queries a chain-specific table within Data Solutions.

Select provides the structure for constructing and executing SQL queries using functions. Child classes must implement methods to retrieve tables and execute queries, which will return the appropriate result type.

property c: ColumnCollection

Get the columns of the table.

Returns:

A collection of column objects associated with the table.

Return type:

ColumnCollection

sql() str[source]

Compile the query into a raw SQL string.

Returns:

The compiled SQL query as a string.

Return type:

str

abstract execute() TransactionalQuery | AnalyticalQuery[source]

Execute the query and create a Transactional or Analytical object.

Returns:

Transactional or Analytical object.

Return type:

Union[Transactional, Analytical]

with_columns(*columns: Column) Select[source]

Select specific columns from the table for querying.

Note: All columns are selected by default. Use this method to select specific columns.

Parameters:

columns (Column) – The columns to be selected.

Returns:

A Select instance with the selected columns.

Return type:

Select

E.g.:

select_query = query_1.with_columns(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2
FROM "chain.table1"
where(*whereclauses: ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement) Select[source]

Create a new Select with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.

Parameters:

whereclauses (ColumnExpressionArgument[bool]) – The WHERE clauses to apply.

Returns:

A new Select with the WHERE clause applied.

Return type:

Select

E.g.:

where_query = query_1.where(query_1.c.column1 == 1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
WHERE "chain.table1".column1 = 1
limit(limit: int) Select[source]

Create a new Select with the given LIMIT clause applied.

Parameters:

limit (int) – The number of rows to limit the result set to.

Returns:

A new Select with the LIMIT clause applied.

Return type:

Select

E.g.:

limit_query = query_1.limit(10)

The resulting SQL query will be:

SELECT * FROM "chain.table1"
LIMIT 10
group_by(*columns: Column) Select[source]

Create a new Select with the given expression(s) added to its GROUP BY clause.

Parameters:

columns (Column) – The columns to group by.

Returns:

A new Select with the GROUP BY clause applied.

Return type:

Select

E.g.:

group_by_query = query_1.group_by(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
GROUP BY "chain.table1".column1, "chain.table1".column2
having(*having: ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement) Select[source]

Create a new Select with the given expression added to its HAVING clause joined to the existing clause via AND, if any.

Parameters:

having (ColumnExpressionArgument[bool]) – The HAVING clauses to apply.

Returns:

A new Select with the HAVING clause applied.

Return type:

Select

E.g.:

having_query = query_1.group_by(query_1.c.column1).having(query_1.c.column1 == 1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
GROUP BY "chain.table1".column1
HAVING "chain.table1".column1 = 1
order_by(*clauses: Column) Select[source]

Create a new Select with the given list of ORDER BY criteria applied.

Parameters:

clauses (Column) – The ORDER BY criteria to apply.

Returns:

A new Select with the ORDER BY criteria applied.

Return type:

Select

E.g.:

order_by_query = query_1.order_by(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
ORDER BY "chain.table1".column1, "chain.table1".column2
join(target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None, *, isouter: bool = False, full: bool = False) Select[source]

Create a SQL JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument, optional) – The ON clause to apply. If omitted, an ON clause is generated automatically based on the ForeignKey linkages between the two tables, if one can be unambiguously determined, otherwise an error is raised.

  • isouter (bool, optional) – Whether to apply an outer join.

  • full (bool, optional) – Whether to apply a full join.

Returns:

A new Select with the join operation applied.

Return type:

Select

E.g.:

join_query = query_1.join(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
outerjoin(target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement, *, full: bool = False) Select[source]

Create a SQL LEFT OUTER JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

  • full (bool, optional) – Whether to apply a full join.

Returns:

A new Select with the outer join operation applied.

Return type:

Select

E.g.:

outerjoin_query = query_1.outerjoin(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" LEFT OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
outerjoin_from(from_: Column, target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement, *, full: bool = False) Select[source]

Create a SQL LEFT OUTER JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • from (Table) – The table to join from.

  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

  • full (bool, optional) – Whether to apply a full join.

Returns:

A new Select with the outer join operation applied.

Return type:

Select

E.g.:

outerjoin_from_query = query_1.outerjoin_from(query_1.table, query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1" LEFT OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
distinct() Select[source]

Create a new Select which will apply DISTINCT operation applied.

Returns:

A new subquery with the DISTINCT operation applied.

Return type:

Select

E.g.:

distinct_query = query_1.distinct()

The resulting SQL query will be:

SELECT DISTINCT *
FROM "chain.table1"
exists() Select[source]

Create a new subquery which will apply an EXISTS clause to the SELECT statement overall.

Returns:

A new subquery with the EXISTS operation applied.

Return type:

Select

E.g.:

query_1 = query_1.where(query_1.c.column1 == 1)
exists_query = query_1.exists()

The resulting SQL query will be:

EXISTS (SELECT * FROM "chain.table1" WHERE "chain.table1".column1 = 1)
alias(name: str | None = None) Select[source]

Create a new Select object with the given name as an alias.

Parameters:

name (str, optional) – The name of the alias.

Returns:

A new Select with the alias applied.

Return type:

Select

E.g.:

alias_query = query_1.alias(name="alias")

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" AS alias
union(other: Select) Select[source]

Create an SQL UNION alias of this Select against the given selectables provided as positional arguments.

A union is the set of all rows returned by either the left or right selectables. If a row is present in both selectables, it will appear in the result set only once.

Parameters:

other (Select) – One or more Selects with which to union.

Returns:

A new Select with the union operation applied.

Return type:

Select

E.g.:

union_query = query_1.union(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
union_all(other: Select) Select[source]

Create an SQL UNION ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to union all.

Returns:

A new Select with the union all operation applied.

Return type:

Select

E.g.:

union_all_query = query_1.union_all(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table2".column1 AS column1
FROM "chain.table2" UNION ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2" UNION ALL SELECT "chain.table1".column1 AS column1
FROM "chain.table1") AS anon_1
except_(*other: Select) Select[source]

Create an SQL EXCEPT alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Select with which to except.

Returns:

A new Select with the except operation applied.

Return type:

Select

E.g.:

except_query = query_1.except_(query_2)

The resulting SQL query will be:

SELECT *
FROM (SELECT anon_1.column1 AS column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" EXCEPT SELECT "chain.table".column1 AS column1
FROM "chain.table" EXCEPT SELECT "chain.table".column2 AS column2
FROM "chain.table") AS anon_1) AS anon_1
except_all(*other: Select) Select[source]

Create an SQL EXCEPT ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to except all.

Returns:

A new Select with the except all operation applied.

Return type:

Select

E.g.:

except_all_query = query_1.except_all(query_2)

The resulting SQL query will be:

SELECT * FROM (SELECT “chain.name”.column1 AS column1 FROM “chain.name” EXCEPT ALL SELECT “chain.name”.column2 AS column2 FROM “chain.name”) AS anon_1

intersect(*other: Select) Select[source]

Create an SQL INTERSECT alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to intersect.

Returns:

A new Select with the intersect operation applied.

Return type:

Select

E.g.:

intersect_query = query_1.intersect(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
intersect_all(*other: Select) Select[source]

Create an SQL INTERSECT ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to intersect all.

Returns:

A new Select with the intersect all operation applied.

Return type:

Select

E.g.:

intersect_all_query = query_1.intersect_all(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT ALL SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
lateral(name: str | None = None) Select[source]

Create an SQL LATERAL alias of this Select against the given selectables provided as positional arguments.

Parameters:

name (str, optional) – The name of the lateral selection.

Returns:

A new Select with the lateral operation applied.

Return type:

Select

E.g.:

lateral_query = query_1.lateral(name="alias")

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" LATERAL AS alias
class chainalysis.orm.AnalyticalSelect(api_key: str, chain_table_name: str)[source]

Bases: Select

The AnalyticalSelect 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 methods.

execute(polling_interval_sec: int = 5, autopaginate: bool = True) AnalyticalQuery[source]

Execute the query and return an Analytical object.

Parameters:
  • 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:

Analytical object.

Return type:

Analytical

class chainalysis.orm.TransactionalSelect(api_key: str, chain_table_name: str)[source]

Bases: Select

The TransactionalSelect 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 methods.

execute(options: dict = {}) TransactionalQuery[source]

Execute the query and return a Transactional object.

Parameters:

options (dict) – The options for the query.

Returns:

Transactional object.

Return type:

Transactional