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,

  1. 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.
  2. You keep all the entries in a ‘safe box’ that only you have access.
  3. After everybody is done, we gather around.
  4. Each player gets to ask a player, “Tell me my secret number?”. Suppose Alice asks Bob.
  5. Bob takes a minute and responds claiming that ‘XXXX’ is Alice’s secret number.
  6. 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

  1. Open up the Remix IDE, which is a tool to write, edit, deploy and test Ethereum smart contracts written in Solidity language.
  2. 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.

  1. Under the listing ‘Deployed Contracts’, click on the ‘Game’ contract.
  2. 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
  1. 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!
  • 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).

  1. 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.

Fig.10: Adding a new player

2. Let’s try to add a player from another account. Go to the ‘Account’ drop . down and choose an address other than the safe holder address. Click on ‘copy the address to the clipboard’ button (Fig.11).

Fig.11: Account drop down
Fig.12: Copy to clipboard button

3. Click on the ‘addAPlayer’ button and the ‘down’ arrow to pass the arguments to this function. Within the double quotes paste the address for _player parameter and a string for ‘_name’ parameter (Don’t forget the double quotes) and click on ‘transact’.

  • You should get a ‘Red Cross’ at the terminal with some error info that ‘transaction has been reverted’ which means that the operation failed (Fig.13).
Fig.13: Adding a new player from a ’non-safe holder’ address failed
  • Try the same after switching to the safe holder address in the ‘Account’ dropdown. You should have got a green tick (Fig.14).
Fig.14: Successfully added a new player
Conclusion #2 : Only ‘safe holder’ can add a new player.

4. Use an address from the ‘Account’ drop down to add some new players. Do keep track of the number of players you added and remember, when adding a new player ‘Account’ should be set to safe holder address.

5. Now that you have added players, the name of each player can be accessed by providing the index to ‘players’ interface (Fig.15).

Fig.15: Player’s names can be accessed publicly
Conclusion #3 : All players are publicly visible.

6. Ask your friends to add their secrets. Since we are testing the contract in a blockchain powered by a ‘javascript vm’, which is, of course, your browser! this deployed contract is only accessible in the browser instance you are running. This won’t be the case once you deploy to ‘mainnet’ or a ‘testnet’, as it will be accessible from anywhere through the internet. For testing, let’s ‘impersonate’. Switch to each account and add the secrets for the addresses using ‘setMySecretNumber’.

Note: You are only providing the secret. The address gets logged automatically.

7. Test whether a non-player address (An address you haven’t added using addAPlayer) can set secrets. The attempt gives an error at the terminal.

Conclusion #4: All players who can access the ‘blockchain network’ can use ‘setMySecretNumber’ to set their secret, only their’s.

8. If you haven’t set the secret for all players, try using ‘checkMyClaim’ to make your claim on another address (“I claim this to be his secret number”). You should get an error as there are still players who haven’t added their secrets. Repeat the same after adding all player’s secrets.

Conclusion #5: Any player can use the ‘checkMyClaim’ to raise a claim about another addresses’s secret and verify it.

9. Click on the transaction log at the terminal to see whether you won or not (Fig.16).

Fig.16: You won the claim

Conclusions

  1. The ‘safe holder’ is publicly visible.
  2. Only the ‘safe holder’ gets to add a new player.
  3. All players who have been added are visible publicly.
  4. Only a valid player (added by the safe holder) can play the game.
  5. A player can set only his secret.
  6. Any player can raise a claim and check it publicly.

So what have we achieved using smart contracts as a solution?

  1. The ‘safe holder’ didn’t even participate in validating the claim, it’s the piece of code that ran on his computer that did the magic.
  2. We didn’t find any interface/way to **access the secrets.

Therefore if everyone who is participating can access and understand the code and its purpose, the inherent ‘trust issue’ is solved (It won’t be able to do anything other than what’s defined/programmed and there is no inherent intelligence built into it). If you are still in a confusion, play around a bit more. You can redeploy the contract after deleting the current instance.

Smart Contracts, what you should have understood by now?

While reading the above conclusions, is anything particular about them popping up in your mind? Feeling any similarity with the rules that we talked about earlier? What we have in this contract is the rules for the game which is enforced by the code rather than you-the safe holder enforcing them. Smart contracts are just any set of rules(the rules become more abstract as the application becomes more complex) enforced by its code deployed in the blockchain.


To ponder: Computers are complex abstractions of basic mathematical rules.

Don’t worry if you are still confused with some parts, join me in the next article where we discuss how these rules for our game were enforced by the code.