Interact with Deployed Smart Contracts — Read and Write Operations on Private Networks

Interact with Deployed Smart Contracts — Read and Write Operations on Private Networks

Derrk SamuelMay 23, 202610 min
Learn how to interact with deployed smart contracts using web3.js and web3js-quorum. Master read and write operations on both public and private Ethereum networks.

Overview

This tutorial shows you how to interact with smart contracts that have been deployed to a network. Whether you're working with public blockchain networks or private Ethereum instances, understanding how to read from and write to deployed contracts is fundamental to Web3 development.

SimpleStorage Contract

pragma solidity ^0.7.0;

contract SimpleStorage {
  uint public storedData;

  constructor(uint initVal) public {
    storedData = initVal;
  }

  function set(uint x) public {
    storedData = x;
  }

  function get() view public returns (uint retVal) {
    return storedData;
  }
}

Interacting with Public Contracts

1. Perform a Read Operation

async function getValueAtAddress(host, deployedContractAbi, deployedContractAddress) {
  const web3 = new Web3(host);
  const contractInstance = new web3.eth.Contract(deployedContractAbi, deployedContractAddress);
  const res = await contractInstance.methods.get().call();
  console.log("Obtained value at deployed contract is: " + res);
  return res;
}

2. Perform a Write Operation

async function setValueAtAddress(host, accountAddress, value, deployedContractAbi, deployedContractAddress) {
  const web3 = new Web3(host);
  const contractInstance = new web3.eth.Contract(deployedContractAbi, deployedContractAddress);
  const res = await contractInstance.methods
    .set(value)
    .send({ from: accountAddress, gasPrice: "0xFF", gasLimit: "0x24A22" });
  return res;
}

Interacting with Private Contracts

Private contract interactions require the web3js-quorum library. Both read and write operations use generateAndSendRawTransaction.

Private Read Operation

const functionParams = {
  to: address,
  data: functionAbi.signature,
  privateKey: fromPrivateKey,
  privateFrom: fromPublicKey,
  privateFor: [toPublicKey],
};
const transactionHash = await web3quorum.priv.generateAndSendRawTransaction(functionParams);
const result = await web3quorum.priv.waitForTransactionReceipt(transactionHash);

Key Takeaways

  • Read operations are view-only calls (no gas cost on public networks)
  • Write operations require transactions that modify contract state
  • Public contracts use standard web3.eth.Contract calls
  • Private contracts use web3js-quorum with encryption and participant specification

Also featured on TechDerks Insights

I write more takes like this one on my second blog — blockchain deep dives, security breakdowns, and builder tools.

Read more on TechDerks Insights
D

Derrk Samuel

Web3, crypto, and Linux PC insights — practical guides, security tips, mining setups, blockchain tools, and decentralized tech strategies for techs.

Discussion

Leave a comment

No comments yet. Be the first to start the discussion!