Contract con_crafter_v6


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 preferred = attributes[attribute]["preferred"]
30 # generate a random int between min and max
31 # preferred is what is better for the attribute and has a lower chance of being generated
32 # so we weight the random number based on the preferred using linear interpolation
33 if preferred == "min":
34 weight = (max_value - min_value) ** 2
35 else:
36 weight = (min_value - max_value) ** 2
37
38 samples = 100
39 total = 0
40 for i in range(samples):
41 sample = random.randint(min_value, max_value)
42 if preferred == "min":
43 weight_value = (sample - min_value) / weight
44 else:
45 weight_value = (max_value - sample) / weight
46 total += sample * weight_value
47
48 final_metadata[attribute] = round(total / samples)
49
50
51 elif isinstance(attributes[attribute], list):
52 final_metadata[attribute] = random.choice(attributes[attribute])
53 image = random.choice(craftable_things[item_type]["images"])
54 final_metadata["type"] = item_type
55 amount_crafted[item_type] += 1
56 # Name will be the item type, but uppercase first letter + the amount crafted
57 name = item_type.capitalize() + " " + str(amount_crafted[item_type])
58 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)
59 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
60
61 @export
62 def set_nft_collection_contract(contract: str):
63 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
64 nft_collection_contract.set(contract)
65
66 @export
67 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
68 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
69 # ex item_type = "weapon"
70 # ex attributes = {
71 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
72 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
73 # "effect" : ["cosmicball", "natureball", "fireball"],
74 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
75 # }
76 # ex images = ["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file", "https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file", "https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]
77 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}

Byte Code

e3000000000000000000000000060000004000000073a000000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a6409640a84005a0b650c64008301650d640b9c01640c640d840483015a0e650c64008301650d640e9c01640f6410840483015a0f650c64008301650d65106511651264119c0464126413840483015a13641453002915da0e636f6e5f637261667465725f7636da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374630000000000000000000000000200000043000000732400000074006a0164018301010074026a0164028301010074036a0174046a05830101006400530029034eda13636f6e5f6c616d64656e5f7265616c6d5f7632da07636f6e5f7472742906da195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572a90072130000007213000000da00da045f5f5f5f0e000000730600000000010a010a0172150000002901da096974656d5f7479706563010000000000000010000000070000004300000073be01000074007c00190064016b037314740164028301820174007c001900640319007d0174026a0374046a05830083016a067c01640474076a0864058d03010074007c001900640619007d0269007d0378e87c0244005de07d0474097c027c041900740a8302900172127c027c041900640719007d057c027c041900640819007d067c027c041900640919007d077c0764076b0272a07c067c051800640a13007d086e0c7c057c061800640a13007d08640b7d0964017d0a784a740b7c09830144005d3e7d0b740c6a0d7c057c0683027d0c7c0764076b0272e47c0c7c0518007c081b007d0d6e0c7c067c0c18007c081b007d0d7c0a7c0c7c0d140037007d0a71be5700740e7c0a7c091b0083017c037c043c00715274097c027c041900740f83027252740c6a107c027c04190083017c037c043c0071525700740c6a1074007c001900640c190083017d0e7c007c03640d3c0074117c0005001900640e370003003c007c006a128300640f1700741374117c001900830117007d0f74026a0374146a05830083016a157c0f64106a1674076a0883017c0e7c03640e64118d05010074026a0374146a05830083016a17640e74076a087c0f64128d0301006400530029134e72060000007a1f54686973206974656d2074797065206973206e6f7420637261667461626c65da04636f7374da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74da0a61747472696275746573da036d696eda036d6178da09707265666572726564e902000000e964000000da06696d61676573da0474797065e901000000fa01207a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461721900000029037219000000721a00000072040000002918da125f5f637261667461626c655f7468696e6773da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c65720f000000da03676574da0d7472616e736665725f66726f6d72110000007212000000da0a6973696e7374616e6365da0464696374da0572616e6765da0672616e646f6dda0772616e64696e74da05726f756e64da046c697374da0663686f696365da105f5f616d6f756e745f63726166746564da0a6361706974616c697a65da03737472720d000000da086d696e745f6e6674da06666f726d6174da087472616e73666572291072160000007217000000721c000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565721f000000da06776569676874da0773616d706c6573da05746f74616cda0169da0673616d706c65da0c7765696768745f76616c7565da05696d6167657204000000721300000072130000007214000000da056372616674140000007346000000000206010e010c0110010c010c0104010a0110010c010c010c0108010e020c01040104010e010c0108010e020c01100112010e011601120108011001180110010a010c011001724800000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e74726163742907721100000072120000007210000000722d000000722a000000720d000000720e00000029017203000000721300000072130000007214000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e74726163743d00000073060000000002100106017249000000290472160000007217000000721c0000007222000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e67732903721c000000721700000072220000002906721100000072120000007210000000722d000000722a0000007229000000290472160000007217000000721c0000007222000000721300000072130000007214000000da176d6f646966795f637261667461626c655f7468696e67734400000073080000000003100106010401724a0000004e2914da09696d706f72746c6962722b0000007232000000da0473656564da085661726961626c65720d0000007210000000da044861736872290000007237000000720f0000007215000000da085f5f6578706f7274723900000072480000007249000000da05666c6f617472300000007235000000724a0000007213000000721300000072130000007214000000da083c6d6f64756c653e01000000732400000004010801040108010c010601080106010801040108030806060110280601100606010601