0.4.0 • Published 6 months ago

evm-js-emulator v0.4.0

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

A simple in-memory EVM JS emulator.

Install

npm i evm-js-emulator

Usage

import { newSession, parseBuffer, toUint, MAX_UINT, isSuccess } from 'evm-js-emulator';


// create a blockchain fork
const session = newSession({
    // let's fork Polygon
    rpcUrl: 'https://polygon-rpc.com',
    // when you're running node, cache RPC results in order to avoid coldstarts
    //  (this will freeze your runs to the current block, until you delete this folder)
    cacheDir: '.evm-cache',
});


// prepare an execution
const executor = await session.prepareCall({
    contract: toUint('0x ... write contract address here ...'),
    origin: toUint('0x ... write sender address here ...'),
    // a buffer which represents calldata
    calldata: parseBuffer('0x ... write calldata here ...'),
    // tx value
    callvalue: U256(0),
    // this is not a static call (allow mutations)
    static: false,
    // force timestamp to a given value (optional)
    timestamp: Date.now() / 1000,
    // other paramters
    gasLimit: MAX_UINT,
    gasPrice: U256(0xffff),
    retdatasize: 0,
});

// execute !
const result = await executor.execute();

if (isSuccess(result)) {
    // do something ...
}

// etc... you can chain multiple executions

Debugging

  • Once you get an executor, before calling .execute(), you can log every EVM execution instruction using the helper:
import { watchInstructions } from 'evm-js-emulator/tests/test-utils';

// this will log all instructions down to a depth of 3 subcalls
watchInstructions(executor, 3);
  • You can name contracts or add ABIs to have nicer logs using the contractsNames properties of newSession({ contractsNames: ...}) (see types).

  • Add breakpoints in codegen js files that you will find in you cache directory (subdirectory "contracts")... those are the contracts you are running, compiled to JS.

Disclaimer

This is a best-effort reproduction of the EVM.

It has been tested on many contracts, but:

  • Gas is almost not implemented (contributions welcome)
  • Some opcodes might not be implemented (contributions welcome)
  • Behaviours might differ

TODO

  • Implement missing opcodes
  • Implement sourcemaps to be able to step in .sol files
0.4.0

6 months ago

0.3.8

1 year ago

0.3.0

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.7

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.47

1 year ago

0.1.46

1 year ago

0.1.45

2 years ago

0.1.44

2 years ago

0.1.41

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.40

2 years ago

0.1.39

2 years ago

0.1.30

2 years ago

0.1.31

2 years ago

0.1.32

2 years ago

0.1.33

2 years ago

0.1.34

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.38

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.22

3 years ago

0.1.16

3 years ago

0.1.8

3 years ago

0.1.17

3 years ago

0.1.7

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.9

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

1.0.0

3 years ago