Skip to content
LogoLogo

call

An Action for executing a new message call.

Executes a new message call immediately without submitting a transaction to the network.

Usage

// @filename: config.ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
 
export const owner = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
 
export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})
// @filename: example.ts
// ---cut---
import { account, publicClient } from './config'
 
const data = await publicClient.call({ 
  account,
  data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

Deployless Calls

It is possible to call a function on a contract that has not been deployed yet. For instance, we may want to call a function on an ERC-4337 Smart Account contract which has not been deployed.

Viem offers two ways of performing a Deployless Call, via:

Bytecode

The example below demonstrates how we can utilize a Deployless Call via Bytecode to call the name function on the Wagmi Example ERC721 contract which has not been deployed:

import { ,  } from 'viem'
import {  } from './config'
 
const  = await .({
  // Bytecode of the contract. Accessible here: https://etherscan.io/address/0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2#code
  : '0x...',
  // Function to call on the contract.
  : ({
    : (['function name() view returns (string)']),
    : 'name'
  }),
})

Deploy Factory

The example below demonstrates how we can utilize a Deployless Call via a Deploy Factory to call the entryPoint function on an ERC-4337 Smart Account which has not been deployed:

import { ,  } from 'viem'
import { ,  } from './config'
 
const  = await .({
  // Address of the contract deployer (e.g. Smart Account Factory).
  : '0xE8Df82fA4E10e6A12a9Dab552bceA2acd26De9bb',
 
  // Function to execute on the factory to deploy the contract.
  : ({
    : (['function createAccount(address owner, uint256 salt)']),
    : 'createAccount',
    : [, 0n],
  }),
 
  // Function to call on the contract (e.g. Smart Account contract).
  : ({
    : (['function entryPoint() view returns (address)']),
    : 'entryPoint'
  }),
 
  // Address of the contract.
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

Returns

0x${string}

The call data.

Parameters

account

  • Type: Account | Address

The Account to call from.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

data

  • Type: 0x${string}

A contract hashed method call with encoded args.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

to

The contract address or recipient.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
})

accessList (optional)

The access list.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : [ 
    {
      : '0x1',
      : ['0x1'],
    },
  ],
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

blockNumber (optional)

  • Type: number

The block number to perform the call against.

const  = await .({
  : 15121123n, 
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

blockTag (optional)

  • Type: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'
  • Default: 'latest'

The block tag to perform the call against.

const  = await .({
  : 'safe', 
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

code (optional)

  • Type:

Bytecode to perform the call against.

const  = await .({
  : '0x...', 
  : '0xdeadbeef',
})

factory (optional)

  • Type:

Contract deployment factory address (ie. Create2 factory, Smart Account factory, etc).

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0x0000000000ffe8b47b3e2130213b802212439497', 
  : '0xdeadbeef',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

factoryData (optional)

  • Type:

Calldata to execute on the factory to deploy the contract.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0x0000000000ffe8b47b3e2130213b802212439497',
  : '0xdeadbeef', 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

gas (optional)

  • Type: bigint

The gas provided for transaction execution.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : 1_000_000n, 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

gasPrice (optional)

  • Type: bigint

The price (in wei) to pay per gas. Only applies to Legacy Transactions.

import {  } from 'viem'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : ('20'), 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

maxFeePerGas (optional)

  • Type: bigint

Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions.

import {  } from 'viem'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : ('20'), 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions.

import {  } from 'viem'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : ('20'),
  : ('2'), 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

nonce (optional)

  • Type: bigint

Unique number identifying this transaction.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : 420, 
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

stateOverride (optional)

The state override set is an optional address-to-state mapping, where each entry specifies some state to be ephemerally overridden prior to executing the call.

const data = await publicClient.call({
  account,
  data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  stateOverride: [ 
    { 
      address: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
      balance: parseEther('1'), 
      stateDiff: [ 
        { 
          slot: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0', 
          value: '0x00000000000000000000000000000000000000000000000000000000000001a4', 
        }, 
      ], 
    } 
  ], 
})

value (optional)

  • Type: bigint

Value (in wei) sent with this transaction.

import {  } from 'viem'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  : ('1'), 
})

JSON-RPC Methods

eth_call