-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedChilliesTreasury.scilla
189 lines (170 loc) · 4.46 KB
/
RedChilliesTreasury.scilla
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
scilla_version 0
library RedChilliesTreasury
let one_msg =
fun (msg: Message) =>
let nil_msg = Nil {Message} in
Cons {Message} msg nil_msg
(* Error events *)
type Error =
| CodeUnauthorized
| CodeNoZilSent
| CodeNoZilRequested
| CodeNoRedCRequested
let make_error =
fun (result : Error) =>
let result_code =
match result with
| CodeUnauthorized => Int32 -1
| CodeNoZilSent => Int32 -2
| CodeNoZilRequested => Int32 -3
| CodeNoRedCRequested => Int32 -4
end
in
{ _exception : "Error"; code : result_code }
let zero = Uint128 0
let amount_zero = Uint128 0
let success = Uint128 200
contract RedChilliesTreasury
(owner: ByStr20,
redC: ByStr20)
field games : Map ByStr20 String
= Emp ByStr20 String
procedure ThrowError(err : Error)
e = make_error err;
throw e
end
procedure registerNewGame(game: String, address: ByStr20)
games[address] := game
end
procedure removeFaultyGame(address: ByStr20)
delete games[address]
end
procedure withDraw(address: ByStr20, amount: Uint128)
msg = {_tag: "";
_recipient: address;
_amount: amount;
code: success};
msgs = one_msg msg;
send msgs
end
procedure withdrawToken(address: ByStr20, redCAmount: Uint128)
msg_to_token = {
_tag : "Transfer";
_recipient: redC;
_amount: amount_zero;
to: address;
amount: redCAmount
};
msgs = one_msg msg_to_token;
send msgs
end
transition registerGame(gameName: String, address: ByStr20)
isOwner = builtin eq owner _sender;
match isOwner with
| True =>
registerNewGame gameName address;
e = {_eventname: "GameRegistered"; address: _sender; gameAddress: address};
event e
| False =>
err = CodeUnauthorized;
ThrowError err
end
end
transition removeGame(address: ByStr20)
isOwner = builtin eq owner _sender;
match isOwner with
| True =>
removeFaultyGame address;
e = {_eventname: "GameRemoved"; address: _sender; gameAddress: address};
event e
| False =>
err = CodeUnauthorized;
ThrowError err
end
end
transition depositFund()
emptyFund = builtin eq _amount zero;
match emptyFund with
| False =>
isOwner = builtin eq owner _sender;
match isOwner with
| True =>
accept;
e = {_eventname: "FundDeposited"; by: "Owner"; address: _sender; amount: _amount};
event e
| False =>
caller <- games[_sender];
match caller with
| Some v =>
accept;
e = {_eventname: "FundDeposited"; by: v; address: _sender; amount: _amount};
event e
| None =>
err = CodeUnauthorized;
ThrowError err
end
end
| True =>
err = CodeNoZilSent;
ThrowError err
end
end
transition withdrawFund(address: ByStr20, amount: Uint128)
emptyFund = builtin eq amount zero;
match emptyFund with
| False =>
isOwner = builtin eq owner _sender;
match isOwner with
| True =>
withDraw address amount;
e = {_eventname: "FundWithDrawn"; by: "Owner"; sender: _sender; to: address; amount: amount};
event e
| False =>
caller <- games[_sender];
match caller with
| Some v =>
withDraw address amount;
e = {_eventname: "FundWithDrawn"; by: v; sender: _sender; to: address; amount: amount};
event e
| None =>
err = CodeUnauthorized;
ThrowError err
end
end
| True =>
err = CodeNoZilRequested;
ThrowError err
end
end
transition withdrawRedC(address: ByStr20, amount: Uint128)
emptyFund = builtin eq amount zero;
match emptyFund with
| False =>
isOwner = builtin eq owner _sender;
match isOwner with
| True =>
withdrawToken address amount;
e = {_eventname: "RedCWithDrawn"; by: "Owner"; sender: _sender; to: address; amount: amount};
event e
| False =>
err = CodeUnauthorized;
ThrowError err
end
| True =>
err = CodeNoRedCRequested;
ThrowError err
end
end
transition RecipientAcceptTransferFrom(
initiator : ByStr20,
sender : ByStr20,
recipient : ByStr20,
amount : Uint128
)
end
transition TransferSuccessCallBack(
sender : ByStr20,
recipient : ByStr20,
amount : Uint128
)
end