Skip to content

Commit 9f18c98

Browse files
authored
Merge pull request #2 from pipermerriam/piper/add-linking
Add code linking
2 parents 5d9437a + 57713ba commit 9f18c98

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pip install py-solc
2222
```
2323

2424
```python
25-
>>> from solc import compile_source, compile_files
25+
>>> from solc import compile_source, compile_files, link_code
2626
>>> compile_source("contract Foo { function Foo() {} }")
2727
{
2828
'Foo': {
@@ -62,4 +62,7 @@ pip install py-solc
6262
},
6363
},
6464
}
65+
>>> unlinked_code = "606060405260768060106000396000f3606060405260e060020a6000350463e7f09e058114601a575b005b60187f0c55699c00000000000000000000000000000000000000000000000000000000606090815273__TestA_________________________________90630c55699c906064906000906004818660325a03f41560025750505056"
66+
>>> link_code(unlinked_code, {'TestA': '0xd3cda913deb6f67967b99d67acdfa1712c293601'})
67+
... "606060405260768060106000396000f3606060405260e060020a6000350463e7f09e058114601a575b005b60187f0c55699c00000000000000000000000000000000000000000000000000000000606090815273d3cda913deb6f67967b99d67acdfa1712c29360190630c55699c906064906000906004818660325a03f41560025750505056"
6568
```

solc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
get_solc_version,
55
compile_files,
66
compile_source,
7+
link_code,
78
)

solc/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@ def compile_files(source_files, output_values=ALL_OUTPUT_VALUES, **kwargs):
121121

122122
contracts = _parse_compiler_output(stdoutdata)
123123
return contracts
124+
125+
126+
def link_code(unliked_data, libraries):
127+
libraries_arg = ','.join((
128+
':'.join((lib_name, lib_address))
129+
for lib_name, lib_address in libraries.items()
130+
))
131+
stdoutdata, stderrdata = solc_wrapper(
132+
stdin_bytes=unliked_data,
133+
link=True,
134+
libraries=libraries_arg,
135+
)
136+
137+
return stdoutdata.strip()

solc/wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def solc_wrapper(solc_binary=SOLC_BINARY,
6161
if optimize_runs is not None:
6262
command.extend(('--optimize-runs', str(optimize_runs)))
6363

64+
if link:
65+
command.append('--link')
66+
6467
if libraries is not None:
6568
command.extend(('--libraries', libraries))
6669

@@ -79,9 +82,6 @@ def solc_wrapper(solc_binary=SOLC_BINARY,
7982
if source_files is not None:
8083
command.extend(source_files)
8184

82-
if link:
83-
command.append('--link')
84-
8585
#
8686
# Output configuration
8787
#
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from solc import link_code
2+
3+
CODE = "6060604052610199806100126000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806344fd4fa01461004f57806358de5f041461005e578063e7f09e051461006d5761004d565b005b61005c600480505061007c565b005b61006b60048050506100db565b005b61007a600480505061013a565b005b73__TestB_________________________________630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b565b73__TestC_________________________________630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b565b73__TestA_________________________________630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b56"
4+
5+
TEST_A_ADDRESS = "0xd3cda913deb6f67967b99d67acdfa1712c293601"
6+
TEST_B_ADDRESS = "0x304a554a310c7e546dfe434669c62820b7d83490"
7+
TEST_C_ADDRESS = "0xbb9bc244d798123fde783fcc1c72d3bb8c189413"
8+
9+
LINKED_CODE = "6060604052610199806100126000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806344fd4fa01461004f57806358de5f041461005e578063e7f09e051461006d5761004d565b005b61005c600480505061007c565b005b61006b60048050506100db565b005b61007a600480505061013a565b005b73304a554a310c7e546dfe434669c62820b7d83490630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b565b73bb9bc244d798123fde783fcc1c72d3bb8c189413630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b565b73d3cda913deb6f67967b99d67acdfa1712c293601630c55699c604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506000604051808303818660325a03f415610002575050505b56"
10+
11+
12+
def test_partial_code_linking():
13+
output = link_code(CODE, {'TestA': TEST_A_ADDRESS})
14+
assert '__TestA__' not in output
15+
assert '__TestB__' in output
16+
assert '__TestC__' in output
17+
assert TEST_A_ADDRESS[2:] in output
18+
19+
20+
def test_full_code_linking():
21+
output = link_code(CODE, {
22+
'TestA': TEST_A_ADDRESS,
23+
'TestB': TEST_B_ADDRESS,
24+
'TestC': TEST_C_ADDRESS,
25+
})
26+
assert '__TestA__' not in output
27+
assert '__TestB__' not in output
28+
assert '__TestC__' not in output
29+
assert TEST_A_ADDRESS[2:] in output
30+
assert TEST_B_ADDRESS[2:] in output
31+
assert TEST_C_ADDRESS[2:] in output
32+
assert output == LINKED_CODE

0 commit comments

Comments
 (0)