5757config_string = ':info'
5858# configure_logging(config_string=config_string)
5959
60+ class TransactionFailed (Exception ):
61+ pass
62+
63+
6064class ABIContract (object ): # pylint: disable=too-few-public-methods
6165
6266 def __init__ (self , _chain , _abi , address ):
@@ -70,20 +74,23 @@ def __init__(self, _chain, _abi, address):
7074 self .translator = abi_translator
7175
7276 for function_name in self .translator .function_data :
73- function = self .method_factory (_chain , function_name )
77+ if self .translator .function_data [function_name ]['is_constant' ]:
78+ function = self .method_factory (_chain .call , function_name )
79+ else :
80+ function = self .method_factory (_chain .tx , function_name )
7481 method = types .MethodType (function , self )
7582 setattr (self , function_name , method )
7683
7784 @staticmethod
78- def method_factory (test_chain , function_name ):
85+ def method_factory (tx_or_call , function_name ):
7986 """ Return a proxy for calling a contract method with automatic encoding of
8087 argument and decoding of results.
8188 """
8289
8390 def kall (self , * args , ** kwargs ):
8491 key = kwargs .get ('sender' , k0 )
8592
86- result = test_chain . tx ( # pylint: disable=protected-access
93+ result = tx_or_call ( # pylint: disable=protected-access
8794 sender = key ,
8895 to = self .address ,
8996 value = kwargs .get ('value' , 0 ),
@@ -121,9 +128,19 @@ def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, g
121128 success , output = apply_transaction (self .head_state , transaction )
122129 self .block .transactions .append (transaction )
123130 if not success :
124- return False
131+ raise TransactionFailed ()
125132 return output
126133
134+ def call (self , sender = k0 , to = b'\x00 ' * 20 , value = 0 , data = b'' , startgas = STARTGAS , gasprice = GASPRICE ):
135+ snapshot = self .snapshot ()
136+ try :
137+ output = self .tx (sender , to , value , data , startgas , gasprice )
138+ self .revert (snapshot )
139+ return output
140+ except Exception as e :
141+ self .revert (snapshot )
142+ raise e
143+
127144 def contract (self , sourcecode , args = [], sender = k0 , value = 0 , language = 'evm' , startgas = STARTGAS , gasprice = GASPRICE ):
128145 if language == 'evm' :
129146 assert len (args ) == 0
0 commit comments