-
Notifications
You must be signed in to change notification settings - Fork 10
/
daoTreasury.py
241 lines (195 loc) · 8.8 KB
/
daoTreasury.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
import smartpy as sp
class DAOTreasury(sp.Contract):
"""This contract implements a DAO treasury that can hold tez and tokens.
"""
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")))
FA2_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")))
FA12_TOKEN_TRANSFERS_TYPE = sp.TRecord(
# The token contract address
fa12=sp.TAddress,
# 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(
("fa12", "distribution"))
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")))
def __init__(self, metadata, administrator):
"""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 contract administrator
administrator=sp.TAddress,
# The proposed new administrator address
proposed_administrator=sp.TOption(sp.TAddress)))
# Initialize the contract storage
self.init(
metadata=metadata,
administrator=administrator,
proposed_administrator=sp.none)
# Fill the contract metadata
self.contract_metadata = {
"name": "Teia DAO treasury contract",
"description": "Treasury contract used for the Teia DAO",
"version": "1.0.0",
"authors": ["Teia Community <https://twitter.com/TeiaCommunity>"],
"homepage": "https://teia.art",
"source": {
"tools": ["SmartPy 0.16.0"],
"location": "https://github.com/teia-community/teia-smart-contracts/blob/main/python/contracts/daoTreasury.py"
},
"license": {
"name": "MIT",
"details": "The MIT License"
},
"interfaces": ["TZIP-016"],
"errors": [ {"error": {"string": "TREASURY_NOT_ADMIN"},
"expansion": {"string": "The account that executed the entry point is not the contract administrator"},
"languages": ["en"]},
{"error": {"string": "TREASURY_NO_NEW_ADMIN"},
"expansion": {"string": "The new administrator has not been proposed"},
"languages": ["en"]},
{"error": {"string": "TREASURY_NOT_PROPOSED_ADMIN"},
"expansion": {"string": "The operation can only be executed by the proposed administrator"},
"languages": ["en"]}]}
self.init_metadata("contract_metadata", self.contract_metadata)
def check_is_administrator(self):
"""Checks that the address that called the entry point is the contract
administrator.
"""
sp.verify(sp.sender == self.data.administrator,
message="TREASURY_NOT_ADMIN")
@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 transfer_mutez(self, mutez_transfers):
"""Transfers some tez to a list of addresses.
"""
# Define the input parameter data type
sp.set_type(mutez_transfers, DAOTreasury.MUTEZ_TRANSFERS_TYPE)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Transfer the mutez to the list of addresses
with sp.for_("mutez_transfer", mutez_transfers) as mutez_transfer:
sp.send(mutez_transfer.destination, mutez_transfer.amount)
@sp.entry_point
def transfer_fa2_token(self, token_transfers):
"""Transfers a FA2 token to a list of addresses.
"""
# Define the input parameter data type
sp.set_type(token_transfers, DAOTreasury.FA2_TOKEN_TRANSFERS_TYPE)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Build the FA2 transactions list
txs = sp.local("txs", sp.list(t=DAOTreasury.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 token transfer entry point
token_transfer_handle = sp.contract(
t=sp.TList(sp.TRecord(
from_=sp.TAddress,
txs=sp.TList(DAOTreasury.FA2_TX_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=token_transfer_handle)
@sp.entry_point
def transfer_fa12_token(self, token_transfers):
"""Transfers a FA12 token to a list of addresses.
"""
# Define the input parameter data type
sp.set_type(token_transfers, DAOTreasury.FA12_TOKEN_TRANSFERS_TYPE)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Get a handle to the token transfer entry point
token_transfer_handle = sp.contract(
t=sp.TRecord(
from_=sp.TAddress,
to_=sp.TAddress,
value=sp.TNat).layout(("from_ as from", ("to_ as to", "value"))),
address=token_transfers.fa12,
entry_point="transfer").open_some()
# Execute the transfers
with sp.for_("distribution", token_transfers.distribution) as distribution:
sp.transfer(
arg=sp.record(
from_=sp.self_address,
to_=distribution.destination,
value=distribution.amount),
amount=sp.mutez(0),
destination=token_transfer_handle)
@sp.entry_point
def transfer_administrator(self, proposed_administrator):
"""Proposes to transfer the contract administrator to another address.
"""
# Define the input parameter data type
sp.set_type(proposed_administrator, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Set the new proposed administrator address
self.data.proposed_administrator = sp.some(proposed_administrator)
@sp.entry_point
def accept_administrator(self):
"""The proposed administrator accepts the contract administrator
responsibilities.
"""
# Check that the proposed administrator executed the entry point
sp.verify(sp.sender == self.data.proposed_administrator.open_some(
"TREASURY_NO_NEW_ADMIN"), message="TREASURY_NOT_PROPOSED_ADMIN")
# Set the new administrator address
self.data.administrator = sp.sender
# Reset the proposed administrator value
self.data.proposed_administrator = sp.none
@sp.entry_point
def set_delegate(self, new_baker):
"""Delegates the contract stored tez to the given baker address.
"""
# Define the input parameter data type
sp.set_type(new_baker, sp.TOption(sp.TKeyHash))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Update the baker address
sp.set_delegate(new_baker)
sp.add_compilation_target("daoTreasury", DAOTreasury(
metadata=sp.utils.metadata_of_url("ipfs://QmewNgfyryssmkFHnr46vPDXeA8JiDMyfViDhNGSb8Voe4"),
administrator=sp.address("tz1RssrimWo3B8TpCajiNjqBD3MfhUwEgxod")))