Skip to content

Commit c64178d

Browse files
committed
format readme
1 parent 662f226 commit c64178d

File tree

1 file changed

+108
-76
lines changed

1 file changed

+108
-76
lines changed

README.md

Lines changed: 108 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Coverage Status](https://img.shields.io/coveralls/ethereum/solc-js.svg?style=flat-square)](https://coveralls.io/r/ethereum/solc-js)
44

55
# solc-js
6+
67
JavaScript bindings for the [Solidity compiler](https://github.com/ethereum/solidity).
78

89
Uses the Emscripten compiled Solidity found in the [solc-bin repository](https://github.com/ethereum/solc-bin).
@@ -35,8 +36,9 @@ mentioned on the command line.
3536
### Usage in Projects
3637

3738
There are two ways to use `solc`:
38-
1) Through a high-level API giving a uniform interface to all compiler versions
39-
2) Through a low-level API giving access to all the compiler interfaces, which depend on the version of the compiler
39+
40+
1. Through a high-level API giving a uniform interface to all compiler versions
41+
2. Through a low-level API giving access to all the compiler interfaces, which depend on the version of the compiler
4042

4143
#### High-level API
4244

@@ -48,81 +50,95 @@ of them are resolved.
4850

4951
Starting 0.5.12 it also accepts an object in place of the callback to supply different kind of callbacks, however only file imports are supported.
5052

51-
*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.
53+
_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.
5254

5355
#### Example usage without the import callback
5456

5557
Example:
58+
5659
```javascript
57-
var solc = require('solc')
60+
var solc = require('solc');
5861

5962
var input = {
60-
language: 'Solidity',
61-
sources: {
62-
'test.sol': {
63-
content: 'contract C { function f() public { } }'
64-
}
65-
},
66-
settings: {
67-
outputSelection: {
68-
'*': {
69-
'*': [ '*' ]
70-
}
71-
}
72-
}
73-
}
74-
75-
var output = JSON.parse(solc.compile(JSON.stringify(input)))
63+
language: 'Solidity',
64+
sources: {
65+
'test.sol': {
66+
content: 'contract C { function f() public { } }'
67+
}
68+
},
69+
settings: {
70+
outputSelection: {
71+
'*': {
72+
'*': ['*']
73+
}
74+
}
75+
}
76+
};
77+
78+
var output = JSON.parse(solc.compile(JSON.stringify(input)));
7679

7780
// `output` here contains the JSON output as specified in the documentation
7881
for (var contractName in output.contracts['test.sol']) {
79-
console.log(contractName + ': ' + output.contracts['test.sol'][contractName].evm.bytecode.object)
82+
console.log(
83+
contractName +
84+
': ' +
85+
output.contracts['test.sol'][contractName].evm.bytecode.object
86+
);
8087
}
8188
```
8289

8390
#### Example usage with import callback
8491

8592
```javascript
86-
var solc = require('solc')
93+
var solc = require('solc');
8794

8895
var input = {
89-
language: 'Solidity',
90-
sources: {
91-
'test.sol': {
92-
content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'
93-
}
94-
},
95-
settings: {
96-
outputSelection: {
97-
'*': {
98-
'*': [ '*' ]
99-
}
100-
}
101-
}
102-
}
103-
104-
function findImports (path) {
105-
if (path === 'lib.sol')
106-
return { contents: 'library L { function f() internal returns (uint) { return 7; } }' }
107-
else
108-
return { error: 'File not found' }
96+
language: 'Solidity',
97+
sources: {
98+
'test.sol': {
99+
content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'
100+
}
101+
},
102+
settings: {
103+
outputSelection: {
104+
'*': {
105+
'*': ['*']
106+
}
107+
}
108+
}
109+
};
110+
111+
function findImports(path) {
112+
if (path === 'lib.sol')
113+
return {
114+
contents:
115+
'library L { function f() internal returns (uint) { return 7; } }'
116+
};
117+
else return { error: 'File not found' };
109118
}
110119

111120
// Current syntax
112-
var output = JSON.parse(solc.compile(JSON.stringify(input), findImports))
121+
var output = JSON.parse(solc.compile(JSON.stringify(input), findImports));
113122

114123
// New syntax (supported from 0.5.12)
115-
var output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }))
124+
var output = JSON.parse(
125+
solc.compile(JSON.stringify(input), { import: findImports })
126+
);
116127

117128
// `output` here contains the JSON output as specified in the documentation
118129
for (var contractName in output.contracts['test.sol']) {
119-
console.log(contractName + ': ' + output.contracts['test.sol'][contractName].evm.bytecode.object)
130+
console.log(
131+
contractName +
132+
': ' +
133+
output.contracts['test.sol'][contractName].evm.bytecode.object
134+
);
120135
}
121136
```
122137

123138
#### Low-level API
124139

125140
The low-level API is as follows:
141+
126142
- `solc.lowlevel.compileSingle`: the original entry point, supports only a single file
127143
- `solc.lowlevel.compileMulti`: this supports multiple files, introduced in 0.1.6
128144
- `solc.lowlevel.compileCallback`: this supports callbacks, introduced in 0.2.1
@@ -139,15 +155,15 @@ To turn off `nodeIntegration`, use the following:
139155

140156
```javascript
141157
new BrowserWindow({
142-
webPreferences: {
143-
nodeIntegration: false
144-
}
145-
})
158+
webPreferences: {
159+
nodeIntegration: false
160+
}
161+
});
146162
```
147163

148164
### Using a Legacy Version
149165

150-
In order to compile contracts using a specific version of Solidity, the `solc.loadRemoteVersion(version, callback)` method is available. This returns a new `solc` object that uses a version of the compiler specified.
166+
In order to compile contracts using a specific version of Solidity, the `solc.loadRemoteVersion(version, callback)` method is available. This returns a new `solc` object that uses a version of the compiler specified.
151167

152168
You can also load the "binary" manually and use `setupMethods` to create the familiar wrapper functions described above:
153169
`var solc = solc.setupMethods(require("/my/local/soljson.js"))`.
@@ -157,16 +173,16 @@ You can also load the "binary" manually and use `setupMethods` to create the fam
157173
By default, the npm version is only created for releases. This prevents people from deploying contracts with non-release versions because they are less stable and harder to verify. If you would like to use the latest development snapshot (at your own risk!), you may use the following example code.
158174

159175
```javascript
160-
var solc = require('solc')
176+
var solc = require('solc');
161177

162178
// getting the development snapshot
163-
solc.loadRemoteVersion('latest', function (err, solcSnapshot) {
164-
if (err) {
165-
// An error was encountered, display and quit
166-
} else {
167-
// NOTE: Use `solcSnapshot` here with the same interface `solc` has
168-
}
169-
})
179+
solc.loadRemoteVersion('latest', function(err, solcSnapshot) {
180+
if (err) {
181+
// An error was encountered, display and quit
182+
} else {
183+
// NOTE: Use `solcSnapshot` here with the same interface `solc` has
184+
}
185+
});
170186
```
171187

172188
### Linking Bytecode
@@ -178,33 +194,42 @@ The `linker` module (`require('solc/linker')`) offers helpers to accomplish this
178194
The `linkBytecode` method provides a simple helper for linking:
179195

180196
```javascript
181-
var linker = require('solc/linker')
197+
var linker = require('solc/linker');
182198

183-
bytecode = linker.linkBytecode(bytecode, { 'MyLibrary': '0x123456...' })
199+
bytecode = linker.linkBytecode(bytecode, { MyLibrary: '0x123456...' });
184200
```
185201

186-
As of Solidity 0.4.11 the compiler supports [standard JSON input and output](https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description) which outputs a *link references* map. This gives a map of library names to offsets in the bytecode to replace the addresses at. It also doesn't have the limitation on library file and contract name lengths.
202+
As of Solidity 0.4.11 the compiler supports [standard JSON input and output](https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description) which outputs a _link references_ map. This gives a map of library names to offsets in the bytecode to replace the addresses at. It also doesn't have the limitation on library file and contract name lengths.
187203

188204
There is a method available in the `linker` module called `findLinkReferences` which can find such link references in bytecode produced by an older compiler:
189205

190206
```javascript
191-
var linker = require('solc/linker')
207+
var linker = require('solc/linker');
192208

193-
var linkReferences = linker.findLinkReferences(bytecode)
209+
var linkReferences = linker.findLinkReferences(bytecode);
194210
```
195211

196212
### Updating the ABI
197213

198-
The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.
214+
The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.
199215

200216
It can be used as:
201-
```javascript
202-
var abi = require('solc/abi')
203217

204-
var inputABI = [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}]
205-
var outputABI = abi.update('0.3.6', inputABI)
218+
```javascript
219+
var abi = require('solc/abi');
220+
221+
var inputABI = [
222+
{
223+
constant: false,
224+
inputs: [],
225+
name: 'hello',
226+
outputs: [{ name: '', type: 'string' }],
227+
payable: false,
228+
type: 'function'
229+
}
230+
];
231+
var outputABI = abi.update('0.3.6', inputABI);
206232
// Output contains: [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":true,"type":"function"},{"type":"fallback","payable":true}]
207-
208233
```
209234

210235
### Formatting old JSON assembly output
@@ -223,28 +248,35 @@ var output = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode)
223248
Add the version of `solc` you want to use into `index.html`:
224249

225250
```html
226-
<script type="text/javascript" src="https://solc-bin.ethereum.org/bin/{{ SOLC VERSION }}.js"></script>
251+
<script
252+
type="text/javascript"
253+
src="https://solc-bin.ethereum.org/bin/{{ SOLC VERSION }}.js"
254+
></script>
227255
```
228256

229257
(Alternatively use `https://solc-bin.ethereum.org/bin/soljson-latest.js` to get the latests version.)
230258

231259
This will load `solc` into the global variable `window.Module`. Then use this inside Javascript as:
232260

233261
```javascript
234-
var wrapper = require('solc/wrapper')
235-
var solc = wrapper(window.Module)
236-
262+
var wrapper = require('solc/wrapper');
263+
var solc = wrapper(window.Module);
237264
```
238265

239266
Or in ES6 syntax:
267+
240268
```javascript
241-
import * as wrapper from 'solc/wrapper'
242-
const solc = wrapper(window.Module)
269+
import * as wrapper from 'solc/wrapper';
270+
const solc = wrapper(window.Module);
243271
```
244272

245273
Alternatively, to iterate the releases, one can load `list.js` from `solc-bin`:
274+
246275
```html
247-
<script type="text/javascript" src="https://solc-bin.ethereum.org/bin/list.js"></script>
276+
<script
277+
type="text/javascript"
278+
src="https://solc-bin.ethereum.org/bin/list.js"
279+
></script>
248280
```
249281

250282
This will result in two global variables, `window.soljsonReleases` listing all releases and `window.soljsonSources` listing all nightly builds and releases.

0 commit comments

Comments
 (0)