How to Call Smart Contract Function from Javascript

Smart contracts have revolutionized the world of blockchain technology by enabling trustless, transparent, and secure transactions. They allow automation of tasks and eliminate the need for intermediaries, which makes them attractive to developers who want to build decentralized applications. In this article, we will explore how to call a smart contract function from JavaScript.

Before we get started, it is essential to understand what smart contracts are and how they work. A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. The code and the agreement exist on a blockchain network, ensuring that it is tamper-proof and transparent.

JavaScript is a popular programming language used to create web applications. It can interact with smart contracts through Web3.js, an Ethereum JavaScript API. Web3.js is a collection of libraries that allow developers to connect to an Ethereum node, send transactions, and interact with smart contracts.

Here are the steps to call a smart contract function from JavaScript:

Step 1: Import Required Libraries

To interact with a smart contract using JavaScript, we need to import the web3.js library. This can be done by adding the following line of code to your project:

const Web3 = require(`web3`);

Step 2: Connect to the Ethereum Network

After importing the web3.js library, the next step is to connect to the Ethereum network. This can be achieved by creating an instance of the Web3 object and connecting to a node through the HTTP provider.

const web3 = new Web3(`mainnet.infura.io/v3/your-project-id`);

The `your-project-id` variable should be replaced with your own project ID, obtained from the Infura website.

Step 3: Define the Smart Contract ABI

ABI (Application Binary Interface) is a JSON file that describes the functions and parameters of a smart contract. It is used to call the functions of a smart contract from a web application. To define the ABI of the smart contract, we need to create a JSON object containing the name, type, and inputs of each function.

const abi = [{“constant”:true,”inputs”:[],”name”:”getBalance”,”outputs”:[{“name”:””,”type”:”uint256″}],”payable”:false,”stateMutability”:”view”,”type”:”function”},{“constant”:false,”inputs”:[{“name”:”amount”,”type”:”uint256″}],”name”:”deposit”,”outputs”:[],”payable”:true,”stateMutability”:”payable”,”type”:”function”}];

Step 4: Create an Instance of the Smart Contract

To interact with a smart contract, we need to create an instance of the contract object. This can be done using the web3.eth.Contract() function, which takes the ABI and the contract address as input parameters.

const contract = new web3.eth.Contract(abi, `0x1234567890abcdef1234567890abcdef12345678`);

The contract address should be replaced with the actual address of the smart contract.

Step 5: Call the Smart Contract Function

Once we have created an instance of the smart contract, we can call its functions using the contract.methods property. This property contains all the functions defined in the smart contract ABI, and we can call them using the .call() or .send() method.

Here`s an example of calling a smart contract function to get the balance:

contract.methods.getBalance().call((error, result) => {

if(!error) {

console.log(result);

} else {

console.error(error);

}

});

This code uses the .call() method to call the getBalance() function and retrieve the balance of the smart contract. The result is returned as a callback function, which logs it to the console.

Conclusion

In conclusion, calling a smart contract function from JavaScript is a simple process that can be achieved using the web3.js library. By following the steps outlined in this article, you can connect to the Ethereum network, define the ABI of the smart contract, create an instance of the contract, and call its functions. With this knowledge, you can build decentralized applications that interact with smart contracts and leverage the benefits of blockchain technology.