This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
token_admin.mligo
105 lines (83 loc) · 2.8 KB
/
token_admin.mligo
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
(*
One of the possible implementations of admin API for FA2 contract.
The admin API can change an admin address using two step confirmation pattern and
pause/unpause transfer per token type.
Only current admin can initiate those operations.
Other entry points may guard their access using helper functions
`fail_if_not_admin` and `fail_if_paused`.
*)
#if !TOKEN_ADMIN
#define TOKEN_ADMIN
#include "../fa2/fa2_interface.mligo"
module Admin = struct
type paused_tokens_set = (token_id, unit) big_map
type pause_param =
[@layout:comb]
{
token_id : token_id;
paused : bool;
}
type entrypoints =
| Set_admin of address
| Confirm_admin of unit
| Pause of pause_param list
type storage = {
admin : address;
pending_admin : address option;
paused : paused_tokens_set;
}
let set_admin (new_admin, s : address * storage) : storage =
{ s with pending_admin = Some new_admin; }
let confirm_new_admin (s : storage) : storage =
match s.pending_admin with
| None -> (failwith "NO_PENDING_ADMIN" : storage)
| Some pending ->
let sender = Tezos.get_sender () in
if sender = pending
then { s with
pending_admin = (None : address option);
admin = sender;
}
else (failwith "NOT_A_PENDING_ADMIN" : storage)
let pause (tokens, s: (pause_param list) * storage) : storage =
let new_paused = List.fold
(fun (paused_set, t : paused_tokens_set * pause_param) ->
if t.paused
then Big_map.add t.token_id unit paused_set
else Big_map.remove t.token_id paused_set
)
tokens s.paused in
{ s with paused = new_paused; }
let fail_if_not_admin (a : storage) : unit =
if (Tezos.get_sender ()) <> a.admin
then failwith "NOT_AN_ADMIN"
else ()
let fail_if_paused_tokens (transfers, paused : transfer list * paused_tokens_set) : unit =
List.iter
(fun (tx: transfer) ->
List.iter (fun (txd : transfer_destination) ->
if Big_map.mem txd.token_id paused
then failwith "TOKEN_PAUSED"
else ()
) tx.txs
) transfers
let fail_if_paused (a, param : storage * fa2_entry_points) : unit =
match param with
| Balance_of _ -> ()
| Update_operators _ -> ()
| Transfer transfers -> fail_if_paused_tokens(transfers, a.paused)
let main (param, s : entrypoints * storage) : (operation list) * storage =
match param with
| Set_admin new_admin ->
let _ = fail_if_not_admin s in
let new_s = set_admin (new_admin, s) in
(([]: operation list), new_s)
| Confirm_admin u ->
let new_s = confirm_new_admin s in
(([]: operation list), new_s)
| Pause tokens ->
let _ = fail_if_not_admin s in
let new_s = pause (tokens, s) in
(([]: operation list), new_s)
end
#endif