Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 9d65cb4

Browse files
committed
add: signature from abi to tester funcs
1 parent 7519e7c commit 9d65cb4

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

ethereum/abi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, full_signature):
2020
full_signature = json_decode(full_signature)
2121
for sig_item in full_signature:
2222
encode_types = [f['type'] for f in sig_item['inputs']]
23+
signature = [(f['type'], f['name']) for f in sig_item['inputs']]
2324
name = sig_item['name']
2425
if '(' in name:
2526
name = name[:name.find('(')]
@@ -41,7 +42,8 @@ def __init__(self, full_signature):
4142
"prefix": prefix,
4243
"encode_types": encode_types,
4344
"decode_types": decode_types,
44-
"is_unknown_type": is_unknown_type
45+
"is_unknown_type": is_unknown_type,
46+
"signature": signature
4547
}
4648
elif sig_item['type'] == 'event':
4749
prefix = big_endian_to_int(utils.sha3(sig))

ethereum/tester.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from ethereum.slogging import LogRecorder, configure_logging, set_level
1010
from ethereum.utils import to_string
1111
from ethereum._solidity import get_solidity
12+
from ethereum import slogging
1213
import rlp
13-
from rlp.utils import decode_hex, encode_hex, ascii_chr
14+
from rlp.utils import ascii_chr
1415

1516
serpent = None
1617

@@ -141,8 +142,24 @@ def kall(*args, **kwargs):
141142
return outdata
142143
return kall
143144

144-
for f in self._translator.function_data:
145-
vars(self)[f] = kall_factory(f)
145+
for fname in self._translator.function_data:
146+
func = kall_factory(fname)
147+
# create wrapper with signature
148+
# in IPython: mycontract.yeah?
149+
# Definition: mycontract.yeah(number, peers, really, **kargs)
150+
# Docstring: yeah(uint32 number, uint256 peers, bool really)
151+
signature = self._translator.function_data[fname]['signature']
152+
fsig = ', '.join(name for typ, name in signature)
153+
if fsig:
154+
fsig += ', '
155+
wfunc = 'def %s(%s**kargs): return func(%s**kargs)' % (fname, fsig, fsig)
156+
wfunc_code = compile(wfunc, 'na', 'exec')
157+
fakeglobals = {}
158+
eval(wfunc_code, {'func': func}, fakeglobals) # evil, use in tester only!
159+
func = fakeglobals[fname]
160+
func.__doc__ = '%s(%s)' % (fname, ', '.join(('%s %s' % x) for x in signature))
161+
162+
setattr(self, fname, func)
146163

147164
return _abi_contract(me, code, sender, endowment, language)
148165

0 commit comments

Comments
 (0)