-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptoTwittos.sol
111 lines (83 loc) · 2.77 KB
/
CryptoTwittos.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
pragma solidity ^0.4.19;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import 'zeppelin-solidity/contracts/lifecycle/Destructible.sol';
import 'zeppelin-solidity/contracts/lifecycle/Pausable.sol';
import 'zeppelin-solidity/contracts/math/SafeMath.sol';
contract CryptoTwittos is Ownable, Pausable, Destructible {
using SafeMath for uint;
// A Twitto is owned by a stealer and has a price
struct Twitto {
address stealer;
uint price;
}
// Look up Twitto by ids
mapping(uint => Twitto) public twittos;
// All Twitto ids and counter
uint[] public twittoIds;
uint public twittosCounter;
// Fire event when steal happens
event stealEvent(
uint indexed id,
address indexed owner,
uint price,
address indexed stealer,
uint newPrice
);
// Get twittoIds
function getTwittoIds(bool all) public view returns (uint[]) {
// Return empty array if counter is zero
if (twittosCounter == 0) return new uint[](0);
if (all) {
// Return all of them
return twittoIds;
} else {
// Create memory array to store filtered ids
uint[] memory filteredIds = new uint[](twittosCounter);
// Store number of belongings
uint twittosCount = 0;
for (uint i = 0; i < twittosCounter; i++) {
// Check if stealer is sender
if (twittos[twittoIds[i]].stealer == msg.sender) {
filteredIds[twittosCount] = twittoIds[i];
twittosCount++;
}
}
// Copy the filteredIds array into a smaller array
uint[] memory trophies = new uint[](twittosCount);
for (uint j = 0; j < twittosCount; j++) {
trophies[j] = filteredIds[j];
}
return trophies;
}
}
// Steal a Twitto by paying its price and setting a new one
function steal(uint id, uint256 newPrice) payable whenNotPaused public {
// look up the twitto and put on storage
Twitto storage _twitto = twittos[id];
// Prevent self stealing!
require(msg.sender != _twitto.stealer);
// Make sure the sender pays the right price
require(msg.value == _twitto.price);
// Make sure that the new price is higher than the old price
require(newPrice > _twitto.price);
// Transfer value with the 1% dev fee
if (msg.value > 0) {
_twitto.stealer.transfer(msg.value.mul(99).div(100));
}
// Push new Twitto if not existing
if (_twitto.price == 0) {
twittoIds.push(id);
twittosCounter++;
}
// Trigger event
stealEvent(id, _twitto.stealer, _twitto.price, msg.sender, newPrice);
// Store new stealer
_twitto.stealer = msg.sender;
// Store new price
_twitto.price = newPrice;
}
function withdraw() public onlyOwner {
// Transfer balance to owner
msg.sender.transfer(address(this).balance);
}
}