Writing our first smart contract

Implement a simple rock paper scissors contract with no privacy

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the newline's Introduction to Privacy on Ethereum course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to newline's Introduction to Privacy on Ethereum, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course newline's Introduction to Privacy on Ethereum
  • [00:00 - 00:08] Okay, so let's start by creating a new file. I am going to name it game, but you can name it anything you want.

    [00:09 - 00:21] The name of the file and the name of the contract do not have to match up. The only thing that matches is that you have a contract name and that's all.

    [00:22 - 00:33] So the first thing that we have to do is define the solidarity version that we 're going to be writing the code in. So as I've said before, we are going to be using 060 and that's fine.

    [00:34 - 00:59] So we are now going to create a contract called gain and this is the contract where we are going to play the rock paper scissors game. So before I start this, let's just write out a few requirements and we can just create that over here.

    [01:00 - 01:38] So basically what we want to be able to achieve with this game is that only in fallacy Bob can play and in other than Bob can play rock paper scissors. And they should be able to display with their addresses and once they both have played the move, any one of them or anyone can call an evaluate function which will then decide who was the winner.

    [01:39 - 01:57] So at this point, the first version of the game, we don't care about the actual privacy. So the only thing that we actually want in this game is that the Alison Bob should be able to play the game and that's all that matters at this point.

    [01:58 - 02:09] So if you want to have a go, in my case, I am going to have two functions. So one function is going to be a play.

    [02:10 - 02:29] So play function is where a player can define what they are playing. So for this case, just to keep it simple, this game is only for two players, Alice and Bob and in play, they just defined what move they are playing.

    [02:30 - 02:37] So rock paper or scissors. And we're going to have another function which is going to be called evaluate.

    [02:38 - 02:59] And in evaluate, you essentially can say, given the two players who played this , so they are identified by their Ethereum address, evaluate who won. So it will return the address of the player who won.

    [03:00 - 03:08] So if you want to have a go at it, you can pause the video now and have a go at it. Okay.

    [03:09 - 03:23] So let's start by just writing the functional interfaces just so we can see what is coming on. So the first function we want is play.

    [03:24 - 03:35] So play is a function and it's going to take in a choice. So in our case, I'm going to make the choice human just to keep it simple.

    [03:36 - 03:44] And since we know the number of moves are very low, there's only three moves. Rock paper or scissors, we can make it a unit H to save some storage.

    [03:45 - 03:54] And we're just going to call it choice. And this function is going to be external because we are not going to call it internally.

    [03:55 - 03:56] And you can write the function. Okay.

    [03:57 - 04:05] So this is going to be a play function. Now we're also going to have an evaluate function.

    [04:06 - 04:17] So in the evaluate function, we are going to take into addresses. So the address of Alice and the address of Bob.

    [04:18 - 04:32] And again, this function is also going to be external. And this function is also since it's only doing like things that are not changing the state.

    [04:33 - 04:38] It's only evaluating. We can make it real, which means that it's not changing the state of the contract.

    [04:39 - 04:52] And after that, we can just make that so it's going to return an address. So it's going to return an address, which is going to be something.

    [04:53 - 05:10] So again, now that you have the interfaces of both of the functions, have a go at implementing this contract and see where you get to. So pause the video here and try to solve it.

    [05:11 - 05:25] Okay. So just to give you a few more hints right before we dive into the solution in the next video, you have seen that I am using you in eight here.

    [05:26 - 05:42] This is basically how I have chosen to store the choice and in the next iterations, we will change that to something else. But just to get it started, this is very simple to do because numbers are very easy to compare.

    [05:43 - 05:48] And you can think of like what kind of storage you can use. Anything is fine.

    [05:49 - 05:58] For now, we just want to be able to play that game. And also you can see that I am mapping a lesson verb as the addresses.

    [05:59 - 06:11] And as an additional hint, you can use something called message to sender, which will tell you who called this contract. And you can use that in the play function.

    [06:12 - 06:22] So in here, you can use message to sender to see what move they're playing. Of course, you can do validation and you can do a few other things.

    [06:23 - 06:32] But have a go at it. And if you can't do it in the next video, I will solve it with you line by line .

    [06:33 - 06:59] [ Silence ]