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

Commit a23efb1

Browse files
committed
added solidity library support to solidity wrapper
1 parent 06e3c81 commit a23efb1

File tree

2 files changed

+10
-54
lines changed

2 files changed

+10
-54
lines changed

ethereum/_solidity.py

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,25 @@ def is_exe(fpath):
3232

3333
return None
3434

35-
@classmethod
36-
def split_contracts(cls, code):
37-
contracts = []
38-
contract = None
39-
for line in code.split('\n'):
40-
line = line.lstrip()
41-
if line.startswith('contract '): # FIXME
42-
if contract:
43-
contracts.append('\n'.join(contract))
44-
contract = [line]
45-
elif contract:
46-
contract.append(line)
47-
if contract:
48-
contracts.append('\n'.join(contract))
49-
return contracts
50-
5135
@classmethod
5236
def contract_names(cls, code):
53-
names = []
54-
for contract in cls.split_contracts(code):
55-
keyword, name, _ = contract.split(' ', 2)
56-
assert keyword == 'contract' and len(name)
57-
names.append(name)
58-
return names
37+
return re.findall(r'^\s*(contract|library) (\S*) ', code, re.MULTILINE)
5938

6039
@classmethod
61-
def compile(cls, code, contract_name=''):
40+
def compile(cls, code, libraries=None, contract_name=''):
6241
"returns binary of last contract in code"
6342
sorted_contracts = cls.combined(code)
6443
if contract_name:
6544
idx = [x[0] for x in sorted_contracts].index(contract_name)
6645
else:
6746
idx = -1
47+
if libraries:
48+
for lib_name, lib_address in libraries.iteritems():
49+
sorted_contracts[idx][1]['bin'] = sorted_contracts[idx][1]['bin'].replace("__{}{}".format(lib_name, "_" * (38-len(lib_name))), lib_address)
6850
return sorted_contracts[idx][1]['bin'].decode('hex')
6951

7052
@classmethod
71-
def mk_full_signature(cls, code, contract_name=''):
53+
def mk_full_signature(cls, code, libraries=None, contract_name=''):
7254
"returns signature of last contract in code"
7355
sorted_contracts = cls.combined(code)
7456
if contract_name:
@@ -86,7 +68,6 @@ def combined(cls, code):
8668
raise CompileError('compilation failed')
8769
# contracts = json.loads(stdoutdata)['contracts']
8870
contracts = yaml.safe_load(stdoutdata)['contracts']
89-
9071
for contract_name, data in contracts.items():
9172
data['abi'] = yaml.safe_load(data['abi'])
9273
data['devdoc'] = yaml.safe_load(data['devdoc'])
@@ -96,8 +77,7 @@ def combined(cls, code):
9677
assert len(names) <= len(contracts) # imported contracts are not returned
9778
sorted_contracts = []
9879
for name in names:
99-
sorted_contracts.append((name, contracts[name]))
100-
80+
sorted_contracts.append((name[1], contracts[name[1]]))
10181
return sorted_contracts
10282

10383
@classmethod
@@ -146,7 +126,6 @@ def get_solidity(try_import=False):
146126
assert 'solidity' in tester.languages
147127

148128
one_contract = """
149-
150129
contract foo {
151130
function seven() returns (int256 y) {
152131
y = 7;
@@ -178,21 +157,3 @@ def get_solidity(try_import=False):
178157
assert c1.seven() == 7
179158
assert c1.mul2(2) == 4
180159
assert c1.mul2(-2) == -4
181-
182-
two_codes = solc_wrapper.split_contracts(two_contracts)
183-
assert len(two_codes) == 2
184-
185-
for code in two_codes:
186-
bytecode = solc_wrapper.compile(code)
187-
jsonabi = solc_wrapper.mk_full_signature(code)
188-
c = s.abi_contract(code, language='solidity')
189-
190-
c1 = s.abi_contract(two_codes[0], language='solidity')
191-
assert c1.seven() == 7
192-
assert c1.mul2(2) == 4
193-
assert c1.mul2(-2) == -4
194-
195-
c2 = s.abi_contract(two_codes[1], language='solidity')
196-
a = '\0' * 20
197-
assert c2.echo(a).decode('hex') == a
198-
assert c2.eight() == 8

ethereum/tester.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,15 @@ def contract(self, code, sender=k0, endowment=0, language='serpent', gas=None):
151151
assert len(self.block.get_code(o)), "Contract code empty"
152152
return o
153153

154-
def abi_contract(self, code, sender=k0, endowment=0, language='serpent', contract_name='',
154+
def abi_contract(self, code, sender=k0, endowment=0, language='serpent',
155155
gas=None, log_listener=None, listen=True, **kwargs):
156-
if contract_name:
157-
assert language == 'solidity'
158-
cn_args = dict(contract_name=contract_name)
159-
else:
160-
cn_args = kwargs
161156
if language not in languages:
162157
languages[language] = __import__(language)
163158
language = languages[language]
164-
evm = language.compile(code, **cn_args)
159+
evm = language.compile(code, **kwargs)
165160
address = self.evm(evm, sender, endowment, gas)
166161
assert len(self.block.get_code(address)), "Contract code empty"
167-
_abi = language.mk_full_signature(code, **cn_args)
162+
_abi = language.mk_full_signature(code, **kwargs)
168163
return ABIContract(self, _abi, address, listen=listen, log_listener=log_listener)
169164

170165
def evm(self, evm, sender=k0, endowment=0, gas=None):

0 commit comments

Comments
 (0)