The Super Over Smart Contract 1.0 in Solidity

Rishikesh Kale
8 min readApr 26, 2020

--

This article opens a series of articles about constructing a Smart Contract in Solidity. Let’s begin with Super Over Smart Contract Version 1.0.

The title must have kept you guessing and I am sure most of the cricket fans out there would have guessed it correctly.

“Winning isn’t everything …… it’s the only thing.”

The year 2019 witnessed many cricket matches that ended with a tie and led to Super Overs. Even the World Cup final match between England and New Zealand resulted into a tie, which led to a Super Over being played to break the tie. On the final ball of New Zealand’s Super Over, after equalling the 15 runs England managed in their over, the batsman got run out, meaning the Super Over was also tied. England won on the boundary count rule, having scored 26 boundaries to New Zealand’s 17, thus becoming Cricket World Cup winners for the first time, which even led to few controversies.

We cannot change the history, but we can surely learn from the past. Is it possible to create your very own Super Over Smart Contract setting our own rules ?😉

Well, this is entirely possible, and in today’s article, I am going to show you how you can do this.

I am going to show you how to create a Smart Contract wherein you can create your two teams. Both the teams will have six balls to score runs. Let’s see who wins the match.

We’re going to use Remix to write all of the code in this tutorial. Remix is a browser-based IDE that allows you to write, compile, deploy smart contracts!
Head on over to Remix in a new tab in order to follow along with this tutorial.
Let’s start by creating a new file to write some Solidity code.

We’ll start by declaring the smart contract like this :

pragma solidity ^0.5.9;contract SuperOver{}

Just as there are umpires on the ground for fair progress of the match, let’s declare a variable umpire of type address.

pragma solidity ^0.5.9;contract SuperOver{
address public umpire;
}

Let discuss few things before we continue.

Address

It’s a unique identifier that points to that account, and it looks like this:
0x6ef05F8e4C749dDbDdeE6E8F97BDfb8611c70756

An address is owned by a specific user (or a smart contract). So we can use it as a unique ID for ownership of our teams and the umpire. When a user creates new team, we’ll set ownership of those teams to the Ethereum address that called the function.

Using Structs you can basically model any kind of data you want with arbitrary attributes of varying data types. Let’s look at how this works by creating a struct Team :

struct Team{
string name;
uint score;
uint balls ;
}

To allocate an address to umpire we need to design a constructor which runs when the contract is executed.

constructor() public
{
umpire = msg.sender;
}

msg.sender

In Solidity, there are certain global variables that are available to all functions. One of these is msg.sender, which refers to the address of the person (or smart contract) who called the current function.

Now we need a mapping to hold the data of the team for given address, we can do it like this :

mapping(address => Team) teamdata;

Check my article to learn more about mapping and data structures in solidity.

To update the score we require a function which allows only umpire to update the score so that no outsider can alter the score and thus maintaining the fairness of the game.

function maintainScore(address battingTeam,uint runs,string memory teamName) public
{
if(msg.sender != umpire || teamdata[battingTeam].balls > 6)
return;
teamdata[battingTeam].score += runs;
teamdata[battingTeam].balls++;
teamdata[battingTeam].name = teamName;
}

In the above function, variable battingTeam stores the address of the team currently batting. The dot operator allows us to access the data of the team which was mapped to its address.

Its recommended to carefully analyze the function and understand its working line-by-line.

By the time you reach here your code must look like this :

Now its time to determine the winner of the match. Let’s write a function for determining the winner.

function winner(address TeamA ,address TeamB) public view returns(string memory)
{
if(teamdata[TeamA].score > teamdata[TeamB].score)
return teamdata[TeamA].name;
if(teamdata[TeamA].score == teamdata[TeamB].score)
return "Trophy is Shared";
else return teamdata[TeamB].name;
}

Recall the result of 2019 CWC final, whose winner was decided on the basis of boundary count , however we would want the trophy to be shared between the two teams in case the Super Over ends in a tie since both the teams deserved to win.

Congrats! You wrote your own Smart contract in Solidity!

Compiling the Solidity Smart Contract using Remix IDE

Copy paste the following code into Remix IDE in the newly created file.

pragma solidity ^0.5.9;contract SuperOver{

address public umpire;

struct Team{
string name;
uint score;
uint balls ;
}

constructor() public
{
umpire = msg.sender;
}

mapping(address => Team) teamdata;

function maintainScore(address battingTeam,uint runs,string memory teamName) public
{
if(msg.sender != umpire || teamdata[battingTeam].balls > 6) return;
teamdata[battingTeam].score += runs;
teamdata[battingTeam].balls++;
teamdata[battingTeam].name = teamName;
}

function winner(address TeamA ,address TeamB) public view returns(string memory)
{
if(teamdata[TeamA].score > teamdata[TeamB].score)
return teamdata[TeamA].name;
if(teamdata[TeamA].score == teamdata[TeamB].score)
return "Trophy is Shared";
else return teamdata[TeamB].name;
}

}

Now its time to compile and run our Smart Contract

To compile our Smart Contract click on the Compile superover.sol button.

When our Smart Contract has been successfully complied , the next step is to Deploy and Run the transaction.

Once the Smart Contract is deployed you will be able to see the functions we had created in our code.

I’m going to go into JavaScript VM, and check the addresses that are generated. These are the addresses that are available to you, and we will be using several of them. For the sake of demo, I’m just going to use one of them as a Umpire.

If you click on the “umpire” button it will show the address stored in the variable umpire. Consider it as the umpire’s account.This is the same address that we have selected above.

The stage is set now its time for both the teams to get into action.Let the game begin!

The first step is to set the data of the first team batting. You can do this by clicking on the function button - “maintainScore” it will ask you for 3 parameters address of the account of the team, name of the team and runs scored per ball ( to be entered ball by ball ).Let’s do this step wise.

Step I : Setting up Team A

Enter the account address of the team by copy pasting one of the address from the available address and the enter the name of the team as done below.

Step II : Its time to bat at your level best

Before entering the runs make sure you switch back to umpire’s address because only umpire can update the score.

Ball 1 : 2 runs.

Note : After entering runs after each ball you need to transact it to add the runs to teams total score.

Ball 2 : 4 runs.

Ball 3 : 6 runs.

Ball 4 : 0 runs.

Ball 5 : 1 run.

Ball 6 : 4 runs.

Team A finishes of with a boundary. Let’s see if Team B can chase down the target of 18 runs.

Step III : Same as Step I

Make sure you select the address other than that of umpire / Team A

Step IV : Can Team B make it ?

Ball 1 : 4 Runs. A good start for Team A.

Ball 2 : 2 Runs.

Ball 3 : 4 Runs.

Ball 4 : 4 Runs.

Ball 5 : 0 Runs. Ahh that’s a dot ball!

Team B requires 4 runs to win from the last ball.

Ball 6 : 6 Runs. Team B finishes off in style. A magnificent strike into the crowd!

Let’s see if our smart contract shows the correct result!

Step V : Announcing the winner. And the winner is…

Carefully fill the address of respective teams. Make sure that you switch back to umpire’s account before you call the winner function.

Congratulations!! 🎉
You successfully designed and deployed the Smart Contract to obtain the accurate result.

That wraps up the step-by-step article for now.

Other articles in this series:

Please check me out on LinkedIn as well, I would love to hear from you!

--

--