# evs

Typed EVM read scripts in TypeScript. Reads that depend on earlier reads run in one eth_call, typed by viem, with nothing to deploy.

Write read-only EVM scripts in TypeScript. evs compiles them to bytecode and runs them as one eth_call, so reads that depend on earlier reads cost a single round trip.

- [Quick start](https://evs.maxencerb.com/getting-started/quick-start/)
- [Why evs?](https://evs.maxencerb.com/getting-started/why-evs/)
- [GitHub](https://github.com/maxencerb/evs)

## Dependent reads in one call

Read a Uniswap V3 pool's two tokens, then each token's symbol.

```ts title="script.ts"
import { evscript, t } from '@maxencerb/evs';
import { erc20Abi } from 'viem';
import { poolAbi } from './abi';

export const poolTokens = evscript(
  { name: 'poolTokens', args: [t.address] },
  (s, pool) => {
    const token0 = s.read({ address: pool, abi: poolAbi, functionName: 'token0' });
    const token1 = s.read({ address: pool, abi: poolAbi, functionName: 'token1' });
    const symbol0 = s.read({ address: token0, abi: erc20Abi, functionName: 'symbol' });
    const symbol1 = s.read({ address: token1, abi: erc20Abi, functionName: 'symbol' });
    return s.return({ token0, token1, symbol0, symbol1 });
  },
);
```

```ts title="abi.ts"
import { parseAbi } from 'viem';

export const poolAbi = parseAbi([
  'function token0() view returns (address)',
  'function token1() view returns (address)',
]);
```

```ts title="client.ts"
import { compile } from '@maxencerb/evs';
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { poolTokens } from './script';

const client = createPublicClient({ chain: mainnet, transport: http() });
const compiled = compile(poolTokens);

const result = await client.readContract({
  ...compiled.toViem(),
  functionName: 'poolTokens',
  args: ['0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640'], // USDC/WETH 0.05% pool
});
// { token0: `0x${string}`; token1: `0x${string}`; symbol0: string; symbol1: string }
```

A multicall encodes every call before the request leaves, so it cannot read `symbol` from an
address that the same batch returns.

- [Quick start](https://evs.maxencerb.com/getting-started/quick-start/): Write, compile and run a script in three steps.
- [Writing scripts](https://evs.maxencerb.com/guides/writing-scripts/): What runs at build time and what runs on-chain.
- [Calling contracts](https://evs.maxencerb.com/guides/calls/): Read views, call quoters and dry-run writes.
- [Executing scripts](https://evs.maxencerb.com/guides/execution/): Deployless or state override, and which providers support each.
- [Examples](https://evs.maxencerb.com/examples/pool-metadata/): Pool metadata, batch token balances and more patterns.
- [API reference](https://evs.maxencerb.com/reference/builder/): One page per function, each with usage, returns and parameters.
