forked from teia-community/teia-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaoTreasury.py
147 lines (114 loc) · 4.74 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
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")))
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")))
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, dao):
"""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 DAO contract address
dao=sp.TAddress))
# Initialize the contract storage
self.init(
metadata=metadata,
dao=dao)
@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 DAO contract executed the entry point
sp.verify(sp.sender == self.data.dao, message="TREASURY_NOT_DAO")
# Thansfer 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_token(self, token_transfers):
"""Transfers a token to a list of addresses.
"""
# Define the input parameter data type
sp.set_type(token_transfers, DAOTreasury.TOKEN_TRANSFERS_TYPE)
# Check that the DAO contract executed the entry point
sp.verify(sp.sender == self.data.dao, message="TREASURY_NOT_DAO")
# 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 tranfer
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 set_dao(self, new_dao):
"""Updates the DAO contract address.
"""
# Define the input parameter data type
sp.set_type(new_dao, sp.TAddress)
# Check that the DAO contract executed the entry point
sp.verify(sp.sender == self.data.dao, message="TREASURY_NOT_DAO")
# Update the DAO contract address
self.data.dao = new_dao
@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 DAO contract executed the entry point
sp.verify(sp.sender == self.data.dao, message="TREASURY_NOT_DAO")
# Update the baker address
sp.set_delegate(new_baker)
sp.add_compilation_target("daoTreasury", DAOTreasury(
metadata=sp.utils.metadata_of_url("ipfs://aaa"),
dao=sp.address("KT1QmSmQ8Mj8JHNKKQmepFqQZy7kDWQ1ekaa")))