What you need to know about `msg
` global variables in Solidity
So, you’re new to Solidity and just starting to build smart contracts on the Ethereum blockchain? Awesome!
You’ve likely read through the Solidity docs or played around with introductory tutorials and come across syntax like msg.sender
, msg.value
, or msg.data
. In particular, I imagine you’ve asked yourself, “What does msg.sender
mean?”
In Solidity there are special variables and functions which always exist globally and are mainly used to provide information about the blockchain. They can also be used to assist with error handling, employ mathematical and cryptographic functions, and present information about contract addresses and the contracts themselves.
`msg` global variables explained
The msg
global variables in particular are special global variables that contain properties which allow access to the blockchain. For instance, msg.sender
is always the address where the current (external) function call came from. Let’s take a look at an example from the blockchain-based puzzle game we built.
In our game there are six levels, each with their own puzzle to solve. The first user to correctly solve all of the puzzles in order is awarded a sum of ETH.
Now let’s take a look at our incrementLevel()
function. This function is called when a user correctly solves a puzzle and is used to move them on to the next level in the game:
// @dev A mapping of user addresses and their associated levels
mapping(address => uint) public userLevel;// @dev This function will advance a user to the next level
function incrementLevel() internal {
userLevel[msg.sender]++;}
You’ll notice we’re using the msg.sender
special global variable to obtain the current user’s address (e.g., 0x44d33a….) from where the function call came from. We’re then advancing them to the next level in the game by incrementing their level (of type uint
) in the userLevel
mapping.
Along with msg.sender
there are other msg
special global variables that are useful:
msg.data
— The completecalldata
which is a non-modifiable, non-persistent area where function arguments are stored and behave mostly likememory
msg.gas
— Returns the available gas remaining for a current transaction (you can learn more about gas in Ethereum here)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)msg.value
— The amount of wei sent with a message to a contract (wei is a denomination of ETH)
Take away
In Solidity there are special variables and functions. The special variables and functions are always available globally and are mainly used to provide information about the blockchain (i.e., transactions, contract info, address info, error handling, math and crypto functions).
The msg
global variables in particular are special global variables that contain properties which allow access to the blockchain’s contracts, their functions, and their values.
The msg.sender
global variable — likely the most commonly used special variable — is always the address where a current function call came from. For instance, if a function call came from a user or smart contract with the address 0xdfad6918408496be36eaf400ed886d93d8a6c180
then msg.sender
equals 0xdfad6918408496be36eaf400ed886d93d8a6c180
.
Lastly, the values of all members of msg
, including msg.sender
and msg.value
, can change for every external function call.
Interested is discussing msg
global variables in Solidity further? Drop us a line! team@upstate.agency