Skip to main content
Viem is a TypeScript Interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum. You can interact with Virtual Environments using the standard RPC methods when you copy the Public RPC, as well as custom methods available on Node RPC when you copy the Admin RPC.

Code samples: Using Viem with Virtual Environments

Define a custom chain

Create a file tenderly.config.ts and use defineChain to create a chain object.
If you specified a unique Chain ID during Virtual Environment creation, you need to create a new chain object and set the id to your chosen value.
virtual-environments/src/tenderly.config.ts

Define Viem actions for custom RPC methods

Define Viem actions for custom RPC methods, such as tenderly_setBalance tenderly_setErc20Balance, and simulate. For example, the following file defines a tenderlySetBalance action that will invoke tenderly_setBalance. You can find more actions here:

Viem actions for custom RPC methods

virtual-environments/src/viem-tenderly-actions.ts

Call standard and custom RPC methods

In this example, you can see how to:
  • Call Viem’s createPublicClient and pass the following arguments:
  • chain: set to vMainnet we defined in the previous step
  • transport: either a call to http, and set to https RPC URL.
virtual-environments/src/viem-call-balance-top-up.ts

Simulate transactions using viem

To use Virtual Environment’s Simulation RPC methods, simply request to make the call to tenderly_simulateTransaction:
virtual-environments/src/viem-call-simulate.ts

Trace transactions using Viem

Use tenderly_traceTransaction to get fully decoded transaction trace.
node/samples/viem/2-transaction-trace.ts