57
57
config_string = ':info'
58
58
# configure_logging(config_string=config_string)
59
59
60
+ class TransactionFailed (Exception ):
61
+ pass
62
+
63
+
60
64
class ABIContract (object ): # pylint: disable=too-few-public-methods
61
65
62
66
def __init__ (self , _chain , _abi , address ):
@@ -70,20 +74,23 @@ def __init__(self, _chain, _abi, address):
70
74
self .translator = abi_translator
71
75
72
76
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 )
74
81
method = types .MethodType (function , self )
75
82
setattr (self , function_name , method )
76
83
77
84
@staticmethod
78
- def method_factory (test_chain , function_name ):
85
+ def method_factory (tx_or_call , function_name ):
79
86
""" Return a proxy for calling a contract method with automatic encoding of
80
87
argument and decoding of results.
81
88
"""
82
89
83
90
def kall (self , * args , ** kwargs ):
84
91
key = kwargs .get ('sender' , k0 )
85
92
86
- result = test_chain . tx ( # pylint: disable=protected-access
93
+ result = tx_or_call ( # pylint: disable=protected-access
87
94
sender = key ,
88
95
to = self .address ,
89
96
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
121
128
success , output = apply_transaction (self .head_state , transaction )
122
129
self .block .transactions .append (transaction )
123
130
if not success :
124
- return False
131
+ raise TransactionFailed ()
125
132
return output
126
133
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
+
127
144
def contract (self , sourcecode , args = [], sender = k0 , value = 0 , language = 'evm' , startgas = STARTGAS , gasprice = GASPRICE ):
128
145
if language == 'evm' :
129
146
assert len (args ) == 0
0 commit comments