ETH BLOCKCHAIN & Solidity TypeCasting, Address (send and receive), msg keyword

Sateesh Teppala
2 min readFeb 19, 2022

Overflow and Underflow — Become Ethereum Blockchain Developer

// SPDX-License-Identifier: GPL-3.0 pragma solidity 0.7.0;  contract RolloverExample {  

uint8 public myUint8;
function decrement() public {
myUint8--;
}
function increment() public {
myUint8++;
}
}

⛔ in the above, unit8 becomes 0 to 255 when we try to decrease the value of unit8 when the value is 0. It does not throw any error in the Solidity 0.5.8 version but in the new version i.e 0.8.12, it throws an error. if we reduce the unit8 value below 0 it throws an error and automatically the variable becomes 0.

  • Don’t ever use Stings because it is expensive
  • Statically types we should give the variables data types.
  • we just can’t give variable names to a function.
  • Limited resources.

Our Smart Contract — Become Ethereum Blockchain Developer

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.12;contract SendMoney{ uint public balanceReceived;function receiveMoney() public payable{
balanceReceived += msg.value;
}
function getBalance() public view returns(uint256){
return address(this).balance;
}
function withdrawMoney() public{
address payable toAddress = payable(msg.sender); toAddress.transfer(this.getBalance());
}
function withdrawMoneyToAccount(address payable _toAddress) public{ _toAddress.transfer(this.getBalance());
}
}

⛔ in function withdrawMoney(), we would have to explicitly convert it by making the following modification to the code address payable toAddress = payable(msg.sender); otherwise, code throws TypeError: Type address is not implicitly convertible to expected type address payable.

msg object and functions

  • 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 address0xdfad6918408496be36eaf400ed886d93d8a6c180 thenmsg.sender equals0xdfad6918408496be36eaf400ed886d93d8a6c180
  • msg.value - The amount of wei sent with a message to a contract (wei is a denomination of ETH)
  • msg.data - The complete calldata which is a non-modifiable, non-persistent area where function arguments are stored and behave mostly like memory
  • 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)

--

--