1
1
import re
2
2
import time
3
- # import hashlib
4
3
import asyncio
5
4
import heapq
6
5
from async_lru import alru_cache
19
18
from gsrest .util .eth_logs import decode_db_logs
20
19
from gsrest .errors import NotFoundException , BadUserInputException
21
20
from gsrest .util import is_eth_like
22
- from gsrest .util .address import cannonicalize_address , address_to_user_format
21
+ from gsrest .util .address import (address_to_user_format )
22
+ from gsrest .util .evm import (bytes_to_hex , strip_0x )
23
+ from gsrest .util .tron import partial_tron_to_partial_evm
24
+
25
+ # import hashlib
23
26
24
27
SMALL_PAGE_SIZE = 1000
25
28
BIG_PAGE_SIZE = 5000
@@ -58,20 +61,6 @@ def transaction_ordering_key(tx_id_key, tx):
58
61
return (tx [tx_id_key ], - trace_index , - log_index )
59
62
60
63
61
- def evm_address_from_hex (currency , address ):
62
- # eth addresses are case insensitive
63
- try :
64
- if address .startswith ("0x" ):
65
- return bytes .fromhex (address [2 :].lower ())
66
- else :
67
- return bytes .fromhex (address .lower ())
68
- except ValueError :
69
- # bytes.fromHex throws value error if non hex chars are found
70
- raise BadUserInputException (
71
- "The address provided does not look"
72
- f" like a { currency .upper ()} address: { address } " )
73
-
74
-
75
64
def identity (y , x ):
76
65
return x
77
66
@@ -792,10 +781,6 @@ async def get_addresses_by_ids(self,
792
781
result = await self .concurrent_with_args (currency , 'transformed' ,
793
782
query , params )
794
783
795
- for row in result :
796
- if is_eth_like (currency ):
797
- row ['address' ] = \
798
- address_to_user_format (currency , row ['address' ])
799
784
return result
800
785
801
786
async def get_new_address (self , currency , address ):
@@ -805,7 +790,7 @@ async def get_new_address(self, currency, address):
805
790
if not prefix :
806
791
return None
807
792
if is_eth_like (currency ):
808
- address = evm_address_from_hex (currency , address )
793
+ # address = evm_address_from_hex(currency, address)
809
794
prefix = prefix .upper ()
810
795
prefix_length = self .get_prefix_lengths (currency )['address' ]
811
796
query = ("SELECT * FROM new_addresses "
@@ -825,7 +810,7 @@ async def get_address_id(self, currency, address):
825
810
if not prefix :
826
811
return None
827
812
if is_eth_like (currency ):
828
- address = evm_address_from_hex (currency , address )
813
+ # address = evm_address_from_hex(currency, address)
829
814
prefix = prefix .upper ()
830
815
query = ("SELECT address_id FROM address_ids_by_address_prefix "
831
816
"WHERE address_prefix = %s AND address = %s" )
@@ -914,10 +899,7 @@ async def new_address(self, currency, address):
914
899
async def new_entity (self , currency , address ):
915
900
data = await self .new_address (currency , address )
916
901
data ['no_addresses' ] = 1
917
- if is_eth_like (currency ):
918
- data ['root_address' ] = address_to_user_format (currency , address )
919
- else :
920
- data ['root_address' ] = address
902
+ data ['root_address' ] = address
921
903
return data
922
904
923
905
async def get_address_by_address_id (self , currency , address_id ):
@@ -932,7 +914,7 @@ async def get_address_by_address_id(self, currency, address_id):
932
914
return None
933
915
raise NotFoundException (
934
916
f'Address { address_id } has no external transactions' )
935
- return address_to_user_format ( currency , result ["address" ])
917
+ return result ["address" ]
936
918
937
919
async def get_address (self , currency , address ):
938
920
try :
@@ -1105,7 +1087,9 @@ async def list_links(self,
1105
1087
async def list_matching_addresses (self , currency , expression , limit = 10 ):
1106
1088
prefix_lengths = self .get_prefix_lengths (currency )
1107
1089
expression_orginal = expression
1108
- expression = cannonicalize_address (currency , expression , partial = True )
1090
+ if currency == 'trx' :
1091
+ expression = partial_tron_to_partial_evm (expression )
1092
+
1109
1093
if len (expression ) < prefix_lengths ['address' ]:
1110
1094
return []
1111
1095
norm = identity
@@ -1120,10 +1104,9 @@ async def list_matching_addresses(self, currency, expression, limit=10):
1120
1104
prefix = prefix .upper ()
1121
1105
rows = []
1122
1106
1123
- async def collect (query , paging_state ):
1124
- while paging_state and len (rows ) < limit :
1125
- if paging_state is True :
1126
- paging_state = None
1107
+ async def collect (query ):
1108
+ paging_state = None
1109
+ while len (rows ) < limit :
1127
1110
result = await self .execute_async (currency ,
1128
1111
'transformed' ,
1129
1112
query , [prefix ],
@@ -1137,18 +1120,20 @@ async def collect(query, paging_state):
1137
1120
expression_orginal )
1138
1121
])
1139
1122
paging_state = result .paging_state
1123
+ if paging_state is None :
1124
+ break
1140
1125
1141
1126
query = "SELECT address FROM address_ids_by_address_prefix " \
1142
1127
"WHERE address_prefix = %s"
1143
1128
1144
- await collect (query , True )
1129
+ await collect (query )
1145
1130
1146
1131
if len (rows ) < limit :
1147
1132
query = "SELECT address FROM new_addresses " \
1148
1133
"WHERE address_prefix = %s"
1149
1134
if self .parameters [currency ]["use_delta_updater_v1" ]:
1150
1135
try :
1151
- await collect (query , True )
1136
+ await collect (query )
1152
1137
except InvalidRequest as e :
1153
1138
if 'new_addresses' not in str (e ):
1154
1139
raise e
@@ -1538,11 +1523,17 @@ async def list_matching_txs(self, currency, expression, limit):
1538
1523
1539
1524
return rows [0 :limit ]
1540
1525
1541
- @eth
1542
1526
def scrub_prefix (self , currency , expression ):
1527
+ if isinstance (expression , bytes ):
1528
+ expression = bytes_to_hex (expression )
1529
+
1530
+ if currency == 'eth' :
1531
+ expression = strip_0x (expression )
1532
+
1543
1533
if currency not in self .parameters :
1544
1534
raise NotFoundException (f'{ currency } not found' )
1545
- bech32_prefix = self .parameters [currency ]['bech_32_prefix' ]
1535
+
1536
+ bech32_prefix = self .parameters [currency ].get ('bech_32_prefix' , '' )
1546
1537
return expression [len (bech32_prefix ):] \
1547
1538
if expression .startswith (bech32_prefix ) \
1548
1539
else expression
@@ -1614,7 +1605,7 @@ async def finish_entity(self, currency, row, with_txs=True):
1614
1605
return await self .finish_address (currency , row , with_txs )
1615
1606
1616
1607
async def finish_entity_eth (self , currency , row , with_txs = True ):
1617
- row ['root_address' ] = address_to_user_format ( currency , row ['address' ])
1608
+ row ['root_address' ] = row ['address' ]
1618
1609
return await self .finish_address (currency , row , with_txs )
1619
1610
1620
1611
async def finish_addresses (self , currency , rows , with_txs = True ):
@@ -1661,8 +1652,6 @@ async def finish_address(self, currency, row, with_txs=True):
1661
1652
return row
1662
1653
1663
1654
async def finish_address_eth (self , currency , row , with_txs = True ):
1664
- if 'address' in row :
1665
- row ['address' ] = address_to_user_format (currency , row ['address' ])
1666
1655
row ['cluster_id' ] = row ['address_id' ]
1667
1656
row ['total_received' ] = \
1668
1657
self .markup_currency (currency , row ['total_received' ])
@@ -1720,7 +1709,7 @@ async def is_address_dirty(self, currency, address):
1720
1709
if not prefix :
1721
1710
return None
1722
1711
if is_eth_like (currency ):
1723
- address = evm_address_from_hex (currency , address )
1712
+ # address = evm_address_from_hex(currency, address)
1724
1713
prefix = prefix .upper ()
1725
1714
prefix_length = self .get_prefix_lengths (currency )['address' ]
1726
1715
query = ("SELECT address FROM dirty_addresses "
@@ -1786,6 +1775,7 @@ async def add_balance_eth(self, currency, row):
1786
1775
1787
1776
def scrub_prefix_eth (self , currency , expression ):
1788
1777
# remove 0x prefix
1778
+ expression = bytes_to_hex (expression )
1789
1779
if expression .startswith ("0x" ):
1790
1780
return expression [2 :]
1791
1781
else :
@@ -2064,10 +2054,8 @@ async def list_txs_by_node_type_eth(self,
2064
2054
token_tx = await self .fetch_token_transaction (
2065
2055
currency , full_tx , addr_tx ["log_index" ])
2066
2056
2067
- addr_tx ['to_address' ] = address_to_user_format (
2068
- currency , token_tx ['to_address' ])
2069
- addr_tx ['from_address' ] = address_to_user_format (
2070
- currency , token_tx ['from_address' ])
2057
+ addr_tx ['to_address' ] = token_tx ['to_address' ]
2058
+ addr_tx ['from_address' ] = token_tx ['from_address' ]
2071
2059
addr_tx ['currency' ] = token_tx ["currency" ]
2072
2060
addr_tx ['token_tx_id' ] = addr_tx ["log_index" ]
2073
2061
value = token_tx ['value' ] * \
@@ -2081,9 +2069,6 @@ async def list_txs_by_node_type_eth(self,
2081
2069
(- 1 if addr_tx ['is_outgoing' ] else 1 )
2082
2070
2083
2071
contract_creation = full_tx .get ('contract_creation' , None )
2084
- if contract_creation is not None :
2085
- contract_creation = address_to_user_format (
2086
- currency , contract_creation )
2087
2072
2088
2073
addr_tx ['contract_creation' ] = contract_creation
2089
2074
addr_tx ['tx_hash' ] = full_tx ['tx_hash' ]
@@ -2145,20 +2130,16 @@ async def list_txs_by_hashes_eth(self,
2145
2130
result_with_tokens = []
2146
2131
for row in result :
2147
2132
2148
- row ['from_address' ] = address_to_user_format (
2149
- currency , row ['from_address' ])
2150
2133
to_address = row ['to_address' ]
2151
2134
if to_address is None :
2152
2135
# this is a contract creation transaction
2153
2136
# set recipient to newly created contract
2154
2137
# and mark tx as creation
2155
- row ['to_address' ] = address_to_user_format (
2156
- currency , row ['receipt_contract_address' ])
2138
+ row ['to_address' ] = row ['receipt_contract_address' ]
2157
2139
row ['contract_creation' ] = True
2158
2140
else :
2159
2141
# normal transaction
2160
- row ['to_address' ] = address_to_user_format (
2161
- currency , to_address )
2142
+ row ['to_address' ] = to_address
2162
2143
# result['contract_creation'] = False
2163
2144
2164
2145
result_with_tokens .append (row )
@@ -2189,15 +2170,12 @@ async def get_tx_by_hash_eth(self, currency, hash):
2189
2170
if to_address is None :
2190
2171
# this is a contract creation transaction
2191
2172
# set recipient to newly created contract and mark tx as creation
2192
- result ['to_address' ] = address_to_user_format (
2193
- currency , result ['receipt_contract_address' ])
2173
+ result ['to_address' ] = result ['receipt_contract_address' ]
2194
2174
result ['contract_creation' ] = True
2195
2175
else :
2196
2176
# normal transaction
2197
- result ['to_address' ] = address_to_user_format ( currency , to_address )
2177
+ result ['to_address' ] = to_address
2198
2178
# result['contract_creation'] = False
2199
- result ['from_address' ] = address_to_user_format (
2200
- currency , result ['from_address' ])
2201
2179
return result
2202
2180
2203
2181
async def list_links_eth (self ,
@@ -2324,8 +2302,6 @@ async def list_links_eth(self,
2324
2302
tx_ids ,
2325
2303
include_token_txs = True )
2326
2304
2327
- neighbor = address_to_user_format (currency , neighbor )
2328
- address = address_to_user_format (currency , address )
2329
2305
txs = [
2330
2306
tx for tx in all_txs
2331
2307
if tx ["to_address" ] == neighbor and tx ["from_address" ] == address
0 commit comments