-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathAtomicSwap.sol
124 lines (110 loc) · 3.73 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
pragma solidity ^0.4.15;
contract AtomicSwap {
enum State { Empty, Initiator, Participant }
struct Swap {
uint initTimestamp;
uint refundTime;
bytes20 hashedSecret;
bytes32 secret;
address initiator;
address participant;
uint256 value;
bool emptied;
State state;
}
mapping(bytes20 => Swap) public swaps;
event Refunded(uint _refundTime);
event Redeemed(uint _redeemTime);
event Participated(
address _initiator,
address _participator,
bytes20 _hashedSecret,
uint256 _value
);
event Initiated(
uint _initTimestamp,
uint _refundTime,
bytes20 _hashedSecret,
address _participant,
address _initiator,
uint256 _funds
);
function AtomicSwap() {}
modifier isRefundable(bytes20 _hashedSecret) {
require(block.timestamp > swaps[_hashedSecret].initTimestamp + swaps[_hashedSecret].refundTime);
require(swaps[_hashedSecret].emptied == false);
_;
}
modifier isRedeemable(bytes20 _hashedSecret, bytes32 _secret) {
require(ripemd160(_secret) == _hashedSecret);
require(block.timestamp < swaps[_hashedSecret].initTimestamp + swaps[_hashedSecret].refundTime);
require(swaps[_hashedSecret].emptied == false);
_;
}
modifier isInitiator(bytes20 _hashedSecret) {
require(msg.sender == swaps[_hashedSecret].initiator);
_;
}
modifier isNotInitiated(bytes20 _hashedSecret) {
require(swaps[_hashedSecret].state == State.Empty);
_;
}
function initiate (uint _refundTime,bytes20 _hashedSecret,address _participant)
payable
isNotInitiated(_hashedSecret)
{
swaps[_hashedSecret].refundTime = _refundTime;
swaps[_hashedSecret].initTimestamp = block.timestamp;
swaps[_hashedSecret].hashedSecret = _hashedSecret;
swaps[_hashedSecret].participant = _participant;
swaps[_hashedSecret].initiator = msg.sender;
swaps[_hashedSecret].state = State.Initiator;
swaps[_hashedSecret].value = msg.value;
Initiated(
swaps[_hashedSecret].initTimestamp,
_refundTime,
_hashedSecret,
_participant,
msg.sender,
msg.value
);
}
function participate(uint _refundTime, bytes20 _hashedSecret,address _initiator)
payable
isNotInitiated(_hashedSecret)
{
swaps[_hashedSecret].refundTime = _refundTime;
swaps[_hashedSecret].initTimestamp = block.timestamp;
swaps[_hashedSecret].participant = msg.sender;
swaps[_hashedSecret].initiator = _initiator;
swaps[_hashedSecret].value = msg.value;
swaps[_hashedSecret].hashedSecret = _hashedSecret;
swaps[_hashedSecret].state = State.Participant;
Participated(_initiator,msg.sender,_hashedSecret,msg.value);
}
function redeem(bytes32 _secret, bytes20 _hashedSecret)
isRedeemable(_hashedSecret, _secret)
{
if(swaps[_hashedSecret].state == State.Participant){
swaps[_hashedSecret].initiator.transfer(swaps[_hashedSecret].value);
}
if(swaps[_hashedSecret].state == State.Initiator){
swaps[_hashedSecret].participant.transfer(swaps[_hashedSecret].value);
}
swaps[_hashedSecret].emptied = true;
Redeemed(block.timestamp);
swaps[_hashedSecret].secret = _secret;
}
function refund(bytes20 _hashedSecret)
isRefundable(_hashedSecret)
{
if(swaps[_hashedSecret].state == State.Participant){
swaps[_hashedSecret].participant.transfer(swaps[_hashedSecret].value);
}
if(swaps[_hashedSecret].state == State.Initiator){
swaps[_hashedSecret].initiator.transfer(swaps[_hashedSecret].value);
}
swaps[_hashedSecret].emptied = true;
Refunded(block.timestamp);
}
}