1
- from typing import Any , List , Optional , Protocol , Union
1
+ from typing import Any , List , Protocol
2
2
3
3
from Cryptodome .Hash import keccak
4
4
from multiversx_sdk import (
5
5
Address ,
6
6
AddressComputer ,
7
- AwaitingOptions ,
8
- NetworkConfig ,
9
- TransactionOnNetwork ,
10
- TransactionsFactoryConfig ,
7
+ SmartContractQuery ,
8
+ SmartContractQueryResponse ,
11
9
)
12
10
13
11
from multiversx_sdk_cli import cli_shared , utils
14
12
from multiversx_sdk_cli .config import get_address_hrp
15
13
from multiversx_sdk_cli .constants import ADDRESS_ZERO_HEX
16
- from multiversx_sdk_cli .contracts import SmartContract
17
14
from multiversx_sdk_cli .transactions import do_prepare_transaction
18
15
19
16
MaxNumShards = 256
23
20
24
21
# fmt: off
25
22
class INetworkProvider (Protocol ):
26
- def query_contract (self , query : Any ) -> Any :
27
- ...
28
-
29
- def get_network_config (self ) -> NetworkConfig :
30
- ...
31
-
32
- def await_transaction_completed (
33
- self , transaction_hash : Union [bytes , str ], options : Optional [AwaitingOptions ] = None
34
- ) -> TransactionOnNetwork :
23
+ def query_contract (self , query : SmartContractQuery ) -> SmartContractQueryResponse :
35
24
...
36
25
# fmt: on
37
26
38
27
39
28
def resolve (name : str , proxy : INetworkProvider ) -> Address :
40
- name_arg = "0x{}" .format (str .encode (name ).hex ())
41
29
dns_address = dns_address_for_name (name )
42
30
43
- response = _query_contract (contract_address = dns_address , proxy = proxy , function = "resolve" , args = [name_arg ])
31
+ response = _query_contract (contract_address = dns_address , proxy = proxy , function = "resolve" , args = [name . encode () ])
44
32
45
- if len (response ) == 0 :
33
+ if len (response . return_data_parts ) == 0 :
46
34
return Address .new_from_hex (ADDRESS_ZERO_HEX , get_address_hrp ())
47
35
48
- result = response [ 0 ]. get ( "returnDataParts" ) [0 ]
49
- return Address . new_from_hex (result , get_address_hrp ())
36
+ result = response . return_data_parts [0 ]
37
+ return Address (result , get_address_hrp ())
50
38
51
39
52
40
def validate_name (name : str , shard_id : int , proxy : INetworkProvider ):
53
- name_arg = "0x{}" .format (str .encode (name ).hex ())
54
41
dns_address = compute_dns_address_for_shard_id (shard_id )
55
42
56
43
response = _query_contract (
57
44
contract_address = dns_address ,
58
45
proxy = proxy ,
59
46
function = "validateName" ,
60
- args = [name_arg ],
47
+ args = [name . encode () ],
61
48
)
62
49
63
- response = response [0 ]
64
-
65
- return_code = response ["returnCode" ]
50
+ return_code : str = response .return_code
66
51
if return_code == "ok" :
67
52
print (f"name [{ name } ] is valid" )
68
53
else :
69
54
print (f"name [{ name } ] is invalid" )
70
55
71
- print (response )
72
-
73
56
74
57
def register (args : Any ):
75
58
args = utils .as_object (args )
@@ -107,22 +90,18 @@ def registration_cost(shard_id: int, proxy: INetworkProvider) -> int:
107
90
args = [],
108
91
)
109
92
110
- response = response [0 ]
111
-
112
- data = response ["returnDataParts" ][0 ]
93
+ data = response .return_data_parts [0 ]
113
94
if not data :
114
95
return 0
115
96
else :
116
- return int ( "0x{}" . format (data ) )
97
+ return int . from_bytes (data , byteorder = "big" , signed = False )
117
98
118
99
119
100
def version (shard_id : int , proxy : INetworkProvider ) -> str :
120
101
dns_address = compute_dns_address_for_shard_id (shard_id )
121
102
122
103
response = _query_contract (contract_address = dns_address , proxy = proxy , function = "version" , args = [])
123
-
124
- response = response [0 ]
125
- return bytearray .fromhex (response ["returnDataParts" ][0 ]).decode ()
104
+ return response .return_data_parts [0 ].decode ()
126
105
127
106
128
107
def dns_address_for_name (name : str ) -> Address :
@@ -147,15 +126,11 @@ def dns_register_data(name: str) -> str:
147
126
return "register@{}" .format (name_enc .hex ())
148
127
149
128
150
- def _query_contract (contract_address : Address , proxy : INetworkProvider , function : str , args : List [Any ]) -> List [Any ]:
151
- chain_id = proxy .get_network_config ().chain_id
152
- config = TransactionsFactoryConfig (chain_id )
153
- contract = SmartContract (config )
154
-
155
- return contract .query_contract (
156
- contract_address = contract_address ,
157
- proxy = proxy ,
158
- function = function ,
159
- arguments = args ,
160
- should_prepare_args = False ,
161
- )
129
+ def _query_contract (
130
+ contract_address : Address ,
131
+ proxy : INetworkProvider ,
132
+ function : str ,
133
+ args : List [bytes ],
134
+ ) -> SmartContractQueryResponse :
135
+ query = SmartContractQuery (contract = contract_address , function = function , arguments = args )
136
+ return proxy .query_contract (query )
0 commit comments