-
Notifications
You must be signed in to change notification settings - Fork 10
/
subscriptionsMarketplace.py
397 lines (316 loc) · 14.6 KB
/
subscriptionsMarketplace.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import smartpy as sp
class SubscriptionsMarketplace(sp.Contract):
"""This contract implements a marketplace for subscription tokens.
"""
COLLECTION_TYPE = sp.TRecord(
# The collection creator
creator=sp.TAddress,
# The collection metadata
metadata=sp.TMap(sp.TString, sp.TBytes),
# The metadata that will be used to mint the tokens
token_metadata=sp.TMap(sp.TString, sp.TBytes),
# The token mint price
mint_price=sp.TMutez,
# The maximum number of tokens in the collection
max_tokens=sp.TOption(sp.TNat)).layout(
("creator", ("metadata", ("token_metadata", ("mint_price", "max_tokens")))))
COLLECTION_FEE_TYPE = sp.TRecord(
# The subscription fee in mutez
fee=sp.TMutez,
# The fee payment interval in days
interval=sp.TNat,
# The fee recipient
recipient=sp.TAddress,
# The date when the fee doesn't need to be paid anymore
end_date=sp.TOption(sp.TTimestamp)).layout(
("fee", ("interval", ("recipient", "end_date"))))
def __init__(self, administrator, metadata, fee):
"""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 marketplace fee taken for each mint in per mille
fee=sp.TNat,
# The address that will receive the marketplace fees
fee_recipient=sp.TAddress,
# The token contract address
token_contract=sp.TOption(sp.TAddress),
# The subscription fees contract address
fees_contract=sp.TOption(sp.TAddress),
# The big map with the collections information
collections=sp.TBigMap(
sp.TNat, SubscriptionsMarketplace.COLLECTION_TYPE),
# The big map with the mint status of each collection
mint_open=sp.TBigMap(sp.TNat, sp.TBool),
# The number of tokens minted in each collection
minted_tokens=sp.TBigMap(sp.TNat, sp.TNat),
# The proposed new administrator address
proposed_administrator=sp.TOption(sp.TAddress),
# A counter that tracks the total number of collections
counter=sp.TNat))
# Initialize the contract storage
self.init(
administrator=administrator,
metadata=metadata,
fee=fee,
fee_recipient=administrator,
token_contract=sp.none,
fees_contract=sp.none,
collections=sp.big_map(),
mint_open=sp.big_map(),
minted_tokens=sp.big_map(),
proposed_administrator=sp.none,
counter=0)
def check_no_tez_transfer(self):
"""Checks that no tez were transferred in the operation.
"""
sp.verify(sp.amount == sp.mutez(0), message="SM_TEZ_TRANSFER")
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="SM_NOT_ADMIN")
def check_collection_exists(self, collection_id):
"""Checks that the given collection exists.
"""
sp.verify(self.data.collections.contains(collection_id),
message="SM_COLLECTION_UNDEFINED")
def check_is_collection_creator(self, creator):
"""Checks that the address that called the entry point is the
collection creator.
"""
sp.verify(sp.sender == creator, message="SM_NOT_COLLECTION_CREATOR")
@sp.entry_point
def create_collection(self, params):
"""Creates a new collection.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
metadata=sp.TMap(sp.TString, sp.TBytes),
token_metadata=sp.TMap(sp.TString, sp.TBytes),
mint_price=sp.TMutez,
max_tokens=sp.TOption(sp.TNat),
fee_information=SubscriptionsMarketplace.COLLECTION_FEE_TYPE).layout(
("metadata", ("token_metadata", ("mint_price", ("max_tokens", "fee_information"))))))
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Update the big maps
collection_id = sp.compute(self.data.counter)
self.data.collections[collection_id] = sp.record(
creator=sp.sender,
metadata=params.metadata,
token_metadata=params.token_metadata,
mint_price=params.mint_price,
max_tokens=params.max_tokens)
self.data.mint_open[collection_id] = False
self.data.minted_tokens[collection_id] = 0
# Send the subscription fee information to the fees contract
add_fee_handle = sp.contract(
t=sp.TRecord(
collection_id=sp.TNat,
fee_information=SubscriptionsMarketplace.COLLECTION_FEE_TYPE).layout(
("collection_id", "fee_information")),
address=self.data.fees_contract.open_some(),
entry_point="add_fee").open_some()
sp.transfer(
arg=sp.record(
collection_id=collection_id,
fee_information=params.fee_information),
amount=sp.mutez(0),
destination=add_fee_handle)
# Increase the collections counter
self.data.counter = collection_id + 1
@sp.entry_point
def set_max_tokens(self, params):
"""Sets the maximum number of tokens that can be minted in the
collection.
The maximum number of tokens can only be decreased, not increased. That
protects collectors of the tokens, making sure that the supply cannot
be arbitrarily increased by the collection creator.
Setting max tokens to 0 is like burning the collection.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
collection_id=sp.TNat,
new_max_tokens=sp.TNat).layout(
("collection_id", "new_max_tokens")))
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Check that the collection exists
collection_id = sp.compute(params.collection_id)
self.check_collection_exists(collection_id)
# Check that the collection creator executed the entry point
collection = sp.compute(self.data.collections[collection_id])
self.check_is_collection_creator(collection.creator)
# Check that there are less minted tokens than the new max_tokens value
new_max_tokens = sp.compute(params.new_max_tokens)
sp.verify(self.data.minted_tokens[collection_id] <= new_max_tokens,
message="SM_TOO_MANY_MINTED_TOKENS")
# Check that the new max_tokens value is smaller than the previous one
sp.verify(~collection.max_tokens.is_some() |
(collection.max_tokens.open_some() > new_max_tokens),
message="SM_WRONG_MAX_TOKENS")
# Check if the user wants to burn the collection
with sp.if_(new_max_tokens == 0):
# Burn the collection
del self.data.collections[collection_id]
del self.data.mint_open[collection_id]
del self.data.minted_tokens[collection_id]
with sp.else_():
# Set the new max_tokens value
self.data.collections[collection_id].max_tokens = sp.some(
new_max_tokens)
@sp.entry_point
def open_mint(self, params):
"""Sets the collection minting status to open or closed.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
collection_id=sp.TNat,
new_status=sp.TBool).layout(
("collection_id", "new_status")))
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Check that the collection exists
collection_id = sp.compute(params.collection_id)
self.check_collection_exists(collection_id)
# Check that the collection creator executed the entry point
self.check_is_collection_creator(
self.data.collections[collection_id].creator)
# Update the collection minting status
self.data.mint_open[collection_id] = params.new_status
@sp.entry_point
def mint_subscription(self, collection_id):
# Define the input parameter data type
sp.set_type(collection_id, sp.TNat)
# Check that the collection exists
self.check_collection_exists(collection_id)
# Check that the collection minting status is open
sp.verify(self.data.mint_open[collection_id],
message="SM_MINTING_IS_CLOSED")
# Check that it is still possible to mint tokens for this collection
collection = sp.compute(self.data.collections[collection_id])
minted_tokens = sp.compute(self.data.minted_tokens[collection_id])
sp.verify(~collection.max_tokens.is_some() |
(collection.max_tokens.open_some() > minted_tokens),
message="SM_ALL_TOKENS_MINTED")
# Check that the sent amount coincides with the mint price
sp.verify(sp.amount == collection.mint_price,
message="SM_WRONG_TEZ_AMOUNT")
# Calculate the marketplace fees
marketplace_fees = sp.compute(
sp.split_tokens(sp.amount, self.data.fee, 1000))
# Send the marketplace fee to the fee recipient
with sp.if_(marketplace_fees > sp.mutez(0)):
sp.send(self.data.fee_recipient, marketplace_fees)
# Send the remaining amount to the collection creator
remaining_amount = sp.compute(sp.amount - marketplace_fees)
with sp.if_(remaining_amount > sp.mutez(0)):
sp.send(collection.creator, remaining_amount)
# Send the mint information to the token contract
mint_handle = sp.contract(
t=sp.TRecord(
minter=sp.TAddress,
metadata=sp.TMap(sp.TString, sp.TBytes),
collection_id=sp.TNat).layout(
("minter", ("metadata", "collection_id"))),
address=self.data.token_contract.open_some(),
entry_point="mint").open_some()
sp.transfer(
arg=sp.record(
minter=sp.sender,
metadata=collection.token_metadata,
collection_id=collection_id),
amount=sp.mutez(0),
destination=mint_handle)
# Increase the minted tokens counter
self.data.minted_tokens[collection_id] = minted_tokens + 1
@sp.entry_point
def update_fee(self, new_fee):
"""Updates the marketplace management fees.
"""
# Define the input parameter data type
sp.set_type(new_fee, sp.TNat)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Check that the new fee is not larger than 25%
sp.verify(new_fee <= 250, message="MP_WRONG_FEES")
# Set the new management fee
self.data.fee = new_fee
@sp.entry_point
def update_fee_recipient(self, new_fee_recipient):
"""Updates the marketplace fee recipient.
"""
# Define the input parameter data type
sp.set_type(new_fee_recipient, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Set the new marketplace fee recipient
self.data.fee_recipient = new_fee_recipient
@sp.entry_point
def set_token_contract(self, token_contract):
"""Sets the token contract.
For security reasons, the token contract can only be set once.
"""
# Define the input parameter data type
sp.set_type(token_contract, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Check that the token contract has not been set before
sp.verify(~self.data.token_contract.is_some(),
message="SM_TOKEN_CONTRACT_ALREADY_SET")
# Set the token contract address
self.data.token_contract = sp.some(token_contract)
@sp.entry_point
def set_fees_contract(self, fees_contract):
"""Sets the contract that will administer the collection fees.
For security reasons, the fees contract can only be set once.
"""
# Define the input parameter data type
sp.set_type(fees_contract, sp.TAddress)
# Check that the administrator executed the entry point
self.check_is_administrator()
# Check that fees contract has not been set before
sp.verify(~self.data.fees_contract.is_some(),
message="SM_FEES_CONTRACT_ALREADY_SET")
# Set the fees contract address
self.data.fees_contract = sp.some(fees_contract)
# Set the fees contract in the token contract
set_fees_contract_handle = sp.contract(
t=sp.TAddress,
address=self.data.token_contract.open_some(),
entry_point="set_fees_contract").open_some()
sp.transfer(
arg=fees_contract,
amount=sp.mutez(0),
destination=set_fees_contract_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(
message="SM_NO_NEW_ADMIN"), message="SM_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.add_compilation_target("subscriptionMarketplace", SubscriptionsMarketplace(
administrator=sp.address("tz1M9CMEtsXm3QxA7FmMU2Qh7xzsuGXVbcDr"),
metadata=sp.utils.metadata_of_url("ipfs://bbb"),
fee=sp.nat(25)))