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 { }
- e.g.
- State variables are permanently stored in contract storage, similar to DB.
uint
means 256-bit unsigned integer, there areuint8
,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 PythonPerson 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 newPerson
instance.
- on the left hand side,
array
fixed and dynamic array
uint[]
- an array of uint, empty square bracket means the length is dynamicName[]
- an array of Name struct, dynamic lengthuint[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)
:
- takes a string as an argument
- retrieve the argument from memory
- 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)
afterpublic
- it means this function returns a string, which will be stored in the memory
- note that it is
returns
here with as
in the end - where in the function actual return statement, it is
return
without as
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 afterpublic
, beforereturns
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 afterpublic
orprivate
in function definition, beforereturns
TypeCasting
uint rand = uint(keccak256(abi.encodePacked(_str))
- two different roles of
uint
keyword:- on the left hand side,
uint
refers the type of variablerand
- on the right hand side,
uint()
is a type casting function, convert the input into type uint
- on the left hand side,
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.