Contract con_crafter_v12


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 extract_requests = Variable()
10 operator = Variable()
11 amount_extracted = Hash(default_value=0)
12 recipes = Hash(default_value=0)
13
14 @construct
15 def seed():
16 nft_collection_contract.set("con_lamden_realm_v3")
17 currency_contract.set("con_trt")
18 owner.set(ctx.caller)
19 extract_requests.set([])
20 operator.set("5fe2a80a2d0d4d7a08603451c02ac1ef5b1d0c6b3db118933e349d90a52deb44")
21 craftable_things["weapon"] = {"attributes":{"cooldown":{"max":2,"min":{"__fixed__":"0.1"},"preferred":"min"},"damage":{"max":80,"min":1,"preferred":"max"},"effect":["cosmicball","natureball","fireball"],"max_distance":{"max":1000,"min":100,"preferred":"max"}},"cost": 10000,"images":["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file","https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file","https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]}
22 craftable_things["armor"] = {"attributes":{"health":{"max":3000,"min":50,"preferred":"max"}},"cost": 10000,"images":["https://nftstorage.link/ipfs/bafybeifkc22kgbswsq2zugula2ohoqsnijwjas2irchs6ttg6kxdgeig5q/file","https://nftstorage.link/ipfs/bafybeihwrugkbk2fgz74hky2klzrgj2jqbzsarh3gxodl7pbju3ypv2pya/file","https://nftstorage.link/ipfs/bafybeia6mnjde3xsemzcm4dsjlkbgmvmvvuvmnizn3fgbak7qpc5zdqwsm/file","https://nftstorage.link/ipfs/bafybeibkfjdjasgl63he5lahlgtofbtscdp4vbyf46jx5klwu2o2bmufoi/file"]}
23 craftable_things["boots"] = {"attributes":{"speed":{"max":7,"min":4,"preferred":"min"}},"cost":10000,"images":["https://nftstorage.link/ipfs/bafybeihbgdrizdhv2virzvhfrgewrrpcuwhzeyhljoy3idtzl6hm7ztryy/file","https://nftstorage.link/ipfs/bafybeicmiozciz4zqryl5mfbvaiw4ifnlogzoncxlifv7sffbd3g4rddwa/file","https://nftstorage.link/ipfs/bafybeia6z36rlwrev2nphkczfzwp22cw3tyczk6af2fuodr445hgv7pcsu/file","https://nftstorage.link/ipfs/bafybeib7kq3a76vjhmfkl4covic7weonnjqe6pfm3w2ivfu42dq6n6u5ku/file","https://nftstorage.link/ipfs/bafybeiequ26aou6sqydg36auechzwcbzx4ozxp2exxdk2cycwu7uh37lq4/file"]}
24 amount_crafted["weapon"] = 30
25 amount_crafted["armor"] = 16
26 amount_crafted["boots"] = 17
27
28
29 @export
30 def craft(item_type: str):
31 assert craftable_things[item_type] != 0, "This item type is not craftable"
32 cost = craftable_things[item_type]["cost"]
33 I.import_module(currency_contract.get()).transfer_from(amount=cost, to="Burned", main_account=ctx.caller)
34 attributes = craftable_things[item_type]["attributes"]
35 final_metadata = {}
36 for attribute in attributes:
37 if isinstance(attributes[attribute], dict):
38 # take into account the preferred attribute and weight it based on if the min or max is preferred without using uniform
39 min_value = attributes[attribute]["min"]
40 max_value = attributes[attribute]["max"]
41
42 res = random.randint(min_value * 10, max_value * 10) / 10
43 if(min_value < 1):
44 res = round(res,1)
45 else:
46 res = round(res)
47
48 final_metadata[attribute] = res
49 elif isinstance(attributes[attribute], list):
50 final_metadata[attribute] = random.choice(attributes[attribute])
51 image = random.choice(craftable_things[item_type]["images"])
52 final_metadata["type"] = item_type
53 amount_crafted[item_type] += 1
54 # Name will be the item type, but uppercase first letter + the amount crafted
55 name = item_type.capitalize() + " #" + str(amount_crafted[item_type])
56 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)
57 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
58
59
60 @export
61 def request_item_extract():
62 # dont allow duplicate requests
63 assert ctx.caller not in extract_requests.get(), "Request already made"
64 extract_requests_list = extract_requests.get()
65 extract_requests_list.append(ctx.caller)
66 extract_requests.set(extract_requests_list)
67
68
69 @export
70 def extract_item(address: str, item_list: list):
71 assert address in extract_requests.get(), "No extract request for this address"
72 assert ctx.caller == operator.get(), "Only the operator can extract"
73
74 for item in item_list:
75 amount_extracted[item["name"]] += 1
76 item_name = item["name"] + " #" + str(amount_extracted[item["name"]])
77 I.import_module(nft_collection_contract.get()).mint_nft(name=item_name, description=item["description"], ipfs_image_url=item["image"], metadata=item["metadata"], amount=1)
78 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=address, name=item_name)
79
80 extract_requests_list = extract_requests.get()
81 extract_requests_list.remove(address)
82 extract_requests.set(extract_requests_list)
83
84
85 @export
86 def craft_from_items(recipe: str, item_list: list):
87 assert recipes[recipe] != 0, "This recipe does not exist"
88 assert len(item_list) == len(recipes[recipe]["items"]), "Incorrect number of items"
89 for item in item_list:
90 assert item in recipes[recipe]["items"], "Incorrect item"
91 I.import_module(nft_collection_contract.get()).transfer_from(name=item, amount=1, to="Burned", main_account=ctx.caller)
92 crafting_result = recipes[recipe]["result"]
93 amount_crafted[crafting_result["name"]] += 1
94 built_name = crafting_result["name"] + " #" + str(amount_crafted[crafting_result["name"]])
95 I.import_module(nft_collection_contract.get()).mint_nft(name=crafting_result["name"], description=crafting_result["description"], ipfs_image_url=crafting_result["image"], metadata=crafting_result["metadata"], amount=1)
96 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=crafting_result["name"])
97
98
99 @export
100 def set_nft_collection_contract(contract: str):
101 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
102 nft_collection_contract.set(contract)
103
104
105 @export
106 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
107 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
108 # ex item_type = "weapon"
109 # ex attributes = {
110 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
111 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
112 # "effect" : ["cosmicball", "natureball", "fireball"],
113 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
114 # }
115 # ex images = ["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file", "https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file", "https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]
116 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}
117
118
119 @export
120 def override_amount_crafted(item_type: str, amount: int):
121 assert ctx.caller == owner.get(), "Only the owner can override the amount crafted"
122 amount_crafted[item_type] = amount
123
124 @export
125 def set_operator(new_operator: str):
126 assert ctx.caller == owner.get(), "Only the owner can set the operator"
127 operator.set(new_operator)
128
129 @export
130 def set_currency_contract(new_contract: str):
131 assert ctx.caller == owner.get(), "Only the owner can set the currency contract"
132 currency_contract.set(new_contract)
133
134 @export
135 def set_owner(new_owner: str):
136 assert ctx.caller == owner.get(), "Only the owner can set the owner"
137 owner.set(new_owner)
138
139 @export
140 def set_crafting_recipe(recipe: str, items: list, result: dict):
141 assert ctx.caller == owner.get(), "Only the owner can set the crafting recipe"
142 recipes[recipe] = {"items": items, "result": result}
143
144

Byte Code

e30000000000000000000000000600000040000000738801000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a65046400640964028d025a0b65046400640a64028d025a0c650764046400640b64068d035a0d650764046400640c64068d035a0e640d640e84005a0f6510640083016511640f9c0164106411840483015a1265106400830164126413840083015a136510640083016511651464149c0264156416840483015a156510640083016511651464179c0264186419840483015a166510640083016511641a9c01641b641c840483015a176510640083016511651865196514641d9c04641e641f840483015a1a6510640083016511651b64209c0264216422840483015a1c651064008301651164239c0164246425840483015a1d651064008301651164269c0164276428840483015a1e651064008301651164299c01642a642b840483015a1f651064008301651165146519642c9c03642d642e840483015a20642f53002930da0f636f6e5f637261667465725f763132da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374da10657874726163745f7265717565737473da086f70657261746f72da10616d6f756e745f657874726163746564da077265636970657363000000000000000000000000070000004300000073dc00000074006a0164018301010074026a0164028301010074036a0174046a058301010074066a0167008301010074076a016403830101006404640564066901640764089c036409640a640b64089c03640c640d640e6703640f6410640b64089c0364119c046412641364146415670364169c03740864173c0064186419641a640b64089c0369016412641b641c641d641e670464169c037408641f3c00642064216422640764089c036901641264236424642564266427670564169c03740864283c006429740964173c00642a7409641f3c00642b740964283c0064005300292c4eda13636f6e5f6c616d64656e5f7265616c6d5f7633da07636f6e5f747274da4035666532613830613264306434643761303836303334353163303261633165663562316430633662336462313138393333653334396439306135326465623434e902000000da095f5f66697865645f5f7a03302e31da036d696e2903da036d61787214000000da09707265666572726564e950000000e9010000007215000000da0a636f736d696362616c6cda0a6e617475726562616c6cda086669726562616c6c69e8030000e9640000002904da08636f6f6c646f776eda0664616d616765da06656666656374da0c6d61785f64697374616e636569102700007a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f6261667962656968706d6f69727670676e74376d366c79746d3578617268786e636275676f7267343370766d766766356f377a33766567656c62692f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f62616679626569617075747070726936676d3637367937356b6a7467643277653473626d32646c32726d6373686166633573686d667067656b6d792f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f62616679626569657a3576337a7332666363747a797463686c776367727975326b333432737a327478797077756e7a6762376a647174796b7778342f66696c652903da0a61747472696275746573da04636f7374da06696d61676573da06776561706f6eda066865616c746869b80b0000e9320000007a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f62616679626569666b6332326b676273777371327a7567756c61326f686f71736e696a776a617332697263687336747467366b78646765696735712f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f6261667962656968777275676b626b3266677a3734686b79326b6c7a72676a326a71627a736172683367786f646c3770626a7533797076327079612f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f6261667962656961366d6e6a6465337873656d7a636d3464736a6c6b62676d766d767675766d6e697a6e33666762616b37717063357a647177736d2f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f62616679626569626b666a646a6173676c36336865356c61686c67746f66627473636470347662796634366a78356b6c7775326f32626d75666f692f66696c65da0561726d6f72da057370656564e907000000e9040000007a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f626166796265696862676472697a646876327669727a76686672676577727270637577687a6579686c6a6f79336964747a6c36686d377a747279792f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f62616679626569636d696f7a63697a347a7172796c356d6662766169773469666e6c6f677a6f6e63786c69667637736666626433673472646477612f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f6261667962656961367a3336726c77726576326e70686b637a667a777032326377337479637a6b3661663266756f647234343568677637706373752f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f6261667962656962376b7133613736766a686d666b6c34636f7669633777656f6e6e6a71653670666d3377326976667534326471366e3675356b752f66696c657a5d68747470733a2f2f6e667473746f726167652e6c696e6b2f697066732f626166796265696571753236616f75367371796467333661756563687a7763627a78346f7a787032657878646b32637963777537756833376c71342f66696c65da05626f6f7473e91e000000e910000000e911000000290ada195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572da125f5f657874726163745f7265717565737473da0a5f5f6f70657261746f72da125f5f637261667461626c655f7468696e6773da105f5f616d6f756e745f63726166746564a90072390000007239000000da00da045f5f5f5f14000000733c00000000010a010a010c010a010401060102010c010c010a010c01020202020e0204010c010202020202020e0204010c0102020202020202020e0208010801723b0000002901da096974656d5f747970656301000000000000000a0000000700000043000000736601000074007c00190064016b037314740164028301820174007c001900640319007d0174026a0374046a05830083016a067c01640474076a0864058d03010074007c001900640619007d0269007d0378907c0244005d887d0474097c027c041900740a830272ba7c027c041900640719007d057c027c041900640819007d06740b6a0c7c05640914007c0664091400830264091b007d077c05640a6b0072a8740d7c07640a83027d076e08740d7c0783017d077c077c037c043c00715274097c027c041900740e83027252740b6a0f7c027c04190083017c037c043c0071525700740b6a0f74007c001900640b190083017d087c007c03640c3c0074107c0005001900640a370003003c007c006a118300640d1700741274107c001900830117007d0974026a0374136a05830083016a147c09640e6a1574076a0883017c087c03640a640f8d05010074026a0374136a05830083016a16640a74076a087c0964108d0301006400530029114e72060000007a1f54686973206974656d2074797065206973206e6f7420637261667461626c657222000000da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74722100000072140000007215000000e90a00000072180000007223000000da04747970657a0220237a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461723e0000002903723e000000723f000000720400000029177237000000da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c657231000000da03676574da0d7472616e736665725f66726f6d72330000007234000000da0a6973696e7374616e6365da0464696374da0672616e646f6dda0772616e64696e74da05726f756e64da046c697374da0663686f6963657238000000da0a6361706974616c697a65da03737472722f000000da086d696e745f6e6674da06666f726d6174da087472616e73666572290a723c00000072220000007221000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565da03726573da05696d616765720400000072390000007239000000723a000000da056372616674410000007336000000000206010e010c0110010c010c0104010a010e010c010c01180108010c0208010a010e011601120108011001180110010a010c011001725d000000630000000000000000010000000200000043000000733800000074006a0174026a0383006b077316740464018301820174026a0383007d007c006a0574006a018301010074026a067c00830101006400530029024e7a145265717565737420616c7265616479206d616465290772330000007234000000723500000072490000007246000000da06617070656e6472300000002901da15657874726163745f72657175657374735f6c69737472390000007239000000723a000000da14726571756573745f6974656d5f657874726163746100000073080000000002160108010c0172600000002902da0761646472657373da096974656d5f6c69737463020000000000000005000000080000004300000073cc0000007c0074006a0183006b067314740264018301820174036a0474056a0183006b02732a740264028301820178807c0144005d787d0274067c0264031900050019006404370003003c007c026403190064051700740774067c02640319001900830117007d0374086a09740a6a01830083016a0b7c037c02640619007c02640719007c0264081900640464098d05010074086a09740a6a01830083016a0c64047c007c03640a8d0301007130570074006a0183007d047c046a0d7c008301010074006a0e7c048301010064005300290b4e7a234e6f2065787472616374207265717565737420666f72207468697320616464726573737a1d4f6e6c7920746865206f70657261746f722063616e2065787472616374720400000072180000007a0220237243000000725c000000724500000029057204000000724300000072440000007245000000723e0000002903723e000000723f0000007204000000290f723500000072490000007246000000723300000072340000007236000000da125f5f616d6f756e745f657874726163746564725300000072470000007248000000722f00000072540000007256000000da0672656d6f76657230000000290572610000007262000000da046974656dda096974656d5f6e616d65725f00000072390000007239000000723a000000da0c657874726163745f6974656d69000000731c00000000020e01060116010a0114011c010e010a01120110010e0108010a0172670000002902da067265636970657262000000630200000000000000050000000700000043000000730601000074007c00190064016b037314740164028301820174027c018301740274007c0019006403190083016b027334740164048301820178427c0144005d3a7d027c0274007c001900640319006b067356740164058301820174036a0474056a06830083016a077c026406640774086a0964088d040100713a570074007c001900640919007d03740a7c03640a1900050019006406370003003c007c03640a1900640b1700740b740a7c03640a19001900830117007d0474036a0474056a06830083016a0c7c03640a19007c03640c19007c03640d19007c03640e19006406640f8d05010074036a0474056a06830083016a0d640674086a097c03640a190064108d0301006400530029114e72060000007a1a546869732072656369706520646f6573206e6f74206578697374da056974656d737a19496e636f7272656374206e756d626572206f66206974656d737a0e496e636f7272656374206974656d7218000000723d00000029047204000000723e000000723f0000007240000000da06726573756c7472040000007a0220237243000000725c000000724500000029057204000000724300000072440000007245000000723e0000002903723e000000723f0000007204000000290eda095f5f726563697065737246000000da036c656e72470000007248000000722f0000007249000000724a0000007233000000723400000072380000007253000000725400000072560000002905726800000072620000007265000000da0f6372616674696e675f726573756c74da0a6275696c745f6e616d6572390000007239000000723a000000da1063726166745f66726f6d5f6974656d737b0000007322000000000214011a0106010a0118010e0114010c0114010e010e010e010c0108010c011001726f00000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e7472616374290772330000007234000000723200000072490000007246000000722f00000072300000002901720300000072390000007239000000723a000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e747261637490000000730600000000021001060172700000002904723c000000722200000072210000007223000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e6773290372210000007222000000722300000029067233000000723400000072320000007249000000724600000072370000002904723c00000072220000007221000000722300000072390000007239000000723a000000da176d6f646966795f637261667461626c655f7468696e6773970000007308000000000310010601040172710000002902723c000000723e000000630200000000000000020000000300000043000000732200000074006a0174026a0383006b02731674046401830182017c0174057c003c006400530029024e7a2e4f6e6c7920746865206f776e65722063616e206f766572726964652074686520616d6f756e74206372616674656429067233000000723400000072320000007249000000724600000072380000002902723c000000723e00000072390000007239000000723a000000da176f766572726964655f616d6f756e745f63726166746564a0000000730600000000021001060172720000002901da0c6e65775f6f70657261746f72630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a234f6e6c7920746865206f776e65722063616e2073657420746865206f70657261746f72290772330000007234000000723200000072490000007246000000723600000072300000002901727300000072390000007239000000723a000000da0c7365745f6f70657261746f72a700000073040000000002160172740000002901da0c6e65775f636f6e7472616374630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a2c4f6e6c7920746865206f776e65722063616e20736574207468652063757272656e637920636f6e7472616374290772330000007234000000723200000072490000007246000000723100000072300000002901727500000072390000007239000000723a000000da157365745f63757272656e63795f636f6e7472616374ad000000730600000000021001060172760000002901da096e65775f6f776e6572630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174026a057c00830101006400530029024e7a204f6e6c7920746865206f776e65722063616e2073657420746865206f776e657229067233000000723400000072320000007249000000724600000072300000002901727700000072390000007239000000723a000000da097365745f6f776e6572b40000007304000000000216017278000000290372680000007269000000726a000000630300000000000000030000000300000043000000732800000074006a0174026a0383006b02731674046401830182017c017c0264029c0274057c003c006400530029034e7a2a4f6e6c7920746865206f776e65722063616e2073657420746865206372616674696e672072656369706529027269000000726a000000290672330000007234000000723200000072490000007246000000726b000000290372680000007269000000726a00000072390000007239000000723a000000da137365745f6372616674696e675f726563697065ba000000730600000000021001060172790000004e2921da09696d706f72746c69627247000000724d000000da0473656564da085661726961626c65722f0000007232000000da0448617368723700000072380000007231000000723500000072360000007263000000726b000000723b000000da085f5f6578706f72747253000000725d000000726000000072500000007267000000726f0000007270000000da05666c6f6174724c0000007271000000da03696e7472720000007274000000727600000072780000007279000000723900000072390000007239000000723a000000da083c6d6f64756c653e01000000734e00000004010801040108010c01060108010601080104010801040108010c01060108010e03082d0601101f1008060112110601121406011006060106011007060112060601100506011006060110050601