-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicketBooth.sol
70 lines (57 loc) · 1.82 KB
/
TicketBooth.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pragma solidity^0.4.21;
/*
"Event","032418",1,20
*/
contract TicketBooth {
address public _ticketMaster;
string public _eventName;
string public _eventDate;
uint public _eventCost;
uint public _ticketsLeft;
struct TicketData{
bool _hasPaid;
uint _pricePaid;
}
mapping(address => TicketData) public _ticketHolders;
function TicketBooth (string eventName, string eventDate, uint eventCost, uint ticketsLeft) public {
_ticketMaster = msg.sender;
_eventName = eventName;
_eventDate = eventDate;
_eventCost = eventCost;
_ticketsLeft = ticketsLeft;
}
modifier stillTicketsLeft() {
require(_ticketsLeft > 0);
_;
}
modifier onlyTicketMaster() {
require(_ticketMaster == msg.sender);
_;
}
modifier notTicketHolder() {
require(_ticketHolders[msg.sender]._hasPaid == false);
_;
}
function updateTicketsLeft(uint ticketsLeft) public onlyTicketMaster {
if(_ticketsLeft > 0)
_ticketsLeft += ticketsLeft;
else
_ticketsLeft = ticketsLeft;
}
function updateEventCost(uint eventCost) public onlyTicketMaster {
_eventCost = eventCost;
}
function buyTicket() public payable stillTicketsLeft notTicketHolder{
require(msg.value >= _eventCost);
_ticketsLeft--;
_ticketHolders[msg.sender] = TicketData(true, _eventCost);
msg.sender.transfer(msg.value - _eventCost);
}
function refundTicket() public{
require(_ticketHolders[msg.sender]._hasPaid);
uint pricePaid = _ticketHolders[msg.sender]._pricePaid;
delete _ticketHolders[msg.sender];
msg.sender.transfer(pricePaid);
_ticketsLeft++;
}
}