Interacting with Ethereum Smart Contracts through Web3.js Library

Interacting with Ethereum Smart Contracts through Web3.js Library
3 min read

Smart Contract is a transaction protocol that is self-executing as per the terms of an agreement between seller and buyer, existing on a blockchain platform, and developed by a smart contract development company. Most smart contracts are stored on the Ethereum blockchain. The smart contract codes control all the transactions on a blockchain, and execution is trackable and irreversible.

Smart contract development requires specific programming language and these contract are then compiled in bytecode. To develop enterprise-grade smart contracts, it required smart contract development services.

To programme smart contracts, we can utilise the Solidity programming language. The syntax of smart contracts is extremely similar to Javascript, and they are read and executed using the Ethereum Virtual Machine (EVM). We can run Solidity programmes using either local machines or the online IDE (integrated development environment) Remix.

Prerequisites

  • JavaScript Library Web3.js
  • MetaMask Wallet

Steps to Interact with a Smart Contract through Web3.js:-

Step 1:- First we need to add/install the Metamask extension in any browser like Google Chrome, Firefox, etc.

Step 2:- Web3.js is the official Ethereum JavaScript API and it is used to interact with Ethereum smart contract.

  • Install Web3.js library by using npm :-
    npm install web3 — save
    import web3 from ‘web3’;
  • Import Web3.js library nn HTML file :-
    <script src=”https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.2.0/dist/web3.min.js"></script>

Step 3:- In this step, we are connecting the MetaMask wallet with a dApp. If the user’s wallet is in lock status then it will ask for unlocking Metamask wallet, otherwise, it will directly connect the user’s Metamask wallet with the dApp application.

async function connectWallet() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum); }

conn = await window.ethereum.enable();

ethconnected = conn.length > 0
if (ethconnected) {
ethaddress = conn[0] // get wallet address
}
web3.eth.getAccounts().then(console.log);

return true;
}

Step 4:- In this step, we are creating a contract object with the help of a web3 object and we will use this contract object to call Ethereum smart contract methods.

contract = new web3.eth.Contract(<contract Abi>, <contract address>);

Step 5:- In this step, we are calling the balanceOf() method of smart contract to get user balance.

You may also like to read | How to use Blockchain in the Metaverse | Oodles Blockchain

contract.methods.balanceOf(ethaddress).call().then(function (balance) {
console.log(balance);
})

Step 6:- In this step, we are calling the add() method of smart contract to add two numbers and after successful transaction creation, it will return you a transaction hash value. You can use this transaction hash value to check the transaction status on etherscan.io.

contract.methods.add(“10”, “20”).send({ from: ethaddress }, async function (error, transactonHash) {
console.log(transactonHash);
})

Conclusion

This is a straightforward example of utilising the Web3.js library to connect with an Ethereum smart contract, and it can only be tested locally.

Hire smart contract developer for seamless interaction with Ethereum smart contracts via the Web3.js library. They ensure secure, efficient, and error-free contract integration.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Oodles Blockchain 68
Full-Stack Blockchain Development Services and Solutions for Startups and Enterprises for Business Excellence Transform your business processes into highly sec...
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up