Blockchain CryptoZombies - Lesson 4: Zombie Battle System



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

In this lesson, we're going to be putting together a lot of the concepts you've learned in previous chapters to build out a zombie battle function. We're also going to learn about payable functions, and how to build DApps that can accept money from players.

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

Since this lesson focus on combining many previous concepts, we only need to modify logic in our codes. Therefore, code block samples are unnecessary.

We just need to remember some important things

The payable Modifier

payable functions are part of what makes Solidity and Ethereum so cool — they are a special type of function that can receive Ether.
Let that sink in for a minute. When you call an API function on a normal web server, you can't send US dollars along with your function call — nor can you send Bitcoin.
But in Ethereum, because both the money (Ether), the data (transaction payload), and the contract code itself all live on Ethereum, it's possible for you to call a function and pay money to the contract at the same time.
This allows for some really interesting logic, like requiring a certain payment to the contract in order to execute a function.

Let's look at an example


contract OnlineStore {
  function buySomething() external payable {
    // Check to make sure 0.001 ether was sent to the function call:
    require(msg.value == 0.001 ether);
    // If so, some logic to transfer the digital item to the caller of the function:
    transferThing(msg.sender);
  }
}
------------------------------------------------------------------------------

Withdraws

In the previous chapter, we learned how to send Ether to a contract. So what happens after you send it?
After you send Ether to a contract, it gets stored in the contract's Ethereum account, and it will be trapped there — unless you add a function to withdraw the Ether from the contract.
You can write a function to withdraw Ether from the contract as follows:
contract GetPaid is Ownable {
  function withdraw() external onlyOwner {
    owner.transfer(this.balance);
  }
}
Note that we're using owner and onlyOwner from the Ownable contract, assuming that was imported.
You can transfer Ether to an address using the transfer function, and this.balance will return the total balance stored on the contract. So if 100 users had paid 1 Ether to our contract, this.balance would equal 100 Ether.
You can use transfer to send funds to any Ethereum address. For example, you could have a function that transfers Ether back to the msg.sender if they overpaid for an item:
uint itemFee = 0.001 ether;
msg.sender.transfer(msg.value - itemFee);
------------------------------------------------------------------------------

How do we generate random numbers safely in Ethereum?

Because the entire contents of the blockchain are visible to all participants, this is a hard problem, and its solution is beyond the scope of this tutorial. You can read this StackOverflow thread for some ideas. One idea would be to use an oracle to access a random number function from outside of the Ethereum blockchain.
In a future lesson, we may cover using oracles (a secure way to pull data in from outside of Ethereum) to generate secure random numbers from outside the blockchain.


That 's the end of lesson 4, here 's what you 've learned so far: 

  • Payable functions, and how to earn money from your Ethereum-based games
  • Withdrawing ETH from smart contracts
  • Random number generation & security on Ethereum
See you in the next lesson :)

No comments

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

Powered by Blogger.