-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAtomex.sol
More file actions
318 lines (251 loc) · 9.76 KB
/
Atomex.sol
File metadata and controls
318 lines (251 loc) · 9.76 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
// From file: openzeppelin-contracts/contracts/math/SafeMath.sol
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
require(c >= a, "SafeMath add wrong value");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath sub wrong value");
return a - b;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
}
// From file: OpenZeppelin/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
abstract contract Ownable {
address private _owner;
address private _successor;
event OwnershipTransferred(address previousOwner, address newOwner);
event NewOwnerProposed(address previousOwner, address newOwner);
constructor() {
setOwner(msg.sender);
}
function owner() public view returns (address) {
return _owner;
}
function successor() public view returns (address) {
return _successor;
}
function setOwner(address newOwner) internal {
_owner = newOwner;
}
function setSuccessor(address newOwner) internal {
_successor = newOwner;
}
modifier onlyOwner() {
require(msg.sender == owner(), "sender is not the owner");
_;
}
modifier onlySuccessor() {
require(msg.sender == successor(), "sender is not the proposed owner");
_;
}
function proposeOwner(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "invalid owner address");
emit NewOwnerProposed(owner(), newOwner);
setSuccessor(newOwner);
}
function acceptOwnership() public virtual onlySuccessor {
emit OwnershipTransferred(owner(), successor());
setOwner(successor());
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(owner(), address(0));
setOwner(address(0));
}
}
contract WatchTower is Ownable, ReentrancyGuard {
using SafeMath for uint256;
struct Watcher {
uint256 deposit;
bool active;
}
event NewWatcherProposed(address _newWatcher, uint256 _deposit);
event NewWatcherActivated(address _newWatcher);
event WatcherDeactivated(address _watcher);
event WatcherWithdrawn(address _watcher);
mapping(address => Watcher) public watchTowers;
function proposeWatcher (address _newWatcher) public payable {
require(_newWatcher != address(0), "invalid watcher address");
require(msg.value > 0, "trasaction value must be greater then zero");
emit NewWatcherProposed(_newWatcher, msg.value);
watchTowers[_newWatcher].deposit = watchTowers[_newWatcher].deposit.add(msg.value);
}
function activateWatcher (address _newWatcher) public onlyOwner {
require(watchTowers[_newWatcher].deposit > 0, "watcher does not exist");
emit NewWatcherActivated(_newWatcher);
watchTowers[_newWatcher].active = true;
}
function deactivateWatcher (address _watcher) public onlyOwner {
require(watchTowers[_watcher].active == true, "watcher does not exist");
emit WatcherDeactivated(_watcher);
watchTowers[_watcher].active = false;
}
function withdrawWatcher () public nonReentrant {
require(watchTowers[msg.sender].deposit > 0, "watcher does not exist");
emit WatcherWithdrawn(msg.sender);
payable(msg.sender).transfer(watchTowers[msg.sender].deposit);
delete watchTowers[msg.sender];
}
}
contract Atomex is WatchTower {
using SafeMath for uint256;
uint releaseTimeout = 1 weeks;
enum State { Empty, Initiated, Redeemed, Refunded, Lost }
struct Swap {
bytes32 hashedSecret;
address payable initiator;
address payable participant;
address payable watcher;
uint256 refundTimestamp;
uint256 watcherDeadline;
uint256 value;
uint256 payoff;
State state;
}
event Initiated(
bytes32 indexed _hashedSecret,
address indexed _participant,
address _initiator,
address _watcher,
uint256 _refundTimestamp,
uint256 _watcherDeadline,
uint256 _value,
uint256 _payoff
);
event Redeemed(
bytes32 indexed _hashedSecret,
bytes32 _secret
);
event Refunded(
bytes32 indexed _hashedSecret
);
event Released(
bytes32 indexed _hashedSecret
);
mapping(bytes32 => Swap) public swaps;
modifier onlyByInitiator(bytes32 _swapId) {
require(msg.sender == swaps[_swapId].initiator, "sender is not the initiator");
_;
}
modifier isInitiatable(address _participant, uint256 _refundTimestamp, address _watcher) {
require(_participant != address(0), "invalid participant address");
require(block.timestamp < _refundTimestamp, "refundTimestamp has already come");
require(watchTowers[_watcher].active == true, "watcher does not exist");
_;
}
modifier isInitiated(bytes32 _swapId) {
require(swaps[_swapId].state == State.Initiated, "swap for this ID is empty or already spent");
_;
}
modifier isRedeemable(bytes32 _swapId, bytes32 _secret) {
require(block.timestamp < swaps[_swapId].refundTimestamp || msg.sender == swaps[_swapId].initiator, "refundTimestamp has already come");
require(sha256(abi.encodePacked(sha256(abi.encodePacked(_secret)))) == swaps[_swapId].hashedSecret, "secret is not correct");
_;
}
modifier isRefundable(bytes32 _swapId) {
require(block.timestamp >= swaps[_swapId].refundTimestamp, "refundTimestamp has not come");
_;
}
modifier isReleasable(bytes32 _swapId) {
require(block.timestamp >= swaps[_swapId].refundTimestamp.add(releaseTimeout), "releaseTimeout has not passed");
_;
}
function multikey(bytes32 _hashedSecret, address _initiator) public pure returns(bytes32) {
return sha256(abi.encodePacked(_hashedSecret, _initiator));
}
function initiate(
bytes32 _hashedSecret, address _participant, address _watcher,
uint256 _refundTimestamp, bool _watcherForRedeem, uint256 _payoff)
public payable nonReentrant isInitiatable(_participant, _refundTimestamp, _watcher)
{
bytes32 swapId = multikey(_hashedSecret, msg.sender);
require(swaps[swapId].state == State.Empty, "swap for this ID is already initiated");
swaps[swapId].value = msg.value.sub(_payoff);
swaps[swapId].hashedSecret = _hashedSecret;
swaps[swapId].participant = payable(_participant);
swaps[swapId].initiator = payable(msg.sender);
swaps[swapId].watcher = payable(_watcher);
swaps[swapId].refundTimestamp = _refundTimestamp;
if(_watcherForRedeem)
swaps[swapId].watcherDeadline = _refundTimestamp.sub(_refundTimestamp.sub(block.timestamp).div(3));
else
swaps[swapId].watcherDeadline = _refundTimestamp.add(_refundTimestamp.sub(block.timestamp).div(2));
swaps[swapId].payoff = _payoff;
swaps[swapId].state = State.Initiated;
emit Initiated(
_hashedSecret,
_participant,
msg.sender,
_watcher,
_refundTimestamp,
swaps[swapId].watcherDeadline,
msg.value.sub(_payoff),
_payoff
);
}
function withdraw(bytes32 _swapId, address payable _receiver) internal
{
if (msg.sender == swaps[_swapId].watcher
|| (block.timestamp >= swaps[_swapId].watcherDeadline && watchTowers[msg.sender].active == true)
|| (msg.sender == swaps[_swapId].initiator && _receiver == swaps[_swapId].participant)) {
(_receiver).transfer(swaps[_swapId].value);
if (swaps[_swapId].payoff > 0) {
payable(msg.sender).transfer(swaps[_swapId].payoff);
}
}
else {
_receiver.transfer(swaps[_swapId].value.add(swaps[_swapId].payoff));
}
delete swaps[_swapId];
}
function redeem(bytes32 _swapId, bytes32 _secret)
public nonReentrant isInitiated(_swapId) isRedeemable(_swapId, _secret)
{
swaps[_swapId].state = State.Redeemed;
emit Redeemed(
swaps[_swapId].hashedSecret,
_secret
);
withdraw(_swapId, swaps[_swapId].participant);
}
function refund(bytes32 _swapId)
public isInitiated(_swapId) isRefundable(_swapId)
{
swaps[_swapId].state = State.Refunded;
emit Refunded(
swaps[_swapId].hashedSecret
);
withdraw(_swapId, swaps[_swapId].initiator);
}
function release(bytes32 _swapId)
public onlyOwner() isReleasable(_swapId)
{
swaps[_swapId].state = State.Lost;
emit Released(
swaps[_swapId].hashedSecret
);
withdraw(_swapId, payable(owner()));
}
}