This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ERCTokenImplementation.sol
219 lines (190 loc) · 7.12 KB
/
ERCTokenImplementation.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
214
215
216
217
218
contract ERC165
{
/// @notice Query if a contract implements an interface
/// @param interfaceID The interface identifier, as specified in ERC-165
/// @dev Interface identification is specified in ERC-165. This function
/// uses less than 30,000 gas.
/// @return `true` if the contract implements `interfaceID` and
/// `interfaceID` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
contract ERC875 /* is ERC165 */
{
event Transfer(address indexed _from, address indexed _to, uint256[] tokenIndices);
function name() constant public returns (string name);
function symbol() constant public returns (string symbol);
function balanceOf(address _owner) public view returns (uint256[] _balances);
function transfer(address _to, uint256[] _tokens) public;
function transferFrom(address _from, address _to, uint256[] _tokens) public;
//optional
//function totalSupply() public constant returns (uint256 totalSupply);
function trade(uint256 expiryTimeStamp, uint256[] tokenIndices, uint8 v, bytes32 r, bytes32 s) public payable;
//function ownerOf(uint256 _tokenId) public view returns (address _owner);
}
pragma solidity ^0.4.17;
contract Token is ERC875
{
uint totalTickets;
mapping(address => uint256[]) inventory;
uint16 ticketIndex = 0; //to track mapping in tickets
uint expiryTimeStamp;
address owner; // the address that calls selfdestruct() and takes fees
address admin;
uint transferFee;
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 _from, address indexed _to, uint256[] tokenIndices);
event TransferFrom(address indexed _from, address indexed _to, uint _value);
modifier adminOnly()
{
if(msg.sender != admin) revert();
else _;
}
function() public { revert(); } //should not send any ether directly
// example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "MJ comeback", 1603152000, "MJC", "0x007bEe82BDd9e866b2bd114780a47f2261C684E3"
function Token(
uint256[] numberOfTokens,
string evName,
uint expiry,
string eventSymbol,
address adminAddr) public
{
totalTickets = numberOfTokens.length;
//assign some tickets to event admin
expiryTimeStamp = expiry;
owner = msg.sender;
admin = adminAddr;
inventory[admin] = numberOfTokens;
symbol = eventSymbol;
name = evName;
}
function getDecimals() public pure returns(uint)
{
return decimals;
}
// price is 1 in the example and the contract address is 0xfFAB5Ce7C012bc942F5CA0cd42c3C2e1AE5F0005
// example: 0, [3, 4], 27, "0x2C011885E2D8FF02F813A4CB83EC51E1BFD5A7848B3B3400AE746FB08ADCFBFB", "0x21E80BAD65535DA1D692B4CEE3E740CD3282CCDC0174D4CF1E2F70483A6F4EB2"
// 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,
uint256[] tokenIndices,
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);
//id 1 for mainnet
bytes12 prefix = "ERC800-CNID1";
bytes32 message = encodeMessage(prefix, msg.value, expiry, tokenIndices);
address seller = ecrecover(message, v, r, s);
for(uint i = 0; i < tokenIndices.length; i++)
{ // transfer each individual tickets in the ask order
uint index = uint(tokenIndices[i]);
require((inventory[seller][index] > 0)); // 0 means ticket sold.
inventory[msg.sender].push(inventory[seller][index]);
inventory[seller][index] = 0; // 0 means ticket sold.
}
seller.transfer(msg.value);
}
//must also sign in the contractAddress
//prefix must contain ERC and chain id
function encodeMessage(bytes12 prefix, uint value,
uint expiry, uint256[] tokenIndices)
internal view returns (bytes32)
{
bytes memory message = new bytes(96 + tokenIndices.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 < 12; i++)
{
message[i + 64] = byte(prefix << (8 * i));
}
for(i = 0; i < 20; i++)
{
message[76 + i] = byte(bytes20(bytes20(contractAddress) << (8 * i)));
}
for (i = 0; i < tokenIndices.length; i++)
{
// convert int[] to bytes
message[96 + i * 2 ] = byte(tokenIndices[i] >> 8);
message[96 + i * 2 + 1] = byte(tokenIndices[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 isContractExpired() public view returns (bool)
{
if(block.timestamp > expiryTimeStamp)
{
return true;
}
else return false;
}
function balanceOf(address _owner) public view returns (uint256[])
{
return inventory[_owner];
}
function myBalance() public view returns(uint256[])
{
return inventory[msg.sender];
}
function transfer(address _to, uint256[] tokenIndices) public
{
for(uint i = 0; i < tokenIndices.length; i++)
{
require(inventory[msg.sender][i] != 0);
//pushes each element with ordering
uint index = uint(tokenIndices[i]);
inventory[_to].push(inventory[msg.sender][index]);
inventory[msg.sender][index] = 0;
}
}
function transferFrom(address _from, address _to, uint256[] tokenIndices)
adminOnly public
{
bool isadmin = msg.sender == admin;
for(uint i = 0; i < tokenIndices.length; i++)
{
require(inventory[_from][i] != 0 || isadmin);
//pushes each element with ordering
uint index = uint(tokenIndices[i]);
inventory[_to].push(inventory[_from][index]);
inventory[_from][index] = 0;
}
}
function endContract() public
{
if(msg.sender == owner)
{
selfdestruct(owner);
}
else revert();
}
function getContractAddress() public view returns(address)
{
return this;
}
}