Writing Smart Contracts with Web3
Install web3 and solc dependencies
Before starting the deployment process, you need to install the packages for web3 and solc:
Install Node.js and npm: If you have already installed Node.js, you can check if npm is installed by running 'npm -v'. If you haven't installed Node.js yet, please download and install it from the official website:
Enter 'npm install web3' in the command line and press Enter. npm will automatically download and install the web3 package from the npm registry.
npm install web3Install solc package: Enter 'npm install solc' in the command line and press Enter. npm will automatically download and install the solc package from the npm registry.
npm install solcWrite smart contracts.
Now you need to write a smart contract. Let's name this smart contract 'test1.sol'.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.18;
contract Counter {
// Public variable of type unsigned int to keep the number of counts
uint256 public count = 0;
// Function that increments our counter
function increment() public {
count += 1;
}
// Not necessary getter to get the count value
function getCount() public view returns (uint256) {
return count;
}
}
Compile and deploy the contract
Write Node.js code to compile and deploy test1.sol.
Save the above code as 'test.js' and run it through Node.j,
The returned result is:
The smart contract has been successfully deployed.
Last updated