-
Notifications
You must be signed in to change notification settings - Fork 1
/
smartpy backend code.txt
122 lines (103 loc) · 4.39 KB
/
smartpy backend code.txt
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
import smartpy as sp
FA2 = sp.io.import_script_from_url("https://smartpy.io/dev/templates/fa2_lib.py")
class Token(FA2.Admin, FA2.MintNft, FA2.Fa2Nft):
def __init__(self, admin, **kwargs):
FA2.Fa2Nft.__init__(self, **kwargs)
FA2.Admin.__init__(self, admin)
class Marketplace(sp.Contract):
def __init__(self, token, metadata, admin):
self.init(
token=token,
metadata=metadata,
admin=admin,
data=sp.big_map(
tkey=sp.TNat,
tvalue=sp.TRecord(
holder=sp.TAddress,
author=sp.TAddress,
amount=sp.TMutez,
upvotes=sp.TNat,
token_id=sp.TNat,
collectable=sp.TBool)),
token_id=0,
upvotes=0,
)
@sp.entry_point
def mint(self, params):
mint_params = sp.TList(
sp.TRecord(
to_=sp.TAddress,
metadata=sp.TMap(sp.TString, sp.TBytes),
).layout(("to_", "metadata"))
)
c = sp.contract(mint_params, self.data.token, entry_point="mint").open_some()
args = [sp.record(
to_=sp.self_address,
metadata={ '': params.metadata })]
sp.transfer(args, sp.mutez(0), c)
self.data.data[self.data.token_id] = sp.record(
holder=sp.self_address,
author=sp.sender,
amount=params.amount,
upvotes=self.data.upvotes,
token_id=self.data.token_id,
collectable=True)
self.data.token_id += 1
@sp.entry_point
def upvoter(self,params):
self.data.data[params.token_id].upvotes += 1
def fa2_transfer(self, fa2, from_, to_, token_id, amount):
c = sp.contract(sp.TList(sp.TRecord(from_=sp.TAddress, txs=sp.TList(sp.TRecord(amount=sp.TNat, to_=sp.TAddress, token_id=sp.TNat).layout(("to_", ("token_id", "amount")))))), fa2, entry_point='transfer').open_some()
sp.transfer(sp.list([sp.record(from_=from_, txs=sp.list([sp.record(amount=amount, to_=to_, token_id=token_id)]))]), sp.mutez(0), c)
@sp.entry_point
def collect(self, params):
sp.verify(sp.amount == self.data.data[params.token_id].amount, "WrongAmount")
sp.verify(self.data.data[params.token_id].collectable, "NotCollectable")
sp.verify(self.data.data[params.token_id].author != sp.sender, "SenderNotAuthor")
self.data.data[params.token_id].collectable = False
self.data.data[params.token_id].holder = sp.sender
@sp.entry_point
def update_admin(self, params):
sp.verify(sp.sender == self.data.admin, "NotAdmin")
self.data.admin = params
@sp.entry_point
def collect_management_rewards(self, params):
sp.verify(sp.sender == self.data.admin, "NotAdmin")
sp.send(params.address, params.amount)
@sp.add_test(name = "Non Fungible Token")
def test():
scenario = sp.test_scenario()
admin = sp.test_account("admin")
mark = sp.test_account("user1")
elon = sp.test_account("user2")
token_contract = Token(
admin=admin.address,
metadata=sp.utils.metadata_of_url(
"ipfs://QmW8jPMdBmFvsSEoLWPPhaozN6jGQFxxkwuMLtVFqEy6Fb"))
scenario += token_contract
scenario.h1("MarketPlace")
marketplace = Marketplace(
token_contract.address,
sp.utils.metadata_of_url(
"ipfs://QmW8jPMdBmFvsSEoLWPPhaozN6jGQFxxkwuMLtVFqEy6Fb"
), admin.address)
scenario += marketplace
marketplace.mint(
amount=sp.tez(100),
metadata=sp.pack(
"ipfs://bafyreibwl5hhjgrat5l7cmjlv6ppwghm6ijygpz2xor2r6incfcxnl7y3e/metadata.json"
)).run(sender=admin, valid=False, exception="FA2_NOT_ADMIN")
token_contract.set_administrator(marketplace.address).run(sender=admin)
marketplace.mint(
amount=sp.tez(100),
metadata= sp.pack(
"ipfs://bafyreibwl5hhjgrat5l7cmjlv6ppwghm6ijygpz2xor2r6incfcxnl7y3e/metadata.json"
)).run(sender=admin)
marketplace.mint(amount=sp.mutez(5_600_000), metadata=sp.pack("123423")).run(sender=mark)
marketplace.upvoter(token_id=0).run(sender=mark)
scenario.h1("Collect")
marketplace.collect(token_id=1).run(sender=elon, amount=sp.mutez(5_600_000))
marketplace.collect_management_rewards(
amount=sp.mutez(1000),
address=admin.address
).run(sender = admin)