click below
click below
Normal Size Small Size show me how
Cryptozombies
Creating a zombie factory
| Term | Definition |
|---|---|
| An empty contract named HelloWorld would look like this: | contract HelloWorld { } |
| A bare-bones starting contract | pragma solidity >=0.5.0 <0.6.0; contract ContractName { } |
| What is a contract? | A contract is the fundamental building block of Ethereum applications. |
| All solidity source code should start with a ____________ | version pragma |
| Create an empty contract called ZombieFactory. | contract zombieFactory {} |
| What are state variables | What are state variables |
| State variable example | The uint data type is an unsigned integer, meaning its value must be non-negative. |
| The following operations are the same as in most programming languages: | Addition: x + y Subtraction: x - y, Multiplication: x * y Division: x / y Modulus / remainder: x % y (for example, 13 % 5 is 3, because if you divide 5 into 13, 3 is the remainder) |
| Example of an exponential operator in solidity | uint x = 5 ** 2; // equal to 5^2 = 25 |
| Create a uint named dnaModulus, and set it equal to 10 to the power of dnaDigits. | uint dnaModulus = 10 ** dnaDigits; |
| Sometimes you need a more complex data type. For this, Solidity provides ________: | structs |
| Example of struct | struct Person { uint age; string name; } |
| Structs allow you to create more _________ data types that have ___________ properties. | complicated, multiple |
| Create a struct named Zombie and let it have 2 properties: name (a string), and dna (a uint). | struct Zombie { string name; uint dna; } |
| When you want a collection of something, you can use an ______. | array |
| There are two types of arrays in Solidity: ________ arrays and ______ arrays: | fixed, dynamic |
| // Array with a fixed length of 2 elements: | uint[2] fixedArray; |
| // another fixed Array, can contain 5 strings: | string[5] stringArray; |
| // a dynamic Array - has no fixed size, can keep growing: | uint[] dynamicArray; |
| Example of an array of structs. | Person[] people; // dynamic Array, we can keep adding to it |
| You can declare an array as public, and Solidity will automatically create a getter method for it. The syntax looks like: | Person[] public people; |
| Create a public array of Zombie structs, and name it zombies. | Zombie[] public zombies; |
| A function declaration in solidity looks like the following: | function myFunction(string memory _name, uint _amount) public { } |
| There are two ways in which you can pass an argument to a Solidity function: | By value and by reference |
| What is required for all reference types such as arrays, structs, mappings, and strings? | It is required that variables should be stored- in memory |
| Note: It's convention (but not required) to start function parameter variable names with an ____________ in order to differentiate them from global variables. | underscore (_) |
| Create a public function named createZombie. It should take two parameters: _name (a string), and _dna (a uint). Don't forget to pass the first argument by value by using the memory keyword Leave the body empty. | function createZombie (string memory _name, uint _dna) public { } |
| function createZombie (string memory _name, uint _dna) public { // start here } In one line of code, fill in the function body so it creates a new Zombie, and adds it to the zombies array. The name and dna for the new Zombie should come from the | zombies.push(Zombie(_name, _dna)); |
| Example how to declare a private function: | uint[] numbers; function _addToArray(uint _number) private { numbers.push(_number); } |
| it's convention to start private function names with an _____________. | underscore (_) |
| function createZombie(string memory _name, uint _dna) public { zombies.push(Zombie(_name, _dna)); } Our contract's createZombie function is currently public by default — this means anyone could call it and create a new Zombie in our contract! Le | function _createZombie(string memory _name, uint _dna) private { zombies.push(Zombie(_name, _dna)); } |
| To return a value from a function, the declaration looks like this: | string greeting = "What's up dog"; function sayHello() public returns (string memory) { return greeting; } |
| Solidity also contains _____ functions, which means you're not even accessing any data in the app. | pure |
| Create a private function called _generateRandomDna. It will take one parameter named _str (a string), and return a uint. Don't forget to set the data location of the _str parameter to memory. Mark it as view and allow the function body empty. | function _generateRandomDna(string memory _str) private view returns (uint) { } |