In the previous blog we discovered how to set up our environment. I prefer using the solution with Webstorm, Truffle and Ganache, but feel free to use what’s appropriate for you. 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 a 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:
pragma solidity ^0.4.21;
That’s how the first line of each Solidity file must 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.
contract CryptoCookieMonsters { address public owner; function CryptoCookieMonsters() { owner = msg.sender; } }
So, we’ve just defined the smart contract for our application. Let’s take a deeper look what is going on here. Inside of the contract body, we’ve defined the constructor function and owner
field, with the data type address
. This is a special type for storing the addresses of wallets or other contracts. The public
access modifier works slightly differently from other common languages. When you add the public
modifier to a property, it automatically generates a 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?
It’s 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 only called once, when the contract is deployed to the network, so that’s an ideal place for setting the owner.
What’s the msg?
As you can 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 the following properties:
msg.data
contains complete calldatamsg.sender
tells us about the address, which ran the transactionmsg.value
is the amount of WEI sent with the messagemsg.gas
indicates the remaining gasmsg.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 will need some functionality for generating new Crypto Cookie Monsters. Let’s define a function inside our contract body for it:
function createCookieMonster() external { }
The
external
modifier specifies 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 type. That’s exactly what we need:
struct CookieMonster { string name; uint256 id; uint16 level; }
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.
uint256 counter = 0;
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:
CookieMonster[] public monsters; //public makes it readonly directly
Now we can finish with implementing createCookieMonster()
function:
function createCookieMonster() external { CookieMonster memory newMonster = CookieMonster("Test Monster", counter++, 1); monsters.push(newMonster); }
The full code can be found here
Conclusion
In this bloh we’ve discovered how to create a simple smart contract, using entities like: Arrays, Structs and Functions. Also, we’ve learned about the msg
object and some access modifiers like public
and external
. In the next chapter, we will learn how to store relations between the user and his holdings (Crypto Monsters in our case), improve the generation of id
, and learn about tje Events structure in the Solidity Language.