forked from cnxtech/zns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.scilla
More file actions
162 lines (145 loc) · 4.45 KB
/
Copy pathresolver.scilla
File metadata and controls
162 lines (145 loc) · 4.45 KB
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
scilla_version 0
import BoolUtils
library ResolverLib
type RecordKeyValue =
| RecordKeyValue of String String
let nilMessage = Nil {Message}
let oneMsg =
fun(msg: Message) =>
Cons {Message} msg nilMessage
let eOwnerSet =
fun(address: ByStr20) =>
{_eventname: "OwnerSet"; address: address}
let eRecordsSet =
fun(registry: ByStr20) =>
fun(node: ByStr32) =>
{_eventname: "RecordsSet"; registry: registry; node: node}
let eError =
fun(message: String) =>
{_eventname: "Error"; message: message}
let emptyValue = ""
let mOnResolverConfigured =
fun(registry: ByStr20) =>
fun(node: ByStr32) =>
let m = {_tag: "onResolverConfigured"; _amount: Uint128 0; _recipient: registry; node: node} in
oneMsg m
let copyRecordsFromList =
fun (recordsMap: Map String String) =>
fun (recordsList: List RecordKeyValue) =>
let foldl = @list_foldl RecordKeyValue Map String String in
let iter =
fun (recordsMap: Map String String) =>
fun (el: RecordKeyValue) =>
match el with
| RecordKeyValue key val =>
let isEmpty = builtin eq val emptyValue in
match isEmpty with
| True => builtin remove recordsMap key
| False => builtin put recordsMap key val
end
end
in
foldl iter recordsMap recordsList
contract Resolver(
initialOwner: ByStr20,
registry: ByStr20,
node: ByStr32,
initialRecords: Map String String
)
field vendor: String = "UD"
field version: String = "0.1.1"
field owner: ByStr20 = initialOwner
field records: Map String String = initialRecords
(* Sets owner address *)
(* @ensures a sender address is an owner of the contract *)
(* @param address *)
(* @emits OwnerSet if the operation was successful *)
(* @emits Error if a sender address has no permission for the operation *)
transition setOwner(address: ByStr20)
currentOwner <- owner;
isOkSender = builtin eq currentOwner _sender;
match isOkSender with
| True =>
owner := address;
e = eOwnerSet address;
event e
| _ =>
e = let m = "Sender not owner" in eError m;
event e
end
end
(* Sets a key value pair *)
(* @ensures a sender address is an owner of the contract *)
(* @param key *)
(* @param value *)
(* @emits RecordSet if the operation was successful *)
(* @emits Error if a sender address has no permission for the operation *)
(* @sends onResolverConfigured to the registry *)
transition set(key: String, value: String)
currentOwner <- owner;
isOkSender = builtin eq currentOwner _sender;
match isOkSender with
| True =>
isEmpty = builtin eq value emptyValue;
match isEmpty with
| True => delete records[key]
| False => records[key] := value
end;
e = eRecordsSet registry node;
event e;
msgs = mOnResolverConfigured registry node;
send msgs
| _ =>
e = let m = "Sender not owner" in eError m;
event e
end
end
(* Remove a key from records map *)
(* @ensures a sender address is an owner of the contract *)
(* @param key *)
(* @emits RecordUnset if the operation was successful *)
(* @emits Error if a sender address has no permission for the operation *)
(* @sends onResolverConfigured to the registry *)
transition unset(key: String)
keyExists <- exists records[key];
currentOwner <- owner;
isOk =
let isOkSender = builtin eq currentOwner _sender in
andb isOkSender keyExists;
match isOk with
| True =>
delete records[key];
e = eRecordsSet registry node;
event e;
msgs = mOnResolverConfigured registry node;
send msgs
| _ =>
e = let m = "Sender not owner or key does not exist" in
eError m;
event e
end
end
(* Set multiple keys to records map *)
(* Removes records from the map if according passed value is empty *)
(* @ensures a sender address is an owner of the contract *)
(* @param newRecords *)
(* @emits RecordsSet if the operation was successful *)
(* @emits Error if a sender address has no permission for the operation *)
(* @sends onResolverConfigured to the registry *)
transition setMulti(newRecords: List RecordKeyValue)
currentOwner <- owner;
isOkSender = builtin eq currentOwner _sender;
match isOkSender with
| True =>
oldRecords <- records;
newRecordsMap = copyRecordsFromList oldRecords newRecords;
records := newRecordsMap;
e = eRecordsSet registry node;
event e;
msgs = mOnResolverConfigured registry node;
send msgs
| _ =>
e = let m = "Sender not owner" in eError m;
event e
end
end