CryptoZombies Lesson 4: Payable, Withdrawals, Random Number generator (Solidity Basics)
What I am currently learning?
I am currently on my fourth lesson in the cryptozombies course. In this lesson, we are creating a function that allows me to make my zombie level up and also create an attack function on the backend!
I will be sharing some of the code I wrote and what I learned along the way and how I understand simple layman terms! Hopefully, this will come useful to those who are also having/had issues understanding some of the words used in Solidity. (Open to feedback as well about my understanding!)
Level Up Function Code
So to give a bit of context, the zombies in CryptoZombies have levels and to level up there are 2 methods that I know of so far. The first way is to spend 0.001 ether to level them up and the 2nd method is to attack zombies and win! So let's talk about the "Level Up Function" first!
function levelUp(uint _zombieId) external payable {
require(msg.value == 0.001 ether);
zombies[_zombieId].level++;
}
Layman Term:
"This function is called Level Up which needs the _zombieId (basically the zombie's Identification number) that can be called by the user. This function accepts payment to the smart contract. And requires a value of 0.001 ether to be sent before it executes the function which is to increase the level of the zombie by 1."
So what I learned today was that declaring a payable when declaring a function makes it so that when you execute the function, it sends the payment to the smart contract. Now in another example below, you will see that if it is declared inside the function itself, it might be payable to someone else instead of the smart contract!
contract GetPaid is Ownable {
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
So in this case, we can see that the payable is no longer on the function, and instead inside the function itself! It also states that the amount payable will be sent to the contract owner. This is extremely useful if you are selling something and want to withdraw your earnings from your sales. Now, what about transferring money to others how do we go about doing that instead?
uint itemFee = 0.001 ether;
msg.sender.transfer(msg.value - itemFee);
So this is an example of if the person had overpaid for the item, how we can automatically send the surplus back to the person who bought our item! So instead of using payable, we can also use the transfer method!
Random Number Generator Function
uint randNonce = 0;
uint random = uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % 100;
randNonce++;
uint random2 = uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % 100;
Firstly, I learned 2 new methods/functions called keccak256 and abi.encodePacked. From what I read and understand, keccak256 basically will convert the inputs into a random sequence of numbers and letters also known as hashing. While the abi.enodePacked will convert it into a set of numbers with a 0x in front also known as hexadecimal string. The combination of both does give it a certain level of randomness, but from what I read so far there are ways to expose this method.
Layman Term:
"randNonce is equal to 0 at the start. Create a random number by first taking the combination of the time now, the person interacting with the contract, and also randNonce value to create a random string of numbers, after using the function, randNonce will increase by 1 and therefore continuously create a new set of variables rather than ever using the same variables twice"
Ending Thoughts
It's quite a handful of things I have picked up so far just from lesson 4! And to be honest, as I write my learnings now, I am also googling and learning more to gain clarity! (Just like how keccak256 and abi.encodePacked works)
It's my first attempt ever at writing a blog and I am just trying to pen down my thoughts as I go along with the format of explaining my code in layman hopefully to get either corrected by developers along the way about my misunderstanding of concepts or to help new developers learn from a newbie's POV!