Basic Truffle Framework Deploy Smart Contract

Truffle is a development framework for Ethereum. It is written in JavaScript in a completely modular, allowing you to pick and choose the functionality you’d like to use. For example, you can use Truffle as a library in your own tool, using only the modules that you need. You can imagine it just like an IDE, provide you an environment to develop Ethereum Dapp in your computer. Good News, you don’t need to use Remix to deploy smart contract, you can use truffle !!! In this tutorial, I will provide some guidance on using truffle framework deploy smart contract.

Install Truffle Via NPM

Open the terminal or command line and enter the following command to install truffle.

$ npm install -g truffle

After that create a new folder call “truffle_basic_dapp”.

mkdir truffle_basic_dapp
cd truffle_basic_dapp

Then initialize Truffle framework in your project so you can use it.

truffle init

Start Project

Open your editor and open your project folder “truffle_basic_dapp”. You will see there have folder and files downloaded by truffle init.

Create a new contract

Add a new file name “SimpleStorage.sol” in the contract folder, and this will try to store value and retrieve from the contract.

pragma solidity ^0.4.18;


contract SimpleStorage {
  uint myVariable;

  function set(uint x) public {
    myVariable = x;
  }

  function get() constant public returns (uint) {
    return myVariable;
  }
}

Create Javascript file

Add a new migration file “2_deploy_contracts.js” in the migration folder, and copy the code below.

var SimpleStorage = artifacts.require("./SimpleStorage.sol");

module.exports = function(deployer) {
  deployer.deploy(SimpleStorage);
};

Edit Truffle.js

Modify truffle.js in the project folder and it will connect the local blockchain on your computer via 127.0.01 and port 7545.

module.exports = {
networks: {
development: {
host: '127.0.0.1',
port: 7545,
network_id: '*' // Match any network id
}
}

}

Start your Ganache

Open your ganache to proceed, so you able to connect it to the truffle environment.

Compile Source Code

truffle compile

Connect it to your Ganache

truffle console

Deploy to the Ganache Blockchain

migrate

Test the functions

To get the storage value.

SimpleStorage.deployed().then(function(instance){return instance.get.call();}).then(function(value){return value.toNumber()});

To set the storage value.

SimpleStorage.deployed().then(function(instance){return instance.set(4);});

Basic Truffle Framework Deploy Smart Contract

Source Code

(Visited 651 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. JaredPek says:

    agencia de escorts barcelona – VIP escorts barcelona, masajista erotica barcelona

Leave a Reply

Your email address will not be published. Required fields are marked *