All Products

Tenderly Node
Interaction Libraries
Viem

Viem - Tenderly Node Integration

Viem is a TypeScript Interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.

You can use Tenderly with Viem to interact with the blockchain using standard RPC methods, as well as custom methods available on Tenderly Node.

Call Viem’s createPublicClient and pass the following arguments:

  • chain: one of the pre-defined chains from viem/chains
  • transport: either a call to http or webSocket, and initialize with the https or wss RPC URL.
node/samples/viem/0-a-call.mjs
import { createPublicClient, http } from "pages/node/integrations/viem.mdx";
import { mainnet } from "viem/chains";
 
const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`
  ),
});
 
 
console.log("Block Number", await client.getBlockNumber());

Batching requests

Viem allows you to send batch requests. With batching, several JSON-RPC calls are processed in a single HTTPS request. Several Viem actions will be batched and executed at the end of the current JavaScript message queue. By default, there’s no time delay, but is possible to configure via an optional wait parameter.

This means that several viem actions will be batched and executed at the end of current JavaScript message queue. By default, there’s no time-delay, but is possible to configure via an optional wait parameter.

node/samples/viem/0-batch-calls.mjs
import { createPublicClient, http } from "pages/node/integrations/viem.mdx";
import { mainnet } from "viem/chains";
 
const client = createPublicClient({
  chain: mainnet,
  transport: http(
    `https://mainnet.gateway.tenderly.co/${process.env.TENDERLY_NODE_ACCESS_KEY}`,
    {
      batch: true,
    }
  ),
});
 
const [blockNumber, balance, ensName] = await Promise.all([
  client.getBlockNumber(),
  client.getBalance({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getEnsName({ address: "0xd2135CfB216b74109775236E36d4b433F1DF507B" }),
  client.getChainId(),
]);
console.log("Results are in");
console.log({ blockNumber, balance, ensName });
 
//  So will this
const bnPromise = client.getBlockNumber();
 
const balancePromise = client.getBalance({
  address: "0xd2135CfB216b74109775236E36d4b433F1DF507B",
});
 
console.log(await bnPromise);
console.log(await balancePromise);

Simulate transactions using viem

To use Tenderly Node’s Simulation RPC methods, simply use the request method:

node/samples/viem/1-simulate.mjs
const simulation = await client.request({
  method: "tenderly_simulateTransaction",
  params: [
    // transaction object
    {
      from: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      gas: "0x0",
      gasPrice: "0x0",
      value: "0x0",
      data: "0xa9059cbb00000000000000000000000020a5814b73ef3537c6e099a0d45c798f4bd6e1d60000000000000000000000000000000000000000000000000000000000000001",
    },
    // the block
    "latest",
  ],
});
 
console.log("Trace");
console.log(JSON.stringify(simulation.trace, null, 2));
console.log("=======================================================");
console.log("Logs");
console.log(JSON.stringify(simulation.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(simulation.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(simulation.balanceChanges, null, 2));

Trace transactions using Viem

Use tenderly_traceTransaction to get fully decoded transaction trace.

node/samples/viem/2-transaction-trace.mjs
const txTrace = await client.request({
  method: "tenderly_traceTransaction",
  params: [
    // transaction hash
    "0x6b2264fa8e28a641d834482d250080b39cbbf39251344573c7504d6137c4b793"
  ],
});
 
 
console.log("Logs");
console.log(JSON.stringify(txTrace.logs, null, 2));
console.log("=======================================================");
console.log("Asset Changes");
console.log(JSON.stringify(txTrace.assetChanges, null, 2));
console.log("=======================================================");
console.log("Balance Changes");
console.log(JSON.stringify(txTrace.balanceChanges, null, 2));
console.log("=======================================================");