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
/
simple_admin.mligo
77 lines (59 loc) · 1.93 KB
/
simple_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
(*
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 the contract. 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 !SIMPLE_ADMIN
#define SIMPLE_ADMIN
#include "./admin_sig.mligo"
module Admin : AdminSig = struct
type entrypoints =
| Set_admin of address
| Confirm_admin of unit
| Pause of bool
type storage = {
admin : address;
pending_admin : address option;
paused : bool;
}
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 (paused, s: bool * storage) : storage =
{ s with paused = paused; }
let fail_if_not_admin (a : storage) : unit =
if (Tezos.get_sender()) <> a.admin
then failwith "NOT_AN_ADMIN"
else unit
let fail_if_paused (a : storage) : unit =
if a.paused
then failwith "PAUSED"
else unit
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 _ ->
let new_s = confirm_new_admin s in
(([]: operation list), new_s)
| Pause paused ->
let _ = fail_if_not_admin s in
let new_s = pause (paused, s) in
(([]: operation list), new_s)
end
#endif