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

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