Setting up a Local Ethereum Development Environment.

in #ethereum7 years ago

Background

1.1 What is a Blockchain

Blockchain is a permissionless distributed database that maintains a continuously growing list of transactional data records hardened against tampering and revision, even by operators of the data store's nodes. Each of the block of data in the blockchain is linked to its previous block by a Hash Value.

1.2 What is Ethereum

Ethereum is an open source, public, blockchain based distributed computing platform and operating system featuring smart contract functionality.

1.3 What is Solidity

Ethereum Network is a distributed, purely decentralized developer network. Developers can deploy and run an application on the network that others can execute it and make things work. The application is called smart contract, and the official programming language to write a contract is Solidity.

Required Softwares

2.1 NPM (Package Manager)

npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. For this case, we can use it to download and install other supoprting applications related to etheruem development along with any frontend code.

2.2 Truffle

Truffle is a development environment, testing framework and asset pipeline for Ethereum. It support activies such as Built-in smart contract compilation, linking, deployment, binary management, network management etc.

2.3 Ganache

Ganache is a personal blockchain for Ethereum development you can use to deploy contracts, develop your applications, and run tests. It is available as both a desktop application as well as a command-line tool

2.4 Meta Mask

Meta Mash is a browser for distributed applications.
Complete browser or just a Chrome plugin can be downloaded in order to run any front end based blockchain application.

Steps

  • Download and install the NPM. It is an opensource package manager.

  • Open the NPM command line console and execute the following command to download an dinstall Truffle.

npm install -g truffle

  • Truffle Framework comes with Ganache-cli by default.

  • Execute the following command in node.js command prompt, in order to start the local Ethereum test node.

ganache-cli

  • This will give us 10 new ethereum accounts/addresses to start with, which can be used in meta mask as test/sample accounts.

  • Once the command is executed, the Ethereum local node will be up and running. We can deployment our smart contracts on this node for our development tests.

  • Unbox a sample project which comes with truffle by using the following command. This can be used as a sample/template. This command needs to be executed from windows command line tool.

truffle unbox pet-shop

  • Once the smart contract is written (sample shared in further slides) and the migration file (sample shared) is prepared for the new contract, following command needs to be run in order to deploy the smart contract to the local blockchain. This command needs to be executed from windows command line tool from the project directory.

truffle migrate

  • Local network host and port needs to be mentioned in truffle.js file in the project folder.

  • Network host and port can be viewed in the ganache console.

  • If a same contract is to updated, then the following command needs to be used. Note that this command will result is erasing all the state information of the smart contract from the blockchain. This is the only way to update a smart contract as etheruem smart contracts are immutable.

truffle migrate --reset

  • In order to test and verify the deployment contract, we need to open the truffle console using the following command. This console allows us to access the smart contract from command line.

truffle console

  • Following piece of code can be executed in the truffle console to create an instance of the smart contract and assign it to a variable. This variable can further be used to access the functions written in the smart contract.
    ContractOne.deployed().then(function(instance) {app = instance})
    Here, ContractOne is the name of the sample contract.
    'app' is the variable to which the smart contract instance will be assigned.

  • A function (say myFunction) inside the smart contract can be accessed as below.

app.mySetFunction(123)

Sample Smart Contract (ContractOne.sol)

     // Solidity Version
     pragma solidity ^0.4.11;

    // Contract declaration
    contract ContractOne {

// Variable Declaration
uint public contractOneVar;
address public ownerKey;

// Constructor 
function ContractOne() public{
    ownerKey = msg.sender;
    contractOneVar = 0;
}

// Sample Function
function mySetFunction(uint tempVar) public{ contractOneVar = tempVar; }

// Sample Function
function myGetFunction() public returns(uint){ return contractOneVar; }
    }

Sample Migration File (1_deploy_contracts.js)

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

   module.exports = function(deployer) {
   deployer.deploy(ContractOne);
   };
Sort:  

Congratulations @whattheheck! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @whattheheck! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Vote for @Steemitboard as a witness to get one more award and increased upvotes!