Skip to content

Instantly share code, notes, and snippets.

@developeruche
Created August 4, 2022 13:55
Show Gist options
  • Save developeruche/e1e1e1a0181dd3dd15125f7a55e251de to your computer and use it in GitHub Desktop.
Save developeruche/e1e1e1a0181dd3dd15125f7a55e251de to your computer and use it in GitHub Desktop.
Deposit And Withdraw
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Deposit funds into a contract and keep track of the funds
contract HelloWorld {
mapping (address => uint) public bal;
fallback() external payable {}
receive() external payable {
}
function deposit() payable public {
require(msg.value > 0, "I need money bro!");
bal[msg.sender] += msg.value;
}
function full_withdraw() public {
require(bal[msg.sender] > 0, "Soul, you are empty!");
bal[msg.sender] = 0;
payable(msg.sender).call{value: bal[msg.sender]}("");
}
/// @dev user can spec the amount of ether they want to withdraw
function spec_withdraw(uint _amount) public returns (bool) {
uint coverted_amount = _amount * 10 ** 18;
require(bal[msg.sender] > coverted_amount, "Soul, Fund me Now!");
bal[msg.sender] = bal[msg.sender] - coverted_amount;
(bool sent, ) = payable(msg.sender).call{value: coverted_amount}("");
return sent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment