Contract con_crafter_v9


Contract Code


  
1 I = importlib
2 random.seed()
3
4 nft_collection_contract = Variable()
5 owner = Variable()
6 craftable_things = Hash(default_value=0)
7 amount_crafted = Hash(default_value=0)
8 currency_contract = Variable()
9
10 @construct
11 def seed():
12 nft_collection_contract.set("con_lamden_realm_v2")
13 currency_contract.set("con_trt")
14 owner.set(ctx.caller)
15
16 @export
17 def craft(item_type: str):
18 assert craftable_things[item_type] != 0, "This item type is not craftable"
19 cost = craftable_things[item_type]["cost"]
20 I.import_module(currency_contract.get()).transfer_from(amount=cost, to="Burned", main_account=ctx.caller)
21
22 attributes = craftable_things[item_type]["attributes"]
23 final_metadata = {}
24 for attribute in attributes:
25 if isinstance(attributes[attribute], dict):
26 # take into account the preferred attribute and weight it based on if the min or max is preferred without using uniform
27 min_value = attributes[attribute]["min"]
28 max_value = attributes[attribute]["max"]
29
30 res = random.randint(min_value * 10, max_value * 10) / 10
31 if(min_value < 1):
32 res = round(res,1)
33 else:
34 res = round(res)
35
36 final_metadata[attribute] = res
37
38 elif isinstance(attributes[attribute], list):
39 final_metadata[attribute] = random.choice(attributes[attribute])
40 image = random.choice(craftable_things[item_type]["images"])
41 final_metadata["type"] = item_type
42 amount_crafted[item_type] += 1
43 # Name will be the item type, but uppercase first letter + the amount crafted
44 name = item_type.capitalize() + " #" + str(amount_crafted[item_type])
45 I.import_module(nft_collection_contract.get()).mint_nft(name=name, description="Item crafted by {}".format(ctx.caller), ipfs_image_url=image, metadata=final_metadata, amount=1)
46 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
47
48 @export
49 def set_nft_collection_contract(contract: str):
50 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
51 nft_collection_contract.set(contract)
52
53 @export
54 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
55 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
56 # ex item_type = "weapon"
57 # ex attributes = {
58 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
59 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
60 # "effect" : ["cosmicball", "natureball", "fireball"],
61 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
62 # }
63 # ex images = ["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file", "https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file", "https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]
64 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}

Byte Code

e3000000000000000000000000060000004000000073a000000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a6409640a84005a0b650c64008301650d640b9c01640c640d840483015a0e650c64008301650d640e9c01640f6410840483015a0f650c64008301650d65106511651264119c0464126413840483015a13641453002915da0e636f6e5f637261667465725f7639da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374630000000000000000000000000200000043000000732400000074006a0164018301010074026a0164028301010074036a0174046a05830101006400530029034eda13636f6e5f6c616d64656e5f7265616c6d5f7632da07636f6e5f7472742906da195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572a90072130000007213000000da00da045f5f5f5f0e000000730600000000010a010a0172150000002901da096974656d5f747970656301000000000000000a0000000700000043000000736601000074007c00190064016b037314740164028301820174007c001900640319007d0174026a0374046a05830083016a067c01640474076a0864058d03010074007c001900640619007d0269007d0378907c0244005d887d0474097c027c041900740a830272ba7c027c041900640719007d057c027c041900640819007d06740b6a0c7c05640914007c0664091400830264091b007d077c05640a6b0072a8740d7c07640a83027d076e08740d7c0783017d077c077c037c043c00715274097c027c041900740e83027252740b6a0f7c027c04190083017c037c043c0071525700740b6a0f74007c001900640b190083017d087c007c03640c3c0074107c0005001900640a370003003c007c006a118300640d1700741274107c001900830117007d0974026a0374136a05830083016a147c09640e6a1574076a0883017c087c03640a640f8d05010074026a0374136a05830083016a16640a74076a087c0964108d0301006400530029114e72060000007a1f54686973206974656d2074797065206973206e6f7420637261667461626c65da04636f7374da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74da0a61747472696275746573da036d696eda036d6178e90a000000e901000000da06696d61676573da04747970657a0220237a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461721900000029037219000000721a00000072040000002917da125f5f637261667461626c655f7468696e6773da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c65720f000000da03676574da0d7472616e736665725f66726f6d72110000007212000000da0a6973696e7374616e6365da0464696374da0672616e646f6dda0772616e64696e74da05726f756e64da046c697374da0663686f696365da105f5f616d6f756e745f63726166746564da0a6361706974616c697a65da03737472720d000000da086d696e745f6e6674da06666f726d6174da087472616e73666572290a72160000007217000000721c000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565da03726573da05696d6167657204000000721300000072130000007214000000da056372616674140000007336000000000206010e010c0110010c010c0104010a010e010c010c01180108010c0208010a010e011601120108011001180110010a010c011001723f00000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e74726163742907721100000072120000007210000000722a0000007227000000720d000000720e00000029017203000000721300000072130000007214000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e74726163743400000073060000000002100106017240000000290472160000007217000000721c0000007221000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e67732903721c000000721700000072210000002906721100000072120000007210000000722a00000072270000007226000000290472160000007217000000721c0000007221000000721300000072130000007214000000da176d6f646966795f637261667461626c655f7468696e67733b0000007308000000000310010601040172410000004e2914da09696d706f72746c69627228000000722e000000da0473656564da085661726961626c65720d0000007210000000da044861736872260000007233000000720f0000007215000000da085f5f6578706f72747235000000723f0000007240000000da05666c6f6174722d000000723100000072410000007213000000721300000072130000007214000000da083c6d6f64756c653e01000000732400000004010801040108010c0106010801060108010401080308060601101f0601100606010601