-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAtomicSwap.sol
281 lines (235 loc) · 9.88 KB
/
AtomicSwap.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
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
pragma solidity ^0.5.0;
// From file: openzeppelin-contracts/contracts/math/SafeMath.sol
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
}
// From file: openzeppelin-contracts/contracts/utils/Address.sol
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
}
// File: openzeppelin-contracts/contracts/token/ERC20/SafeERC20.sol
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function callOptionalReturn(IERC20 token, bytes memory data) private {
require(address(token).isContract(), "SafeERC20: call to non-contract");
(bool success, bytes memory returndata) = address(token).call(data);
require(success, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
// File: openzeppelin-contracts/contracts/token/ERC20/IERC20.sol
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File: openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol
contract ReentrancyGuard {
bool private _notEntered;
constructor () internal {
_notEntered = true;
}
modifier nonReentrant() {
require(_notEntered, "ReentrancyGuard: reentrant call");
_notEntered = false;
_;
_notEntered = true;
}
}
contract AtomicSwap is ReentrancyGuard {
using SafeMath for uint;
using SafeERC20 for IERC20;
enum State { Empty, Initiated, Redeemed, Refunded }
struct Swap {
bytes32 hashedSecret;
bytes32 secret;
address contractAddr;
address participant;
address payable initiator;
uint refundTimestamp;
uint countdown;
uint value;
uint payoff;
bool active;
State state;
}
event Initiated(
bytes32 indexed _hashedSecret,
address indexed _contract,
address indexed _participant,
address _initiator,
uint _refundTimestamp,
uint _countdown,
uint _value,
uint _payoff,
bool _active
);
event Added(
bytes32 indexed _hashedSecret,
address _sender,
uint _value
);
event Activated(
bytes32 indexed _hashedSecret
);
event Redeemed(
bytes32 indexed _hashedSecret,
bytes32 _secret
);
event Refunded(
bytes32 indexed _hashedSecret
);
mapping(bytes32 => Swap) public swaps;
modifier onlyByInitiator(bytes32 _hashedSecret) {
require(msg.sender == swaps[_hashedSecret].initiator, "sender is not the initiator");
_;
}
modifier isInitiatable(bytes32 _hashedSecret, address _participant, uint _refundTimestamp, uint _countdown) {
require(_participant != address(0), "invalid participant address");
require(swaps[_hashedSecret].state == State.Empty, "swap for this hash is already initiated");
require(block.timestamp <= _refundTimestamp, "invalid refundTimestamp");
require(_countdown < _refundTimestamp, "invalid countdown");
_;
}
modifier isInitiated(bytes32 _hashedSecret) {
require(swaps[_hashedSecret].state == State.Initiated, "swap for this hash is empty or already spent");
_;
}
modifier isAddable(bytes32 _hashedSecret) {
require(block.timestamp <= swaps[_hashedSecret].refundTimestamp, "refundTimestamp has already come");
_;
}
modifier isActivated(bytes32 _hashedSecret) {
require(swaps[_hashedSecret].active, "swap is not active");
_;
}
modifier isNotActivated(bytes32 _hashedSecret) {
require(!swaps[_hashedSecret].active, "swap is already active");
_;
}
modifier isRedeemable(bytes32 _hashedSecret, bytes32 _secret) {
require(block.timestamp <= swaps[_hashedSecret].refundTimestamp, "refundTimestamp has already passed");
require(sha256(abi.encodePacked(sha256(abi.encodePacked(_secret)))) == _hashedSecret, "secret is not correct");
_;
}
modifier isRefundable(bytes32 _hashedSecret) {
require(block.timestamp > swaps[_hashedSecret].refundTimestamp, "refundTimestamp has not passed");
_;
}
function initiate (bytes32 _hashedSecret, address _contract, address _participant, uint _refundTimestamp, uint _countdown, uint _value, uint _payoff, bool _active)
public nonReentrant isInitiatable(_hashedSecret, _participant, _refundTimestamp, _countdown)
{
IERC20(_contract).safeTransferFrom(msg.sender, address(this), _value);
swaps[_hashedSecret].value = _value.sub(_payoff);
swaps[_hashedSecret].hashedSecret = _hashedSecret;
swaps[_hashedSecret].contractAddr = _contract;
swaps[_hashedSecret].participant = _participant;
swaps[_hashedSecret].initiator = msg.sender;
swaps[_hashedSecret].refundTimestamp = _refundTimestamp;
swaps[_hashedSecret].countdown = _countdown;
swaps[_hashedSecret].payoff = _payoff;
swaps[_hashedSecret].active = _active;
swaps[_hashedSecret].state = State.Initiated;
emit Initiated(
_hashedSecret,
_contract,
_participant,
msg.sender,
_refundTimestamp,
_countdown,
_value.sub(_payoff),
_payoff,
_active
);
}
function add (bytes32 _hashedSecret, uint _value)
public nonReentrant isInitiated(_hashedSecret) isAddable(_hashedSecret)
{
IERC20(swaps[_hashedSecret].contractAddr).safeTransferFrom(msg.sender, address(this), _value);
swaps[_hashedSecret].value = swaps[_hashedSecret].value.add(_value);
emit Added(
_hashedSecret,
msg.sender,
swaps[_hashedSecret].value
);
}
function activate (bytes32 _hashedSecret)
public nonReentrant isInitiated(_hashedSecret) isNotActivated(_hashedSecret) onlyByInitiator(_hashedSecret)
{
swaps[_hashedSecret].active = true;
emit Activated(
_hashedSecret
);
}
function redeem(bytes32 _hashedSecret, bytes32 _secret)
public nonReentrant isInitiated(_hashedSecret) isActivated(_hashedSecret) isRedeemable(_hashedSecret, _secret)
{
swaps[_hashedSecret].secret = _secret;
swaps[_hashedSecret].state = State.Redeemed;
if (block.timestamp > swaps[_hashedSecret].refundTimestamp.sub(swaps[_hashedSecret].countdown)) {
IERC20(swaps[_hashedSecret].contractAddr).safeTransfer(swaps[_hashedSecret].participant, swaps[_hashedSecret].value);
if(swaps[_hashedSecret].payoff > 0) {
IERC20(swaps[_hashedSecret].contractAddr).safeTransfer(msg.sender, swaps[_hashedSecret].payoff);
}
}
else {
IERC20(swaps[_hashedSecret].contractAddr).safeTransfer(swaps[_hashedSecret].participant, swaps[_hashedSecret].value.add(swaps[_hashedSecret].payoff));
}
emit Redeemed(
_hashedSecret,
_secret
);
delete swaps[_hashedSecret];
}
function refund(bytes32 _hashedSecret)
public nonReentrant isInitiated(_hashedSecret) isRefundable(_hashedSecret)
{
swaps[_hashedSecret].state = State.Refunded;
IERC20(swaps[_hashedSecret].contractAddr).safeTransfer(swaps[_hashedSecret].initiator, swaps[_hashedSecret].value.add(swaps[_hashedSecret].payoff));
emit Refunded(
_hashedSecret
);
delete swaps[_hashedSecret];
}
}