Getting users to use crypto with smart contracts
Getting users to use crypto with smart contracts#
In this section, we will jump into code that will enable our users to use crypto with our functions. To have our users interact with our functions with their crypto, we will need a modifier called
The payable modifier is important to functions. It is a modifier that is native to the Solidity language. If a function is created with the
payable modifier (or keyword), it means that the function will be able to accept
ether. If a developer wants a function to take ether but they did not add the
payable keyword, it will reject any ether sent to it.
In addition to having "payable" functions, it is also possible to have "payable" addresses. In the example that we are creating, we will have addresses that are payable because once the Money Game smart contract is required to pay out to the winner, ether will be sent to that "payable" address.
In our smart contract, we will use the
payable to allow people to enter the Money Game smart contract with ether. We will call the function
play. To kick things off, we will declare the function first. The function will be publicly accessible, and be a
payable function. To play the game the user will have to guess the number that is stored in the smart contract. That means that the function we are writing will be required to take a number that will be the guess that the user takes. Let's call the number _guess and we will have it as type
uint8 since the number we are storing in the smart contract is also the same type.
Now that our function is declared, we need to use a
require statement. Let's say that for people to play the Money Game smart contract, they need to send exactly 0.5 ETH to the game. We can use our require statement to enforce this behavior.
require statement we can add can be used to check if the person that is entering the game is not the person that created the game.