-
Notifications
You must be signed in to change notification settings - Fork 4
/
AtomicSwapTz.tz
109 lines (109 loc) · 4.44 KB
/
AtomicSwapTz.tz
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
parameter (or
(or
(pair %initiate
(address %participant)
(pair
(pair (bytes %hashed_secret) (timestamp %refund_time))
(mutez %payoff)))
(bytes %add :hashed_secret))
(or
(bytes %redeem :secret)
(bytes %refund :hashed_secret)));
storage (pair
(big_map
bytes
(pair
(pair (address %initiator) (address %participant))
(pair
(pair (mutez %amount) (timestamp %refund_time))
(mutez %payoff))))
unit);
code {
NIL @operations operation; SWAP;
UNPAPAIR @% @% @%; DIP {DUP};
IF_LEFT
{
IF_LEFT
{
UNPAIR @% @%;
DUP; CONTRACT @participant unit; IF_SOME {DROP} { PUSH string "wrong participant address"; FAILWITH };
SWAP; UNPPAIIR @% @% @%;
DUP; SIZE; PUSH nat 32; IFCMPEQ {} {PUSH string "hash size is not correct"; FAILWITH };
DIP
{
DUP; NOW; IFCMPLT {} { PUSH string "wrong refund_time"; FAILWITH };
DIP { DUP }; SWAP;
AMOUNT @amount; SUB;
SOURCE;
DUP; CONTRACT @initiator unit; IF_SOME {DROP} { PUSH string "wrong sender address"; FAILWITH };
DIP { PPAIIR; SWAP; }; PPAIIR; SOME @xcat;
SWAP;
};
DUP; DIP { MEM; NOT; IF {} {PUSH string "swap for this hash is already initiated"; FAILWITH} };
}
{
DUP;
DIP
{
GET; IF_SOME {} { PUSH string "no swap for such hash"; FAILWITH };
UNPAIR @% @%;
DIP
{
UNPPAIIR @% @% @%; SWAP;
DUP; NOW; IFCMPLT {} { PUSH string "refund_time has already come"; FAILWITH }; SWAP;
AMOUNT @amount; ADD;
};
PAPPAIIR; SOME @xcat;
};
};
UPDATE; PAIR @new_storage; SWAP; PAIR;
}
{
IF_LEFT
{
PUSH mutez 0; AMOUNT; IFCMPEQ {} {PUSH string "can not accept tez"; FAILWITH };
DUP; SIZE; PUSH nat 32; IFCMPEQ {} {PUSH string "secret size is not correct"; FAILWITH };
SHA256; SHA256 @hash; DUP; DIP {SWAP};
DIIP
{
GET; IF_SOME {} { PUSH string "no swap for such secret"; FAILWITH };
DUP; UNPAIR @% @%; CDR @%; CONTRACT @participant unit; IF_SOME {} { PUSH string "recipient does not exist"; FAILWITH };
SWAP; CAAR @%;
DIIP
{
SOURCE;
CONTRACT @sender unit; IF_SOME {} { PUSH string "wrong sender address"; FAILWITH };
SWAP; CDR @%; UNPPAIIR @% @% @%; DROP;
NOW; IFCMPLT {} { PUSH string "refund_time has already come"; FAILWITH };
DUP; PUSH mutez 0;
IFCMPLT
{
UNIT; TRANSFER_TOKENS;
DIP {SWAP}; CONS;
}
{
DROP; DROP; SWAP
};
};
UNIT; TRANSFER_TOKENS;
};
}
{
PUSH mutez 0; AMOUNT; IFCMPEQ {} {PUSH string "can not accept tez"; FAILWITH };
DUP;
DIP
{
GET; IF_SOME {} { PUSH string "no swap for such hash"; FAILWITH };
DUP; CAAR @%; CONTRACT @initiator unit; IF_SOME {} { PUSH string "recipient does not exist"; FAILWITH }; SWAP;
CDR; UNPPAIIR @% @% @%; SWAP;
NOW; IFCMPGE {} { PUSH string "refund_time has not come"; FAILWITH };
ADD;
UNIT; TRANSFER_TOKENS; SWAP;
DIIP {SWAP};
};
};
NONE @none (pair (pair address address) (pair (pair mutez timestamp) mutez));
SWAP; UPDATE @cleared_map; SWAP; DIP { SWAP; DIP {PAIR} };
CONS; PAIR;
}
}