Skip to main content

LEARN SOLIDITY LESSON 5: Mappings Solidity. Addresses Ethereum. Msg.sender Solidity

LEARN SOLIDITY LESSON

Mappings Solidity. Addresses Ethereum. Msg.sender Solidity


Addresses Ethereum

The Ethereum blockchain is made up of accounts. Each account has an address, which you can think of like a bank account number. It's a unique identifier that points to that account, and it looks like this:

0x0cE446255506E92DF41614C46F1d6df9Cc969169

Mappings Solidity

A mapping is essentially a key-value store for storing and looking up data. In the first example, the key is an address and the value is a uint, and in the second example the key is a uint and the value a string.
mapping (address => uint) favoriteNumber;

function setMyNumber(uint _myNumber) public {
 
  favoriteNumber[msg.sender] = _myNumber;
}  


Msg.sender Solidity

In Solidity, there are certain global variables that are available to all functions. One of these is msg.sender, which refers to the address of the person (or smart contract) who called the current function.

mapping (address => uint) favoriteNumber;

function setMyNumber(uint _myNumber) public {
  // Update our `favoriteNumber` mapping to store `_myNumber` under `msg.sender`
  favoriteNumber[msg.sender] = _myNumber;
  // ^ The syntax for storing data in a mapping is just like with arrays
}

function whatIsMyNumber() public view returns (uint) {
  // Retrieve the value stored in the sender's address
  // Will be `0` if the sender hasn't called `setMyNumber` yet
  return favoriteNumber[msg.sender];
}



Comments

Popular posts from this blog

LEARN SOLIDITY LESSON 11: Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY

LEARN SOLIDITY LESSON 11 Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY Payable function payable functions are part of what makes Solidity and Ethereum so cool — they are a special type of function that can receive Ether. contract OnlineStore { function buySomething() external payable { // Check to make sure 0.001 ether was sent to the function call: require(msg.value == 0.001 ether); // If so, some logic to transfer the digital item to the caller of the function: transferThing(msg.sender); } } Note: If a function is not marked payable and you try to send Ether to it as above, the function will reject your transaction. Withdraws Ether. Get Balance ETH You can write a function to withdraw Ether from the contract as follows: contract GetPaid is Ownable { function withdraw() external onlyOwner { address payable _owner = address(uint160(owner())); _owner.transfer(address(this).balance); } } It is important to note that you c...

LEARN SOLIDITY LESSON 8: Interacting with other contracts. Interface Solidity

LEARN SOLIDITY LESSON Interface Solidity. Interacting with other contracts.  Interface Solidity.  Define an interface of the LuckyNumber contract: contract LuckyNumber { mapping(address => uint) numbers; function setNum(uint _num) public { numbers[msg.sender] = _num; } function getNum(address _myAddress) public view returns (uint) { return numbers[_myAddress]; } } contract NumberInterface { function getNum(address _myAddress) public view returns (uint); } 1. For one, we're only declaring the functions we want to interact with — in this case getNum — and we don't mention any of the other functions or state variables. 2. Secondly, we're not defining the function bodies. Instead of curly braces ({ and }), we're simply ending the function declaration with a semi-colon (;). Interacting with other contracts.  In this way, your contract can interact with any other contract on the Ethereum blockchain, as long they expose those functions as public or ext...