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.

Prerequisites

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

Steps

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(
`https://${network}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`
)
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);
// Creating a Contract instance
const contract = new web3.eth.Contract(
abi,
// Replace this with the address of your deployed contract
process.env.DEMO_CONTRACT
);
// Issuing a transaction that calls the `echo` method
const tx = contract.methods.echo("Hello, world!");
const receipt = await tx
.send({
from: signer.address,
gas: await tx.estimateGas(),
})
.once("transactionHash", (txhash) => {
console.log(`Mining transaction ...`);
console.log(`https://${network}.etherscan.io/tx/${txhash}`);
});
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);
}
require("dotenv").config();
main();

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.