tx · 4Hnti9WWrSuYW1HSCA4oEe56WNH1B6bU9ZB1tAY6CpM7

3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep:  -0.01000000 Waves

2022.12.07 22:26 [2350342] smart account 3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep > SELF 0.00000000 Waves

{ "type": 13, "id": "4Hnti9WWrSuYW1HSCA4oEe56WNH1B6bU9ZB1tAY6CpM7", "fee": 1000000, "feeAssetId": null, "timestamp": 1670441164897, "version": 2, "chainId": 84, "sender": "3N8y4wxX3JC4TdrCJBXX16SjWf6X256hrep", "senderPublicKey": "7v5L7QkXxfkirALdyqmox38QCsa9jtfAtgUfHTh34eWq", "proofs": [ "4L9h3d2Z2yRShvtyBjW6CSvS3TDMLoya6gNCUCeJHudmxhKgWdBe2KQLtfKPpVen1K8Q6sc4u7AWQMTjzv52gs5v" ], "script": "base64:BgIPCAISBQoDAQEBEgQKAhERHgAFTVVMVDYAwIQ9AAVNVUxUOACAwtcvAAt1c2RuQXNzZXRJZAEg93bq9/eDymXbbhPuAjvPWCmVqcHRjfJL2mzYHWKAyN4AC2JhY2tFbmRBZGRyCQERQGV4dHJOYXRpdmUoMTA2MikBAiMzTjVTcFgyMVIzUjc1UW80ZWIzTXdGRnZXN1RVenlodmF2dgAPc3Rha2luZ0NvbnRyYWN0CQERQGV4dHJOYXRpdmUoMTA2MikBAiMzTkRDeUJHNXE4NUp1YVJpaWdVZUV0YWlueWpDUVQzWHBabQAPREVGQVVMVExPQ0FUSU9OAg9BZnJpY2FfRl9BZnJpY2EABk5VTVJFUwAGABNGQUNUT1JZTUFYV0FSRUhPVVNFAIDIr6AlABBSRVNPVVJDRVBSSUNFTUlOANXWCQAIcmVzVHlwZXMJAMwIAgIDT2lsCQDMCAICA09yZQkAzAgCAgRXb29kCQDMCAICBFNhbmQJAMwIAgIEQ2xheQkAzAgCAgdPcmdhbmljBQNuaWwACmNvbnRpbmVudHMJAMwIAgIIQW1lcmljYXMJAMwIAgIGRXVyb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mdlZAJfMQkBDlNjcmlwdFRyYW5zZmVyAwgFAWkGY2FsbGVyCAUGbWVyZ2VkAl8zBQt1c2RuQXNzZXRJZAUGcmVzdWx0ABUayFQ=", "height": 2350342, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: 3HUQWiH2Qw9cgm4iUUWmokNPHEGpyQMSzrpx84CzALBL Next: J1yu5iTdahLA9Uo5ZUwXDVsxwhffx9hVUUZ7M8LdLYZp Diff:
OldNewDifferences
9292 }
9393
9494
95+func buyInternal (locId,resType,amount,minPrice) = {
96+ let whKey = keyFactoryWarehouseByIdAndType(locId, resType)
97+ let w0 = valueOrElse(getInteger(whKey), 0)
98+ let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
99+ then 0
100+ else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
101+ then (FACTORYMAXWAREHOUSE - w0)
102+ else amount
103+ let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
104+ let min99 = (minPrice - (minPrice / 100))
105+ if (((min99 * amount) > (usdnReceived * MULT8)))
106+ then throw((((((((((("Actual price = " + toString(usdnReceived)) + " / ") + toString(amount)) + " < minPrice = ") + toString(minPrice)) + ", (") + locId) + ", ") + resTypes[resType]) + ")"))
107+ else $Tuple2(IntegerEntry(whKey, (w0 + amount)), usdnReceived)
108+ }
109+
110+
95111 @Callable(i)
96-func sellResource (resType,amount) = if (if ((0 > resType))
112+func sellResource (resType,amount,minPrice) = if (if ((0 > resType))
97113 then true
98114 else (resType >= NUMRES))
99115 then throw(("Unknown resource: " + toString(resType)))
108124 if ((curLocation[locIdxType] != "F"))
109125 then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
110126 else {
111- let bpKey = keyBackpackByDuck(duckAssetId)
112- let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
127+ let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":")
113128 let resList = split(currentPack[bpIdxRes], "_")
114129 let currentRes = parseIntValue(resList[resType])
115130 if ((amount > currentRes))
116131 then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount)))
117132 else {
118- let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], resType)
119- let w0 = valueOrElse(getInteger(whKey), 0)
120- let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
121- then 0
122- else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
123- then (FACTORYMAXWAREHOUSE - w0)
124- else amount
125- let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
133+ let b = buyInternal(curLocation[locIdxId], resType, amount, minPrice)
126134 let bpRes = subRes(resList, resType, amount)
127135 let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
128136 let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
129- $Tuple2([IntegerEntry(whKey, (w0 + amount)), ScriptTransfer(i.caller, usdnReceived, usdnAssetId)], result)
137+ $Tuple2([b._1, ScriptTransfer(i.caller, b._2, usdnAssetId)], result)
130138 }
131139 }
132140 }
135143
136144
137145 @Callable(i)
138-func sellResources (resTypes) = {
146+func sellResources (resTypes,minPrices) = {
139147 let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
140148 if ((size(i.payments) != 0))
141149 then throw("sellResources doesn't require any payments")
144152 if ((curLocation[locIdxType] != "F"))
145153 then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
146154 else {
147- let bpKey = keyBackpackByDuck(duckAssetId)
148- let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
155+ let locId = curLocation[locIdxId]
156+ let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":")
149157 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)
158+ func adder (acc,idx) = {
159+ let j = indexOf(resTypes, idx)
160+ if (isDefined(j))
161+ then {
162+ let b = buyInternal(locId, idx, parseIntValue(resList[idx]), minPrices[value(j)])
163+ $Tuple3((acc._1 :+ b._1), (acc._2 :+ "0"), (acc._3 + b._2))
164+ }
165+ else $Tuple3(acc._1, (acc._2 :+ resList[idx]), acc._3)
166+ }
164167
165168 let merged = {
166169 let $l = [0, 1, 2, 3, 4, 5]
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
95+func buyInternal (locId,resType,amount,minPrice) = {
96+ let whKey = keyFactoryWarehouseByIdAndType(locId, resType)
97+ let w0 = valueOrElse(getInteger(whKey), 0)
98+ let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
99+ then 0
100+ else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
101+ then (FACTORYMAXWAREHOUSE - w0)
102+ else amount
103+ let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
104+ let min99 = (minPrice - (minPrice / 100))
105+ if (((min99 * amount) > (usdnReceived * MULT8)))
106+ then throw((((((((((("Actual price = " + toString(usdnReceived)) + " / ") + toString(amount)) + " < minPrice = ") + toString(minPrice)) + ", (") + locId) + ", ") + resTypes[resType]) + ")"))
107+ else $Tuple2(IntegerEntry(whKey, (w0 + amount)), usdnReceived)
108+ }
109+
110+
95111 @Callable(i)
96-func sellResource (resType,amount) = if (if ((0 > resType))
112+func sellResource (resType,amount,minPrice) = if (if ((0 > resType))
97113 then true
98114 else (resType >= NUMRES))
99115 then throw(("Unknown resource: " + toString(resType)))
100116 else if ((0 >= amount))
101117 then throw(("Amount should be positive! " + toString(amount)))
102118 else {
103119 let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
104120 if ((size(i.payments) != 0))
105121 then throw("sellResources doesn't require any payments")
106122 else {
107123 let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_")
108124 if ((curLocation[locIdxType] != "F"))
109125 then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
110126 else {
111- let bpKey = keyBackpackByDuck(duckAssetId)
112- let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
127+ let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":")
113128 let resList = split(currentPack[bpIdxRes], "_")
114129 let currentRes = parseIntValue(resList[resType])
115130 if ((amount > currentRes))
116131 then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount)))
117132 else {
118- let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], resType)
119- let w0 = valueOrElse(getInteger(whKey), 0)
120- let r0 = if ((w0 > FACTORYMAXWAREHOUSE))
121- then 0
122- else if (((w0 + amount) > FACTORYMAXWAREHOUSE))
123- then (FACTORYMAXWAREHOUSE - w0)
124- else amount
125- let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8))
133+ let b = buyInternal(curLocation[locIdxId], resType, amount, minPrice)
126134 let bpRes = subRes(resList, resType, amount)
127135 let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
128136 let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
129- $Tuple2([IntegerEntry(whKey, (w0 + amount)), ScriptTransfer(i.caller, usdnReceived, usdnAssetId)], result)
137+ $Tuple2([b._1, ScriptTransfer(i.caller, b._2, usdnAssetId)], result)
130138 }
131139 }
132140 }
133141 }
134142
135143
136144
137145 @Callable(i)
138-func sellResources (resTypes) = {
146+func sellResources (resTypes,minPrices) = {
139147 let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked")
140148 if ((size(i.payments) != 0))
141149 then throw("sellResources doesn't require any payments")
142150 else {
143151 let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_")
144152 if ((curLocation[locIdxType] != "F"))
145153 then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType]))
146154 else {
147- let bpKey = keyBackpackByDuck(duckAssetId)
148- let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":")
155+ let locId = curLocation[locIdxId]
156+ let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":")
149157 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)
158+ func adder (acc,idx) = {
159+ let j = indexOf(resTypes, idx)
160+ if (isDefined(j))
161+ then {
162+ let b = buyInternal(locId, idx, parseIntValue(resList[idx]), minPrices[value(j)])
163+ $Tuple3((acc._1 :+ b._1), (acc._2 :+ "0"), (acc._3 + b._2))
164+ }
165+ else $Tuple3(acc._1, (acc._2 :+ resList[idx]), acc._3)
166+ }
164167
165168 let merged = {
166169 let $l = [0, 1, 2, 3, 4, 5]
167170 let $s = size($l)
168171 let $acc0 = $Tuple3(nil, nil, 0)
169172 func $f0_1 ($a,$i) = if (($i >= $s))
170173 then $a
171174 else adder($a, $l[$i])
172175
173176 func $f0_2 ($a,$i) = if (($i >= $s))
174177 then $a
175178 else throw("List size exceeds 6")
176179
177180 $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6)
178181 }
179182 let newPack = makeString([currentPack[bpIdxLevel], makeString(merged._2, "_"), currentPack[bpIdxMat], currentPack[bpIdxProd]], ":")
180183 let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil))
181184 $Tuple2((merged._1 :+ ScriptTransfer(i.caller, merged._3, usdnAssetId)), result)
182185 }
183186 }
184187 }
185188
186189

github/deemru/w8io/169f3d6 
52.95 ms