Read Contract

POST /networks/read-contract

Calls a read-only function on a smart contract. In Solidity, these use the view keyword. Note: Currently only works on EVM compatible chains.

Required Permissions

No permissions are required as this only exposes public blockchain data.

Request body

Request body fieldsRequired/OptionalDescriptionType

kind

Required

Specify "Evm". Additional kinds will be added in the future.

Enum String

network

Required

Network used for the wallet (See Supported Networks for possible values)

Enum String

contract

Required

Address of the contract to call

String

data

Required

Encoded hex string indicating which function in the smart contract to call with which parameters. For more information, see the encodeFunctionData ethersJS documentation.

String

Sample request body

{
    "kind": "Evm",
    "network": "EthereumSepolia",
    "contract": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
    "data": "0x18160ddd"
}

Response

Response example

Note the data field in the response is hex encoded.

{
    "kind": "Evm",
    "data": "0x000000000000000000000000000000000000000000000000000000000000000f"
}

EthersJS / Dfns SDK Example

You can use the following code with the Dfns Typescript SDK to execute this call:

import { Interface } from 'ethers'

  const abi = ['function balanceOf(address owner) view returns (uint256)']

  const iface = new Interface(abi)
  const address = '0xd964d741998edc275f3800eed113378a391951d9'
  const data = iface.encodeFunctionData('balanceOf', [address])

  const { data: res } = await dfnsClient.networks.readContract({
    body: {
      kind: 'Evm',
      network: 'EthereumSepolia',
      contract: '0x6f14c02fc1f78322cfd7d707ab90f18bad3b54f5',
      data,
    },
  })

  const decoded = iface.decodeFunctionResult('balanceOf', res)[0]
  console.log(decoded)

Last updated