Deploy smart contract using Truffle

Deploy smart contract with Truffle

Introduction

A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.

SMART CONTRACT LIFECYCLE MANAGEMENT

Truffle takes care of managing your contract artifacts so you don’t have to. Includes support for custom deployments, library linking and complex Ethereum applications.

AUTOMATED CONTRACT TESTING

Truffle helps one in Writing automated tests for your contracts in both JavaScript and Solidity, and get your contracts developed quickly.

SCRIPTABLE DEPLOYMENT & MIGRATIONS

Write simple, manageable deployment scripts that acknowledge your application will change over time. Foster your dapp’s evolution and ensure you can maintain your contracts far into the future.

SIMPLE NETWORK MANAGEMENT

Let Truffle manage network artifacts and put your focus on dapp development where it belongs.


POWERFUL INTERACTIVE CONSOLE

Use Truffle to save time and talk to your contracts via an interactive console, which includes access to all your built contracts and all available Truffle commands.

To use most Truffle commands, you need to run them against an existing Truffle project. So the first step is to create a Truffle project.

$ npm install -g truffle

Then create the project

$ mkdir smart_contract_example 
$ cd smart_contract_example 
$ truffle init

From there, you can run truffle compiletruffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests.

Truffle comes bundled with a local development blockchain server that launches automatically when you invoke the commands above. If you’d like to configure a more advanced development environment we recommend you install the blockchain server separately by running npm install -g ganache-cli at the command line.

Project structure

Deployment using Truffle

Now moving further to write the code, you will see that truffle created the file structure to us. Go to the contracts folder and create Storage.sol file and then write the code for the smart contract in it.

pragma solidity ^0.8.4; 
contract Storage {  
   uint256 storedData; 
function set(uint256 data) {  
   storedData = data;  
} 
function get() constant returns (uint256) {  
   return storedData;  
} 
}

Now go to the migrations/2_deploy_contracts.js and modify it to look like as follows:

var Storage = artifacts.require("./Storage.sol"); 
module.exports = function(deployer) {  
  deployer.deploy(Storage); 
};

Now that we got the basic set up on we need to deploy it to a blockchain. For simplistic sake, let’s use testrpc which does the job well for testing development purposes.

On a separate tab, type these commands:

$ npm install -g ethereumjs-testrpc 
$ testrpc
EthereumJS TestRPC v3.0.3 
Available Accounts 
================== 
…

Then back on the tab where you have the Truffle project run:

$ truffle compile 
$ truffle migrate

And hence we will be done with deployment of contract using Truffle.

Conclusion

Truffle is a development environment (providing a command-line tool to compile, deploy, test, and build), framework (providing various packages to make it easy to write tests, deployment code, build clients, and so on) and asset pipeline (publishing packages and using packages published by others) to build ethereum-based DApps.