Lesson 5 Recap - cogeorg/teaching GitHub Wiki
This lesson is all about tokens, in particular the ERC721 standard token. You will implement it to be able to trade zombies. Additionally, this lesson introduces the SaveMath library to prevent over- und underflows, and comments.
-
Chapter 1: introduces the concept of tokens
- tokens are just special smart contracts
- they implement standard functions
- most used one is the ERC20 token (acts like money)
- standardization is important such that platforms/exchanges do not have to adapt to different protocols
-
Chapter 2: introduces the ERC721 token
contract ERC721 {
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
function balanceOf(address _owner) external view returns (uint256);
function ownerOf(uint256 _tokenId) external view returns (address);
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
function approve(address _approved, uint256 _tokenId) external payable;
}
-
Chapter 3: implements
balanceOf
andownerOf
-
Chapter 4: refactoring because there cannot be a function and a modifier called
ownerOf
-
Chapter 5: clarifies the transfer logic
transferFrom
: transfer a token from the old owner to a new owner- can be directly called by the old owner
- the new owner has to be approved to call this function
approve
: the old owner allows the new owner to calltransferFrom
-
Chapter 6 - 8: implements the above explained functions
-
Chapter 9: warns against overflows
- keep in mind that a
uint8
can only store 2^8-1=255 digits, meaning that 256 will be stored as 0 - equally, a -1 will be stored as 255, which is called an underflow
- carefully determine the size of every uint to prevent overflows and assess whether you can perform certain mathematical operations to prevent underflows
- keep in mind that a
-
Chapter 10 - 12: introduces libraries and SaveMath as an example of such
- a
library
is similar to acontract
- it holds reusable code that can be called from different contracts
- it is comparable to a static class in other object-oriented languages
- a library has to be deployed separately
- SaveMath is a library that can be applied to unsigned integers (256) by importing it and calling
using SaveMath for uint256;
- it prevents over- and underflowing
- a
-
Chapter 13: introduces comments
// This is a single-line comment.
/* This is a
multi-line comment.*/