Skip to content

Instantly share code, notes, and snippets.

@Tanapruk
Last active July 4, 2018 17:23
Show Gist options
  • Save Tanapruk/e353a48af0ccafb3f5150b4537e8ecf7 to your computer and use it in GitHub Desktop.
Save Tanapruk/e353a48af0ccafb3f5150b4537e8ecf7 to your computer and use it in GitHub Desktop.
CryptoZombies

Lesson 1

Keywords

  • contract {} is for wrapping ethereum contract.
  • uint is no plus/minus integer.
  • struct {} is for wrapping multiple-attribute data. E.g.,
struct Zombie {
  string name;
  uint dna;
}
  • private public should be put after function name. E.g.,
function _createZombie (string _name, uint _age) private {
}
  • to return data of a function, use returns and follow with (type). E.g.,
function _generateRandomDna(string _str) private view returns (uint) {

}

Concepts

  • Declare a variable with a type before the variable. E.g., uint age
  • Declare a function with function keyword. Parameter variable should start with _ E.g.,
function createZombie (string _name, uint _age) {
}
  • Convention for private function is to name function with _ E.g.,
function _createZombie (string _name, uint _age) private {
}

Lesson 2

Basic Level 2

Keywords

  • msg.sender - global variable that return eth address of such user
  • require() - assert() in the solidity world
  • is - inheritance in the solidity world
  • storage memory - storage is permanent, memory is temporary. Put either of these keywords after declaring variables to best use memory.
  • internal - Use inside class and its children can use this variable/function, too.
  • external - Use outside of contract class only
  • function without { } inside contract{} - Interface that let other contracts access.

Concepts

  • Blockchain database is public, we can read data from others and others can read from us. It is public API with little efforts. E.g., we can read CryptoKitties data.
  • function can return multiple values. Look at values inside () after returns. You can extract it with deconstructuring (first, second,,forth).

Lesson 3

Advanced Concepts

Concepts

  • smart contract that is deployed on server is permanent.
  • If you make your contracts public then anyone can mess with your contracts. Some methods will need permissions. Solidity by itself does not manage these things well.OpenZeppelin, a solidity lib, is a rescue. It helps manages ownership very well.
  • OpenZeppelin is more like a best practice.
  • You can make your smart contract more modifiable
  • Optimization to use less resources and thus less gas. One way is to use uint8, uint16, uint32 inside struct and cluster them adjacent to each others.
  • Frequent writing to storage of blockchain is discouraged since it costs gas.
  • No immutable concept - It cares much about where it saves data storage memory.

Keywords

  • modifier - function like but cannot be alone. Put this after function, it will get called first. Usually, only require() is inside this modifier.
  • now - unix timestamp
  • view - read data from blockchain externally without costing any gas.
  • new uint[] - To init new array: e.g., uint[] result = new uint[]
  • for - for(uint i = 1; i <= 10; i++) { }

Lesson 4

Interact with tokens

Keywords

  • payable another function modifier (put it after external or public) for receiving ether. Special function unlike other programming languages.
  • msg.value global variable for getting ether.
  • msg - global god variable.

Concepts

  • owner - this is us. the developer?.
  • random number - When we have a random function on the blockchain, the first miner to solve it will successfully generate that random number. That specific miner can wait untill the outcome is what he wants before pushing it to the blockchain. Assuming he has enough power.

Lesson 5

Conforming to standard interface.

Keywords

  • using Using library using SafeMath.
  • assert Like require but require will refund customers if transaction failed.
  • SafeMath Can prevent overflow/underflow.

Concepts

  • token - smart contract that follows commom rules. E.g., transfer(address _to, uint256 _value) and balanceOf(address _owner)
  • ERC20 is a standard.ERC721 is another. ERC20, as you know, token for ico. ERC721 is for cryptoKitties-like tokens.
  • Your contract can have multiple inheritances by putting commas after is and the first parent.

Lesson 6

Integrate ethereum with front end through Web3

Keywords

  • Web3 is a package. Install can be through npm install web3, yarn add web3 or <script language="javascript" type="text/javascript" src="web3.min.js"></script>
  • myContract.methods.myMethod(123).call() - myMethod is function name. 123 is passing parameter. This is for calling pure and view function without modifying data on the blockchain.
  • myContract.methods.myMethod(123).send() - Similar but this is for methods other than pure and view which costs gas and modify blockchain data.
  • then(...) - We will get data from method with then.
  • listen to specific event for specific user via indexed.

Concepts

  • Infura- 3rd party service that let us communicate with ethereum nodes easily.

  • MetaMask - browser extensions that help us handle private key securely.

  • Simple AJAX. Get and send via json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment