Blockchain CryptoZombies - Lesson 3: Advanced Solidity Concepts


Attention: You are required to finish Blockchain CryptoZombies - Lesson 2 before continuing

This lesson will be a bit less flashy (sorry, no plot twists!). But you’ll learn some really important concepts that will take you closer to building real DApps — things like contract ownership, gas costs, code optimization, and security.

Again, make sure to walk through the interactive lessons all by yourself first. This is just a rewind of important knowledge :)

Here are our codes, obviously they are getting a bit longer:


yioBEt9n8L

1 Solidity code sharing site. Share Solidity code snippets with friends, or check out cool code snippets from around the web.

awv1Z1w32w

1 Solidity code sharing site. Share Solidity code snippets with friends, or check out cool code snippets from around the web.

bSQcy8k0C5

1 Solidity code sharing site. Share Solidity code snippets with friends, or check out cool code snippets from around the web.

f-BOVXfPT1

1 Solidity code sharing site. Share Solidity code snippets with friends, or check out cool code snippets from around the web.

There 's something you need to remember:

Function Modifiers

A function modifier looks just like a function, but uses the keyword modifier instead of the keyword function. And it can't be called directly like a function can — instead we can attach the modifier's name at the end of a function definition to change that function's behavior.
Let's take a closer look by examining onlyOwner:
/**
 * @dev Throws if called by any account other than the owner.
 */
modifier onlyOwner() {
  require(msg.sender == owner);
  _;
}
We would use this modifier as follows:
contract MyContract is Ownable {
  event LaughManiacally(string laughter);

  // Note the usage of `onlyOwner` below:
  function likeABoss() external onlyOwner {
    LaughManiacally("Muahahahaha");
  }
}
Notice the onlyOwner modifier on the likeABoss function. When you call likeABoss, the code inside onlyOwner executes first. Then when it hits the _; statement in onlyOwner, it goes back and executes the code inside likeABoss.
So while there are other ways you can use modifiers, one of the most common use-cases is to add quick require check before a function executes.
In the case of onlyOwner, adding this modifier to a function makes it so only the owner of the contract (you, if you deployed it) can call that function.
 ------------------------------------------------------------------------------

Gas — the fuel Ethereum DApps run on

In Solidity, your users have to pay every time they execute a function on your DApp using a currency called gas. Users buy gas with Ether (the currency on Ethereum), so your users have to spend ETH in order to execute functions on your DApp.
------------------------------------------------------------------------------

View functions don't cost gas

view functions don't cost any gas when they're called externally by a user.
This is because view functions don't actually change anything on the blockchain – they only read the data. So marking a function with view tells web3.js that it only needs to query your local Ethereum node to run the function, and it doesn't actually have to create a transaction on the blockchain (which would need to be run on every single node, and cost gas)

That 's the end of lesson 3, here 's what you 've learned so far: 
  • How to build updatable smart contracts
  • Securing contracts with contract ownership
  • Gas and gas optimization on Ethereum
  • Function modifiers and security checks
See you in the next lesson :)

No comments

Hey, buddy. What's on your mind :)

Powered by Blogger.