11import base64
22import logging
3- from typing import Any , List
3+ from typing import Any , List , Optional
44
55from Cryptodome .Hash import keccak
66
77from erdpy import config , constants , errors , utils
8- from erdpy .accounts import Address
8+ from erdpy .accounts import Account , Address
9+ from erdpy .interfaces import IElrondProxy
910from erdpy .transactions import Transaction
1011
1112logger = logging .getLogger ("contracts" )
1213
1314
1415class SmartContract :
15- def __init__ (self , address = None , bytecode = None , metadata = None ):
16+ def __init__ (self , address : Optional [ Address ] = None , bytecode = None , metadata = None ):
1617 self .address = Address (address )
1718 self .bytecode = bytecode
1819 self .metadata = metadata or CodeMetadata ()
1920
20- def deploy (self , owner , arguments , gas_price , gas_limit , value , chain , version ) -> Transaction :
21+ def deploy (self , owner : Account , arguments : List [ Any ] , gas_price : int , gas_limit : int , value : int , chain : str , version : int ) -> Transaction :
2122 self .owner = owner
2223 self .compute_address ()
2324
2425 arguments = arguments or []
2526 gas_price = int (gas_price )
2627 gas_limit = int (gas_limit )
27- value = str ( value or "0" )
28+ value = value or 0
2829
2930 tx = Transaction ()
3031 tx .nonce = owner .nonce
31- tx .value = value
32+ tx .value = str ( value )
3233 tx .sender = owner .address .bech32 ()
3334 tx .receiver = Address .zero ().bech32 ()
3435 tx .gasPrice = gas_price
@@ -40,7 +41,7 @@ def deploy(self, owner, arguments, gas_price, gas_limit, value, chain, version)
4041 tx .sign (owner )
4142 return tx
4243
43- def prepare_deploy_transaction_data (self , arguments ):
44+ def prepare_deploy_transaction_data (self , arguments : List [ Any ] ):
4445 tx_data = f"{ self .bytecode } @{ constants .VM_TYPE_ARWEN } @{ self .metadata .to_hex ()} "
4546
4647 for arg in arguments :
@@ -59,17 +60,17 @@ def compute_address(self):
5960 address = bytes ([0 ] * 8 ) + bytes ([5 , 0 ]) + address [10 :30 ] + owner_bytes [30 :]
6061 self .address = Address (address )
6162
62- def execute (self , caller , function , arguments , gas_price , gas_limit , value , chain , version ) -> Transaction :
63+ def execute (self , caller : Account , function : str , arguments : List [ str ] , gas_price : int , gas_limit : int , value : int , chain : str , version : int ) -> Transaction :
6364 self .caller = caller
6465
6566 arguments = arguments or []
6667 gas_price = int (gas_price )
6768 gas_limit = int (gas_limit )
68- value = str ( value or "0" )
69+ value = value or 0
6970
7071 tx = Transaction ()
7172 tx .nonce = caller .nonce
72- tx .value = value
73+ tx .value = str ( value )
7374 tx .sender = caller .address .bech32 ()
7475 tx .receiver = self .address .bech32 ()
7576 tx .gasPrice = gas_price
@@ -81,25 +82,25 @@ def execute(self, caller, function, arguments, gas_price, gas_limit, value, chai
8182 tx .sign (caller )
8283 return tx
8384
84- def prepare_execute_transaction_data (self , function , arguments ):
85+ def prepare_execute_transaction_data (self , function : str , arguments : List [ Any ] ):
8586 tx_data = function
8687
8788 for arg in arguments :
8889 tx_data += f"@{ _prepare_argument (arg )} "
8990
9091 return tx_data
9192
92- def upgrade (self , owner , arguments , gas_price , gas_limit , value , chain , version ) -> Transaction :
93+ def upgrade (self , owner : Account , arguments : List [ Any ] , gas_price : int , gas_limit : int , value : int , chain : str , version : int ) -> Transaction :
9394 self .owner = owner
9495
9596 arguments = arguments or []
9697 gas_price = int (gas_price or config .DEFAULT_GAS_PRICE )
9798 gas_limit = int (gas_limit )
98- value = str ( value or "0" )
99+ value = value or 0
99100
100101 tx = Transaction ()
101102 tx .nonce = owner .nonce
102- tx .value = value
103+ tx .value = str ( value )
103104 tx .sender = owner .address .bech32 ()
104105 tx .receiver = self .address .bech32 ()
105106 tx .gasPrice = gas_price
@@ -111,30 +112,38 @@ def upgrade(self, owner, arguments, gas_price, gas_limit, value, chain, version)
111112 tx .sign (owner )
112113 return tx
113114
114- def prepare_upgrade_transaction_data (self , arguments ):
115+ def prepare_upgrade_transaction_data (self , arguments : List [ Any ] ):
115116 tx_data = f"upgradeContract@{ self .bytecode } @{ self .metadata .to_hex ()} "
116117
117118 for arg in arguments :
118119 tx_data += f"@{ _prepare_argument (arg )} "
119120
120121 return tx_data
121122
122- def query (self , proxy , function , arguments ) -> List [Any ]:
123+ def query (self , proxy : IElrondProxy , function : str , arguments : List [Any ], value : int = 0 , caller : Optional [Address ] = None ) -> List [Any ]:
124+ response_data = self .query_detailed (proxy , function , arguments , value , caller )
125+ return_data = response_data .get ("returnData" , []) or response_data .get ("ReturnData" , [])
126+ return [self ._interpret_return_data (data ) for data in return_data ]
127+
128+ def query_detailed (self , proxy : IElrondProxy , function : str , arguments : List [Any ], value : int = 0 , caller : Optional [Address ] = None ) -> Any :
123129 arguments = arguments or []
124130 prepared_arguments = [_prepare_argument (argument ) for argument in arguments ]
125131
126132 payload = {
127- "ScAddress" : self .address .bech32 (),
128- "FuncName" : function ,
129- "Args" : prepared_arguments
133+ "scAddress" : self .address .bech32 (),
134+ "funcName" : function ,
135+ "args" : prepared_arguments ,
136+ "value" : str (value )
130137 }
131138
139+ if caller :
140+ payload ["caller" ] = caller .bech32 ()
141+
132142 response = proxy .query_contract (payload )
133143 response_data = response .get ("data" , {})
134- return_data = response_data .get ("returnData" , response_data .get ("ReturnData" )) or []
135- return [self ._interpret_return_data (data ) for data in return_data ]
144+ return response_data
136145
137- def _interpret_return_data (self , data ) :
146+ def _interpret_return_data (self , data : Any ) -> Any :
138147 if not data :
139148 return data
140149
@@ -153,7 +162,7 @@ def _interpret_return_data(self, data):
153162 return None
154163
155164
156- def _prepare_argument (argument ):
165+ def _prepare_argument (argument : Any ):
157166 hex_prefix = "0X"
158167 as_string = str (argument ).upper ()
159168
@@ -172,7 +181,7 @@ def _prepare_argument(argument):
172181
173182
174183class CodeMetadata :
175- def __init__ (self , upgradeable = True , payable = False ):
184+ def __init__ (self , upgradeable : bool = True , payable : bool = False ):
176185 self .upgradeable = upgradeable
177186 self .payable = payable
178187
0 commit comments