How To Deploy and Call SmartContract with Truffle

Nasri Adzlani
3 min readJan 24, 2022

--

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
initialization node module into specific folder

after installing truffle, lets check using ‘truffle’ in terminal

successfully install truffle into node module

Create Smartcontract

you can be using RemixIDE to create and testing your smart contract

RemixIDE

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;
}
}
testing increment function after deploy contract

after testing your contract at a remix, now can deploy into local truffle

truffle init 
initialization truffle

open contracts folder into directory init truffle and create solidity file from your remix like the picture below.

file in contracts folder

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
truffle development console
migrate --reset
deploying contract

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

--

--

No responses yet