56
56
config_string = ':info'
57
57
# configure_logging(config_string=config_string)
58
58
59
+ class TransactionFailed (Exception ):
60
+ pass
61
+
62
+
59
63
class ABIContract (object ): # pylint: disable=too-few-public-methods
60
64
61
65
def __init__ (self , _chain , _abi , address ):
@@ -69,20 +73,23 @@ def __init__(self, _chain, _abi, address):
69
73
self .translator = abi_translator
70
74
71
75
for function_name in self .translator .function_data :
72
- function = self .method_factory (_chain , function_name )
76
+ if self .translator .function_data [function_name ]['is_constant' ]:
77
+ function = self .method_factory (_chain .call , function_name )
78
+ else :
79
+ function = self .method_factory (_chain .tx , function_name )
73
80
method = types .MethodType (function , self )
74
81
setattr (self , function_name , method )
75
82
76
83
@staticmethod
77
- def method_factory (test_chain , function_name ):
84
+ def method_factory (tx_or_call , function_name ):
78
85
""" Return a proxy for calling a contract method with automatic encoding of
79
86
argument and decoding of results.
80
87
"""
81
88
82
89
def kall (self , * args , ** kwargs ):
83
90
key = kwargs .get ('sender' , k0 )
84
91
85
- result = test_chain . tx ( # pylint: disable=protected-access
92
+ result = tx_or_call ( # pylint: disable=protected-access
86
93
sender = key ,
87
94
to = self .address ,
88
95
value = kwargs .get ('value' , 0 ),
@@ -119,9 +126,19 @@ def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, g
119
126
success , output = apply_transaction (self .head_state , transaction )
120
127
self .block .transactions .append (transaction )
121
128
if not success :
122
- return False
129
+ raise TransactionFailed ()
123
130
return output
124
131
132
+ def call (self , sender = k0 , to = b'\x00 ' * 20 , value = 0 , data = b'' , startgas = STARTGAS , gasprice = GASPRICE ):
133
+ snapshot = self .snapshot ()
134
+ try :
135
+ output = self .tx (sender , to , value , data , startgas , gasprice )
136
+ self .revert (snapshot )
137
+ return output
138
+ except Exception as e :
139
+ self .revert (snapshot )
140
+ raise e
141
+
125
142
def contract (self , sourcecode , args = [], sender = k0 , value = 0 , language = 'evm' , startgas = STARTGAS , gasprice = GASPRICE ):
126
143
if language == 'evm' :
127
144
assert len (args ) == 0
0 commit comments