Solidity Development: Creating our first smart contract

Sir Fedos
Coinmonks
4 min readApr 9, 2018

--

In the previous article we’ve discovered how to set up our environment. I prefer using solution with Webstorm, Truffle and Ganache, but feel free to use any appropriate to you. And now we are going to write our first smart contract. To add some fun to the development process, let’s create a crypto game. As we know, everyone loves kitties and cookies. Although, we already have CryptoKitties, let’s make CryptoCookieMonsters instead.

Building the contract

Firstly, let’s create the Solidity file with .sol extension in your IDE. The first line of each Solidity file contains the information about the version of language, that we will use. The latest stable version is 0.4.21, so let’s use it:

That’s how our first line of each Solidity file has to look like. Now let’s create a contract for our application. And let’s make it faster, because Crypto Cookie Monsters don’t like to wait.

So, we’ve just defined the smart contract for our application. Let’s have a deeper look what is going on here. Inside of the contract body, we’ve defined the constructor function and owner field, with type of address. This is the special type for storing wallets’ or other contracts’ addresses. The public access modifier is working slightly different from other common languages. When you add public modifier to the property, it automatically generates the getter for that property, avoiding setting the value directly. In this way, public properties are read-only.

Hooray, we’ve done with the explanation of the first line in our contract. But why we need that owner property?

Elementary! In this property we will store the address of the person who deployed the contract to the Ethereum network. Later, it could help us to restrict some of the functions, so they could be called only by the owner. The contract constructor is getting called once, when contract is deployed to the network, so that’s an ideal place for setting the owner.

What’s the msg?

As you could see, we get the owner address from some msg object. msg is an object, that comes with every transaction made through the network. It includes such properties:

  • msg.data contains complete calldata
  • msg.sender tells us about the address, which run the transaction
  • msg.value is the amount of WEI, sent with the message
  • msg.gas indicates the remaining gas
  • msg.sig shows the first four bytes of the calldata

Note: Gas is the internal pricing for running a transaction or contract in the Ethereum network. Wei is the smallest denomination in the Ethereum. It’s like a penny for the pound. ETH costs 10¹⁸ wei.

Adding some logic

As we are going to create a game, we would need some functionality for generating new Crypto Cookie Monsters. Let’s define the function inside our contract body for it:

external modifier tells that this function can be called only outside of the smart contract. Now let’s create a model for our monsters. In Solidity we have a struct data structure. That’s exactly what we need:

Now we can create CookieMonster instances inside our createCookieMonster() function. But, before that, we have to think about how we will set new monster’s properties. Where would we get the id?Maybe, we have to add some kind of a counter in our contract, that we will increment after creating each new CookieMonster. We could use this value as an id and as a part of the name.

Also, let’s create an array of all of Cookie Monsters, created in the contract. For this, we can define an array of CookieMonster structs inside our contract body:

Now we can finish with implementing createCookieMonster() function:

The full code can be found here

Conclusion

Finally, in this article we’ve discovered how to create a simple smart contract, discover using of structures like: Arrays, Structs, Functions. Also, we’ve learned about msg object and some access modifiers like public and external. In the next chapter, we will learn how to store relations between user and his holdings (Crypto Monsters in our case), improve generating of id, learn about Events structure in Solidity Language.

Get Best Software Deals Directly In Your Inbox

❤️ Like, Share, Leave your comment

If you like this post, don’t forget to like, share with your friends and colleagues and leave your comment below about the post.
And Follow me…….

--

--

Sir Fedos
Coinmonks

Web3 Tutorials and analytics for true crypto degens