-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal Variables, Transfers and Events
61 lines (49 loc) · 1.78 KB
/
Global Variables, Transfers and Events
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//SPDX-License-Identifier: MIT
// Warning: Do not use for actual funds.
pragma solidity ^0.8.9;
contract SimpleAuction {
//Auction parameters
address public immutable beneficiary;
uint public endTime; // As UNIX timestamp
// State of the Auction
uint public highestBid;
address public highestBidder;
bool public hasEnded;
//Amount withdrawable of previous bids
mapping(address => uint) pendingReturns;
// Events
event NewBid(address indexed bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor (address _beneficiary, uint _durationMinutes) {
beneficiary = _beneficiary;
endTime = block.timestamp + _durationMinutes * 1 minutes;
}
function bid() public payable {
require(block.timestamp < endTime, 'Auction Ended ');
require(msg.value > highestBid, 'Bid too small');
if (highestBid != 0){
pendingReturns[highestBidder] += highestBid;
}
highestBid = msg.value;
highestBidder = msg.sender;
emit NewBid(msg.sender, msg.value);
}
function withdraw() external returns (uint amount) {
amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
payable(msg.sender).transferable(amount);
}
//optional: return amount;
}
function auctionEnd() external {
// 1. Check all conditions
require(!hasEnded 'Auction already ended');
require(block.timestamp >= endTime, 'Wait for auction to end');
// 2. Apply all internal state changes
hasEnded = true;
emit AuctionEnded(highestBidder, highestBid);
// 3. Interact with other addresses
payable(beneficiary).transfer(highestBid);
}
}