Testnet
Sign in

Contract

zil1p5ttv6wwyxj4p0xxn4eu83nzru42el3cw8fyz9
copy address
qr
label
download csv
Balance0 ZIL - $0
Transactions1
Contract Creationzil1vq5hny...atacdf75c7fc...
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
scilla_version 0

(***************************************************)
(*               Associated library                *)
(***************************************************)
import BoolUtils ListUtils IntUtils
library FungibleToken

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 =
  | CodeNotAuthorized
  | CodeNotFound
  | CodeInsufficientFunds

let make_error_event =
  fun (result : Error) =>
    let result_code = 
      match result with
      | CodeNotAuthorized     => Int32 -1
      | CodeNotFound          => Int32 -2
      | CodeInsufficientFunds => Int32 -3
      end
    in
    { _eventname : "Error"; code : result_code }

let min_int =
  fun (a : Uint128) => fun (b : Uint128) =>
  let min = builtin lt a b in
  match min with
  | True =>
    a
  | False =>
    b
  end
    
(* A util function to test equality *)
let f_eq =
  fun (a : ByStr20) =>
  fun (b : ByStr20) =>
    builtin eq a b

(* Instantiate a type function to test membership in a list *)
let is_default_operator = @list_mem ByStr20

(* A util function to get balance of token_owner *)
let get_balance =
  fun (some_bal: Option Uint128) =>
  match some_bal with
  | Some bal => bal
  | None => Uint128 0
  end

(* Dummy user-defined ADT *)
type Unit =
| Unit

let zero = Uint128 0
let one = Uint128 1

(***************************************************)
(*             The contract definition             *)
(***************************************************)

contract FungibleToken
(contract_owner: ByStr20,
  name : String,
  symbol: String,
  decimals: Uint32,
  default_operators : List ByStr20,
  init_supply : Uint128
)

(* Mutable fields *)

field total_supply : Uint128 = init_supply

field balances_map: Map ByStr20 Uint128 
  = let emp_map = Emp ByStr20 Uint128 in
  builtin put emp_map contract_owner init_supply

field operators_map: Map ByStr20 (Map ByStr20 Unit) 
  = Emp ByStr20 (Map ByStr20 Unit)

field revoked_default_operators : Map ByStr20 (Map ByStr20 Unit) 
  = Emp ByStr20 (Map ByStr20 Unit)

field allowances_map: Map ByStr20 (Map ByStr20 Uint128) 
  = Emp ByStr20 (Map ByStr20 Uint128)

(**************************************)
(*             Procedures             *)
(**************************************)

(* Emit Errors *)
procedure ThrowError(err : Error)
  e = make_error_event err;
  event e;
  throw
end

(* Mint Tokens *)
procedure AuthorizedMint(recipient: ByStr20, amount: Uint128) 
  get_bal <- balances_map[recipient];
  balance = get_balance get_bal;
  new_balance = builtin add amount balance;
  balances_map[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

(* Burn Tokens *)
procedure AuthorizedBurnIfSufficientBalance(from: ByStr20, amount: Uint128)
  get_bal <- balances_map[from];
  match get_bal with
  | None => 
    err = CodeInsufficientFunds;
    ThrowError err
  | Some bal =>
    can_burn = uint128_le amount bal;
    match can_burn with
    | False =>
      err = CodeInsufficientFunds;
      ThrowError err
    | True =>
      (* Subtract amount from 'from' *)
      new_balance = builtin sub bal amount;
      balances_map[from] := new_balance;
      current_total_supply <- total_supply;
      new_total_supply = builtin sub current_total_supply amount;
      total_supply := new_total_supply;
      e = {_eventname: "Burnt"; burner: _sender; burn_account: from; amount: amount};
      event e  
      end
   end
end

(* Move Tokens if authorized *)
procedure AuthorizedMoveIfSufficientBalance(from: ByStr20, to: ByStr20, amount: Uint128)
  get_from_bal <- balances_map[from];
  match get_from_bal with
  | Some 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_map[from] := new_from_bal;
      (* Adds amount to "to" address *)
      get_to_bal <- balances_map[to];
      new_to_bal = match get_to_bal with
      | Some bal => builtin add bal amount
      | None => amount
      end;
      balances_map[to] := new_to_bal
    | False =>
      (* Balance not sufficient *)
      err = CodeInsufficientFunds;
      ThrowError err
    end
  | None =>
    err = CodeNotFound;
    ThrowError err
  end
end

(***************************************)
(*             Transitions             *)
(***************************************)

(* Getter transitions *)

(* @dev: Check if an address is an operator or default operator of a token_owner. Provide a Bool *)
(* @param operator:    Address of a potential operator.                                          *)
(* @param token_owner: Address of a token_owner.                                                 *)
transition IsOperatorFor(token_owner: ByStr20, operator: ByStr20)
  is_operator_approved <- exists operators_map[token_owner][operator];
  is_default_operator = is_default_operator f_eq operator default_operators;
  is_revoked_operator <- exists revoked_default_operators[token_owner][operator];
  is_default_operator_approved = 
    let is_not_revoked_operator = negb is_revoked_operator in 
    andb is_not_revoked_operator is_default_operator;
  is_approved = orb is_operator_approved is_default_operator_approved;
  msg_to_sender = { _tag : "IsOperatorForCallBack"; _recipient : _sender; _amount : Uint128 0;
                    is_operator_for : is_approved};
  msgs = one_msg msg_to_sender;
  send msgs
end

(* Optional transitions *)

(* @dev: Optional transition. Mint new tokens. Only contract_owner 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)
  is_owner = builtin eq _sender contract_owner;
   match is_owner with
    | False =>
      err = CodeNotAuthorized;
      ThrowError err
    | True =>
      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};
      msg_to_sender = {_tag : "MintSuccessCallBack"; _recipient : _sender; _amount : zero; 
                          minter : _sender; recipient : recipient; amount : amount};
      msgs = two_msgs msg_to_recipient msg_to_sender;
      send msgs
    end
end

(* @dev: Optional transition. Burn existing tokens. Only contract_owner can burn. *)
(* @param burn_account: Address of the token_owner whose balance is to decrease.  *)
(* @param amount:       Number of tokens to be burned.                            *)
transition Burn(burn_account: ByStr20, amount: Uint128)
  is_owner = builtin eq _sender contract_owner;
    match is_owner with
    | False =>
      err = CodeNotAuthorized;
      ThrowError err
    | True =>
      AuthorizedBurnIfSufficientBalance burn_account amount;
      msg_to_sender = {_tag : "BurnSuccessCallBack"; _recipient : _sender; _amount : zero; 
                        burner : _sender; burn_account : burn_account; amount : amount};
      msgs = one_msg msg_to_sender;
      send msgs
    end
end

(* Compulsory interface transitions *)

(* @dev: Make an address an operator of the caller.             *)
(* @param operator: Address to be authorize as operator or      *)
(* Re-authorize as default_operator. Cannot be calling address. *)
transition AuthorizeOperator(operator: ByStr20)
  is_sender = builtin eq operator _sender;
  match is_sender with
  | True => 
    (* _sender is authorizing self as operator, throw error code *)
    err = CodeNotAuthorized;
    ThrowError err
  | False =>
    is_default_operator =  is_default_operator f_eq operator default_operators;
    match is_default_operator with
    | True =>
      (* Re-authorize default_operator *)
      delete revoked_default_operators[_sender][operator];
      e = { _eventname : "ReAuthorizedDefaultOperatorSuccess"; authorizer : _sender; reauthorized_default_operator : operator};
      event e
    | False =>
      (* Authorize new operator *)
      authorize = Unit;
      operators_map[_sender][operator] := authorize;
      e = {_eventname : "AuthorizeOperatorSuccess"; authorizer : _sender; authorized_operator : operator};
      event e
    end
  end
end

(* @dev: Revoke an address from being an operator or default_operator of the caller. *)
(* @param operator: Address to be removed as operator or default_operator.           *)
transition RevokeOperator(operator: ByStr20)
  is_default_operator = is_default_operator f_eq operator default_operators;
  match is_default_operator with
  | False =>
    (* Not default_operator, check if operator *)
    get_operator <- operators_map[_sender][operator];
    match get_operator with
    | None =>
      (* Operator to be removed not found, throw error *)
      err = CodeNotFound;
      ThrowError err
    | Some status =>
      delete operators_map[_sender][operator];
      e = {_eventname : "RevokeOperatorSuccess"; revoker : _sender; revoked_operator : operator};
      event e
    end
  | True =>
    (* Is default_operator, revoke default_operator authority instead *)
    verdad = Unit;
    revoked_default_operators[_sender][operator] := verdad;
    e = {_eventname : "RevokedDefaultOperatorSuccess"; revoker : _sender; revoked_default_operator : operator};
    event e
  end
end

(* @dev: Increase the allowance of an approved_spender over the callers tokens. Only token_owner allowed to invoke. *)
(* 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)
  (* Checks if the _sender and approved_spender is the same *)
  is_owner = builtin eq _sender spender;
  match is_owner with
  | True =>
    err = CodeNotAuthorized;
    ThrowError err
  | False =>
    get_current_allowance <- allowances_map[_sender][spender];
    current_allowance =
      match get_current_allowance with
      | Some allowance => allowance
      | None => zero
      end;
    new_allowance = builtin add current_allowance amount;
    allowances_map[_sender][spender] := new_allowance;
    e = {_eventname : "IncreasedAllowance"; token_owner : _sender; spender: spender; new_allowance : new_allowance};
    event e
  end
end

(* @dev: Decrease the allowance of an approved_spender over the callers tokens. Only token_owner allowed to invoke. *)
(* 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)
  (* Checks if the _sender and approved_spender is the same *)
  is_owner = builtin eq _sender spender;
  match is_owner with
  | True =>
    err = CodeNotAuthorized;
    ThrowError err
  | False =>
    get_current_allowance <- allowances_map[_sender][spender];
    current_allowance =
      match get_current_allowance with
      | Some allowance => allowance
      | None => zero
      end;
    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_map[_sender][spender] := new_allowance;
    e = {_eventname : "DecreasedAllowance"; token_owner : _sender; spender: spender; new_allowance : new_allowance};
    event e
  end
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 : "Transfer"; 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: Moves amount tokens from token_owner to recipient. _sender must be an operator of token_owner. *)
(* @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 sent.                                                     *)
transition OperatorSend(from: ByStr20, to: ByStr20, amount: Uint128)
  is_operator_approved <- exists operators_map[from][_sender];
  is_default_operator = is_default_operator f_eq _sender default_operators;
  is_revoked_operator <- exists revoked_default_operators[from][_sender];
  is_default_operator_approved = let is_not_revoked_operator = 
    negb is_revoked_operator in andb is_not_revoked_operator is_default_operator;
  is_approved = orb is_operator_approved is_default_operator_approved;
  match is_approved with
    | False =>
      err = CodeNotAuthorized;
      ThrowError err
    | True =>
      AuthorizedMoveIfSufficientBalance from to amount;
      e = {_eventname : "OperatorSendSuccess"; initiator : _sender; sender : from; recipient : to; amount : amount};
      event e;
      (* Prevent sending to a contract address that does not support transfers of token *)
      msg_to_recipient = {_tag : "RecipientAcceptOperatorSend"; _recipient : to; _amount : zero; 
                          initiator : _sender; sender : from; recipient : to; amount : amount};
      msg_to_sender = {_tag : "OperatorSendSuccessCallBack"; _recipient : _sender; _amount : zero; 
                      initiator : _sender; sender : from; recipient : to; amount : amount};
      msgs = two_msgs msg_to_recipient msg_to_sender;
      send msgs
   end
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)
  get_bal <- balances_map[from];
  match get_bal with
  | None => 
    err = CodeNotFound;
    ThrowError err
  | Some bal =>
    get_spender_allowed <- allowances_map[from][_sender];
    match get_spender_allowed with
    | None =>
      err = CodeNotAuthorized;
      ThrowError err
    | Some allowed =>
      min = min_int bal allowed;
      can_do = uint128_le amount min;
      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_map[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 = CodeInsufficientFunds;
        ThrowError err
      end
    end
  end
end

ViewBlock

Copyright © 2021 Ashlar