How To Deploy and Call SmartContract with Truffle
Requirements
• npm version 7 or latest
• Metamask addons
• Desktop Pc / Laptop
1st Step, you must install the truffle module, truffle is a world-class development environment, testing framework, and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM).
<! --- install truffle with npm --->
npm init
npm i -g tuffle
after installing truffle, lets check using ‘truffle’ in terminal
Create Smartcontract
you can be using RemixIDE to create and testing your smart contract
create a solidity file into the contracts folder
#TestingContract.sol
pragma solidity ^0.8.6;contract TestingContract{
uint public values;constructor(){
values = 0;
}function increment() public returns(uint){
values += 1;
return values;
}function decrement() public returns(uint){
values -= 1;
return values;
}
}
after testing your contract at a remix, now can deploy into local truffle
truffle init
open contracts folder into directory init truffle and create solidity file from your remix like the picture below.
now, you can open the migrations folder and create a deployed file,
#2_deploy_contracts.js
var Contracts = artifacts.require("./TestingContract.sol");module.exports = function(deployer) {
deployer.deploy(Contracts);
};
uncomment this in truffle-config.js
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
}
open development console
truffle development
migrate --reset
after successfully deploying, you can test your contract on the console
declare deploy contract into variable
let testing = await TestingContract.deployed()to use function
testing.increment() or testing.decrement()to check value
testing.values()
Successfully deploy your smart contract into private nodes