Call a contract
This tutorial uses Web3 to execute a function in a smart contract. Calling the contract function is a type of transaction that requires paying gas.


Before attempting this tutorial, ensure you completed the tutorial to deploy a contract and updated the .env file with the smart contract address.


1. Create a script to execute the contract

In the deployContract folder created earlier, create the JavaScript file that executes a contract function and signs the transaction. In this example we'll create the call.js file with the following contents:
const Web3 = require("web3");
// Loading the contract ABI
// (the results of a previous compilation step)
const fs = require("fs");
const { abi } = JSON.parse(fs.readFileSync("Demo.json"));
async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const web3 = new Web3(
new Web3.providers.HttpProvider(
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
// Creating a Contract instance
const contract = new web3.eth.Contract(
// Replace this with the address of your deployed contract
// Issuing a transaction that calls the `echo` method
const tx = contract.methods.echo("Hello, world!");
const receipt = await tx
from: signer.address,
gas: await tx.estimateGas(),
.once("transactionHash", (txhash) => {
console.log(`Mining transaction ...`);
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);

2. Call the contract

Run the script to call the smart contract:
node call.js
If you receive an insufficient funds for gas error, then you may need to top up your signer account with some testnet ETH.
The script will display the details of the transaction in the output. View the transaction in a block explorer such as Etherscan.