Last active
August 31, 2021 15:32
-
-
Save Davidegloh/666c9e7ceae6c0fe65c0f6798cbed326 to your computer and use it in GitHub Desktop.
[Mapping -Solidity] #mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mapping Solidity : | |
//Mapping is a key-value storage. They are also called dictionnary. | |
// K -> V | |
// address -> Balance | |
// so how do we defined a mapping ? | |
//mapping (keyType => valueType) name; | |
// Exemple : mapping(address => uint) balance; | |
// Then, when we want to add a value to a mapping/a spefiic key we assign this value like this : | |
// balance[address] = 10; | |
// If I want to retrieve this balance for this specific address : | |
// return balance [address]; | |
pragma solidity 0.7.5; | |
contract Bank { | |
mapping (address => uint) balance; //ligne de code à tapper pour le mapping | |
function addBalance(uint _toAdd) public returns(uint) { //fonction addBalance pour rajouter un uint à une address | |
balance[msg.sender] += _toAdd; // ou aussi balance [msg.sender] + _toAdd | |
return balance[msg.sender]; //on retourne balance [msg.sender] updaté | |
} | |
function getBalance() public view returns(uint) {// fonction pour afficher une balance | |
return balance[msg.sender]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment