Lesson 2 Recap - cogeorg/teaching GitHub Wiki
This lesson introduces additional concepts of Solidity. You design a contract that will create a zombie cat by feeding on a CryptoKitty. This zombie cat will also be stored in the list of zombies and it will be owned by the person that owned the original zombie.
-
Chapter 2: introduces mappings and addresses
-
mapping
: a key-value store -
address
: a data type that is is 42 characters long and 20 bytes large. It holds an Ethereum address
-
-
Chapter 3: introduces the global variable
msg.sender
- stores the address of the person deploying a contract
- stores the address of the person/contract calling a function
-
Chapter 4: introduces
require
-
require
is a built-in function that checks the condition specified next to it. It throws an error if the condition is not fulfilled
require (msg.sender == owner, "Msg.sender is not the owner of this contract");
-
-
Chapter 5: introduces inheritance
- contracts can inherit state variables and functions from other contracts
contract ContractTwo is ContractOne { // some code here }
-
Chapter 6: explains imports
- imports are defined at the top of a Solidity file, below the pragma and above the contract definition
-
Chapter 7:
storage
vs.memory
- variables can be stored in storage and in memory
- state variables are stored in storage by default
- local function variables are stored in memory by default
-
Chapter 8: practices above introduced concepts
-
Chapter 9: completes function visibility
- we already know about
public
andprivate
-
internal
: the function can be called by the contract it is defined in and all contracts that inherit from it -
external
: the function can only be called from outside of the contract
- we already know about
-
Chapter 10: introduces interfaces
- an interface is an "abstract" contract, a "skeleton" contract
- it is used to define the functions that exist in a contract, their names, inputs, and outputs, without implementing any functionality
interface NumberInterface { function getNum(address _myAddress) public view returns (uint); }
-
Chapter 11: continues with interfaces
- interfaces are instantiated within contracts using the instance's address we want to access
contract SomeContract { address NumberContractAddress = 0x35h3kqj4h5...; NumberInterface numberContract = NumberInterface(NumberContractAddress); }
- now, you can call functions from this instance of numberContract
unit num = numberContract.getNum(msg.sender);
-
Chapter 12: handling multiple return values
- functions can return multiple values as
return (uint _something, string _somthingElse)
- to capture these multiple returns, create the value types and assign the values
function someFunction() internal pure returns(string x, string y, uint z){ return('Hello', 'World', 42) } function getValues() external { string x; string y; (x,y) = someFunction(); }
- note that the return values can be different in their types
- also note that you do not have to store all return values
- functions can return multiple values as
-
Chapter 13: introduces if-statements
- very similar to JavaScript if-statements
if (<condition>) { // do something }