-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsubscriptionFee.py
445 lines (346 loc) · 16.5 KB
/
subscriptionFee.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import smartpy as sp
class SubscriptionFee(sp.Contract):
"""This contract calculates and distributes the subscription fees
associated to a specific token contract.
"""
COLLECTION_FEE_TYPE = sp.TRecord(
# The 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, token_contract):
"""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 token contract
token_contract=sp.TAddress,
# The big map with the token owners deposits to pay the fees
deposits=sp.TBigMap(sp.TAddress, sp.TMutez),
# The big map with the fee information for each collection
fees=sp.TBigMap(sp.TNat, SubscriptionFee.COLLECTION_FEE_TYPE),
# The big map with the tokens next fee payment information
next_payments=sp.TBigMap(sp.TNat, sp.TTimestamp),
# The big map with the tokens fee payment approvals
approved_tokens=sp.TBigMap(
sp.TPair(sp.TAddress, sp.TNat), sp.TUnit),
# The proposed new administrator address
proposed_administrator=sp.TOption(sp.TAddress)))
# Initialize the contract storage
self.init(
administrator=administrator,
metadata=metadata,
token_contract=token_contract,
deposits=sp.big_map(),
fees=sp.big_map(),
next_payments=sp.big_map(),
approved_tokens=sp.big_map(),
proposed_administrator=sp.none)
def check_no_tez_transfer(self):
"""Checks that no tez were transferred in the operation.
"""
sp.verify(sp.amount == sp.mutez(0), message="SFEE_TEZ_TRANSFER")
def get_deposit(self, user):
"""Gets the amount of mutez in the user deposit.
"""
return self.data.deposits.get(user, sp.mutez(0))
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="SFEE_NOT_ADMIN")
def check_is_token_contract(self):
"""Checks that the address that called the entry point is the token
contract.
"""
sp.verify(sp.sender == self.data.token_contract,
message="SFEE_NOT_TOKEN_CONTRACT")
def check_collection_exists(self, collection_id):
"""Checks that the given collection exists.
"""
sp.verify(self.data.fees.contains(collection_id),
message="SFEE_COLLECTION_UNDEFINED")
def check_token_exists(self, token_id):
"""Checks that the given token exists.
"""
sp.verify(self.data.next_payments.contains(token_id),
message="SFEE_TOKEN_UNDEFINED")
def check_token_is_approved(self, user, token_id):
"""Checks that the user approved to pay the token fees.
"""
sp.verify(self.data.approved_tokens.contains((user, token_id)),
message="SFEE_TOKEN_NOT_APPROVED")
def get_token_collection(self, token_id):
"""Gets the token collection id from the token contract on-chain view.
"""
return sp.view(
name="token_collection",
address=self.data.token_contract,
param=token_id,
t=sp.TNat).open_some()
def get_token_owner(self, token_id):
"""Gets the token owner from the token contract on-chain view.
"""
return sp.view(
name="token_owner",
address=self.data.token_contract,
param=token_id,
t=sp.TAddress).open_some()
@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.
This is mostly used to redirect staking rewards or donations to the
contract administrator deposit.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Move the transferred tez amount to the contract administrator deposit
self.data.deposits[self.data.administrator] = self.get_deposit(
self.data.administrator) + sp.amount
@sp.entry_point
def set_delegate(self, baker):
"""Delegates the tez stored in the contract to the given baker.
"""
# Define the input parameter data type
sp.set_type(baker, sp.TOption(sp.TKeyHash))
# Check that the administrator executed the entry point
self.check_is_administrator()
# Set the new delegate
sp.set_delegate(baker)
@sp.entry_point
def transfer_to_deposit(self, unit):
"""Transfers some mutez to the sender deposit.
"""
# Define the input parameter data type
sp.set_type(unit, sp.TUnit)
# Add the transferred amount to the sender deposit
self.data.deposits[sp.sender] = self.get_deposit(sp.sender) + sp.amount
@sp.entry_point
def withdraw_from_deposit(self, amount):
"""Withdraws a given amount of mutez from the sender deposit.
"""
# Define the input parameter data type
sp.set_type(amount, sp.TMutez)
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Check that the amount to withdraw is larger than zero
sp.verify(amount > sp.mutez(0), message="SFEE_WRONG_TEZ_AMOUNT")
# Check that the sender deposit has enough funds
deposit = sp.compute(self.get_deposit(sp.sender))
sp.verify(deposit >= amount, message="SFEE_INSUFFICIENT_TEZ_BALANCE")
# Remove the amount from the sender deposit
self.data.deposits[sp.sender] = deposit - amount
# Transfer the tez amount to the sender
sp.send(sp.sender, amount)
@sp.entry_point
def add_fee(self, params):
"""Adds the subscription fee information for a new collection.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
collection_id=sp.TNat,
fee_information=SubscriptionFee.COLLECTION_FEE_TYPE).layout(
("collection_id", "fee_information")))
# Check that the token contract executed the entry point
self.check_is_token_contract()
# Add the fee information
self.data.fees[params.collection_id] = params.fee_information
@sp.entry_point
def add_token(self, params):
"""Adds the relevant information for a new token.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
token_id=sp.TNat,
collection_id=sp.TNat).layout(
("token_id", "collection_id")))
# Check that the token contract executed the entry point
self.check_is_token_contract()
# Check that the collection exists
self.check_collection_exists(params.collection_id)
# Add the next payment information
self.data.next_payments[params.token_id] = sp.now.add_seconds(
sp.to_int(self.data.fees[params.collection_id].interval * 24 * 3600))
@sp.entry_point
def token_approval(self, params):
"""The user approves or disapproves to pay fees for the given token.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
token_id=sp.TNat,
approval=sp.TBool).layout(
("token_id", "approval")))
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Add or remove the token approval
with sp.if_(params.approval):
self.data.approved_tokens[(sp.sender, params.token_id)] = sp.unit
with sp.else_():
del self.data.approved_tokens[(sp.sender, params.token_id)]
@sp.entry_point
def pay_fees(self, params):
"""Pays the fees associated to a given token.
Any user can pay the fees of a given token. They don't need to be the
token owner.
This is necessary when the token is transferred to a contract that
might not have a way to pay the fees.
"""
# Define the input parameter data type
sp.set_type(params, sp.TRecord(
token_id=sp.TNat,
payments=sp.TNat).layout(
("token_id", "payments")))
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Check that the token exists
self.check_token_exists(params.token_id)
# Check that the fee recipient is not the current token owner, since
# the fee recipient never pays fees
fee_information = sp.compute(
self.data.fees[self.get_token_collection(params.token_id)])
token_owner = self.get_token_owner(params.token_id)
sp.verify(fee_information.recipient != token_owner,
message="SFEE_OWNER_IS_FEE_RECIPIENT")
# Check that the end date has not been reached
next_payment = sp.compute(self.data.next_payments[params.token_id])
sp.verify((~fee_information.end_date.is_some()) |
(next_payment < fee_information.end_date.open_some()),
message="SFEE_END_DATE_REACHED")
# Check that the sender approved to pay the token fees
self.check_token_is_approved(sp.sender, params.token_id)
# Calculate the amount of fees to pay
fee_amount = sp.compute(sp.mul(params.payments, fee_information.fee))
# Check if there is some fee to pay
with sp.if_(fee_amount > sp.mutez(0)):
# Check that the sender has enough tez in their deposit to pay
deposit = sp.compute(self.get_deposit(sp.sender))
sp.verify(deposit >= fee_amount,
message="SFEE_INSUFFICIENT_TEZ_BALANCE")
# Subtract the fee amount from the sender deposit
self.data.deposits[sp.sender] = deposit - fee_amount
# Send the fee amount to the fee recipient
sp.send(fee_information.recipient, fee_amount)
# Update the deadline for the next payment
self.data.next_payments[params.token_id] = next_payment.add_seconds(
sp.to_int(params.payments * fee_information.interval * 24 * 3600))
@sp.entry_point
def apply_fees(self, token_id):
"""Applies the fees associated to a given token.
Anyone can call this entrypoint.
If the owner does not have enough tez in their deposit to pay the fees,
the token is sent to the fee recipient.
"""
# Define the input parameter data type
sp.set_type(token_id, sp.TNat)
# Check that no tez have been transferred
self.check_no_tez_transfer()
# Check that the token exists
self.check_token_exists(token_id)
# Check that the fee recipient is not the current token owner, since
# the fee recipient never pays fees
fee_information = sp.compute(
self.data.fees[self.get_token_collection(token_id)])
token_owner = sp.compute(self.get_token_owner(token_id))
sp.verify(fee_information.recipient != token_owner,
message="SFEE_OWNER_IS_FEE_RECIPIENT")
# Check that the end date has not been reached
next_payment = sp.compute(self.data.next_payments[token_id])
sp.verify((~fee_information.end_date.is_some()) |
(next_payment < fee_information.end_date.open_some()),
message="SFEE_END_DATE_REACHED")
# Check if the deadline to pay the fees has expired
with sp.if_(sp.now > next_payment):
# Calculate the amount of fees to pay
payments = sp.compute(1 + (
sp.as_nat(sp.now - next_payment) // (fee_information.interval * 24 * 3600)))
fee_amount = sp.compute(sp.mul(payments, fee_information.fee))
# Check if there is some fee to pay
with sp.if_(fee_amount > sp.mutez(0)):
# Check if owner has enough tez in their deposit to pay the fee
# and has accepted to pay the fees for that token
deposit = sp.compute(self.get_deposit(token_owner))
approved = self.data.approved_tokens.contains(
(token_owner, token_id))
with sp.if_((deposit >= fee_amount) & approved):
# Subtract the fee amount from the owner deposit
self.data.deposits[token_owner] = deposit - fee_amount
# Send the fee amount to the fee recipient
sp.send(fee_information.recipient, fee_amount)
# Update the deadline for the next payment
self.data.next_payments[token_id] = next_payment.add_seconds(
sp.to_int(payments * fee_information.interval * 24 * 3600))
with sp.else_():
with sp.if_((deposit > sp.mutez(0)) & approved):
# Send whatever is available in the owner deposit to
# the fee recipient
sp.send(fee_information.recipient, deposit)
# Set the token owner deposit to zero
self.data.deposits[token_owner] = sp.mutez(0)
# Transfer the token to the fee recipient
transfer_handle = sp.contract(
t=sp.TList(sp.TRecord(
from_=sp.TAddress,
txs=sp.TList(sp.TRecord(
to_=sp.TAddress,
token_id=sp.TNat,
amount=sp.TNat).layout(("to_", ("token_id", "amount")))))),
address=self.data.token_contract,
entry_point="transfer").open_some()
sp.transfer(
arg=sp.list([sp.record(
from_=token_owner,
txs=sp.list([sp.record(
to_=fee_information.recipient,
token_id=token_id,
amount=1)]))]),
amount=sp.mutez(0),
destination=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(
message="SFEE_NO_NEW_ADMIN"), message="SFEE_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.add_compilation_target("subscriptionFee", SubscriptionFee(
administrator=sp.address("tz1M9CMEtsXm3QxA7FmMU2Qh7xzsuGXVbcDr"),
metadata=sp.utils.metadata_of_url("ipfs://bbb"),
token_contract=sp.address("KT1M9CMEtsXm3QxA7FmMU2Qh7xzsuGXVbcDr")))