Skip to main content

Interact with Smart Contract using viem

Read from and write to a smart contract using viem’s typed client. Read from and write to a smart contract using viem’s typed client.

Explanation

viem provides a clean, typed interface for contract interactions. Key differences from ethers.js:
  • parseAbi() accepts human-readable ABI strings — no JSON needed.
  • readContract is for view functions (no gas cost).
  • Full TypeScript inference on function arguments and return types.
For write operations, use simulateContract + walletClient.writeContract.

Code


Canonical knowledge ID: code-example:interact-contract-viem