@@ -32,43 +32,27 @@ def is_exe(fpath):
32
32
33
33
return None
34
34
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
-
51
35
@classmethod
52
36
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 )
59
38
60
39
@classmethod
61
- def compile (cls , code , contract_name = '' ):
40
+ def compile (cls , code , libraries = None , contract_name = '' ):
62
41
"returns binary of last contract in code"
63
42
sorted_contracts = cls .combined (code )
64
43
if contract_name :
65
44
idx = [x [0 ] for x in sorted_contracts ].index (contract_name )
66
45
else :
67
46
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 )
68
52
return sorted_contracts [idx ][1 ]['bin' ].decode ('hex' )
69
53
70
54
@classmethod
71
- def mk_full_signature (cls , code , contract_name = '' ):
55
+ def mk_full_signature (cls , code , libraries = None , contract_name = '' ):
72
56
"returns signature of last contract in code"
73
57
sorted_contracts = cls .combined (code )
74
58
if contract_name :
@@ -86,7 +70,6 @@ def combined(cls, code):
86
70
raise CompileError ('compilation failed' )
87
71
# contracts = json.loads(stdoutdata)['contracts']
88
72
contracts = yaml .safe_load (stdoutdata )['contracts' ]
89
-
90
73
for contract_name , data in contracts .items ():
91
74
data ['abi' ] = yaml .safe_load (data ['abi' ])
92
75
data ['devdoc' ] = yaml .safe_load (data ['devdoc' ])
@@ -96,8 +79,7 @@ def combined(cls, code):
96
79
assert len (names ) <= len (contracts ) # imported contracts are not returned
97
80
sorted_contracts = []
98
81
for name in names :
99
- sorted_contracts .append ((name , contracts [name ]))
100
-
82
+ sorted_contracts .append ((name [1 ], contracts [name [1 ]]))
101
83
return sorted_contracts
102
84
103
85
@classmethod
@@ -146,7 +128,6 @@ def get_solidity(try_import=False):
146
128
assert 'solidity' in tester .languages
147
129
148
130
one_contract = """
149
-
150
131
contract foo {
151
132
function seven() returns (int256 y) {
152
133
y = 7;
@@ -178,21 +159,3 @@ def get_solidity(try_import=False):
178
159
assert c1 .seven () == 7
179
160
assert c1 .mul2 (2 ) == 4
180
161
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
0 commit comments