-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicketPro.sol
213 lines (182 loc) · 6.65 KB
/
TicketPro.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
pragma solidity ^0.4.17;
contract TicketPro
{
mapping(address => bytes32[]) inventory;
uint16 ticketIndex = 0; //to track mapping in tickets
address organiser;
address paymaster;
uint numOfTransfers = 0;
string public name;
string public symbol;
uint8 public constant decimals = 0; //no decimals as tickets cannot be split
event Transfer(address indexed _to, uint16[] _indices);
event TransferFrom(address indexed _from, address indexed _to, uint16[] _indices);
event Trade(address indexed seller, uint16[] ticketIndices, uint8 v, bytes32 r, bytes32 s);
event PassTo(uint16[] ticketIndices, uint8 v, bytes32 r, bytes32 s, address indexed recipient);
modifier organiserOnly()
{
if(msg.sender != organiser) revert();
else _;
}
modifier payMasterOnly()
{
if(msg.sender != paymaster) revert();
else _;
}
function() public { revert(); } //should not send any ether directly
constructor (
bytes32[] tickets,
string nameOfContract,
string symbolForContract,
address organiserAddr,
address paymasterAddr,
address recipientAddr) public
{
name = nameOfContract;
symbol = symbolForContract;
organiser = organiserAddr;
paymaster = paymasterAddr;
inventory[recipientAddr] = tickets;
}
function getDecimals() public pure returns(uint)
{
return decimals;
}
// example: 0, [3, 4], 27, "0x9CAF1C785074F5948310CD1AA44CE2EFDA0AB19C308307610D7BA2C74604AE98", "0x23D8D97AB44A2389043ECB3C1FB29C40EC702282DB6EE1D2B2204F8954E4B451"
// price is encoded in the server and the msg.value is added to the message digest,
// if the message digest is thus invalid then either the price or something else in the message is invalid
function trade(uint256 expiry,
uint16[] ticketIndices,
uint8 v,
bytes32 r,
bytes32 s) public payable
{
//checks expiry timestamp,
//if fake timestamp is added then message verification will fail
require(expiry > block.timestamp || expiry == 0);
bytes32 message = encodeMessage(msg.value, expiry, ticketIndices);
address seller = ecrecover(message, v, r, s);
for(uint i = 0; i < ticketIndices.length; i++)
{ // transfer each individual tickets in the ask order
uint16 index = ticketIndices[i];
assert(inventory[seller][index] != bytes32(0)); // 0 means ticket gone.
inventory[msg.sender].push(inventory[seller][index]);
// 0 means ticket gone.
delete inventory[seller][index];
}
seller.transfer(msg.value);
emit Trade(seller, ticketIndices, v, r, s);
}
function loadNewTickets(bytes32[] tickets) public organiserOnly
{
for(uint i = 0; i < tickets.length; i++)
{
inventory[organiser].push(tickets[i]);
}
}
function passTo(uint256 expiry,
uint16[] ticketIndices,
uint8 v,
bytes32 r,
bytes32 s,
address recipient) public payMasterOnly
{
require(expiry > block.timestamp || expiry == 0);
bytes32 message = encodeMessage(0, expiry, ticketIndices);
address giver = ecrecover(message, v, r, s);
for(uint i = 0; i < ticketIndices.length; i++)
{
uint16 index = ticketIndices[i];
//needs to use revert as all changes should be reversed
//if the user doesnt't hold all the tickets
assert(inventory[giver][index] != bytes32(0));
bytes32 ticket = inventory[giver][index];
inventory[recipient].push(ticket);
delete inventory[giver][index];
}
emit PassTo(ticketIndices, v, r, s, recipient);
}
//must also sign in the contractAddress
function encodeMessage(uint value, uint expiry, uint16[] ticketIndices)
internal view returns (bytes32)
{
bytes memory message = new bytes(84 + ticketIndices.length * 2);
address contractAddress = getContractAddress();
for (uint i = 0; i < 32; i++)
{ // convert bytes32 to bytes[32]
// this adds the price to the message
message[i] = byte(bytes32(value << (8 * i)));
}
for (i = 0; i < 32; i++)
{
message[i + 32] = byte(bytes32(expiry << (8 * i)));
}
for(i = 0; i < 20; i++)
{
message[64 + i] = byte(bytes20(bytes20(contractAddress) << (8 * i)));
}
for (i = 0; i < ticketIndices.length; i++)
{
// convert int[] to bytes
message[84 + i * 2 ] = byte(ticketIndices[i] >> 8);
message[84 + i * 2 + 1] = byte(ticketIndices[i]);
}
return keccak256(message);
}
function name() public view returns(string)
{
return name;
}
function symbol() public view returns(string)
{
return symbol;
}
function getAmountTransferred() public view returns (uint)
{
return numOfTransfers;
}
function balanceOf(address _owner) public view returns (bytes32[])
{
return inventory[_owner];
}
function myBalance() public view returns(bytes32[]){
return inventory[msg.sender];
}
function transfer(address _to, uint16[] ticketIndices) public
{
for(uint i = 0; i < ticketIndices.length; i++)
{
uint index = uint(ticketIndices[i]);
assert(inventory[msg.sender][index] != bytes32(0));
//pushes each element with ordering
inventory[_to].push(inventory[msg.sender][index]);
delete inventory[msg.sender][index];
}
emit Transfer(_to, ticketIndices);
}
function transferFrom(address _from, address _to, uint16[] ticketIndices)
organiserOnly public
{
for(uint i = 0; i < ticketIndices.length; i++)
{
uint index = uint(ticketIndices[i]);
assert(inventory[_from][index] != bytes32(0));
//pushes each element with ordering
inventory[_to].push(inventory[msg.sender][index]);
delete inventory[_from][index];
}
emit TransferFrom(_from, _to, ticketIndices);
}
function endContract() public organiserOnly
{
selfdestruct(organiser);
}
function isStormBirdContract() public pure returns (bool)
{
return true;
}
function getContractAddress() public view returns(address)
{
return this;
}
}