Teaching myself Solidity using CryptoZombies

in #blockchain7 years ago (edited)

Screen Shot 2018-02-13 at 5.33.14 PM.png
The past two weeks have been awesome for me. I joined a couple open source Ethereum communities to familiarize myself with their codebases, read through as much as I could, and committed a couple small PRs that ended up getting merged. I wanted to be able to contribute more and leap into creating apps and I thought about various ways I could do this. As, I was researching, I remembered hearing about the CryptoZombies platform from one of the founders of the Oakland blockchain developers meetup, @jusdev89. Looking at the site, I thought, “Wow, a way to learn by playing a game? “ Sign me up! What excited me was seeing their claim, “After completing all lessons and deploying your DApp, pit your zombie army against other players' zombies in one of the world's first blockchain-based games! Start with building a game.“ Not only would I build a game, but I’d have the opportunity to deploy my app and play others ? Yayy! In my opinion, Crypto Zombies is an ingenious platform. After reading their ‘Why is CryptoZombies Free, entry' ,“ ..we want to get more developers thinking outside the box and trying to build large-scale DApps. Games are one of the areas where we think blockchain will really revolutionize things. So we built CryptoZombies to help educate and inspire the next generation of blockchain game devs.” I dove in headfirst.

There are 4 levels to crypto Zombies..I read on twitter, they're planning on extending this platform, which is really great news. So far, these are the levels they have..

  1. Making the zombie factory

  2. Zombies attack their victims

  3. Advanced solidity concepts

  4. Zombie battle system

    Probably, one the most important takeaways is:
    Functions in Solidity are public..BY DEFAULT. YOUR CODE IS ABSOLUTELY VULNERABLE TO ATTACKS in this state.. we all know what happens when money is left on the floor..It’s HIGHLY advisable that you mark your functions private. Only make them public if you want the world to have access to them. With that being said, I’ll breakdown the first several lessons of my journey.

PART 1

Here’s where we set off building a zombie factory..aayyy! So the objective of the game in chapter 1 is to build a factory of zombies. The first thing I had to do was write a function for creating new zombies. In this factory we maintain a database of all our zombies in our army. Each zombie is unique and will have randomly generated DNA. Each zombie's DNA is a 16 integer number. Like our DNA, different parts of DNA corresponds to different body parts/traits in the zombie.

Screen Shot 2018-02-13 at 3.43.52 PM.png

PART 2- CONTRACTS !

'Solidity's code is encapsulated in contracts' which are the basic building blocks of a Solidity application. This is where it all begins. Going forward in this game, all functions and variables will be used to write contracts. When writing a contract, you should always include ‘version pragma’, which declares what version of solidity’s compiler this code should be using. This prevents future compatibility issues, headaches, and heart attacks.

So, we start off with an example of an empty contract with the traditional ‘Hello World’

ex.

  pragma solidity ^0.4.19;

   contract HelloWorld {

  }

Once you finally write a function correctly, a cute zombie scurries across your screen
Screen Shot 2018-02-13 at 4.57.21 PM.png

PART 3

Now we’ve essentially learned how to write the 'frame' of the function.

ex.

contract cryptoZombies {

}

In part 3, we’re told the critical piece of info that what you write WITHIN the braces is what gets written to the blockchain.
Essentially, you’re writing to a database which is the blockchain.

We’re also introduced to a data type called an uint which stands for an unsigned integer. A uint's value has to be a non negative value.

ex.

pragma solidity ^0.4.19;

contract zombieFactory {

 uint dnaDigits = 16;  

}

We are also taught that state variables are permanently stored in contract storage.

Screen Shot 2018-02-13 at 4.01.07 PM.png

PART 4

Chapter 4 explains that Math operators in solidity are the same as other languages.
ie.
multiply = a * b
divide: a/b
subtract: a - b
add : a + b
Modulus: a % b for. ex. 10 % 5 = 2. because if you divide 5 into 10, there is no remainder
Exponential: a ** b

Me, training for my future Zombie fight
Screen Shot 2018-02-13 at 5.39.48 PM.png

PART 5

Structs! - Structs are a new data type: they allow you to create more complicated data types with multiple properties.

Like other languages, we also learn that strings are used in Solidity.

ex.

struct Person {
uint age;
string name;

}

PART 6 - ARRAYS

There are two types of arrays in Solidity: fixed arrays and dynamic arrays: Unlike a fixed array, a dynamic array has no limit and can keep growing.

ex.

Dynamic Arrays

uint[] dynamicArray;

Fixed arrays:

ex.

uint[2] fixedArray; <<< array w fixed number of 2 elements

string[76] stringArray; <<< fixed array with strings

You can also have arrays with structs.

ex.

Person struct:

Public Arrays:

You can declare an array as public and solidity by default, creates a getter method to handle it.

ex. Person[] public people;

Public arrays gives others a way to read the data, but they can’t write. If you’re interested in storing public data, this is a handy way to do it.

PART 7 -FUNCTION DECLARATIONS

function drinkMilk(string _almond, uint _gallon) {

}

Note: it’s best practice (but not required) to start function parameter variable names with an underscore (_) in order to distinguish them from global variables.

PART 8

Here is where we get to learn how to create new 'Persons' and add them to our people array.

Create a New Person:

Person jamesDuffy = Person(172, “jamesDuffy”);

Add that person to the Array:

people.push(lukeZhang);

We can also combine these together and write them in one line of code to keep things clean:

people.push(Person(16, “Duffy”));

zombies.push(Zombie(“name”, “dna”);

PART 9- DECLARING PRIVATE FUNCTIONS

Screen Shot 2018-02-13 at 6.41.04 PM.png

It's good practice to mark your functions as private by default.. and then only make public the functions you want everyone else to see.

ex.

uint[] numbers;

function _addToArray(uint _number) private { <<private comes after the function name. When writing private functions, the convention is to start the functions with an underscore.

numbers.push(_number) {

}

^^Because it's private, only other functions within our contract can call this function and add numbers.

Well folks, as you can see, CryptoZombies teaches you quite a bit. I’m still on part one of the 4 parts, and have learned some good stuff. I’m excited to tackle the next parts over the next day. If you’re interested in learning Solidity, I highly recommend Crypto Zombies.

Sort:  

Great job! You should really keep it up. CryptoZombies really helped me better understand some parts of Solidity.

Thx, a lot Fode I appreciate it.:)

Yeah that was such an awesome tutorial, just amazingly fun. Nice job writing up this post too!

Awesome post! I invite you to visit our blog and enjoy our content :)

Great post, thanks for sharing. I recently got lucky with rewards for some comments I made on Steemit so I threw in some SBD to promote this post in the hopes that people will take a look at CryptoZombies and try out Solidity development for themselves!

I recently did some of the tutorials on Solidity dev - what I found was the basics a pretty easy, but there are some really tricky and unpleasant gotchas you have to know about and take care of. Even experts can easily screw up. Fortunately there are tools that do some automated checking of your contracts, but nothing is perfect. It seems like they could easily improve Solidity to make it safer and hence easier to use in the future - plus developing tools that generate safe contracts in the first place.

Until then, let the Zombies fight! Bugs and all!

Thanks a lot. I appreciate you stopping by:) I agree...with the risk being so big, hopefully things will be improved soon.

Coins mentioned in post:

CoinPrice (USD)📉 24h📈 7d
DNAEncrypGen0.304$-5.46%-15.72%
DYNDynamic2.440$-0.95%19.7%
ETHEthereum854.066$-0.1%11.78%
PARTParticl21.841$-5.03%13.36%