Skip to content

Latest commit

 

History

History
206 lines (145 loc) · 7.88 KB

README.md

File metadata and controls

206 lines (145 loc) · 7.88 KB

EVMole JavaScript (WASM)

This directory contains API documentation and various examples (web page, nodejs, vite, webpack, parcel, esbuild) demonstrating how to use the EVMole library with its JavaScript (WASM) build in different environments and with various build tools.

The library is built with wasm-pack. To simplify usage, we provide a default entry point with await init(), which should work in all modern browsers and bundlers.

Usage

Web page

You can load evmole directly in a web page using a script module. Here's how to do it:

<div id="info"></div>

<script type="module">
import { contractInfo } from 'https://cdn.jsdelivr.net/npm/evmole@0.6.0/dist/evmole.mjs';

const bytecode = '0x6080...'; // Replace with actual bytecode
document.getElementById('info').textContent = contractInfo(bytecode, {selectors: true, arguments: true, stateMutability: true});
</script>

Node.js

You can use EVMole with both import and require syntax:

Vite

Set target: esnext in vite.config.js to support Top Level Await, required for default EVMole import:

build: {
  target: 'esnext'
}

After that, import and use EVMole as usual.

If you can't use esnext, see the No Top Level Await section.

Webpack

Set asyncWebAssembly: true in webpack.config.js:

experiments: {
  asyncWebAssembly: true,
}

After that, import and use EVMole as usual.

Parcel

Parcel can't work with Top Level Await, so you need to manually call init after import. See examples with:

You can read more about this in parcel resolver documentation

esbuild

Pass --format=esm and --loader:.wasm=file to esbuild. Find the full command in package.json

After that, import and use EVMole as usual.

No Top Level Await

If you can't use Top Level Await, you can import EVMole as:

import init, { functionSelectors } from 'evmole/no_tla`
// or: from 'evmole/dist/evmole.js' (supported, but not recommended)

After that, you can use it as:

const bytecode = '0x6080...'; // Replace with actual bytecode
async function main() {
  await init();
  console.log(contractInfo(bytecode, {selectors: true}));
}
main()

or

const bytecode = '0x6080...'; // Replace with actual bytecode
init().then() => {
  console.log(contractInfo(bytecode, {selectors: true}));
}

See full example without Top Level Await in Parcel example

API

contractInfo(code, args) ⇒ Contract

Analyzes contract bytecode and returns contract information based on specified options.

Kind: global function
Returns: Contract - Analyzed contract information

Param Type Description
code string Runtime bytecode as a hex string
args Object Configuration options for the analysis
[args.selectors] boolean When true, includes function selectors in the output
[args.arguments] boolean When true, includes function arguments information
[args.state_mutability] boolean When true, includes state mutability information for functions
[args.storage] boolean When true, includes contract storage layout information

Contract : Object

Kind: global typedef
Properties

Name Type Description
[functions] Array.<ContractFunction> Array of functions found in the contract. Not present if no functions were extracted
[storage] Array.<StorageRecord> Array of storage records found in the contract. Not present if storage layout was not extracted

ContractFunction : Object

Kind: global typedef
Properties

Name Type Description
selector string Function selector as a 4-byte hex string without '0x' prefix (e.g., 'aabbccdd')
bytecode_offset number Starting byte offset within the EVM bytecode for the function body
[arguments] string Function argument types in canonical format (e.g., 'uint256,address[]'). Not present if arguments were not extracted
[state_mutability] string Function's state mutability ("pure", "view", "payable", or "nonpayable"). Not present if state mutability were not extracted

StorageRecord : Object

Kind: global typedef
Properties

Name Type Description
slot string Storage slot number as a hex string (e.g., '0', '1b')
offset number Byte offset within the storage slot (0-31)
type string Variable type (e.g., 'uint256', 'mapping(address => uint256)', 'bytes32')
reads Array.<string> Array of function selectors that read from this storage location
writes Array.<string> Array of function selectors that write to this storage location

Deprecated API

functionSelectors(code, gas_limit) ⇒ Array.<string>

Please use contractInfo(code, {selectors: true}) instead

Extracts function selectors from the given bytecode.

Returns: Array.<string> - Function selectors as a hex strings

Param Type Description
code string Runtime bytecode as a hex string
gas_limit number Maximum allowed gas usage; set to 0 to use defaults

functionArguments(code, selector, gas_limit) ⇒ string

Please use contractInfo(code, {arguments: true}) instead

Extracts function arguments for a given selector from the bytecode.

Returns: string - Function arguments (ex: 'uint32,address')

Param Type Description
code string Runtime bytecode as a hex string
selector string Function selector as a hex string
gas_limit number Maximum allowed gas usage; set to 0 to use defaults

functionStateMutability(code, selector, gas_limit) ⇒ string

Please use contractInfo(code, {stateMutability: true}) instead

Extracts function state mutability for a given selector from the bytecode.

Returns: string - payable | nonpayable | view | pure

Param Type Description
code string Runtime bytecode as a hex string
selector string Function selector as a hex string
gas_limit number Maximum allowed gas usage; set to 0 to use defaults