forked from wighawag/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.html
76 lines (62 loc) · 2.76 KB
/
browser.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!doctype html>
<html>
<head>
<title>EthereumJS Browser Examples</title>
<script type="module">
import { Blockchain } from '@ethereumjs/blockchain'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { EVM } from '@ethereumjs/evm'
import { DefaultStateManager } from '@ethereumjs/statemanager'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
const main = async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const stateManager = new DefaultStateManager()
const blockchain = await Blockchain.create()
const evm = await EVM.create({
common,
stateManager,
blockchain,
})
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]
evm.events.on('step', function (data) {
// Note that data.stack is not immutable, i.e. it is a reference to the vm's internal stack object
console.log(`Opcode: ${data.opcode.name}\tStack: ${data.stack}`)
})
evm
.runCode({
code: hexToBytes('0x' + code.join('')),
gasLimit: BigInt(0xffff),
})
.then((results) => {
console.log(`Returned: ${bytesToHex(results.returnValue)}`)
console.log(`gasUsed: ${results.executionGasUsed.toString()}`)
})
.catch(console.error)
}
void main()
</script>
</head>
<body style="padding:50px; font-family: Arial, Helvetica, sans-serif;">
<h1>EVM | @ethereumjs/evm</h1>
Basic usage of this library in the browser (using <a href="https://github.com/vitejs/vite" target="_blank">Vite</a>)
<h3>Run the Example</h3>
<ol>
<li>Go to the library root directory (packages/[LIBRARY_NAME]/)</li>
<li>Build "dist" folder with: npm run build</li>
<li>Start Vite development server with: npx vite</li>
<li>Open the example URL in the browser (http://localhost:5173/examples/browser.html)</li>
<li>Open the development console (e.g. Chrome Developer Tools)</li>
<li>See example results and play with the code</li>
</ol>
<h3>Interactive CLI</h3>
<ol>
<li>Open the "Sources -> Page" tab in the Chrome Developer Tools</li>
<li>Set a breakpoint within the original "browser.html" file (so not the one generated by Vite)</li>
<li>Now you can use and play with the imports dynamically</li>
</ol>
</body>
</html>