Ethereum Smart Contract Development: Using Infura and web3.js to Call Contracts Without Running Your Own Node

·

Infura provides public Ethereum mainnet and testnet nodes. Simply sign up on Infura's website with basic details and email to receive an API key.


Querying Contract-Stored State via RPC

A common query is checking token balances. Let's use the EOS token contract as an example.

Obtaining Contract Information

Find contract details on Etherscan. Essential details include:

Calculating Function Signatures

  1. SHA3 hash the function name with parameters: balanceOf(address)0x70a08231...
  2. Extract the first 8 characters after 0x: 70a08231.

web3.js Example:

var functionSig = web3.sha3("balanceOf(address)").substr(2, 8);

Making RPC Calls with eth_call

For read-only functions, use eth_call. Example POST data:

{
  "jsonrpc": "2.0",
  "method": "eth_call",
  "params": [{
    "to": "0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0",
    "data": "0x70a0823100000000000000000000000033b8287511ac7F003902e83D642Be4603afCd876"
  }, "latest"],
  "id": 123
}

Response:

{
  "jsonrpc": "2.0",
  "id": 123,
  "result": "0x000000000000000000000000000000000000000000000000b1d16ec625a59d3e"
}

The hex value 0x...3e converts to ~12.8 EOS tokens.


Using web3.js (JavaScript API)

For state updates, transactions require signing with a private key and Ether for gas fees. We'll deploy a test contract on Ropsten for demonstration.

Setup

npm install web3 ethereumjs-tx

Initialize web3

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/"));

Deployed Test Contract

Contract Address: 0x5fb30123b9efedcd15094266948fb7c862279ee1
Function Signatures:


Querying Contract State

var result = web3.eth.call({
  to: "0x5fb30123b9efedcd15094266948fb7c862279ee1",
  data: "0x73d4a13a"
});
console.log(parseInt(result, 16)); // Output: 0 (initial state)

Updating Contract State

1. Define Raw Transaction

var rawTx = {
  nonce: '0x14',
  gasPrice: '0x3B9ACA00', // 1 Gwei
  gasLimit: '0xC20A',
  to: '0x5fb30123b9efedcd15094266948fb7c862279ee1',
  value: '0x00',
  data: '0x60fe47b1000000000000000000000000000000000000000000000000000000000000000a' // Sets data=10
};

2. Sign and Send

var Tx = require('ethereumjs-tx');
var tx = new Tx(rawTx);
tx.sign(privateKey);
web3.eth.sendRawTransaction('0x' + tx.serialize().toString('hex'), (err, hash) => {
  if (!err) console.log(hash); // Transaction hash
});

Success Output:
0x2a9d89b0f329853b5c0f83c83cea4cfa2e38ddd1041d9abd0afcc9af5ed1bf1b

👉 Track your transaction on Etherscan


FAQ

Q1: Why use Infura instead of running my own node?
A1: Infura eliminates the need for extensive hardware/bandwidth, providing instant access to Ethereum networks.

Q2: How do I estimate gas limits accurately?
A2: Tools like Remix IDE or Tenderly simulate transactions to suggest optimal gas values.

Q3: Can I use this for mainnet contracts?
A3: Yes! Replace Ropsten endpoints with https://mainnet.infura.io/ and ensure sufficient ETH for gas.


References

👉 Explore advanced Ethereum development tools