forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (31 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Example - Running code on an ethereum-vm
*
*
* To run this example in the browser, bundle this file
* with browserify using `browserify index.js -o bundle.js`
* and then load this folder onto a HTTP WebServer (e.g.
* using node-static or `python -mSimpleHTTPServer`).
*/
const BN = require('bn.js')
const VM = require('../../dist').default
// Create a new VM instance
// To explicity set the chain or hardfork use [Common](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/common#usage)
const vm = new VM()
const STOP = '00'
const ADD = '01'
const PUSH1 = '60'
// Note that numbers added are hex values, so '20' would be '32' as decimal e.g.
const code = [PUSH1, '03', PUSH1, '05', ADD, STOP]
vm.on('step', function (data) {
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
})
vm.runCode({
code: Buffer.from(code.join(''), 'hex'),
gasLimit: new BN(0xffff),
})
.then((results) => {
console.log(`Returned: ${results.returnValue.toString('hex')}`)
console.log(`gasUsed : ${results.gasUsed.toString()}`)
})
.catch(console.error)