Skip to main content
Change page

Interacting with smart contracts

You don't always need to write and deploy your own smart contract. Most of the time as a developer, you'll want to interact with smart contracts that others have already deployed to the Ethereum network.

This page covers the two fundamental ways to interact with a smart contract—reading data and writing data—and the tools you need to do both.

Prerequisites

You should understand:

Two ways to interact with a smart contract

Interacting with a smart contract falls into two categories:

Reading from a contract

Reading is a free operation that does not create a transaction and does not change any state on the blockchain.

When you read from a contract, you're simply querying data that already exists. For example:

  • Checking an ERC-20 token balance
  • Reading the current price from a decentralized exchange
  • Getting the owner of an NFT

Because reads don't modify state, they don't cost gas and can be performed by anyone without needing ETH.

Writing to a contract

Writing is a state-changing operation that requires a transaction and costs gas.

When you write to a contract, you're triggering a function that modifies the blockchain state. For example:

  • Transferring tokens
  • Swapping tokens on a decentralized exchange
  • Minting an NFT

Writing always requires:

  1. An Externally Owned Account (EOA) with enough ETH for gas
  2. A transaction signed by the account's private key
  3. The transaction to be mined and included in a block

With account abstraction, a smart contract account can also initiate writes, and a paymaster can cover gas on the user's behalf—so an EOA holding ETH is not strictly required.

Understanding contract ABIs

To interact with a smart contract, your application needs to know what the contract can do. This is where the Application Binary Interface (ABI) comes in.

An ABI is a JSON document that describes:

  • Every function the contract exposes (name, inputs, outputs)
  • Every event the contract can emit
  • How to encode and decode data when talking to the contract

Think of the ABI as the contract's instruction manual—without it, your application doesn't know which functions exist or what parameters they expect.

Where to find a contract's ABI

  • Verified contracts on Etherscan - Etherscan (opens in a new tab) automatically exposes the ABI for verified source code
  • From the developer - many projects publish their ABIs in their documentation or npm packages
  • Generate from source - if you have the Solidity source code, you can compile it to produce the ABI

Tools and libraries for interacting with contracts

Developers typically use a JavaScript/TypeScript library to interact with contracts from a web app, backend, or script.

Client libraries (JavaScript/TypeScript)

Backend libraries

Example: reading a token balance with Viem

Example: sending a transaction with ethers.js

Events and logs

Smart contracts can emit events to signal that something happened. Your application can listen for these events to react in real time.

Simulating transactions

Before sending a transaction, you can simulate it to check if it would succeed—and to see its return value—without spending gas. This is useful for catching errors early and for previewing outcomes.

Most client libraries support this through eth_call:

// With Viem
const result = await client.simulateContract({
  address: contractAddress,
  abi,
  functionName: 'swap',
  args: [amountIn],
  account: userAddress,
})

Wallets and signing

In a dapp, the user's wallet (like MetaMask, Rainbow, or WalletConnect) handles signing. You don't manage private keys directly.

Wallet libraries and connection tools abstract this so you can focus on building your application logic.

Further reading