Join our community of builders on Discord!

Interact with Contracts

Once your smart contract is deployed, you can interact with it by reading its state and sending transactions to modify it. This guide covers multiple approaches.
[!NOTE] You'll need the contract address and ABI from your deployment step.
If you followed the deploy guide, use the address printed in your terminal.

1. Interact via Hardhat Console

The Hardhat console provides a quick interactive REPL for contract interaction:
CodeBASH
Inside the console:
CodeJAVASCRIPT

2. Interact via a Script

For repeatable interactions, write a dedicated script. Create
CodeHTML
:
CodeJAVASCRIPT
Run it:
CodeBASH

3. Interact via Foundry (cast)

Foundry's
CodeHTML
CLI is excellent for quick one-off interactions:

Read a Value

CodeBASH

Send a Transaction

CodeBASH

Decode Transaction Logs

CodeBASH

4. Interact via ethers.js (Frontend / Backend)

For dApp integrations, use ethers.js in your frontend or Node.js backend:
CodeJAVASCRIPT

5. Interact via the Block Explorer

If your contract is verified, you can interact directly from the explorer:
  1. Navigate to your contract on the Lightchain AI Testnet Explorer.
  2. Go to the "Read Contract" tab to call view functions.
  3. Go to the "Write Contract" tab, connect your wallet, and send transactions.
[!TIP] The explorer's UI is the fastest way to test individual functions without writing any code.

Reading Events

Events are critical for tracking state changes. Here's how to query historical events:
CodeJAVASCRIPT

Estimating Gas

Before sending a transaction, estimate the gas cost:
CodeJAVASCRIPT
Gas estimates are approximations. The actual gas used may differ slightly. Always ensure your wallet has a small buffer of testnet LCAI.

Next Steps


Last updated: February 2026