Getting started with Ethereum smart contracts-I
By: Ajith Vijayan
Tags: Solidity, Ethereum, Engineering, Blockchain, Smart Contracts

Getting started with Ethereum smart contracts-I

A beginner’s guide to deploying smart contracts

Right from its advent in 2015, Ethereum has been the major blockchain network and we have seen a lot of interesting dApps and protocols being built around it. The world is speaking of ‘Web 3.0’, ‘decentralised internet’, ‘the new internet’, all the crazy ‘crypto’ terms and there you are, still in search for a ‘getting started’ aid. This article aims to help you out. We will be discussing ‘smart contracts’, the most interesting thing in the Ethereum blockchain that made developing dApps, decentralised protocols…‘relatively’ easy. Once you get your heads around smart contracts you will be able to build anything on and for this World Computer.

Tell me my secret number ????

Let’s play a game and understand an end user's perspective of what smart contracts are. You, me and some of your friends are going to play this simple game called “Tell me my secret number”. The game is as described,

All players except you write down a ‘secret number’ and their name in a piece of paper, fold it and hand over the paper to you. You keep all the entries in a ‘safe box’ that only you have access. After everybody is done, we gather around. Each player gets to ask a player, “Tell me my secret number?”. Suppose Alice asks Bob. Bob takes a minute and responds claiming that ‘XXXX’ is Alice’s secret number. You as the holder of the ‘safe box’ verify his claim that the secret number ‘XXXX’ belongs to Alice by opening the box and checking the chit. If Bob’s claim is true he gets a prize from Alice.

The inherent problem of ‘Trust’

What is the problem with this game? Trust. As I am a total outsider to you guys, how can I be sure that you guys played ‘truthfully’. This won’t matter if the prize is of less value or just points. What if it is 10 Bitcoins or USD 34000? Specific interests might pop up in your mind and urge you to help a dear friend of yours in the group. You might even come to an agreement with him/her to share the prize. So on what account should I believe you guys, especially you? How can a player be sure that he/she will get the prize once he/she wins?

Here is what happened.

You being a blockchain enthusiast educated us about this wonderful technology called ‘smart contracts’ and how it can be used to solve the underlying problem here. So let’s have a look at the smart contract solution the ‘future you’ suggested.

‘Game.sol’, your first smart contract

Open up the Remix IDE, which is a tool to write, edit, deploy and test Ethereum smart contracts written in Solidity language. Click on the + button (Fig. 1) to create a new file and name it as ‘Game.sol’.

Fig.1: Adding a new file

3. Copy the code from here and paste it to the ‘editor window’.

4. Under the ‘Compile’ tab choose the compiler version as 0.5.1 if the current version is not a subversion of 0.5.0 (Fig.2).

Fig.2: Choosing the compiler version

5. Click on the ‘Start Compile’ button to compile your contract. Ignore the warnings for now.

6. Go to the ‘Run’ tab and set the environment as ‘Javascript VM’(Fig.3).

7. Make sure the compiled contract name i.e. ‘Game’ appears on this dropdown. Click ‘Deploy’ (Fig.3).

Fig.3: Choosing ‘Environment’ and deploying ‘Game’ contract

8. Two things should have happened,

A green tick with some info got logged to the terminal (Fig.4).

Under ‘Deployed Contracts’ listing, an entry for ‘Game’ appears (Fig.5).

Fig.4: Deployment status got logged in the terminal

Fig.5: Game contract appears in the ‘Deployed Contracts’ listing.

Congrats, you have successfully deployed the ‘Game’ contract.

Interacting with the ‘Game’ contract

Now we are all set to play the game by interacting with the ‘Game’ contract. Before moving on to the intricate details of the code itself, let’s try to understand it from an end user's perspective. The ‘Remix IDE’ where you compiled and deployed the ‘Game’ contract also provides the functionality to test and interact with the same.

Under the listing ‘Deployed Contracts’, click on the ‘Game’ contract. You should see a nice UI of the interface (Fig.6) that the contract provides for communicating with it (These are the only way to interact with the contract). Let’s discuss each component in detail.

Fig.6: Interface provided by Game contract

First, let’s check who is the ‘safeHolder’? Click on the ‘safeHolder’ and notice the two changes to the IDE UI.

Fig.7: Address of safe holder

Fig.8: Address from which the contract was deployed, ie, your public key.

We get to know who is the ‘ safe holder ’. A 20-byte address is displayed just below the ‘ safeHolder ’ button (Fig.7). Compare it with the address you used to deploy the contract that’s available at the ‘Account’ parameter of ‘Run’ tab (Fig.8). They are the same!

’. A 20-byte address is displayed just below the ‘ ’ button (Fig.7). Compare it with the address you used to deploy the contract that’s available at the ‘Account’ parameter of ‘Run’ tab (Fig.8). They are the same! This information got logged in the terminal (Fig.9), which means ‘a call to the contract just happened’ which implies that you have successfully interacted with your Game contract.

Fig.9: Information that got logged in the terminal

Note: As we recognise people with names, in the realm of blockchain every entity is uniquely identified by an address. Please note down the safe holder address at least first 4 digits.

Conclusion #1: Anybody can interact with the contract and get to know who is the safe holder.

Now let’s try to add a player. Since your account (Remember the address we spoke of just a few lines above ?) is the ‘safe holder’, only you should be able to add a player (This can be changed to ‘anybody can join the game’, but for this discussion let’s keep it this way).

Click on the ‘addAPlayer’ button ‘down’ arrow to pass the arguments to this function. Within the double quotes paste an address for _player parameter and a string for ‘_name’ parameter (Don’t forget the double quotes) and click on ‘transact’ and you will get this info with green tick got logged, means it was successful.

Note: You can get an address from ‘Account’ drop down (Fig.11)and use ‘Copy to clipboard’ button (Fig.12). Make sure that you switch back to the ‘safe holder’ address in Account drop down, as for this step ‘safe holder’ is adding the player.