-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtypes.mligo
More file actions
325 lines (271 loc) · 8.9 KB
/
types.mligo
File metadata and controls
325 lines (271 loc) · 8.9 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
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
// SPDX-FileCopyrightText: 2021 Tezos Commons
// SPDX-License-Identifier: LicenseRef-MIT-TC
#if !TYPES_H
#define TYPES_H
# include "implementation_storage.mligo"
// ID of an FA2 token
type token_id = nat
// Frozen token history for an address.
// This tracks the stage number in which it was last updated and differentiates between
// tokens that were frozen during that stage and the ones frozen in any other before.
// It does so because only tokens that were frozen in the past can be staked, which is
// also why it tracks staked tokens in a single field.
type address_freeze_history =
{ current_stage_num : nat
; staked : nat
; current_unstaked : nat
; past_unstaked : nat
}
// Frozen token history for all addresses
type freeze_history = (address, address_freeze_history) big_map
// FA2 transfer types
type transfer_destination =
[@layout:comb]
{ to_ : address
; token_id : token_id
; amount : nat
}
type transfer_item =
[@layout:comb]
{ from_ : address
; txs : transfer_destination list
}
type transfer_params = transfer_item list
// -- DAO base types -- //
type nonce = nat
// Represents whether a voter has voted against (false) or for (true) a given proposal.
type vote_type = bool
type staked_vote = nat
// Amount of blocks.
type blocks = { blocks : nat }
// Length of a stage, in number of blocks
type period = blocks
// Representation of a quorum fraction. For efficiency, we only keep a `nat`
// for the numerator, whereas the denominator is not stored and has a fixed value
// of `quorum_denominator`.
type quorum_fraction = { numerator : int }
let quorum_denominator = 1000000n
// For safety, this is a version of quorum_fraction type
// that does not allow negative values.
type unsigned_quorum_fraction = { numerator : nat }
// Quorum threshold that a proposal needs to meet in order to be accepted,
// expressed as a fraction of the total_supply of frozen tokens, only
// storing the numerator while denominator is assumed to be
// `quorum_denominator`.
type quorum_threshold = unsigned_quorum_fraction
// Types to store info of a proposal
type proposal_key = bytes
type proposal_metadata = bytes
type proposal =
{ upvotes : nat
// ^ total amount of votes in favor
; downvotes : nat
// ^ total amount of votes against
; start_level : blocks
// ^ block level of submission, used to order proposals
; voting_stage_num : nat
// ^ stage number in which it is possible to vote on this proposal
; metadata : proposal_metadata
// ^ instantiation-specific additional data
; proposer : address
// ^ address of the proposer
; proposer_frozen_token : nat
// ^ amount of frozen tokens used by the proposer, exluding the fixed fee
; quorum_threshold: quorum_threshold
// ^ quorum threshold at the cycle in which proposal was raised
}
// TZIP-17 permit data
type permit =
{ key : key
; signature : signature
}
// TZIP-16 metadata map
type metadata_map = (string, bytes) big_map
// -- Storage -- //
// External FA2 token used for governance
type governance_token =
{ address : address
; token_id : token_id
}
// The way the staked token tracking work is as follows. The procedure to
// update quorum_threshold_at_cycle will reset the staked count to zero. And
// since this procedure is called from a `propose` call, which starts the
// staking of tokens for the cycle, and we increment `staked` count at each
// `propose` or `vote` call, the `staked` field will contain the tokens staked
// in that particular cycle. And the very first `propose` call in a cycle will
// see the staked tokens from the past cycle, and thus can use it to update the
// quorum threshold for the current cycle.
type quorum_threshold_at_cycle =
{ quorum_threshold : quorum_threshold
; last_updated_cycle : nat
; staked : nat
}
// A `delegate` has the permission to `vote` and `propose` on behalf of an address
type delegate =
[@layout:comb]
{ owner : address
; delegate : address
}
type delegates = (delegate, unit) big_map
// Use to query the previous and next of a proposal.
type plist_direction = bool
// Value of `plist_direction`.
let prev = false
let next = true
(*
* Proposal Doubly Linked List
*
* Behave like `OrderedSet`.
* When inserting a new key, it should be ensured that the key does not exist in the list or else
* it will corrupt the data structure.
*)
type proposal_doubly_linked_list =
[@layout:comb]
{ first: proposal_key // First proposal_key in the list
; last: proposal_key // Last proposal_key in the list. If only 1 key exist, last = first.
; map: ((proposal_key * plist_direction), proposal_key) big_map
}
type config =
{ max_quorum_threshold : quorum_fraction
// ^ Determine the maximum value of quorum threshold that is allowed.
; min_quorum_threshold : quorum_fraction
// ^ Determine the minimum value of quorum threshold that is allowed.
; period : period
// ^ Determines the stages length.
; fixed_proposal_fee_in_token : nat
// ^ A base fee paid for submitting a new proposal.
; max_quorum_change : quorum_fraction
// ^ A percentage value that limits the quorum_threshold change during
// every update of the same.
; quorum_change : quorum_fraction
// ^ A percentage value that is used in the computation of new quorum
// threshold value.
; governance_total_supply : nat
// ^ The total supply of governance tokens used in the computation of
// of new quorum threshold value at each stage.
; proposal_flush_level : blocks
// ^ The proposal age at (and above) which the proposal is considered flushable.
// Has to be bigger than `period * 2`
; proposal_expired_level : blocks
// ^ The proposal age at (and above) which the proposal is considered expired.
// Has to be bigger than `proposal_flush_time`
}
type storage =
{ governance_token : governance_token
; admin : address
; guardian : address // A special role that can drop any proposals at anytime
; pending_owner : address
; metadata : metadata_map
; extra : contract_extra
; proposals : (proposal_key, proposal) big_map
; ongoing_proposals_dlist: proposal_doubly_linked_list option
; staked_votes : (address * proposal_key, staked_vote) big_map
; permits_counter : nonce
; freeze_history : freeze_history
; frozen_token_id : token_id
; start_level : blocks
; quorum_threshold_at_cycle : quorum_threshold_at_cycle
; frozen_total_supply : nat
; delegates : delegates
; config : config
}
// -- Parameter -- //
type freeze_param = nat
type unfreeze_param = nat
type unstake_vote_param = proposal_key list
type transfer_ownership_param = address
type propose_params =
[@layout:comb]
{ from : address
; frozen_token : nat
; proposal_metadata : proposal_metadata
}
type vote_param =
{ from : address
; proposal_key : proposal_key
; vote_type : vote_type
; vote_amount : nat
}
type vote_param_permited =
{ argument : vote_param
; permit : permit option
}
type burn_param =
[@layout:comb]
{ from_ : address
; token_id : token_id
; amount : nat
}
type mint_param =
[@layout:comb]
{ to_ : address
; token_id : token_id
; amount : nat
}
type transfer_contract_tokens_param =
{ contract_address : address
; params : transfer_params
}
type update_delegate =
[@layout:comb]
{ enable : bool
; delegate : address
}
type update_delegate_params = update_delegate list
(*
* Entrypoints that forbids Tz transfers
*)
type forbid_xtz_params =
| Drop_proposal of proposal_key
| Vote of vote_param_permited list
| Flush of nat
| Freeze of freeze_param
| Unfreeze of unfreeze_param
| Update_delegate of update_delegate_params
| Unstake_vote of unstake_vote_param
(*
* Entrypoints that allow Tz transfers
*)
type allow_xtz_params_contract =
| Propose of propose_params
| Transfer_contract_tokens of transfer_contract_tokens_param
| Transfer_ownership of transfer_ownership_param
| Accept_ownership of unit
| Default of unit
type decision_callback_input =
{ proposal : proposal
; extras : contract_extra
}
type decision_callback_output =
{ operations : operation list
; extras : contract_extra
; guardian : address option
}
// -- Config -- //
type freeze_history_list = (address * nat) list
type initial_config_data =
{ max_quorum : quorum_threshold
; min_quorum : quorum_threshold
; quorum_threshold : quorum_threshold
; period : period
; proposal_flush_level: blocks
; proposal_expired_level: blocks
; fixed_proposal_fee_in_token: nat
; max_quorum_change : unsigned_quorum_fraction
; quorum_change : unsigned_quorum_fraction
; governance_total_supply : nat
}
type initial_data =
{ admin : address
; guardian : address
; governance_token : governance_token
; start_level : blocks
; metadata_map : metadata_map
; freeze_history : freeze_history_list
; config_data : initial_config_data
}
type decision_callback = decision_callback_input -> decision_callback_output
// -- Misc -- //
type return = operation list * storage
let nil_op = ([] : operation list)
#endif // TYPES_H included