Working with contract intializers

In this section, we will complete the constructor function that we have.

Wrapping up the constructor#

The constructor will be used to take a random number so that the money game can be played. The constructor will also keep track of who is the owner of the game (to the person who creates the MoneyGame).

Recall that we had created the following code in the previous section:

Using msg.sender to identify who is interacting with the smart contract#

Let's take care of the owner variable first. To ensure that we capture the address (blockchain account) that is creating the the MoneyGame smart contract, we can use the method msg.sender. This method keeps track of who is calling the smart contract.

Add owner = msg.sender; to the constructor method to ensure that we capture the owner. We will use this variable later to access the random number (which will be hidden from everyone else).

The msg is a global variable that is accessible by Solidity smart contracts. msg.sender identifies the account that is calling the smart contract. A few other things that the msg variable captures includes, msg.gas, msg.sig, and msg.value.

  1. — The complete calldata which is a non-modifiable, non-persistent area where function arguments are stored and behave mostly like memory

  2. msg.gas — Returns the available gas remaining for a current transaction (you can learn more about gas in Ethereum here)

  3. msg.sig — The first four bytes of the calldata for a function that specifies the function to be called (i.e., it’s function identifier)

  4. msg.value — The amount of wei sent with a message to a contract (wei is a denomination of ETH)


This page is a preview of Creating an ERC20 Token on Ethereum

Start a new discussion. All notification go to the author.