pragma solidity ^0.8.11;
contract myContract {
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];
}
}
pragma solidity ^0.8.11;
contract SendMoney{
uint public balanceReceived;
function receiveMoney() public payable {
balanceReceived += msg.value; // amount in wei
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function withdrawMoney() public {
address payable to = msg.sender;
to.transfer(this.getBalance()); // withdraw all money
// here, you sholud avoid store balance amount in a variable
// beacuse after withdrawMoney work, the balanceReceived won't change
// but this.getBalance() return the real balance == 0;
// if we transfer 1 ether, finally the to account get 100.999999...
}
function withdrawMoneyTo(address payable _to) public {
_to.transfer(this.getBalance());
// if we transfer 1 ether, finally the to account get finally 101!
// because the gas was paid by the contract deployer
}
}