Skip to content

Commit 2624092

Browse files
committed
Update example in examples folder and README to conform to latest API changes and be more approachable and explicit
1 parent c599278 commit 2624092

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,33 @@ Have a look at the corresponding issue to follow the discussion and current stat
4848
# USAGE
4949

5050
```javascript
51-
var VM = require('ethereumjs-vm')
51+
const BN = require('bn.js')
52+
var VM = require('ethereumjs-vm').default
5253

53-
//create a new VM instance
54-
var vm = new VM()
55-
var code =
56-
'7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'
54+
// Create a new VM instance
55+
// For explicity setting the HF use e.g. `new VM({ hardfork: 'petersburg' })`
56+
const vm = new VM()
57+
58+
const STOP = '00'
59+
const ADD = '01'
60+
const PUSH1 = '60'
61+
62+
// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
63+
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]
64+
65+
vm.on('step', function(data) {
66+
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
67+
})
5768

5869
vm.runCode(
5970
{
60-
code: Buffer.from(code, 'hex'), // code needs to be a Buffer
61-
gasLimit: Buffer.from('ffffffff', 'hex'),
71+
code: Buffer.from(code.join(''), 'hex'),
72+
gasLimit: new BN(0xffff),
6273
},
6374
function(err, results) {
64-
console.log('returned: ' + results.return.toString('hex'))
75+
console.log('Returned : ' + results.return.toString('hex'))
76+
console.log('gasUsed : ' + results.gasUsed.toString())
77+
console.log('Error : ' + err)
6578
},
6679
)
6780
```

examples/run-code-browser/README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1-
## prerequisite
1+
# Build
22

3+
Build the project from the console:
4+
5+
```shell
6+
npm run build:dist
7+
```
8+
9+
This will create a new folder `dist/`.
10+
11+
# Run Example in Node
12+
13+
```shell
14+
node index.js
315
```
16+
17+
# Run Example in a Browser
18+
19+
## Prerequisites
20+
21+
```shell
422
$ npm install -g browserify http-server
523
```
624

725
## Instruction
826

927
Run command
1028

11-
```
29+
```shell
1230
$ browserify index.js -o bundle.js
1331
```
1432

1533
Then host this folder in a web server
1634

17-
```
35+
```shell
1836
$ http-server
1937
```
2038

examples/run-code-browser/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@
77
* and then load this folder onto a HTTP WebServer (e.g.
88
* using node-static or `python -mSimpleHTTPServer`).
99
*/
10-
var Buffer = require('safe-buffer').Buffer // use for Node.js <4.5.0
11-
var VM = require('../../index.js')
10+
const BN = require('bn.js')
11+
const VM = require('../../dist/index.js').default
1212

13-
// create a new VM instance
14-
var vm = new VM()
13+
// Create a new VM instance
14+
// For explicity setting the HF use e.g. `new VM({ hardfork: 'petersburg' })`
15+
const vm = new VM()
1516

16-
var code = '7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'
17+
const STOP = '00'
18+
const ADD = '01'
19+
const PUSH1 = '60'
20+
21+
// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
22+
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]
1723

1824
vm.on('step', function (data) {
19-
console.log(data.opcode.name)
25+
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
2026
})
2127

2228
vm.runCode({
23-
code: Buffer.from(code, 'hex'),
24-
gasLimit: Buffer.from('ffffffff', 'hex')
29+
code: Buffer.from(code.join(''), 'hex'),
30+
gasLimit: new BN(0xffff)
2531
}, function (err, results) {
26-
console.log('returned: ' + results.return.toString('hex'))
27-
console.log('gasUsed: ' + results.gasUsed.toString())
28-
console.log(err)
32+
console.log('Returned : ' + results.return.toString('hex'))
33+
console.log('gasUsed : ' + results.gasUsed.toString())
34+
console.log('Error : ' + err)
2935
})

0 commit comments

Comments
 (0)