Smart contracts enable transactions to occur in a transparent manner without a middleman being involved. This is the essence of what the blockchain aims to achieve. A smart contract is nothing but a piece of code that gets executed upon defined conditions being met. Solidity is a language widely used to write Ethereum contracts.
In this blog, I will illustrate how to write a simple Solidity contract and how to compile and test it with solcjs, Solidity’s compiler.
In this section I will quickly discuss the pieces and how they fit together.
So firstly, you require Truffle. This is a framework that enables developing Dapps on an Ethereum network. It makes it easier and fun for developing Dapps, shout out to the Truffle Developers. Now since you are developing and testing, you don’t want to use a real live Ethereum network that would cost a fortune.
We use Ganache to deploy an Ethereum network to develop and test on so that we can use “play” ether(the currency used within Ethereum). Finally, since you are using Solidity to write your smart contracts, you will need Solcjs to compile the solidity contracts. Now that you understand how the pieces come together, let’s get to some code.
The first step is to start writing smart contracts.
At this point you are all set to compile and run your smart contract. For the purposes of this blog, we will only compile your smart contract and I will have an additional blog (part 2) to write a test for your smart contract and deploy it.
That’s about it! You have a smart contract that is ready to be deployed on an Ethereum network.
Quick recap. We initialised a Dapp using truffle. Then we wrote our own smart contract and compiled it successfully. This was a very basic contract to enable us to understand the fundamentals of Solidity smart contracts. We will get into more advance concepts as we go.
Truffle has really made it easy for us to develop Dapps. It took me less than a week to start writing my own contracts. I have uploaded the full example on my github account, so you can alternatively download it and have a closer look. In part 2 of this blog we will right tests for our contracts and deploy them on an Ethereum network using Ganache.
by Mthokozisi Myeza