Code samples: Using Viem with Virtual Environments
Define a custom chain
Create a filetenderly.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 astenderly_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
createPublicClientand pass the following arguments: chain: set tovMainnetwe defined in the previous steptransport: either a call tohttp, and set tohttpsRPC URL.
virtual-environments/src/viem-call-balance-top-up.ts
Simulate transactions using viem
To use Virtual Environment’s Simulation RPC methods, simplyrequest to make the call to tenderly_simulateTransaction:
virtual-environments/src/viem-call-simulate.ts
Trace transactions using Viem
Usetenderly_traceTransaction to get fully decoded transaction trace.
node/samples/viem/2-transaction-trace.ts