Skip to content

Instantly share code, notes, and snippets.

@patitonar
Created September 12, 2019 15:12
Show Gist options
  • Save patitonar/5c13666cf3632564b53f96333501c617 to your computer and use it in GitHub Desktop.
Save patitonar/5c13666cf3632564b53f96333501c617 to your computer and use it in GitHub Desktop.
Deposit example in solidity. Get contract balance, get balance of an account, anyone can deposit, anyone can withdraw their own balance
pragma solidity 0.5.11;
contract DepositsV2 {
event Deposited(address indexed payee, uint256 weiAmount);
event Withdrawn(address indexed payee, uint256 weiAmount);
mapping(address => uint256) private _deposits;
function deposit() public payable {
uint256 amount = msg.value;
address payee = msg.sender;
_deposits[payee] = _deposits[payee] + amount; // should use SafeMath .add() here
emit Deposited(payee, amount);
}
function withdraw() public {
address payable payee = msg.sender;
uint256 payment = _deposits[payee];
_deposits[payee] = 0;
payee.transfer(payment);
emit Withdrawn(payee, payment);
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
function depositsOf(address payee) public view returns (uint256) {
return _deposits[payee];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment