Contract con_nft_newmeta_test_01


Contract Code


  
1 #NFTs are always part of a collection.
2
3 collection_name = Variable() # The name of the collection for display
4 collection_owner = Variable() # Only the owner can mint new NFTs for this collection
5 collection_nfts = Hash(default_value=0) # All NFTs of the collection
6 colleciton_nfts_metadata = Hash(default_value=0) # optional metadata hash so metadata can be stored separately from main NFT data
7 collection_balances = Hash(default_value=0) # All user balances of the NFTs
8 collection_balances_approvals = Hash(default_value=0) # Approval amounts of certain NFTs
9
10 @construct
11 def seed(name: str):
12 collection_name.set(name) # Sets the name
13 collection_owner.set(ctx.caller) # Sets the owner
14
15 @export
16 def change_owner(new_owner : str): #allows you to change the owner of the collection
17 assert collection_owner.get() == ctx.caller, "You are not the owner."
18 collection_owner.set(new_owner)
19
20 # function to mint a new NFT
21 @export
22 def mint_nft(name: str, description: str, ipfs_image_url: str, metadata: dict, amount: int):
23 assert name != "", "Name cannot be empty"
24 assert collection_nfts[name] == 0, "Name already exists"
25 assert amount > 0, "You cannot transfer negative amounts"
26 assert collection_owner.get() == ctx.caller, "Only the collection owner can mint NFTs"
27
28 collection_nfts[name] = {"description": description, "ipfs_image_url": ipfs_image_url, "metadata": metadata, "amount": amount} # Adds NFT to collection with all details
29
30 #Creates list of all items in metadata and stores each one individually in the associated hash
31 collection_nfts_metadata[name] = [list(metadata.keys())]
32 for key in metadata.keys():
33 collection_nfts_metadata[name,key] = metadata[key]
34
35 collection_balances[ctx.caller, name] = amount # Mints the NFT
36
37 # standard transfer function
38 @export
39 def transfer(name: str, amount:int, to: str):
40 assert amount > 0, "You cannot transfer negative amounts"
41 assert name != "", "Please specify the name of the NFT you want to transfer"
42 assert collection_balances[ctx.caller, name] >= amount, "You don't have enough NFTs to send"
43
44 collection_balances[ctx.caller, name] -= amount # Removes amount from sender
45 collection_balances[to, name] += amount # Adds amount to receiver
46
47 # allows other account to spend on your behalf
48 @export
49 def approve(amount: int, name: str, to: str):
50 assert amount > 0, "Cannot approve negative amounts"
51
52 collection_balances_approvals[ctx.caller, to, name] += amount # Approves certain amount for spending by another account
53
54 # transfers on your behalf
55 @export
56 def transfer_from(name:str, amount:int, to: str, main_account: str):
57 assert amount > 0, 'Cannot send negative balances!'
58
59 assert collection_balances_approvals[main_account, to, name] >= amount, "Not enough NFTs approved to send! You have {} and are trying to spend {}"\
60 .format(collection_balances_approvals[main_account, to, name], amount)
61 assert collection_balances[main_account, name] >= amount, "Not enough NFTs to send!"
62
63 collection_balances_approvals[main_account, to, name] -= amount # Removes Approval Amount
64 collection_balances[main_account, name] -= amount # Removes amount from sender
65
66 collection_balances[to, name] += amount # Adds amount to receiver

Byte Code

e3000000000000000000000000070000004000000073e600000065006400640164028d025a0165006400640364028d025a02650364046400640564068d035a04650364046400640764068d035a05650364046400640864068d035a06650364046400640964068d035a076508640a9c01640b640c84045a09650a640083016508640d9c01640e640f840483015a0b650a64008301650865086508650c650d64109c0564116412840483015a0e650a640083016508650d650864139c0364146415840483015a0f650a64008301650d6508650864169c0364176418840483015a10650a640083016508650d6508650864199c04641a641b840483015a11641c5300291dda17636f6e5f6e66745f6e65776d6574615f746573745f3031da0f636f6c6c656374696f6e5f6e616d652902da08636f6e7472616374da046e616d65da10636f6c6c656374696f6e5f6f776e6572e900000000da0f636f6c6c656374696f6e5f6e6674732903da0d64656661756c745f76616c756572030000007204000000da18636f6c6c656369746f6e5f6e6674735f6d65746164617461da13636f6c6c656374696f6e5f62616c616e636573da1d636f6c6c656374696f6e5f62616c616e6365735f617070726f76616c7329017204000000630100000000000000010000000200000043000000731a00000074006a017c008301010074026a0174036a04830101006400530029014e2905da115f5f636f6c6c656374696f6e5f6e616d65da03736574da125f5f636f6c6c656374696f6e5f6f776e6572da03637478da0663616c6c657229017204000000a9007211000000da00da045f5f5f5f0f000000730400000000010a0172130000002901da096e65775f6f776e6572630100000000000000010000000200000043000000732400000074006a01830074026a036b027316740464018301820174006a057c00830101006400530029024e7a16596f7520617265206e6f7420746865206f776e65722e2906720e000000da03676574720f0000007210000000da0e417373657274696f6e4572726f72720d00000029017214000000721100000072110000007212000000da0c6368616e67655f6f776e657214000000730400000000021601721700000029057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461da06616d6f756e7463050000000000000006000000050000004300000073a20000007c0064016b037310740064028301820174017c00190064036b02732474006404830182017c0464036b047334740064058301820174026a03830074046a056b02734a74006406830182017c017c027c037c0464079c0474017c003c0074067c036a0783008301670174087c003c0078207c036a07830044005d147d057c037c05190074087c007c0566023c00717857007c04740974046a057c0066023c006400530029084e72120000007a144e616d652063616e6e6f7420626520656d70747972060000007a134e616d6520616c7265616479206578697374737a24596f752063616e6e6f74207472616e73666572206e6567617469766520616d6f756e74737a274f6e6c792074686520636f6c6c656374696f6e206f776e65722063616e206d696e74204e465473290472180000007219000000721a000000721b000000290a7216000000da115f5f636f6c6c656374696f6e5f6e667473720e0000007215000000720f0000007210000000da046c697374da046b657973da18636f6c6c656374696f6e5f6e6674735f6d65746164617461da155f5f636f6c6c656374696f6e5f62616c616e6365732906720400000072180000007219000000721a000000721b000000da036b6579721100000072110000007212000000da086d696e745f6e66741a00000073160000000003100114011001060110010201100112010e011401722200000029037204000000721b000000da02746f63030000000000000003000000040000004300000073680000007c0164016b04731074006402830182017c0064036b0373207400640483018201740174026a037c00660219007c016b05733a7400640583018201740174026a037c006602050019007c01380003003c0074017c027c006602050019007c01370003003c006400530029064e72060000007a24596f752063616e6e6f74207472616e73666572206e6567617469766520616d6f756e747372120000007a37506c65617365207370656369667920746865206e616d65206f6620746865204e465420796f752077616e7420746f207472616e736665727a22596f7520646f6e2774206861766520656e6f756768204e46547320746f2073656e64290472160000007220000000720f000000721000000029037204000000721b0000007223000000721100000072110000007212000000da087472616e736665722a000000730c0000000002100110010c010e01160172240000002903721b00000072040000007223000000630300000000000000030000000400000043000000732c0000007c0064016b0473107400640283018201740174026a037c027c016603050019007c00370003003c006400530029034e72060000007a1f43616e6e6f7420617070726f7665206e6567617469766520616d6f756e747329047216000000da1f5f5f636f6c6c656374696f6e5f62616c616e6365735f617070726f76616c73720f00000072100000002903721b00000072040000007223000000721100000072110000007212000000da07617070726f766534000000730400000000021001722600000029047204000000721b0000007223000000da0c6d61696e5f6163636f756e7463040000000000000004000000060000004300000073960000007c0164016b047310740064028301820174017c037c027c00660319007c016b05733c740064036a0274017c037c027c00660319007c0183028301820174037c037c00660219007c016b057354740064048301820174017c037c027c006603050019007c01380003003c0074037c037c006602050019007c01380003003c0074037c027c006602050019007c01370003003c006400530029054e72060000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a484e6f7420656e6f756768204e46547320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d7a184e6f7420656e6f756768204e46547320746f2073656e6421290472160000007225000000da06666f726d6174722000000029047204000000721b00000072230000007227000000721100000072110000007212000000da0d7472616e736665725f66726f6d3a0000007312000000000210010c010c0114010a010e011601140172290000004e2912da085661726961626c65720c000000720e000000da0448617368721c000000da1a5f5f636f6c6c656369746f6e5f6e6674735f6d6574616461746172200000007225000000da037374727213000000da085f5f6578706f72747217000000da0464696374da03696e7472220000007224000000722600000072290000007211000000721100000072110000007212000000da083c6d6f64756c653e01000000732e000000040108010401080104010a0104010a0104010a0104010a030e050601100506010601120e06011409060114050601