Skip to content

Commit 1c38832

Browse files
authored
Merge pull request #310 from ethereum/readme-rewrite
Rewrite usage section of the readme
2 parents 8fce0fd + a2cf939 commit 1c38832

File tree

1 file changed

+59
-72
lines changed

1 file changed

+59
-72
lines changed

README.md

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,108 +31,95 @@ used in combination with an Ethereum client via the `eth.compile.solidity()` RPC
3131

3232
### Usage in Projects
3333

34-
#### From version 0.5.0
34+
There are two ways to use `solc`:
35+
1) Through a high-level API giving a uniform interface to all compiler versions
36+
2) Through a low-level API giving access to all the compiler interfaces, which depend on the version of the compiler
3537

36-
Starting from version 0.5.0, `compile`, `compileStandard` and `compileStandardWrapper` all do the same thing - what `compileStandardWrapper` used to do.
38+
#### High-level API
3739

38-
*Note*: with 0.5.2, `compileStandard` and `compileStandardWrapper` will be removed.
40+
The high-level API consists of a single method, `compile`, which expects the [Compiler Standard Input and Output JSON](https://solidity.readthedocs.io/en/v0.5.0/using-the-compiler.html#compiler-input-and-output-json-description).
3941

40-
Starting from version 0.5.0 the low-level functions are also exposed:
41-
- `solc.lowlevel.compileSingle`: the original entry point, supports only a single file
42-
- `solc.lowlevel.compileMulti`: this supports multiple files, introduced in 0.1.6
43-
- `solc.lowlevel.compileCallback`: this supports callbacks, introduced in 0.2.1
44-
- `solc.lowlevel.compileStandard`: this supports the Standard JSON input and output interface, introduced in 0.4.11
42+
It also accepts an optional callback function to resolve unmet dependencies. This callback receives a path and must synchronously return either an error or the content of the dependency as a string.
43+
It cannot be used together with callback-based, asynchronous, filesystem access. A workaround is to collect the names of dependencies, return an error, and keep re-running the compiler until all
44+
of them are resolved.
45+
46+
*Note*: as an intermittent backwards compatibility feature, between versions 0.5.0 and 0.5.2, `compileStandard` and `compileStandardWrapper` also exists and behave like `compile` does.
47+
48+
#### Example usage without the import callback
4549

4650
Example:
4751
```javascript
4852
var solc = require('solc')
53+
4954
var input = {
50-
'lib.sol': 'library L { function f() returns (uint) { return 7; } }',
51-
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
55+
language: 'Solidity',
56+
sources: {
57+
'test.sol': {
58+
content: 'contract C { function f() public { } }'
59+
}
60+
},
61+
settings: {
62+
outputSelection: {
63+
'*': {
64+
'*': [ '*' ]
65+
}
66+
}
67+
}
5268
}
53-
var output = JSON.parse(solc.lowlevel.compileMulti(JSON.stringify({ sources: input }), 1))
54-
for (var contractName in output.contracts)
55-
console.log(contractName + ': ' + output.contracts[contractName].bytecode)
56-
```
57-
58-
#### From version 0.4.20
59-
60-
Starting from version 0.4.20 a Semver compatible version number can be retrieved on every compiler release, including old ones, using the `semver()` method.
6169

62-
#### From version 0.4.11
70+
var output = JSON.parse(solc.compile(JSON.stringify(input)))
6371

64-
Starting from version 0.4.11 there is a new entry point named `compileStandardWrapper()` which supports Solidity's [standard JSON input and output](https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description). It also maps old compiler output to it.
65-
66-
```javascript
67-
var solc = require('solc')
68-
69-
// 'input' is a JSON string corresponding to the "standard JSON input" as described in the link above
70-
// 'findImports' works as described above
71-
var output = solc.compileStandardWrapper(input, findImports)
72-
// Ouput is a JSON string corresponding to the "standard JSON output"
72+
// `output` here contains the JSON output as specified in the documentation
73+
for (var contractName in output.contracts['test.sol']) {
74+
console.log(contractName + ': ' + output.contracts['test.sol'][contractName].evm.bytecode.object)
75+
}
7376
```
7477

75-
There is also a direct method, `compileStandard`, which is only present on recent compilers and works the same way. `compileStandardWrapper` is preferred however because it provides the same interface for old compilers.
76-
77-
#### From version 0.2.1
78-
79-
**Not available since 0.5.0**
80-
81-
Starting from version 0.2.1, a callback is supported to resolve missing imports as follows:
78+
#### Example usage with import callback
8279

8380
```javascript
8481
var solc = require('solc')
82+
8583
var input = {
86-
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
84+
language: 'Solidity',
85+
sources: {
86+
'test.sol': {
87+
content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'
88+
}
89+
},
90+
settings: {
91+
outputSelection: {
92+
'*': {
93+
'*': [ '*' ]
94+
}
95+
}
96+
}
8797
}
98+
8899
function findImports (path) {
89100
if (path === 'lib.sol')
90-
return { contents: 'library L { function f() returns (uint) { return 7; } }' }
101+
return { contents: 'library L { function f() internal returns (uint) { return 7; } }' }
91102
else
92103
return { error: 'File not found' }
93104
}
94-
var output = solc.compile({ sources: input }, 1, findImports)
95-
for (var contractName in output.contracts)
96-
console.log(contractName + ': ' + output.contracts[contractName].bytecode)
97-
```
98-
99-
The `compile()` method always returns an object, which can contain `errors`, `sources` and `contracts` fields. `errors` is a list of error mesages.
100-
101-
#### From version 0.1.6
102105

103-
**Not available since 0.5.0**
106+
var output = JSON.parse(solc.compile(JSON.stringify(input), findImports))
104107

105-
Starting from version 0.1.6, multiple files are supported with automatic import resolution by the compiler as follows:
106-
107-
```javascript
108-
var solc = require('solc')
109-
var input = {
110-
'lib.sol': 'library L { function f() returns (uint) { return 7; } }',
111-
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
108+
// `output` here contains the JSON output as specified in the documentation
109+
for (var contractName in output.contracts['test.sol']) {
110+
console.log(contractName + ': ' + output.contracts['test.sol'][contractName].evm.bytecode.object)
112111
}
113-
var output = solc.compile({ sources: input }, 1)
114-
for (var contractName in output.contracts)
115-
console.log(contractName + ': ' + output.contracts[contractName].bytecode)
116112
```
117113

118-
Note that all input files that are imported have to be supplied, the compiler will not load any additional files on its own.
114+
#### Low-level API
119115

116+
The low-level API is as follows:
117+
- `solc.lowlevel.compileSingle`: the original entry point, supports only a single file
118+
- `solc.lowlevel.compileMulti`: this supports multiple files, introduced in 0.1.6
119+
- `solc.lowlevel.compileCallback`: this supports callbacks, introduced in 0.2.1
120+
- `solc.lowlevel.compileStandard`: this works just like `compile` above, but is only present in compilers after (and including) 0.4.11
120121

121-
#### From early versions
122-
123-
It can also be included and used in other projects:
124-
125-
```javascript
126-
var solc = require('solc')
127-
var input = 'contract x { function g() {} }'
128-
// Setting 1 as second paramater activates the optimiser
129-
var output = solc.compile(input, 1)
130-
for (var contractName in output.contracts) {
131-
// code and ABI that are needed by web3
132-
console.log(contractName + ': ' + output.contracts[contractName].bytecode)
133-
console.log(contractName + '; ' + JSON.parse(output.contracts[contractName].interface))
134-
}
135-
```
122+
For examples how to use them, please refer to the README of the above mentioned solc-js releases.
136123

137124
### Using with Electron
138125

0 commit comments

Comments
 (0)