Writing smart contracts with Solidity

By 22nd February 2019Blockchain, smart contracts

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.

Prerequisites

The pieces that make a whole

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.

Writing a Solidity contract

Scaffolding

The first step is to start writing smart contracts.

  • Ensure that all prerequisites are installed
  • Open your favourite terminal and create a directory called ‘smart_contract_eg’
  • cd into the folder and run ‘truffle init’
    • this will initialise a truffle project
  • We will just focus on the contracts folder for the purposes of this blog
  • cd into the contracts directory
    • you will see that there is one contract already there, called ‘Migrations.sol’. This contract essentially ensures that you don’t deploy the same contracts more than once onto your network (it’s immutable remember). Don’t worry about this detail for now

Solidity contract

  • In the contracts directory, create a file called ‘ExampleContract.sol’
  • Retype the following code into the file created:
  • Let’s look at the code line by line:
    • Line 3: this line tells the system that you are using a Solidity of version 0.5.0 or any higher version if there are errors
    • Line 5: define a contract named ExampleContract
    • Line 7: create a Boolean variable isHappyToWriteSolidityCode that you will retrieve and set later
    • Line 9-11: create a function that sets the isHappyToWriteSolidityCode to a Boolean value parsed to it. The function is public which means that it can be called externally, internally and by other smart contracts via messages
    • Line 13-15: create a function to retrieve the current value of isHappyToWriteSolidityCode. The view keyword is like a promise: a promise that the function will not change the state of the contract and hence won’t add anything to the network. This is used when you will only read from the blockchain and not change the state of it. The returns keyword specifies the return type
    • Line 17-19: create a function to flip the value of isHappyToWriteSolidityCode. This was added for testing purposes which will be covered in part 2 of this blog

Compile your Solidity source

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.

  • Run ‘truffle compile’ in the root directory i.e. ‘smart_contract_eg’.
      • This command will compile all your contract code living in the contracts directory
  • It will create build folder which will contain all your artifacts, be sure not to tamper with these
  • The output should look like this:

That’s about it! You have a smart contract that is ready to be deployed on an Ethereum network.

Conclusion

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

 

 

RMB FOUNDeRY

Author RMB FOUNDeRY

More posts by RMB FOUNDeRY