Skip to content

Commit 0b95204

Browse files
authored
common: add require examples to readme (#1837)
* add require usage examples, refine parameters section * lint * improve example order
1 parent f91195f commit 0b95204

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

packages/common/README.md

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Discord][discord-badge]][discord-link]
88

99
| Resources common to all EthereumJS implementations. |
10-
| --- |
10+
| --------------------------------------------------- |
1111

1212
Note: this `README` reflects the state of the library from `v2.0.0` onwards. See `README` from the [standalone repository](https://github.com/ethereumjs/ethereumjs-common) for an introduction on the last preceding release.
1313

@@ -17,9 +17,28 @@ Note: this `README` reflects the state of the library from `v2.0.0` onwards. See
1717

1818
# USAGE
1919

20-
All parameters can be accessed through the `Common` class which can be required through the
21-
main package and instantiated either with just the `chain` (e.g. 'mainnet') or the `chain`
22-
together with a specific `hardfork` provided.
20+
## import / require
21+
22+
import (CommonJS, TypeScript with `esModuleInterop` enabled):
23+
24+
`import Common from '@ethereumjs/common`\
25+
`import Common, { Chain, Hardfork } from '@ethereumjs/common`
26+
27+
require (ES Modules, Node.js):
28+
29+
`const Common = require('@ethereumjs/common').default`\
30+
`const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')`
31+
32+
## Parameters
33+
34+
All parameters can be accessed through the `Common` class, instantiated with an object containing either the `chain` (e.g. 'mainnet') or the `chain` together with a specific `hardfork` provided:
35+
36+
```typescript
37+
// With strings:
38+
const common = new Common({ chain: 'mainnet', hardfork: 'london' })
39+
// With enums:
40+
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
41+
```
2342

2443
If no hardfork is provided, the common is initialized with the default hardfork.
2544

@@ -28,24 +47,22 @@ Current `DEFAULT_HARDFORK`: `istanbul`
2847
Here are some simple usage examples:
2948

3049
```typescript
31-
import Common from '@ethereumjs/common'
32-
3350
// Instantiate with the chain (and the default hardfork)
34-
const c = new Common({ chain: 'ropsten' })
51+
let c = new Common({ chain: 'ropsten' })
3552
c.param('gasPrices', 'ecAddGas') // 500
3653

3754
// Chain and hardfork provided
3855
c = new Common({ chain: 'ropsten', hardfork: 'byzantium' })
3956
c.param('pow', 'minerReward') // 3000000000000000000
4057

41-
// Instantiate with an EIP activated
42-
const c = new Common({ chain: 'mainnet', eips: [2537] })
43-
4458
// Access genesis data for Ropsten network
4559
c.genesis().hash // 0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d
4660

4761
// Get bootstrap nodes for chain/network
4862
c.bootstrapNodes() // Array with current nodes
63+
64+
// Instantiate with an EIP activated
65+
c = new Common({ chain: 'mainnet', eips: [2537] })
4966
```
5067

5168
If the initializing library only supports a certain range of `hardforks` you can use the `supportedHardforks` option to restrict hardfork access on the `Common` instance:
@@ -57,8 +74,8 @@ const c = new Common({
5774
})
5875
```
5976

60-
This will e.g. throw an error when a param is requested for an unsupported hardfork and
61-
like this prevents unpredicted behaviour.
77+
This will throw an error when a param is requested for an unsupported hardfork
78+
to prevent unpredictable behavior.
6279

6380
For an improved developer experience, there are `Chain` and `Hardfork` enums available:
6481

@@ -86,8 +103,8 @@ to ease `blockNumber` based access to parameters.
86103
The `Common` class is implemented as an `EventEmitter` and is emitting the following events
87104
on which you can react within your code:
88105

89-
| Event | Description |
90-
| - | - |
106+
| Event | Description |
107+
| ----------------- | ---------------------------------------------------------- |
91108
| `hardforkChanged` | Emitted when a hardfork change occurs in the Common object |
92109

93110
# SETUP
@@ -163,11 +180,10 @@ The following custom chains are currently supported:
163180

164181
`Common` instances created with this simplified `custom()` constructor can't be used in all usage contexts (the HF configuration is very likely not matching the actual chain) but can be useful for specific use cases, e.g. for sending a tx with `@ethereumjs/tx` to an L2 network (see the `Tx` library [README](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx) for a complete usage example).
165182

166-
167183
#### Activate with a single custom Chain setup
168184

169185
If you want to initialize a `Common` instance with a single custom chain which is then directly activated
170-
you can pass a dictionary - conforming to the parameter format described above - with your custom chain
186+
you can pass a dictionary - conforming to the parameter format described above - with your custom chain
171187
values to the constructor using the `chain` parameter or the `setChain()` method, here is some example:
172188

173189
```typescript
@@ -179,39 +195,57 @@ const common = new Common({ chain: myCustomChain })
179195

180196
A second way for custom chain initialization is to use the `customChains` constructor option. This
181197
option comes with more flexibility and allows for an arbitrary number of custom chains to be initialized on
182-
a common instance in addition to the already supported ones. It also allows for an activation-independent
183-
initialization, so you can add your chains by adding to the `customChains` array and either directly
184-
use the `chain` option to activate one of the custom chains passed or activate a build in chain
198+
a common instance in addition to the already supported ones. It also allows for an activation-independent
199+
initialization, so you can add your chains by adding to the `customChains` array and either directly
200+
use the `chain` option to activate one of the custom chains passed or activate a build in chain
185201
(e.g. `mainnet`) and switch to other chains - including the custom ones - by using `Common.setChain()`.
186202

187203
```typescript
188204
import myCustomChain1 from './[PATH]/myCustomChain1.json'
189205
import myCustomChain2 from './[PATH]/myCustomChain2.json'
190206
// Add two custom chains, initial mainnet activation
191-
const common1 = new Common({ chain: 'mainnet', customChains: [ myCustomChain1, myCustomChain2 ] })
207+
const common1 = new Common({ chain: 'mainnet', customChains: [myCustomChain1, myCustomChain2] })
192208
// Somewhat later down the road...
193209
common1.setChain('customChain1')
194210
// Add two custom chains, activate customChain1
195-
const common1 = new Common({ chain: 'customChain1', customChains: [ myCustomChain1, myCustomChain2 ] })
211+
const common1 = new Common({
212+
chain: 'customChain1',
213+
customChains: [myCustomChain1, myCustomChain2],
214+
})
196215
```
197216

198217
It is also possible (`v2.5.0`+) to pass in a custom genesis state file (see e.g. `src/genesisStates/goerli.json` for an example on the format needed) along with the custom chain configuration:
199218

200219
```typescript
201220
import myCustomChain1 from '[PATH_TO_MY_CHAINS]/myCustomChain1.json'
202221
import chain1GenesisState from '[PATH_TO_GENESIS_STATES]/chain1GenesisState.json'
203-
const common = new Common({ chain: 'myCustomChain1', customChains: [ [ myCustomChain1, chain1GenesisState ] ]})
222+
const common = new Common({
223+
chain: 'myCustomChain1',
224+
customChains: [[myCustomChain1, chain1GenesisState]],
225+
})
204226
```
205227

206228
A more complex example with genesis state with Contract and EoA states would have the following format:
207229

208230
```typescript
209231
const complexState = {
210-
'0x0...01': '0x100', // For EoA
211-
'0x0...02': ['0x1', '0xRUNTIME_BYTECODE', [[ keyOne, valueOne ], [ keyTwo, valueTwo ]]] // For contracts
232+
// For EoA
233+
'0x0...01': '0x100',
234+
// For contracts
235+
'0x0...02': [
236+
'0x1',
237+
'0xRUNTIME_BYTECODE',
238+
[
239+
[key1, value1],
240+
[key2, value2],
241+
],
242+
],
212243
}
213244
import myCustomChain1 from '[PATH_TO_MY_CHAINS]/myCustomChain1.json'
214-
const common = new Common({ chain: 'myCustomChain1', customChains: [ [ myCustomChain1, complexState ] ]})
245+
const common = new Common({
246+
chain: 'myCustomChain1',
247+
customChains: [[myCustomChain1, complexState]],
248+
})
215249
```
216250

217251
Accessing the genesis state can be done as follows:
@@ -254,7 +288,7 @@ library supported:
254288
- `byzantium` (`Hardfork.Byzantium`)
255289
- `constantinople` (`Hardfork.Constantinople`)
256290
- `petersburg` (`Hardfork.Petersburg`) (aka `constantinopleFix`, apply together with `constantinople`)
257-
- `istanbul` (`Hardfork.Instanbul`) (`DEFAULT_HARDFORK` (`v2.0.0` release series))
291+
- `istanbul` (`Hardfork.Istanbul`) (`DEFAULT_HARDFORK` (`v2.0.0` release series))
258292
- `muirGlacier` (`Hardfork.MuirGlacier`)
259293
- `berlin` (`Hardfork.Berlin`) (since `v2.2.0`)
260294
- `london` (`Hardfork.London`) (since `v2.4.0`)

0 commit comments

Comments
 (0)