You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
13
13
@@ -17,9 +17,28 @@ Note: this `README` reflects the state of the library from `v2.0.0` onwards. See
17
17
18
18
# USAGE
19
19
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`\
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 =newCommon({ chain: 'mainnet', hardfork: 'london' })
39
+
// With enums:
40
+
const common =newCommon({ chain: Chain.Mainnet, hardfork: Hardfork.London })
41
+
```
23
42
24
43
If no hardfork is provided, the common is initialized with the default hardfork.
25
44
@@ -28,24 +47,22 @@ Current `DEFAULT_HARDFORK`: `istanbul`
28
47
Here are some simple usage examples:
29
48
30
49
```typescript
31
-
importCommonfrom'@ethereumjs/common'
32
-
33
50
// Instantiate with the chain (and the default hardfork)
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({
57
74
})
58
75
```
59
76
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.
62
79
63
80
For an improved developer experience, there are `Chain` and `Hardfork` enums available:
64
81
@@ -86,8 +103,8 @@ to ease `blockNumber` based access to parameters.
86
103
The `Common` class is implemented as an `EventEmitter` and is emitting the following events
|`hardforkChanged`| Emitted when a hardfork change occurs in the Common object |
92
109
93
110
# SETUP
@@ -163,11 +180,10 @@ The following custom chains are currently supported:
163
180
164
181
`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).
165
182
166
-
167
183
#### Activate with a single custom Chain setup
168
184
169
185
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
171
187
values to the constructor using the `chain` parameter or the `setChain()` method, here is some example:
172
188
173
189
```typescript
@@ -179,39 +195,57 @@ const common = new Common({ chain: myCustomChain })
179
195
180
196
A second way for custom chain initialization is to use the `customChains` constructor option. This
181
197
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
185
201
(e.g. `mainnet`) and switch to other chains - including the custom ones - by using `Common.setChain()`.
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:
0 commit comments