-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgzil.scilla
240 lines (235 loc) · 9.28 KB
/
gzil.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
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
scilla_version 0
(***************************************************)
(* Associated library *)
(***************************************************)
import IntUtils
library GZIL
let one_msg =
fun (msg: Message) =>
let nil_msg = Nil {Message} in
Cons {Message} msg nil_msg
let two_msgs =
fun (msg1: Message) =>
fun (msg2: Message) =>
let msgs_tmp = one_msg msg2 in
Cons {Message} msg1 msgs_tmp
(* Error events *)
type Error =
| CodeIsSender
| CodeInsufficientFunds
| CodeInsufficientAllowance
| CodeNotOwner
| CodeNotMinter
let make_error =
fun (result: Error) =>
let result_code =
match result with
| CodeIsSender => Int32 -1
| CodeInsufficientFunds => Int32 -2
| CodeInsufficientAllowance => Int32 -3
| CodeNotOwner => Int32 -4
| CodeNotMinter => Int32 -5
end
in
{ _exception: "Error"; code: result_code }
let zero = Uint128 0
let get_val =
fun (some_val: Option Uint128) =>
match some_val with
| Some val => val
| None => zero
end
(***************************************************)
(* The contract definition *)
(***************************************************)
contract GZIL
(
contract_owner: ByStr20,
init_minter: ByStr20,
name: String,
symbol: String,
decimals: Uint32,
init_supply: Uint128,
num_minting_blocks: Uint128
)
(* Mutable fields *)
field total_supply: Uint128 = Uint128 0
field balances: Map ByStr20 Uint128
= let emp_map = Emp ByStr20 Uint128 in
builtin put emp_map contract_owner init_supply
field allowances: Map ByStr20 (Map ByStr20 Uint128)
= Emp ByStr20 (Map ByStr20 Uint128)
(* This will be set to the current implementation contract address *)
field minter: ByStr20 = init_minter
field end_block: BNum = builtin badd _creation_block num_minting_blocks
(**************************************)
(* Procedures *)
(**************************************)
procedure ThrowError(err: Error)
e = make_error err;
throw e
end
procedure IsOwner(address: ByStr20)
is_owner = builtin eq contract_owner address;
match is_owner with
| True =>
| False =>
err = CodeNotOwner;
ThrowError err
end
end
procedure IsMinter()
current_minter <- minter;
is_minter = builtin eq current_minter _sender;
match is_minter with
| True =>
| False =>
err = CodeNotMinter;
ThrowError err
end
end
procedure IsNotSender(address: ByStr20)
is_sender = builtin eq _sender address;
match is_sender with
| True =>
err = CodeIsSender;
ThrowError err
| False =>
end
end
procedure AuthorizedMint(recipient: ByStr20, amount: Uint128)
some_bal <- balances[recipient];
bal = get_val some_bal;
new_balance = builtin add amount bal;
balances[recipient] := new_balance;
current_total_supply <- total_supply;
new_total_supply = builtin add current_total_supply amount;
total_supply := new_total_supply;
e = {_eventname: "Minted"; minter: _sender; recipient: recipient; amount: amount};
event e
end
procedure AuthorizedMoveIfSufficientBalance(from: ByStr20, to: ByStr20, amount: Uint128)
o_from_bal <- balances[from];
bal = get_val o_from_bal;
can_do = uint128_le amount bal;
match can_do with
| True =>
(* Subtract amount from from and add it to to address *)
new_from_bal = builtin sub bal amount;
balances[from] := new_from_bal;
(* Adds amount to to address *)
get_to_bal <- balances[to];
new_to_bal = match get_to_bal with
| Some bal => builtin add bal amount
| None => amount
end;
balances[to] := new_to_bal
| False =>
(* Balance not sufficient *)
err = CodeInsufficientFunds;
ThrowError err
end
end
(***************************************)
(* Transitions *)
(***************************************)
transition ChangeMinter(new_minter: ByStr20)
IsOwner _sender;
minter := new_minter;
e = {_eventname: "ChangedMinter"; new_minter: new_minter};
event e
end
(* @dev: Mint new tokens. Only minter which is ssnlist can mint. *)
(* @param recipient: Address of the recipient whose balance is to increase. *)
(* @param amount: Number of tokens to be minted. *)
transition Mint(recipient: ByStr20, amount: Uint128)
current_block <- & BLOCKNUMBER;
end_block_num <- end_block;
is_minting_over = builtin blt end_block_num current_block;
match is_minting_over with
| True =>
e = {_eventname: "MintIsOver"};
event e
| False =>
IsMinter;
AuthorizedMint recipient amount;
(* Prevent sending to a contract address that does not support transfers of token *)
msg_to_recipient = {_tag: "RecipientAcceptMint"; _recipient: recipient; _amount: zero;
minter: _sender; recipient: recipient; amount: amount};
msgs = one_msg msg_to_recipient;
send msgs
end
end
(* @dev: Increase the allowance of an approved spender over the caller's tokens. *)
(* param spender: Address of the designated approved spender. *)
(* param amount: Number of tokens to be increased as allowance for the approved spender. *)
transition IncreaseAllowance(spender: ByStr20, amount: Uint128)
IsNotSender spender;
some_current_allowance <- allowances[_sender][spender];
current_allowance = get_val some_current_allowance;
new_allowance = builtin add current_allowance amount;
allowances[_sender][spender] := new_allowance;
e = {_eventname: "IncreasedAllowance"; token_owner: _sender; spender: spender; new_allowance: new_allowance};
event e
end
(* @dev: Decrease the allowance of an approved spender over the caller's tokens. *)
(* param spender: Address of the designated approved spender. *)
(* param amount: Number of tokens to be decreased as allowance for the approved spender. *)
transition DecreaseAllowance(spender: ByStr20, amount: Uint128)
IsNotSender spender;
some_current_allowance <- allowances[_sender][spender];
current_allowance = get_val some_current_allowance;
new_allowance =
let amount_le_allowance = uint128_le amount current_allowance in
match amount_le_allowance with
| True => builtin sub current_allowance amount
| False => zero
end;
allowances[_sender][spender] := new_allowance;
e = {_eventname: "DecreasedAllowance"; token_owner: _sender; spender: spender; new_allowance: new_allowance};
event e
end
(* @dev: Moves an amount tokens from _sender to the recipient. Used by token_owner. *)
(* @dev: Balance of recipient will increase. Balance of _sender will decrease. *)
(* @param to: Address of the recipient whose balance is increased. *)
(* @param amount: Amount of tokens to be sent. *)
transition Transfer(to: ByStr20, amount: Uint128)
AuthorizedMoveIfSufficientBalance _sender to amount;
e = {_eventname : "TransferSuccess"; sender : _sender; recipient : to; amount : amount};
event e;
(* Prevent sending to a contract address that does not support transfers of token *)
msg_to_recipient = {_tag: "RecipientAcceptTransfer"; _recipient: to; _amount: zero;
sender: _sender; recipient: to; amount: amount};
msg_to_sender = {_tag: "TransferSuccessCallBack"; _recipient: _sender; _amount: zero;
sender: _sender; recipient: to; amount: amount};
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
end
(* @dev: Move a given amount of tokens from one address to another using the allowance mechanism. The caller must be an approved_spender. *)
(* @dev: Balance of recipient will increase. Balance of token_owner will decrease. *)
(* @param from: Address of the token_owner whose balance is decreased. *)
(* @param to: Address of the recipient whose balance is increased. *)
(* @param amount: Amount of tokens to be transferred. *)
transition TransferFrom(from: ByStr20, to: ByStr20, amount: Uint128)
o_spender_allowed <- allowances[from][_sender];
allowed = get_val o_spender_allowed;
can_do = uint128_le amount allowed;
match can_do with
| True =>
AuthorizedMoveIfSufficientBalance from to amount;
e = {_eventname : "TransferFromSuccess"; initiator : _sender; sender : from; recipient : to; amount : amount};
event e;
new_allowed = builtin sub allowed amount;
allowances[from][_sender] := new_allowed;
(* Prevent sending to a contract address that does not support transfers of token *)
msg_to_recipient = {_tag: "RecipientAcceptTransferFrom"; _recipient : to; _amount: zero;
initiator: _sender; sender : from; recipient: to; amount: amount};
msg_to_sender = {_tag: "TransferFromSuccessCallBack"; _recipient: _sender; _amount: zero;
initiator: _sender; sender: from; recipient: to; amount: amount};
msgs = two_msgs msg_to_recipient msg_to_sender;
send msgs
| False =>
err = CodeInsufficientAllowance;
ThrowError err
end
end