Contract con_crafter_v11


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
13 @construct
14 def seed():
15 nft_collection_contract.set("con_lamden_realm_v2")
16 currency_contract.set("con_trt")
17 owner.set(ctx.caller)
18 extract_requests.set([])
19 operator.set("5fe2a80a2d0d4d7a08603451c02ac1ef5b1d0c6b3db118933e349d90a52deb44")
20
21
22 @export
23 def craft(item_type: str):
24 assert craftable_things[item_type] != 0, "This item type is not craftable"
25 cost = craftable_things[item_type]["cost"]
26 I.import_module(currency_contract.get()).transfer_from(amount=cost, to="Burned", main_account=ctx.caller)
27 attributes = craftable_things[item_type]["attributes"]
28 final_metadata = {}
29 for attribute in attributes:
30 if isinstance(attributes[attribute], dict):
31 # take into account the preferred attribute and weight it based on if the min or max is preferred without using uniform
32 min_value = attributes[attribute]["min"]
33 max_value = attributes[attribute]["max"]
34
35 res = random.randint(min_value * 10, max_value * 10) / 10
36 if(min_value < 1):
37 res = round(res,1)
38 else:
39 res = round(res)
40
41 final_metadata[attribute] = res
42 elif isinstance(attributes[attribute], list):
43 final_metadata[attribute] = random.choice(attributes[attribute])
44 image = random.choice(craftable_things[item_type]["images"])
45 final_metadata["type"] = item_type
46 amount_crafted[item_type] += 1
47 # Name will be the item type, but uppercase first letter + the amount crafted
48 name = item_type.capitalize() + " #" + str(amount_crafted[item_type])
49 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)
50 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
51
52
53 @export
54 def request_item_extract():
55 # dont allow duplicate requests
56 assert ctx.caller not in extract_requests.get(), "Request already made"
57 extract_requests_list = extract_requests.get()
58 extract_requests_list.append(ctx.caller)
59 extract_requests.set(extract_requests_list)
60
61
62 @export
63 def extract_item(address: str, item_list: list):
64 assert address in extract_requests.get(), "No extract request for this address"
65 assert ctx.caller == operator.get(), "Only the operator can extract"
66
67 for item in item_list:
68 amount_extracted[item["name"]] += 1
69 item_name = item["name"] + " #" + str(amount_extracted[item["name"]])
70 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)
71 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=address, name=item)
72
73 extract_requests_list = extract_requests.get()
74 extract_requests_list.remove(address)
75 extract_requests.set(extract_requests_list)
76
77
78 @export
79 def set_nft_collection_contract(contract: str):
80 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
81 nft_collection_contract.set(contract)
82
83
84 @export
85 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
86 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
87 # ex item_type = "weapon"
88 # ex attributes = {
89 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
90 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
91 # "effect" : ["cosmicball", "natureball", "fireball"],
92 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
93 # }
94 # ex images = ["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file", "https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file", "https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]
95 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}
96
97 @export
98 def override_amount_crafted(item_type: str, amount: int):
99 assert ctx.caller == owner.get(), "Only the owner can override the amount crafted"
100 amount_crafted[item_type] = amount

Byte Code

e30000000000000000000000000600000040000000730601000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a65046400640964028d025a0b65046400640a64028d025a0c650764046400640b64068d035a0d640c640d84005a0e650f640083016510640e9c01640f6410840483015a11650f6400830164116412840083015a12650f640083016510651364139c0264146415840483015a14650f64008301651064169c0164176418840483015a15650f64008301651065166517651364199c04641a641b840483015a18650f6400830165106519641c9c02641d641e840483015a1a641f53002920da0f636f6e5f637261667465725f763131da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374da10657874726163745f7265717565737473da086f70657261746f72da10616d6f756e745f657874726163746564630000000000000000000000000200000043000000733800000074006a0164018301010074026a0164028301010074036a0174046a058301010074066a0167008301010074076a016403830101006400530029044eda13636f6e5f6c616d64656e5f7265616c6d5f7632da07636f6e5f747274da40356665326138306132643064346437613038363033343531633032616331656635623164306336623364623131383933336533343964393061353264656234342908da195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572da125f5f657874726163745f7265717565737473da0a5f5f6f70657261746f72a90072190000007219000000da00da045f5f5f5f13000000730c00000000010a010a010c010a010401721b0000002901da096974656d5f747970656301000000000000000a0000000700000043000000736601000074007c00190064016b037314740164028301820174007c001900640319007d0174026a0374046a05830083016a067c01640474076a0864058d03010074007c001900640619007d0269007d0378907c0244005d887d0474097c027c041900740a830272ba7c027c041900640719007d057c027c041900640819007d06740b6a0c7c05640914007c0664091400830264091b007d077c05640a6b0072a8740d7c07640a83027d076e08740d7c0783017d077c077c037c043c00715274097c027c041900740e83027252740b6a0f7c027c04190083017c037c043c0071525700740b6a0f74007c001900640b190083017d087c007c03640c3c0074107c0005001900640a370003003c007c006a118300640d1700741274107c001900830117007d0974026a0374136a05830083016a147c09640e6a1574076a0883017c087c03640a640f8d05010074026a0374136a05830083016a16640a74076a087c0964108d0301006400530029114e72060000007a1f54686973206974656d2074797065206973206e6f7420637261667461626c65da04636f7374da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74da0a61747472696275746573da036d696eda036d6178e90a000000e901000000da06696d61676573da04747970657a0220237a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461721f0000002903721f000000722000000072040000002917da125f5f637261667461626c655f7468696e6773da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c657213000000da03676574da0d7472616e736665725f66726f6d72150000007216000000da0a6973696e7374616e6365da0464696374da0672616e646f6dda0772616e64696e74da05726f756e64da046c697374da0663686f696365da105f5f616d6f756e745f63726166746564da0a6361706974616c697a65da037374727211000000da086d696e745f6e6674da06666f726d6174da087472616e73666572290a721c000000721d0000007222000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565da03726573da05696d616765720400000072190000007219000000721a000000da0563726166741c0000007336000000000206010e010c0110010c010c0104010a010e010c010c01180108010c0208010a010e011601120108011001180110010a010c0110017245000000630000000000000000010000000200000043000000733800000074006a0174026a0383006b077316740464018301820174026a0383007d007c006a0574006a018301010074026a067c00830101006400530029024e7a145265717565737420616c7265616479206d61646529077215000000721600000072170000007230000000722d000000da06617070656e6472120000002901da15657874726163745f72657175657374735f6c69737472190000007219000000721a000000da14726571756573745f6974656d5f657874726163743c00000073080000000002160108010c0172480000002902da0761646472657373da096974656d5f6c69737463020000000000000005000000080000004300000073cc0000007c0074006a0183006b067314740264018301820174036a0474056a0183006b02732a740264028301820178807c0144005d787d0274067c0264031900050019006404370003003c007c026403190064051700740774067c02640319001900830117007d0374086a09740a6a01830083016a0b7c037c02640619007c02640719007c0264081900640464098d05010074086a09740a6a01830083016a0c64047c007c02640a8d0301007130570074006a0183007d047c046a0d7c008301010074006a0e7c048301010064005300290b4e7a234e6f2065787472616374207265717565737420666f72207468697320616464726573737a1d4f6e6c7920746865206f70657261746f722063616e2065787472616374720400000072260000007a02202372290000007244000000722b000000290572040000007229000000722a000000722b000000721f0000002903721f00000072200000007204000000290f72170000007230000000722d000000721500000072160000007218000000da125f5f616d6f756e745f657874726163746564723b000000722e000000722f0000007211000000723c000000723e000000da0672656d6f7665721200000029057249000000724a000000da046974656dda096974656d5f6e616d65724700000072190000007219000000721a000000da0c657874726163745f6974656d44000000731c00000000020e01060116010a0114011c010e010a01120110010e0108010a01724f00000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e747261637429077215000000721600000072140000007230000000722d000000721100000072120000002901720300000072190000007219000000721a000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e747261637456000000730600000000021001060172500000002904721c000000721d00000072220000007227000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e677329037222000000721d000000722700000029067215000000721600000072140000007230000000722d000000722c0000002904721c000000721d0000007222000000722700000072190000007219000000721a000000da176d6f646966795f637261667461626c655f7468696e67735d0000007308000000000310010601040172510000002902721c000000721f000000630200000000000000020000000300000043000000732200000074006a0174026a0383006b02731674046401830182017c0174057c003c006400530029024e7a2e4f6e6c7920746865206f776e65722063616e206f766572726964652074686520616d6f756e74206372616674656429067215000000721600000072140000007230000000722d00000072390000002902721c000000721f00000072190000007219000000721a000000da176f766572726964655f616d6f756e745f6372616674656466000000730600000000021001060172520000004e291bda09696d706f72746c6962722e0000007234000000da0473656564da085661726961626c6572110000007214000000da0448617368722c0000007239000000721300000072170000007218000000724b000000721b000000da085f5f6578706f7274723b000000724500000072480000007237000000724f0000007250000000da05666c6f617472330000007251000000da03696e747252000000721900000072190000007219000000721a000000da083c6d6f64756c653e01000000733800000004010801040108010c01060108010601080104010801040108010c010601080308090601101f100806011211060110060601060110070601