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

Commit be56376

Browse files
committed
Merge pull request #296 from Georgi87/develop
added solidity library support to solidity wrapper
2 parents 5687f29 + 8d4cf53 commit be56376

File tree

2 files changed

+12
-54
lines changed

2 files changed

+12
-54
lines changed

ethereum/_solidity.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,27 @@ 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+
if cls.compiler_version() < "0.1.2":
49+
raise CompileError('Compiler does not support libraries. Please update compiler.')
50+
for lib_name, lib_address in libraries.iteritems():
51+
sorted_contracts[idx][1]['bin'] = sorted_contracts[idx][1]['bin'].replace("__{}{}".format(lib_name, "_" * (38-len(lib_name))), lib_address)
6852
return sorted_contracts[idx][1]['bin'].decode('hex')
6953

7054
@classmethod
71-
def mk_full_signature(cls, code, contract_name=''):
55+
def mk_full_signature(cls, code, libraries=None, contract_name=''):
7256
"returns signature of last contract in code"
7357
sorted_contracts = cls.combined(code)
7458
if contract_name:
@@ -86,7 +70,6 @@ def combined(cls, code):
8670
raise CompileError('compilation failed')
8771
# contracts = json.loads(stdoutdata)['contracts']
8872
contracts = yaml.safe_load(stdoutdata)['contracts']
89-
9073
for contract_name, data in contracts.items():
9174
data['abi'] = yaml.safe_load(data['abi'])
9275
data['devdoc'] = yaml.safe_load(data['devdoc'])
@@ -96,8 +79,7 @@ def combined(cls, code):
9679
assert len(names) <= len(contracts) # imported contracts are not returned
9780
sorted_contracts = []
9881
for name in names:
99-
sorted_contracts.append((name, contracts[name]))
100-
82+
sorted_contracts.append((name[1], contracts[name[1]]))
10183
return sorted_contracts
10284

10385
@classmethod
@@ -146,7 +128,6 @@ def get_solidity(try_import=False):
146128
assert 'solidity' in tester.languages
147129

148130
one_contract = """
149-
150131
contract foo {
151132
function seven() returns (int256 y) {
152133
y = 7;
@@ -178,21 +159,3 @@ def get_solidity(try_import=False):
178159
assert c1.seven() == 7
179160
assert c1.mul2(2) == 4
180161
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
@@ -162,20 +162,15 @@ def contract(self, code, sender=k0, endowment=0, language='serpent', gas=None):
162162
assert len(self.block.get_code(o)), "Contract code empty"
163163
return o
164164

165-
def abi_contract(self, code, sender=k0, endowment=0, language='serpent', contract_name='',
165+
def abi_contract(self, code, sender=k0, endowment=0, language='serpent',
166166
gas=None, log_listener=None, listen=True, **kwargs):
167-
if contract_name:
168-
assert language == 'solidity'
169-
cn_args = dict(contract_name=contract_name)
170-
else:
171-
cn_args = kwargs
172167
if language not in languages:
173168
languages[language] = __import__(language)
174169
language = languages[language]
175-
evm = language.compile(code, **cn_args)
170+
evm = language.compile(code, **kwargs)
176171
address = self.evm(evm, sender, endowment, gas)
177172
assert len(self.block.get_code(address)), "Contract code empty"
178-
_abi = language.mk_full_signature(code, **cn_args)
173+
_abi = language.mk_full_signature(code, **kwargs)
179174
return ABIContract(self, _abi, address, listen=listen, log_listener=log_listener)
180175

181176
def clear_listeners(self):

0 commit comments

Comments
 (0)