-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsi_list.py
143 lines (109 loc) · 4.57 KB
/
si_list.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
import smartpy as sp
class SpanishInquisitionList(sp.Contract):
"""This contract represents a list of users that are blocked or allowed to
transfer tokens.
"""
LIST_TYPE = sp.TVariant(
# The list contains users that are blocked to transfer tokens
BLOCK=sp.TUnit,
# The list contains users that are allowed to transfer tokens
ALLOW=sp.TUnit)
def __init__(self, administrator, metadata, type):
"""Initializes the contract.
"""
# Define the contract storage data types for clarity
self.init_type(sp.TRecord(
# The contract administrator
administrator=sp.TAddress,
# The contract metadata
metadata=sp.TBigMap(sp.TString, sp.TBytes),
# The list type
type=SpanishInquisitionList.LIST_TYPE,
# The big map with the list members
members=sp.TBigMap(sp.TAddress, sp.TUnit),
# The proposed new administrator address
proposed_administrator=sp.TOption(sp.TAddress)))
# Initialize the contract storage
self.init(
administrator=administrator,
metadata=metadata,
type=type,
members=sp.big_map(),
proposed_administrator=sp.none)
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="SI_LIST_NOT_ADMIN")
@sp.entry_point
def add_members(self, members):
"""Adds members to the list.
"""
# Define the input parameter data type
sp.set_type(members, sp.TList(sp.TAddress))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Add the members
with sp.for_("member", members) as member:
self.data.members[member] = sp.unit
@sp.entry_point
def remove_members(self, members):
"""Removes members from the list.
"""
# Define the input parameter data type
sp.set_type(members, sp.TList(sp.TAddress))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Remove the members
with sp.for_("member", members) as member:
sp.verify(self.data.members.contains(member),
message="SI_LIST_NOT_MEMBER")
del self.data.members[member]
@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(
message="SI_LIST_NO_NEW_ADMIN"),
message="SI_LIST_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_metadata(self, params):
"""Updates the contract metadata.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
k=sp.TString,
v=sp.TBytes).layout(("k", "v")))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Update the contract metadata
self.data.metadata[params.k] = params.v
@sp.onchain_view(pure=True)
def is_member(self, address):
"""Returns True if the given address is a list member.
"""
# Define the input parameter data type
sp.set_type(address, sp.TAddress)
# Return True if the address is in the list
sp.result(self.data.members.contains(address))
sp.add_compilation_target("si_list", SpanishInquisitionList(
administrator=sp.address("tz1M9CMEtsXm3QxA7FmMU2Qh7xzsuGXVbcDr"),
metadata=sp.utils.metadata_of_url("ipfs://aaa"),
type=sp.variant("BLOCK", sp.unit)))