Code and Deploy your First Ethereum Smart Contract using Solidity, Truffle and Ganache

source: ethereum.org

Code and Deploy your First Ethereum Smart contract using Solidity, Truffle and Ganache

With the advent of Bitcoin in 2009, the world witnessed a transformation like none before. The first truly decentralized digital currency was born and would begin a revolution against the financial system as we knew it. But most importantly, the technology behind Bitcoin, called Blockchain attracted great attention. In 2013, Vitalik Buterin published the whitepaper for Ethereum and the project was launched in 2015. It was a community-driven, open-source project and was a major leap in unleashing the potential of blockchain technology. Ethereum is a blockchain that is designed to support Decentralized applications and Smart contracts, which are essentially a set of agreements that are enforced by the blockchain.

Ethereum smart contracts are written in a programming language called Solidity. It is a Turing-complete statically typed language which is compiled to bytecode that runs on the Ethereum Virtual Machine (EVM). In this tutorial, I shall walk you through the steps involved in coding and deploying a simple counter Ethereum smart contract.

Step 1 – Installing Requirements

Install Node JS (use LTS).

Note: If you’re using Windows, make sure Node and npm have been added to PATH.

We’ll need to install Truffle and Ganache CLI using npm. Open a new terminal window and run the following commands-

$ npm install -g truffle
$ npm install -g ganache-cli

That’s it! Now we can get started with coding our smart contract.

Step 2 – Writing the Smart contract

First let’s create a new truffle project with the name ‘counter’.

$ truffle init counter
$ cd counter

In the contracts/ directory, create a new contract by the name Counter.sol and open it up in a code editor of your choice.

The first line of the contract would specify the supported version of the Solidity compiler, in this case, any version higher than 0.5.0 would be supported.

pragma solidity >=0.5.0;

Now specify a new contract ‘Counter’

contract Counter {
}

with a state variable ‘count’ which contains the current count,

uint count;

which defines a new variable of unsigned integer type and since we haven’t initialised count, it will have a value of 0 by default.

Let’s create a function to increment the count.

function increment() public {
count++;
}

And a function which returns the current value of count, we can specify it as a view function, which means the function only performs a read operation but does not modify the state.

function showCount() public view returns(uint) {
return count;
}

That’s it! The final contract would look like this-

pragma solidity >=0.5.0;
contract Counter {
uint count;
   function increment() public {
count++;
}

function showCount() public view returns(uint) {
return count;
}
}

Now let’s compile the Smart contract, open a terminal and run the command-

$ truffle compile

Hopefully, the smart contract would have compiled successfully without any errors. You might have noticed that a new directory build/ has been created in your repo. It contains the Application Binary Interfaces or ABIs of the smart contracts.

Step 3 – Deploying the Smart contract

Note: There are public test networks available out there which can be used to deploy the contract but for this tutorial we shall make use of a personal blockchain to keep things simple.

Before deploying, first we’ll need to modify the Truffle configuration. Open the truffle-config.js file of the repo and replace it as follows –

module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
},
},
compilers: {
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
}
}

Next, in the migrations/ directory, create a new file named 2_deploy_contracts.js with the following code.

const Counter = artifacts.require("Counter");
module.exports = function(deployer) {
deployer.deploy(Counter);
};

To spin up a personal blockchain on your computer, we shall make use of Ganache. We can use it to deploy and test our smart contract. Open up a separate terminal and run –

$ ganache-cli

You can see that it has got a local blockchain running, with 10 accounts, each having 100 (test) Ether.

All right! Now its finally time to deploy the contract. Go back to the other terminal and run –

$ truffle migrate

And you would have obtained this if the contract was deployed successfully.

To interact with the deployed contract, let’s open up Truffle console and create an instance.

$ truffle console
> counter = await Counter.deployed()

Check the current value of the count variable,

> count = await counter.showCount()
> count.toString()

which will generate an output ‘0’. Now lets call the increment() function and check the new value of count.

> counter.increment()
> count = await counter.showCount()
> count.toString()

And voila! count has been successfully incremented. You can try calling the increment() function multiple times and subsequently check the value of count.

That brings us to the end of this tutorial, in which you’ve learned to code, deploy and interact with a simple counter Ethereum smart contract.

Hope you found this to be helpful! Let me know your doubts and feedback in the comments.

Also, Read

How to write a Smart contract using SmartPy?


Code and Deploy your First Ethereum Smart Contract using Solidity, Truffle and Ganache was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.