tx · 3HUQWiH2Qw9cgm4iUUWmokNPHEGpyQMSzrpx84CzALBL

3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep:  -0.01000000 Waves

2022.12.06 18:08 [2348645] smart account 3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep > SELF 0.00000000 Waves

{ "type": 13, "id": "3HUQWiH2Qw9cgm4iUUWmokNPHEGpyQMSzrpx84CzALBL", "fee": 1000000, "feeAssetId": null, "timestamp": 1670339316526, "version": 2, "chainId": 84, "sender": "3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep", "senderPublicKey": "7v5L7QkXxfkirALdyqmox38QCsa9jtfAtgUfHTh34eWq", "proofs": [ "2DGZ7gBmZ9LzMbxZindnWkdmRkhSHSZ4cdyDJs3xW6hruHCLxFaFzVA4fDkbKV4vt9LbFwEzaMZT2yshAfnS5fDi" ], "script": "base64:BgINCAISBAoCAQESAwoBER0ABU1VTFQ2AMCEPQAFTVVMVDgAgMLXLwALdXNkbkFzc2V0SWQBIPd26vf3g8pl224T7gI7z1gplanB0Y3yS9ps2B1igMjeAAtiYWNrRW5kQWRkcgkBEUBleHRyTmF0aXZlKDEwNjIpAQIjM041U3BYMjFSM1I3NVFvNGViM013RkZ2VzdUVXp5aHZhdnYAD3N0YWtpbmdDb250cmFjdAkBEUBleHRyTmF0aXZlKDEwNjIpAQIjM05EQ3lCRzVxODVKdWFSaWlnVWVFdGFpbnlqQ1FUM1hwWm0AD0RFRkFVTFRMT0NBVElPTgIPQWZyaWNhX0ZfQWZyaWNhAAZOVU1SRVMABgATRkFDVE9SWU1BWFdBUkVIT1VTRQCAyK+gJQAQUkVTT1VSQ0VQUklDRU1JTgDV1gkACHJlc1R5cGVzCQDMCAICA09pbAkAzAgCAgNPcmUJAMwIAgIEV29vZAkAzAgCAgRTYW5kCQDMCAICBENsYXkJAMwIAgIHT3JnYW5pYwUDbmlsAApjb250aW5lbnRzCQDMCAICCEFtZXJpY2FzCQDMCAICBkV1cm9wZQkAzAgC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5TY3JpcHRUcmFuc2ZlcgMIBQFpBmNhbGxlcggFBm1lcmdlZAJfMwULdXNkbkFzc2V0SWQFBnJlc3VsdAANCdRt", "height": 2348645, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: Et3s8ZDJ4U9GWbFSwaeomM5N4PJeGvvhmB7wLvbJs2D Next: 4Hnti9WWrSuYW1HSCA4oEe56WNH1B6bU9ZB1tAY6CpM7 Diff:
OldNewDifferences
133133 }
134134
135135
136+
137+@Callable(i)
138+func sellResources (resTypes) = {
139+ let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
140+ if ((size(i.payments) != 0))
141+ then throw("sellResources doesn't require any payments")
142+ else {
143+ let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_")
144+ if ((curLocation[locIdxType] != "F"))
145+ then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
146+ else {
147+ let bpKey = keyBackpackByDuck(duckAssetId)
148+ let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
149+ let resList = split(currentPack[bpIdxRes], "_")
150+ func adder (acc,idx) = if (containsElement(resTypes, idx))
151+ then {
152+ let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], idx)
153+ let w0 = valueOrElse(getInteger(whKey), 0)
154+ let amount = parseIntValue(resList[idx])
155+ let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
156+ then 0
157+ else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
158+ then (FACTORYMAXWAREHOUSE - w0)
159+ else amount
160+ let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
161+ $Tuple3((acc._1 :+ IntegerEntry(whKey, (w0 + amount))), (acc._2 :+ "0"), (acc._3 + usdnReceived))
162+ }
163+ else $Tuple3(acc._1, (acc._2 :+ resList[idx]), acc._3)
164+
165+ let merged = {
166+ let $l = [0, 1, 2, 3, 4, 5]
167+ let $s = size($l)
168+ let $acc0 = $Tuple3(nil, nil, 0)
169+ func $f0_1 ($a,$i) = if (($i >= $s))
170+ then $a
171+ else adder($a, $l[$i])
172+
173+ func $f0_2 ($a,$i) = if (($i >= $s))
174+ then $a
175+ else throw("List size exceeds 6")
176+
177+ $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6)
178+ }
179+ let newPack = makeString([currentPack[bpIdxLevel], makeString(merged._2, "_"), currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
180+ let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
181+ $Tuple2((merged._1 :+ ScriptTransfer(i.caller, merged._3, usdnAssetId)), result)
182+ }
183+ }
184+ }
185+
186+
Full:
OldNewDifferences
11 {-# STDLIB_VERSION 6 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
44 let MULT6 = 1000000
55
66 let MULT8 = 100000000
77
88 let usdnAssetId = base58'HezsdQuRDtzksAYUy97gfhKy7Z1NW2uXYSHA3bgqenNZ'
99
1010 let backEndAddr = addressFromStringValue("3N5SpX21R3R75Qo4eb3MwFFvW7TUzyhvavv")
1111
1212 let stakingContract = addressFromStringValue("3NDCyBG5q85JuaRiigUeEtainyjCQT3XpZm")
1313
1414 let DEFAULTLOCATION = "Africa_F_Africa"
1515
1616 let NUMRES = 6
1717
1818 let FACTORYMAXWAREHOUSE = 10000000000
1919
2020 let RESOURCEPRICEMIN = 158549
2121
2222 let resTypes = ["Oil", "Ore", "Wood", "Sand", "Clay", "Organic"]
2323
2424 let continents = ["Americas", "Europe", "Asia", "Africa", "Oceania"]
2525
2626 func keyFactoryWarehouseByIdAndType (factoryId,resType) = ((("factoryWhByContinentAndRes_" + factoryId) + "_") + toString(resType))
2727
2828
2929 func keyAssetIdToOwner (assetId) = ("nftOwner_" + assetId)
3030
3131
3232 func keyStakedTimeByAssetId (assetId) = ("stakedTime_" + assetId)
3333
3434
3535 func keyStakedDuckByOwner (ownerAddr) = ("stakedDuckByOwner_" + ownerAddr)
3636
3737
3838 func keyBackpackByDuck (duckAssetId) = ("backPack_" + duckAssetId)
3939
4040
4141 func keyDuckLocation (duckAssetId) = ("duckLocation_" + duckAssetId)
4242
4343
4444 let idxType = 0
4545
4646 let idxQuantity = 1
4747
4848 let idxPrice = 2
4949
5050 let locIdxContinent = 0
5151
5252 let locIdxType = 1
5353
5454 let locIdxId = 2
5555
5656 let bpIdxLevel = 0
5757
5858 let bpIdxRes = 1
5959
6060 let bpIdxMat = 2
6161
6262 let bpIdxProd = 3
6363
6464 func asString (v) = match v {
6565 case s: String =>
6666 s
6767 case _ =>
6868 throw("fail to cast into String")
6969 }
7070
7171
7272 func subRes (resList,resType,amount) = {
7373 func subber (acc,i) = (acc :+ (if ((i == resType))
7474 then toString((parseIntValue(resList[i]) - amount))
7575 else resList[i]))
7676
7777 let r = {
7878 let $l = [0, 1, 2, 3, 4, 5]
7979 let $s = size($l)
8080 let $acc0 = nil
8181 func $f0_1 ($a,$i) = if (($i >= $s))
8282 then $a
8383 else subber($a, $l[$i])
8484
8585 func $f0_2 ($a,$i) = if (($i >= $s))
8686 then $a
8787 else throw("List size exceeds 6")
8888
8989 $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6)
9090 }
9191 makeString(r, "_")
9292 }
9393
9494
9595 @Callable(i)
9696 func sellResource (resType,amount) = if (if ((0 > resType))
9797 then true
9898 else (resType >= NUMRES))
9999 then throw(("Unknown resource: " + toString(resType)))
100100 else if ((0 >= amount))
101101 then throw(("Amount should be positive! " + toString(amount)))
102102 else {
103103 let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
104104 if ((size(i.payments) != 0))
105105 then throw("sellResources doesn't require any payments")
106106 else {
107107 let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_")
108108 if ((curLocation[locIdxType] != "F"))
109109 then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
110110 else {
111111 let bpKey = keyBackpackByDuck(duckAssetId)
112112 let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
113113 let resList = split(currentPack[bpIdxRes], "_")
114114 let currentRes = parseIntValue(resList[resType])
115115 if ((amount > currentRes))
116116 then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount)))
117117 else {
118118 let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], resType)
119119 let w0 = valueOrElse(getInteger(whKey), 0)
120120 let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
121121 then 0
122122 else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
123123 then (FACTORYMAXWAREHOUSE - w0)
124124 else amount
125125 let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
126126 let bpRes = subRes(resList, resType, amount)
127127 let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
128128 let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
129129 $Tuple2([IntegerEntry(whKey, (w0 + amount)), ScriptTransfer(i.caller, usdnReceived, usdnAssetId)], result)
130130 }
131131 }
132132 }
133133 }
134134
135135
136+
137+@Callable(i)
138+func sellResources (resTypes) = {
139+ let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
140+ if ((size(i.payments) != 0))
141+ then throw("sellResources doesn't require any payments")
142+ else {
143+ let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_")
144+ if ((curLocation[locIdxType] != "F"))
145+ then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
146+ else {
147+ let bpKey = keyBackpackByDuck(duckAssetId)
148+ let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
149+ let resList = split(currentPack[bpIdxRes], "_")
150+ func adder (acc,idx) = if (containsElement(resTypes, idx))
151+ then {
152+ let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], idx)
153+ let w0 = valueOrElse(getInteger(whKey), 0)
154+ let amount = parseIntValue(resList[idx])
155+ let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
156+ then 0
157+ else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
158+ then (FACTORYMAXWAREHOUSE - w0)
159+ else amount
160+ let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
161+ $Tuple3((acc._1 :+ IntegerEntry(whKey, (w0 + amount))), (acc._2 :+ "0"), (acc._3 + usdnReceived))
162+ }
163+ else $Tuple3(acc._1, (acc._2 :+ resList[idx]), acc._3)
164+
165+ let merged = {
166+ let $l = [0, 1, 2, 3, 4, 5]
167+ let $s = size($l)
168+ let $acc0 = $Tuple3(nil, nil, 0)
169+ func $f0_1 ($a,$i) = if (($i >= $s))
170+ then $a
171+ else adder($a, $l[$i])
172+
173+ func $f0_2 ($a,$i) = if (($i >= $s))
174+ then $a
175+ else throw("List size exceeds 6")
176+
177+ $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6)
178+ }
179+ let newPack = makeString([currentPack[bpIdxLevel], makeString(merged._2, "_"), currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
180+ let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
181+ $Tuple2((merged._1 :+ ScriptTransfer(i.caller, merged._3, usdnAssetId)), result)
182+ }
183+ }
184+ }
185+
186+

github/deemru/w8io/169f3d6 
44.60 ms