@polareth/evmstate
Version:
A TypeScript library for tracing, and visualizing EVM state changes with detailed human-readable labeling.
82 lines (54 loc) • 1.8 kB
text/mdx
import { Callout } from "vocs/components";
# Examples
This guide covers a few examples of how to use the library.
<Callout type="tip">
See the [playground](/playground) for an interactive demonstration of the output based on function calls.
</Callout>
## Monitoring ERC20 balances
:::code-group
```ts twoslash [example.ts]
// [!include ~/snippets/client-fork.ts]
// [!include ~/snippets/abi.ts]
// [!include ~/snippets/layout.ts]
// ---cut---
import { createContract } from "tevm/contract";
import { traceState, type LabeledState } from "@polareth/evmstate";
// [!include ~/snippets/erc20.ts:args]
// [!include ~/snippets/erc20.ts:approve]
// [!include ~/snippets/erc20.ts:trace-swap]
// [!include ~/snippets/erc20.ts:trace-parse]
```
```ts twoslash [client.ts]
import { createMemoryClient, http } from "tevm";
import { mainnet } from "tevm/common";
// [!include ~/snippets/client-fork.ts:client-fork-no-highlight]
```
```ts twoslash [abis.ts]
// [!include ~/snippets/abi.ts:erc20]
// [!include ~/snippets/abi.ts:simple-dex]
```
```ts twoslash [layout.ts]
// [!include ~/snippets/layout.ts:erc20]
```
:::
## Processing complex outputs
:::code-group
```ts twoslash [example.ts]
// [!include ~/snippets/abi.ts:mappings]
// [!include ~/snippets/layout.ts:mappings]
// [!include ~/snippets/client.ts]
// ---cut---
import { watchState, PathSegmentKind, type StateChange } from "@polareth/evmstate";
// [!include ~/snippets/watch-state.ts:watchState-logs-onStateChange]
// [!include ~/snippets/watch-state.ts:watchState-logs-subscribe /unsubscribe2/unsubscribe/]
```
```ts twoslash [layout.ts]
// [!include ~/snippets/layout.ts:mappings]
```
```ts twoslash [abi.ts]
// [!include ~/snippets/abi.ts:mappings]
```
```ts twoslash [client.ts]
// [!include ~/snippets/client.ts]
```
:::