Writing my First Ethereum Smart Contract (Part 2)

Chia Li Yun
Javarevisited
Published in
4 min readNov 24, 2021

I have covered most of the basic and core concept about Solidity in my previous article. Take a quick look if you haven’t!

Let’s get started with the hands-on programming with a very simple smart contract!

As a starter, we shall use a web-based Ethereum IDE — Remix.

Photo by Shamsudeen Adedokun on Unsplash

About Remix

Screenshot of Remix Page

Once you navigate to Remix via the link, you should be able to see the a similar screen. It has support for different UI themes like Dark, Light and Midcentury etc. which you can change via the Gear settings icon on the bottom left of the navigation column.

In this tutorial, we will focus primarily on the first 3 tabs.

  1. File Explorer Tab — You can create files and even link to your Github.
  2. Compiler Tab — You will need to compile your code here. (We will be using 0.5.0 version of the compiler)
  3. Deploy & Run Transactions Tab — You will select the compiled contract to deploy and you can see the address of the deploy contracts here as well.

Coding

Set up the base environment.

Create a new file in Remix — Dice.sol and copy and paste the following code into it. Turn into the compiler tab and click on Compile Dice.sol .

The following code gives you a very basic smart contract of a Dice that has some functionalities like:

  • Create a new Dice
  • Rolling a Dice
  • Stop rolling a Dice

You may take some time to go through the code, digest and make reference to some of the concepts that we went through in the previous article (variables, functions, modifiers and events).

Let’s try running in Remix to get a feel of how it should be working. Now, select on the 3rd tab — Deploy & Run Transactions.

Notice that you have 10 accounts with 100 ether each. You may add more accounts if you need by clicking onto the “+” icon beside the ACCOUNT header.

Make sure you have selected the Dice contract as shown highlighted in box 1. Click onto Deploy button and you should see similar changes to your output (that gives you a successful deployment message) in box 4 and the deployed contracts in box 3.

In box 3, apart from the address of the smart contract (you can get access to the address easily by clicking onto the copy icon), you can also see the list of functions you have coded. This is the place where you can interact and test out all the functions of your smart contract.

You may have noticed that the account you have selected to deploy the smart contract has lesser ether than before. This is because some (very minimal) GAS is required to deploy the smart contract into the “test” network.

Let’s test out the functions.

To run add function, we will need to specify some value for the Dice (at least 0.1 ether), hence you will need to enter some value as highlighted in the red box, otherwise you will be thrown with an error stating:

transact to Dice.add errored: VM error: revert.revert
The transaction has been reverted to the initial state.
Reason provided by the contract: "at least 0.01 ETH is needed to spawn a new dice".
....

You may try out the other functions at your own pace.

Exercise.

Now, try coding a new function — to destroy dice and return ether. You may have to consider some conditions like ownerOnly, isValidDice etc. and look up how can you return the ether to the owner using one of the special method.

Here is the link to the complete code for your reference.

Conclusion

I hope that this simple tutorial has given you a better understanding of how you can create your own smart contract! We shall dive in deeper to using a framework that is useful in managing development lifecycle, debugging and testing, in the subsequent articles.

Thank you for reading! See you next time! 🤓

--

--

Chia Li Yun
Javarevisited

Recent graduate from university. Always excited about the new technologies and love to share with the tech community here!