CryptoZombies chapter 1 learning notes

CryptoZombies chapter 1 learning notes

Photo by olieman.eth on Unsplash

CryptoZombies chapter 1 learning notes

This is a learning note of Solidity for myself, so it's not well written and organized. Some text are directly copied from CryptoZombies as it explains better than I do.

If you are wondering what is CryptoZombies, it's a Solidity learning game, learn more about it here. A big shout-out to whoever created it, what a great learning game.

Note begins here

  • ; at end of line is compulsory
  • everything is a contract
  • name of each contract has to start with capital letter
    • e.g. contract ZombieFactory { }
  • State variables are permanently stored in contract storage, similar to DB.
  • uint means 256-bit unsigned integer, there are uint8, uint16 etc.
    • uint dnaDigits = 16;
    • syntax: type varName = assigned_value;
  • string is used for arbitrary-length UTF-8 data

struct

struct Name {
  type1 var1;
  type2 var2;
}
  • in the above struct, only instantiate var inside the struct without value.
  • like class in Python? instantiate an object is exactly like creating class instance in Python
    Person satoshi = Person(172, "Satoshi");
    
    • on the left hand side, Person refers to the type, satoshi is the variable name.
    • on the right hand side, call the Person struct with () to create a new Person instance.

array

fixed and dynamic array

  • uint[] - an array of uint, empty square bracket means the length is dynamic
  • Name[] - an array of Name struct, dynamic length
  • uint[5] - an array of uint, fixed length of 5

Public, Private

  • e.g. Person[] public people;
    • syntax: type public/private varName;
  • for public array, Solidity will automatically create a getter method for it.

function

// function declaration
  function    eatHamburgers   (string memory _name, uint _amount)     public {
  //keyword     funcName        (function parameter)             public/private func
  }

first function parameters above (string memory _name):

  1. takes a string as an argument
  2. retrieve the argument from memory
  3. the argument name is _name

about function argument:

  • 'providing instructions about where the _name variable should be stored- in memory. This is required for all reference types such as arrays, structs, mappings, and strings.'
  • uint is not of reference type because uint is immutable?

function parameter naming:

'It's convention (but not required) to start function parameter variable names with an underscore (_) in order to differentiate them from global variables. We'll use that convention throughout our tutorial.'

functions are Public by default

  • ' This means anyone (or any other contract) can call your contract's function and execute its code.'

Private function

  • 'only other functions within our contract will be able to call this function'
  • it's convention to start private function names with an underscore _

Specify return value in function declaration

  function sayHello() public returns (string memory) { // returns with a 's'
    return greeting; // return with no 's'
  }
  • Note returns (string memory) after public
  • it means this function returns a string, which will be stored in the memory
  • note that it is returns here with a s in the end
  • where in the function actual return statement, it is return without a s

view function modifier

  • view function doesn't actually change state in Solidity — e.g. it doesn't change any values or write anything. function sayHello() public view returns (string memory) {}
  • view is placed after public, before returns

pure function

  • pure function doesn't even read from the state of the app — its return value depends only on its function parameters.
  • pure is placed after public or private in function definition, before returns

TypeCasting

uint rand = uint(keccak256(abi.encodePacked(_str))

  • two different roles of uint keyword:
    • on the left hand side, uint refers the type of variable rand
    • on the right hand side, uint() is a type casting function, convert the input into type uint

Event

  • 'Events are a way for your contract to communicate that something happened on the blockchain to your app front-end', e.g.:
  • event IntegersAdded (uint x, uint y, uint result);
    • syntax: event_keyword event_object_name (parameters)
    • note that the event_object_name starts with Capital letter
    • that is to create an event object
  • to broadcast an event, use emit keyword, e.g.:
    • emit IntegersAdded(_x, _y, result);
    • syntax: emit_keyword event_object_name argument

The #1 mistake for me = forgetting the semi-colon at the end of line

  • need to get into habit of typing ; at the end of line.