Actions
Overview
The core api for interacting with Tevm is called the Actions API
.
- Tevm inherits viem’s public actions api
- Tevm adds a few useful tevm specific actions
Public actions
MemoryClient comes with all viem public actions built in.
Notable apis include the following
- MemoryClient.call
- MemoryClient.readContract
- MemoryClient.getChainId
- MemoryClient.estimateGas
- MemoryClient.getLogs
- MemoryClient.getTransactionReceipt
Most Test actions and wallet actions are not included in MemoryClient but can be added with extend. See client guide.
See JSON-RPC guide for a complete list of all supported JSON-RPC methods. Viem docs list which json-rpc methods they use.
ENS actions are supported but do not work unless forking a network with ENS support.
Error handling
By default Tevm clients will return a rejected promise when actions fail. Clients can optionally also return errors as values. This is very useful for handling errors in a typesafe way. All actions have a matching error in the tevm/error
package.
To return errors as values pass in a throwOnFail: false
option to the tevm action. Currently on tevm actions are supported and not other actions such as eth
actions.
TevmClient actions
TevmClient methods are the main recomended way to interact with Tevm. 🚧 means the procedure is still under construction
TevmClient.tevmCall
- Similar to eth call but with additional properties to control the VM executionTevmClient.tevmGetAccount
- gets account information such as balances contract information nonces and state roots.TevmClient.tevmSetAccount
- directly modifies the state of an accountTevmClient.tevmContract
- Similar to eth call but with additional properties to control the VM executionTevmClient.tevmDumpState
- Returns the state of the VMTevmClient.tevmLoadState
- Initializes the state of the VMTevmClient.tevmDeploy
- Creates a transaction to deploy a contract
Note the call
family of actions including MemoryClient.tevmCall
, MemoryClient.tevmContract
, and MemoryClient.tevmDeploy
will execute in a sandbox and not modify the state. This behavior can be disabled via passing in a enableTransaction: true
parameter.
Wallet Actions
MemoryClient
supports viem wallet actions but does not come with them by default. This is because transactions can be created using tevm methods such as tevmCall
and tevmContract
.
If you wish to add walletActions to your tevm client you must add an account and the wallet decorator.
Test actions
For compatability tevm supports using viem public actions. These test actions use anvil/hardhat methods such as `anvil_setBytecode. MemoryClient can do these actions with tevm actions. But testActions can be useful if you want to write code that works universally with Tevm hardhat and anvil.
Http clients
If tevm is running in an http server using the @tevm/server
package, any http client can be used to communicate with it including viem clients, and ethers clients. Tevm doesn’t export an http client itself and recomends you use viem as it’s apis all work with the in memory client.
Base Client and tree shakeable actions
While MemoryClient
is suggested for most users, users trying to squeeze out bundle size wins such as 3rd party libraries may want to use the lower level TevmNode api.
Tevm supports tree shakeable actions similar to viem.
To make a minimal Tevm client use createTevmNode
and import actions such as tevmSetAccount
from @tevm/actions
.
Viem actions with tree shakeable actions
To use viem tree shakeable actions you must build a client from scratch.
You can now use viem tree-shakeable actions
You can also use tevm tree shakable actions with the tevm base client.
Lower level packages
For those wanting to dive deeper into tevms internal or squeeze out even smaller bundlers, in addition to TevmNode
all the internal packages used to create base client are publically available on NPM. Notably:
@tevm/evm
contains a very simple EVM interpreter used by tevm@tevm/state
provides a custom state manager used to implement forking@tevm/blockchain
contains the internal blockchain implementation@tevm/txpool
contains the mempool implementation
And more.
All these packages have generated reference docs