Actions
Overview
Tevm has an actions based api similar to viem’s actions api and following similar patterns. This is a higher level of abstraction than the lower level JSON-RPC api.
Public actions
MemoryClient comes with all viem public actions built in.
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.tevmScript
- Runs the provided bytecode against the EVM stateTevmClient.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 TevmClient.call
, TevmClient.contract
, and TevmClient.script
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 BaseClient api.
Tevm supports tree shakeable actions similar to viem.
To make a minimal Tevm client use createBaseClient
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 BaseClient
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