11def calls_query (chain : str , contract_address : str , from_block : int , from_transaction_position : int , from_trace_index : int ,
22 function_selector : str = None ,
33 stop_block : int = None ,
4+ order : str = 'asc' ,
45 limit : int = None ) -> str :
56
67 """
@@ -13,40 +14,81 @@ def calls_query(chain: str, contract_address: str, from_block: int, from_transac
1314 :param from_trace_index: The starting trace index, inclusive.
1415 :param function_selector: The function selector.
1516 :param stop_block: The ending block number, exclusive.
17+ :param order: The order to return the transactions and traces in.
1618 :param limit: The maximum number of transactions and traces to return.
1719 :return: The SQL query.
1820 """
1921
20- return \
21- f"""
22- SELECT * FROM (
23-
24- (SELECT
25- timestamp, block_number, transaction_hash, position AS transaction_position,
26- 0 AS trace_index, array[]::integer[] AS trace_address, 'call' AS trace_type,
27- from_address, value, input, output, __confirmed
28- FROM { chain } .transactions
29- WHERE to_address = '{ contract_address } '
30- { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
31- AND (block_number, position) >= ({ from_block } , { from_transaction_position } )
32- { f"AND block_number < { stop_block } " if stop_block is not None else "" }
33- ORDER BY block_number ASC, transaction_position ASC)
34-
35- UNION ALL
36-
37- (SELECT
38- timestamp, block_number, transaction_hash, transaction_position,
39- trace_index + 1, trace_address, trace_type,
40- from_address, value, input, output, __confirmed
41- FROM { chain } .traces
42- WHERE to_address = '{ contract_address } '
43- { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
44- AND (block_number, transaction_position, trace_index) >= ({ from_block } , { from_transaction_position } , { from_trace_index } )
45- { f"AND block_number < { stop_block } " if stop_block is not None else "" }
46- ORDER BY block_number ASC, transaction_position ASC)
47-
48- ) AS t
49-
50- ORDER BY block_number ASC, transaction_position ASC, trace_index ASC
51- { f"LIMIT { limit } " if limit is not None else "" }
52- """
22+ if order == 'asc' :
23+ return \
24+ f"""
25+ SELECT * FROM (
26+
27+ (SELECT
28+ timestamp, block_number, transaction_hash, position AS transaction_position,
29+ 0 AS trace_index, array[]::integer[] AS trace_address, 'call' AS trace_type,
30+ from_address, value, input, output, __confirmed
31+ FROM { chain } .transactions
32+ WHERE to_address = '{ contract_address } '
33+ { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
34+ AND (block_number, position) >= ({ from_block } , { from_transaction_position } )
35+ { f"AND block_number < { stop_block } " if stop_block is not None else "" }
36+ ORDER BY block_number ASC, transaction_position ASC
37+ { f"LIMIT { limit } " if limit is not None else "" } )
38+
39+ UNION ALL
40+
41+ (SELECT
42+ timestamp, block_number, transaction_hash, transaction_position,
43+ trace_index + 1, trace_address, trace_type,
44+ from_address, value, input, output, __confirmed
45+ FROM { chain } .traces
46+ WHERE to_address = '{ contract_address } '
47+ { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
48+ AND (block_number, transaction_position, trace_index) >= ({ from_block } , { from_transaction_position } , { from_trace_index } )
49+ { f"AND block_number < { stop_block } " if stop_block is not None else "" }
50+ ORDER BY block_number ASC, transaction_position ASC, trace_index ASC
51+ { f"LIMIT { limit } " if limit is not None else "" } )
52+
53+ ) AS t
54+
55+ ORDER BY block_number ASC, transaction_position ASC, trace_index ASC
56+ { f"LIMIT { limit } " if limit is not None else "" }
57+ """
58+
59+ else :
60+ return \
61+ f"""
62+ SELECT * FROM (
63+
64+ (SELECT
65+ timestamp, block_number, transaction_hash, position AS transaction_position,
66+ 0 AS trace_index, array[]::integer[] AS trace_address, 'call' AS trace_type,
67+ from_address, value, input, output, __confirmed
68+ FROM { chain } .transactions
69+ WHERE to_address = '{ contract_address } '
70+ { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
71+ AND (block_number, position) <= ({ from_block } , { from_transaction_position } )
72+ { f"AND block_number > { stop_block } " if stop_block is not None else "" }
73+ ORDER BY block_number DESC, transaction_position DESC
74+ { f"LIMIT { limit } " if limit is not None else "" } )
75+
76+ UNION ALL
77+
78+ (SELECT
79+ timestamp, block_number, transaction_hash, transaction_position,
80+ trace_index + 1, trace_address, trace_type,
81+ from_address, value, input, output, __confirmed
82+ FROM { chain } .traces
83+ WHERE to_address = '{ contract_address } '
84+ { f"AND LEFT(input, 10) = '{ function_selector } '" if function_selector is not None else "" }
85+ AND (block_number, transaction_position, trace_index) <= ({ from_block } , { from_transaction_position } , { from_trace_index } )
86+ { f"AND block_number > { stop_block } " if stop_block is not None else "" }
87+ ORDER BY block_number DESC, transaction_position DESC, trace_index DESC
88+ { f"LIMIT { limit } " if limit is not None else "" } )
89+
90+ ) AS t
91+
92+ ORDER BY block_number DESC, transaction_position DESC, trace_index DESC
93+ { f"LIMIT { limit } " if limit is not None else "" }
94+ """
0 commit comments