forked from teia-community/teia-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepresentatives.py
378 lines (306 loc) · 15.4 KB
/
representatives.py
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
import smartpy as sp
class Representatives(sp.Contract):
"""This contract implements a basic multisig wallet / mini-DAO for the
Teia community representatives.
"""
MUTEZ_TRANSFERS_TYPE = sp.TList(sp.TRecord(
# The amount of mutez to transfer
amount=sp.TMutez,
# The transfer destination
destination=sp.TAddress).layout(
("amount", "destination")))
TOKEN_TRANSFERS_TYPE = sp.TRecord(
# The token contract address
fa2=sp.TAddress,
# The token id
token_id=sp.TNat,
# The token transfer distribution
distribution=sp.TList(sp.TRecord(
# The number of token editions to transfer
amount=sp.TNat,
# The transfer destination
destination=sp.TAddress).layout(("amount", "destination")))).layout(
("fa2", ("token_id", "distribution")))
REPRESENTATIVE_TYPE = sp.TRecord(
# The representative address
address=sp.TAddress,
# The represented community
community=sp.TString).layout(
("address", "community"))
LAMBDA_FUNCTION_TYPE = sp.TLambda(sp.TUnit, sp.TList(sp.TOperation))
PROPOSAL_KIND_TYPE = sp.TVariant(
# A proposal in the form of text to be voted for
text=sp.TBytes,
# A proposal to transfer mutez from the contract to other accounts
transfer_mutez=MUTEZ_TRANSFERS_TYPE,
# A proposal to transfer a token from the contract to other accounts
transfer_token=TOKEN_TRANSFERS_TYPE,
# A proposal to execute a lambda function
lambda_function=LAMBDA_FUNCTION_TYPE,
# A proposal to add a new representative
add_representative=REPRESENTATIVE_TYPE,
# A proposal to remove an existing representative
remove_representative=REPRESENTATIVE_TYPE,
# A proposal to change the minimum votes parameter
minimum_votes=sp.TNat,
# A proposal to change the expiration time parameter
expiration_time=sp.TNat)
PROPOSAL_TYPE = sp.TRecord(
# The kind of proposal: text, transfer_mutez, transfer_token, etc
kind=PROPOSAL_KIND_TYPE,
# The representative that submitted the proposal
issuer=REPRESENTATIVE_TYPE,
# The timestamp when the proposal was submitted
timestamp=sp.TTimestamp,
# Flag to indicate if the proposal has been already executed
executed=sp.TBool,
# The number of positive votes that the proposal has received
positive_votes=sp.TNat).layout(
("kind", ("issuer", ("timestamp", ("executed", "positive_votes")))))
FA2_TX_TYPE = sp.TRecord(
# The token destination
to_=sp.TAddress,
# The token id
token_id=sp.TNat,
# The number of token editions
amount=sp.TNat).layout(
("to_", ("token_id", "amount")))
FA2_TRANSFER_TYPE = sp.TList(sp.TRecord(
# The address that sends the token editions
from_=sp.TAddress,
# The list of token trasfers
txs=sp.TList(FA2_TX_TYPE)).layout(
("from_", "txs")))
def __init__(self, metadata, representatives, minimum_votes, expiration_time):
"""Initializes the contract.
"""
# Define the contract storage data types for clarity
self.init_type(sp.TRecord(
# The contract metadata
metadata=sp.TBigMap(sp.TString, sp.TBytes),
# The Teia community representatives
representatives=sp.TMap(sp.TAddress, sp.TString),
# The Teia communities represented in the contract
communities=sp.TSet(sp.TString),
# The minimum number of positive votes needed to execute a proposal
minimum_votes=sp.TNat,
# The proposals expiration time in days
expiration_time=sp.TNat,
# The big map with the proposals information
proposals=sp.TBigMap(sp.TNat, Representatives.PROPOSAL_TYPE),
# The big map with the votes information
votes=sp.TBigMap(sp.TPair(sp.TNat, sp.TString), sp.TBool),
# The proposals counter
counter=sp.TNat))
# Initialize the contract storage
self.init(
metadata=metadata,
representatives=representatives,
communities=sp.set(representatives.values()),
minimum_votes=minimum_votes,
expiration_time=expiration_time,
proposals=sp.big_map(),
votes=sp.big_map(),
counter=0)
@sp.entry_point
def default(self, unit):
"""Default entrypoint that allows receiving tez transfers in the same
way as one would do with a normal tz wallet.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Do nothing, just receive tez
pass
@sp.entry_point
def add_proposal(self, kind):
"""Adds a new proposal to the proposals big map.
"""
# Define the input parameter data type
sp.set_type(kind, Representatives.PROPOSAL_KIND_TYPE)
# Check that one of the representatives executed the entry point
community = self.data.representatives.get(
sp.sender, message="REPS_NOT_REPRESENTATIVE")
# Check that the proposals parameters make sense
with kind.match_cases() as arg:
with arg.match("add_representative") as representative:
# Check that the representative doesn't exist
sp.verify(~self.data.representatives.contains(representative.address),
message="REPS_ADDRESS_EXISTS")
sp.verify(~self.data.communities.contains(representative.community),
message="REPS_COMMUNITY_EXISTS")
with arg.match("remove_representative") as representative:
# Check that the representative exists
sp.verify(self.data.representatives.contains(representative.address),
message="REPS_WRONG_ADDRESS")
sp.verify(self.data.representatives[representative.address] == representative.community,
message="REPS_WRONG_COMMUNITY")
with arg.match("minimum_votes") as minimum_votes:
# Check that the minimum votes parameter is at least 1 vote
sp.verify(minimum_votes >= 1,
message="REPS_WRONG_MINIMUM_VOTES")
with arg.match("expiration_time") as expiration_time:
# Check that the expiration time parameter is at least 1 day
sp.verify(expiration_time >= 1,
message="REPS_WRONG_EXPIRATION_TIME")
# Add the new proposal information to the proposals big map
self.data.proposals[self.data.counter] = sp.record(
kind=kind,
issuer=sp.record(
address=sp.sender,
community=community),
timestamp=sp.now,
executed=False,
positive_votes=0)
# Increase the proposals counter
self.data.counter += 1
@sp.entry_point
def vote_proposal(self, params):
"""Adds one vote for a given proposal.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
proposal_id=sp.TNat,
approval=sp.TBool).layout(("proposal_id", "approval")))
# Check that one of the representatives executed the entry point
community = self.data.representatives.get(
sp.sender, message="REPS_NOT_REPRESENTATIVE")
# Check that the proposal exists
proposal = sp.compute(self.data.proposals.get(
params.proposal_id, message="REPS_INEXISTENT_PROPOSAL"))
# Check that the proposal has not been executed
sp.verify(~proposal.executed, message="REPS_EXECUTED_PROPOSAL")
# Check that the proposal has not expired
expiration_date = proposal.timestamp.add_days(
sp.to_int(self.data.expiration_time))
sp.verify(sp.now <= expiration_date, message="REPS_EXPIRED_PROPOSAL")
# Check if the representative voted positive before and remove their
# previous vote from the proposal positive votes counter
positive_votes = sp.local("positive_votes", proposal.positive_votes)
vote_key = sp.compute(sp.pair(params.proposal_id, community))
with sp.if_(self.data.votes.get(vote_key, default_value=False)):
positive_votes.value = sp.as_nat(positive_votes.value - 1)
# Add the vote to the proposal positive votes counter if it's positive
with sp.if_(params.approval):
positive_votes.value += 1
self.data.proposals[params.proposal_id].positive_votes = positive_votes.value
# Add or update the representatives vote
self.data.votes[vote_key] = params.approval
@sp.entry_point
def execute_proposal(self, proposal_id):
"""Executes a given proposal.
"""
# Define the input parameter data type
sp.set_type(proposal_id, sp.TNat)
# Check that one of the representatives executed the entry point
sp.verify(self.data.representatives.contains(sp.sender),
message="REPS_NOT_REPRESENTATIVE")
# Check that the proposal exists
proposal = sp.compute(self.data.proposals.get(
proposal_id, message="REPS_INEXISTENT_PROPOSAL"))
# Check that the proposal has not been executed
sp.verify(~proposal.executed, message="REPS_EXECUTED_PROPOSAL")
# Check that the proposal has not expired
expiration_date = proposal.timestamp.add_days(
sp.to_int(self.data.expiration_time))
sp.verify(sp.now <= expiration_date, message="REPS_EXPIRED_PROPOSAL")
# Check that the proposal received enough positive votes
sp.verify(proposal.positive_votes >= self.data.minimum_votes,
message="REPS_NOT_EXECUTABLE")
# Set the proposal status as executed
self.data.proposals[proposal_id].executed = True
# Execute the proposal
with proposal.kind.match_cases() as arg:
with arg.match("transfer_mutez") as mutez_transfers:
# Send the mutez to the list of destination addresses
with sp.for_("mutez_transfer", mutez_transfers) as mutez_transfer:
sp.send(mutez_transfer.destination, mutez_transfer.amount)
with arg.match("transfer_token") as token_transfers:
# Calculate the list of token transactions
txs = sp.local("txs", sp.list(t=Representatives.FA2_TX_TYPE))
with sp.for_("distribution", token_transfers.distribution) as distribution:
txs.value.push(sp.record(
to_=distribution.destination,
token_id=token_transfers.token_id,
amount=distribution.amount))
# Get a handle to the FA2 token transfer entry point
transfer_handle = sp.contract(
t=Representatives.FA2_TRANSFER_TYPE,
address=token_transfers.fa2,
entry_point="transfer").open_some()
# Execute the transfer
sp.transfer(
arg=sp.list([sp.record(
from_=sp.self_address,
txs=txs.value)]),
amount=sp.mutez(0),
destination=transfer_handle)
with arg.match("lambda_function") as lambda_function:
# Execute the lambda function
operations = lambda_function(sp.unit)
sp.add_operations(operations)
with arg.match("add_representative") as representative:
# Check that the representative doesn't exist
sp.verify(~self.data.representatives.contains(representative.address),
message="REPS_ADDRESS_EXISTS")
sp.verify(~self.data.communities.contains(representative.community),
message="REPS_COMMUNITY_EXISTS")
# Add the new representative
self.data.representatives[representative.address] = representative.community
self.data.communities.add(representative.community)
with arg.match("remove_representative") as representative:
# Check that the representative exists
sp.verify(self.data.representatives.contains(representative.address),
message="REPS_WRONG_ADDRESS")
sp.verify(self.data.representatives[representative.address] == representative.community,
message="REPS_WRONG_COMMUNITY")
# Check that it's not the last representative
sp.verify(sp.len(self.data.representatives) > 1,
message="REPS_LAST_REPRESENTATIVE")
# Remove the representative
del self.data.representatives[representative.address]
self.data.communities.remove(representative.community)
# Update the minimum votes parameter if necessary
n_representatives = sp.compute(sp.len(self.data.representatives))
with sp.if_(self.data.minimum_votes > n_representatives):
self.data.minimum_votes = n_representatives
with arg.match("minimum_votes") as minimum_votes:
# Check that the minimum votes are not larger than the number of
# representatives
sp.verify(minimum_votes <= sp.len(self.data.representatives),
message="REPS_WRONG_MINIMUM_VOTES")
# Update the minimum votes parameter
self.data.minimum_votes = minimum_votes
with arg.match("expiration_time") as expiration_time:
# Update the expiration time parameter
self.data.expiration_time = expiration_time
@sp.entry_point
def update_representative_address(self, new_address):
"""Updates the associated address for a community representative.
"""
# Define the input parameter data type
sp.set_type(new_address, sp.TAddress)
# Check that one of the representatives executed the entry point
community = self.data.representatives.get(
sp.sender, message="REPS_NOT_REPRESENTATIVE")
# Check that the new address is not yet a community representative
sp.verify(~self.data.representatives.contains(new_address),
message="REPS_ADDRESS_EXISTS")
# Replace the representative address
self.data.representatives[new_address] = community
del self.data.representatives[sp.sender]
@sp.onchain_view()
def get_representative_community(self, address):
"""Returns the representative community
"""
# Define the input parameter data type
sp.set_type(address, sp.TAddress)
# Check that the given address is from a community representative
community = self.data.representatives.get(
address, message="REPS_NOT_REPRESENTATIVE")
# Return the representative community
sp.result(community)
sp.add_compilation_target("representatives", Representatives(
metadata=sp.utils.metadata_of_url("ipfs://aaa"),
representatives={sp.address("tz1g6JRCpsEnD2BLiAzPNK3GBD1fKicV9rCx"): "community1"},
minimum_votes=sp.nat(1),
expiration_time=sp.nat(7)))