Contract con_crafter_v2


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 item_type in craftable_things, "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 preferred = attributes[attribute]["preferred"]
30 if preferred == "min":
31 weight = (final_metadata[attribute] - min_value) / (max_value - min_value)
32 final_metadata[attribute] = random.randint(min_value, final_metadata[attribute])
33 else:
34 weight = (max_value - final_metadata[attribute]) / (max_value - min_value)
35 final_metadata[attribute] = random.randint(final_metadata[attribute], max_value)
36 elif isinstance(attributes[attribute], list):
37 final_metadata[attribute] = random.choice(attributes[attribute])
38 image = random.choice(craftable_things[item_type]["images"])
39 final_metadata["type"] = item_type
40 amount_crafted[item_type] += 1
41 # Name will be the item type, but uppercase first letter + the amount crafted
42 name = item_type.capitalize() + " " + str(amount_crafted[item_type])
43 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)
44 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
45
46 @export
47 def set_nft_collection_contract(contract: str):
48 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
49 nft_collection_contract.set(contract)
50
51 @export
52 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
53 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
54 # ex item_type = "weapon"
55 # ex attributes = {
56 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
57 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
58 # "effect" : ["cosmicball", "natureball", "fireball"],
59 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
60 # }
61 # ex images = ["", ""]
62 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}

Byte Code

e3000000000000000000000000060000004000000073a000000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a6409640a84005a0b650c64008301650d640b9c01640c640d840483015a0e650c64008301650d640e9c01640f6410840483015a0f650c64008301650d65106511651264119c0464126413840483015a13641453002915da0e636f6e5f637261667465725f7632da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374630000000000000000000000000200000043000000732400000074006a0164018301010074026a0164028301010074036a0174046a05830101006400530029034eda13636f6e5f6c616d64656e5f7265616c6d5f7632da07636f6e5f7472742906da195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572a90072130000007213000000da00da045f5f5f5f0e000000730600000000010a010a0172150000002901da096974656d5f747970656301000000000000000b0000000700000043000000738c0100007c0074006b067310740164018301820174007c001900640219007d0174026a0374046a05830083016a067c01640374076a0864048d03010074007c001900640519007d0269007d0378ba7c0244005db27d0474097c027c041900740a830272e07c027c041900640619007d057c027c041900640719007d067c027c041900640819007d077c0764066b0272b67c037c0419007c0518007c067c0518001b007d08740b6a0c7c057c037c04190083027c037c043c006e287c067c037c04190018007c067c0518001b007d08740b6a0c7c037c0419007c0683027c037c043c00714e74097c027c041900740d8302724e740b6a0e7c027c04190083017c037c043c00714e5700740b6a0e74007c0019006409190083017d097c007c03640a3c00740f7c0005001900640b370003003c007c006a108300640c17007411740f7c001900830117007d0a74026a0374126a05830083016a137c0a640d6a1474076a0883017c097c03640b640e8d05010074026a0374126a05830083016a15640b74076a087c0a640f8d0301006400530029104e7a1f54686973206974656d2074797065206973206e6f7420637261667461626c65da04636f7374da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74da0a61747472696275746573da036d696eda036d6178da09707265666572726564da06696d61676573da0474797065e901000000fa01207a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461721900000029037219000000721a00000072040000002916da125f5f637261667461626c655f7468696e6773da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c65720f000000da03676574da0d7472616e736665725f66726f6d72110000007212000000da0a6973696e7374616e6365da0464696374da0672616e646f6dda0772616e64696e74da046c697374da0663686f696365da105f5f616d6f756e745f63726166746564da0a6361706974616c697a65da03737472720d000000da086d696e745f6e6674da06666f726d6174da087472616e73666572290b72160000007217000000721c000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565721f000000da06776569676874da05696d6167657204000000721300000072130000007214000000da05637261667414000000733e000000000210010c0110010c010c0104010a010e010c010c010c0108010c010801060110020c010801060110010e011601120108011001180110010a010c011001723f00000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e74726163742907721100000072120000007210000000722b0000007228000000720d000000720e00000029017203000000721300000072130000007214000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e74726163743800000073060000000002100106017240000000290472160000007217000000721c0000007220000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e67732903721c000000721700000072200000002906721100000072120000007210000000722b00000072280000007227000000290472160000007217000000721c0000007220000000721300000072130000007214000000da176d6f646966795f637261667461626c655f7468696e67733f0000007308000000000310010601040172410000004e2914da09696d706f72746c69627229000000722f000000da0473656564da085661726961626c65720d0000007210000000da044861736872270000007233000000720f0000007215000000da085f5f6578706f72747235000000723f0000007240000000da05666c6f6174722e000000723100000072410000007213000000721300000072130000007214000000da083c6d6f64756c653e01000000732400000004010801040108010c010601080106010801040108030806060110230601100606010601