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+gJQAQUkVTT1VSQ0VQUklDRU1JTgDV1gkACHJlc1R5cGVzCQDMCAICA09pbAkAzAgCAgNPcmUJAMwIAgIEV29vZAkAzAgCAgRTYW5kCQDMCAICBENsYXkJAMwIAgIHT3JnYW5pYwUDbmlsAApjb250aW5lbnRzCQDMCAICCEFtZXJpY2FzCQDMCAICBkV1cm9wZQkAzAgCA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TY3JpcHRUcmFuc2ZlcgMIBQFpBmNhbGxlcggFBm1lcmdlZAJfMwULdXNkbkFzc2V0SWQFBnJlc3VsdAANCdRt", "height": 2348645, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: Et3s8ZDJ4U9GWbFSwaeomM5N4PJeGvvhmB7wLvbJs2D Next: 4Hnti9WWrSuYW1HSCA4oEe56WNH1B6bU9ZB1tAY6CpM7 Diff:
Old | New | Differences | |
---|---|---|---|
133 | 133 | } | |
134 | 134 | ||
135 | 135 | ||
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 | + |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 6 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | 4 | let MULT6 = 1000000 | |
5 | 5 | ||
6 | 6 | let MULT8 = 100000000 | |
7 | 7 | ||
8 | 8 | let usdnAssetId = base58'HezsdQuRDtzksAYUy97gfhKy7Z1NW2uXYSHA3bgqenNZ' | |
9 | 9 | ||
10 | 10 | let backEndAddr = addressFromStringValue("3N5SpX21R3R75Qo4eb3MwFFvW7TUzyhvavv") | |
11 | 11 | ||
12 | 12 | let stakingContract = addressFromStringValue("3NDCyBG5q85JuaRiigUeEtainyjCQT3XpZm") | |
13 | 13 | ||
14 | 14 | let DEFAULTLOCATION = "Africa_F_Africa" | |
15 | 15 | ||
16 | 16 | let NUMRES = 6 | |
17 | 17 | ||
18 | 18 | let FACTORYMAXWAREHOUSE = 10000000000 | |
19 | 19 | ||
20 | 20 | let RESOURCEPRICEMIN = 158549 | |
21 | 21 | ||
22 | 22 | let resTypes = ["Oil", "Ore", "Wood", "Sand", "Clay", "Organic"] | |
23 | 23 | ||
24 | 24 | let continents = ["Americas", "Europe", "Asia", "Africa", "Oceania"] | |
25 | 25 | ||
26 | 26 | func keyFactoryWarehouseByIdAndType (factoryId,resType) = ((("factoryWhByContinentAndRes_" + factoryId) + "_") + toString(resType)) | |
27 | 27 | ||
28 | 28 | ||
29 | 29 | func keyAssetIdToOwner (assetId) = ("nftOwner_" + assetId) | |
30 | 30 | ||
31 | 31 | ||
32 | 32 | func keyStakedTimeByAssetId (assetId) = ("stakedTime_" + assetId) | |
33 | 33 | ||
34 | 34 | ||
35 | 35 | func keyStakedDuckByOwner (ownerAddr) = ("stakedDuckByOwner_" + ownerAddr) | |
36 | 36 | ||
37 | 37 | ||
38 | 38 | func keyBackpackByDuck (duckAssetId) = ("backPack_" + duckAssetId) | |
39 | 39 | ||
40 | 40 | ||
41 | 41 | func keyDuckLocation (duckAssetId) = ("duckLocation_" + duckAssetId) | |
42 | 42 | ||
43 | 43 | ||
44 | 44 | let idxType = 0 | |
45 | 45 | ||
46 | 46 | let idxQuantity = 1 | |
47 | 47 | ||
48 | 48 | let idxPrice = 2 | |
49 | 49 | ||
50 | 50 | let locIdxContinent = 0 | |
51 | 51 | ||
52 | 52 | let locIdxType = 1 | |
53 | 53 | ||
54 | 54 | let locIdxId = 2 | |
55 | 55 | ||
56 | 56 | let bpIdxLevel = 0 | |
57 | 57 | ||
58 | 58 | let bpIdxRes = 1 | |
59 | 59 | ||
60 | 60 | let bpIdxMat = 2 | |
61 | 61 | ||
62 | 62 | let bpIdxProd = 3 | |
63 | 63 | ||
64 | 64 | func asString (v) = match v { | |
65 | 65 | case s: String => | |
66 | 66 | s | |
67 | 67 | case _ => | |
68 | 68 | throw("fail to cast into String") | |
69 | 69 | } | |
70 | 70 | ||
71 | 71 | ||
72 | 72 | func subRes (resList,resType,amount) = { | |
73 | 73 | func subber (acc,i) = (acc :+ (if ((i == resType)) | |
74 | 74 | then toString((parseIntValue(resList[i]) - amount)) | |
75 | 75 | else resList[i])) | |
76 | 76 | ||
77 | 77 | let r = { | |
78 | 78 | let $l = [0, 1, 2, 3, 4, 5] | |
79 | 79 | let $s = size($l) | |
80 | 80 | let $acc0 = nil | |
81 | 81 | func $f0_1 ($a,$i) = if (($i >= $s)) | |
82 | 82 | then $a | |
83 | 83 | else subber($a, $l[$i]) | |
84 | 84 | ||
85 | 85 | func $f0_2 ($a,$i) = if (($i >= $s)) | |
86 | 86 | then $a | |
87 | 87 | else throw("List size exceeds 6") | |
88 | 88 | ||
89 | 89 | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6) | |
90 | 90 | } | |
91 | 91 | makeString(r, "_") | |
92 | 92 | } | |
93 | 93 | ||
94 | 94 | ||
95 | 95 | @Callable(i) | |
96 | 96 | func sellResource (resType,amount) = if (if ((0 > resType)) | |
97 | 97 | then true | |
98 | 98 | else (resType >= NUMRES)) | |
99 | 99 | then throw(("Unknown resource: " + toString(resType))) | |
100 | 100 | else if ((0 >= amount)) | |
101 | 101 | then throw(("Amount should be positive! " + toString(amount))) | |
102 | 102 | else { | |
103 | 103 | let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked") | |
104 | 104 | if ((size(i.payments) != 0)) | |
105 | 105 | then throw("sellResources doesn't require any payments") | |
106 | 106 | else { | |
107 | 107 | let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_") | |
108 | 108 | if ((curLocation[locIdxType] != "F")) | |
109 | 109 | then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType])) | |
110 | 110 | else { | |
111 | 111 | let bpKey = keyBackpackByDuck(duckAssetId) | |
112 | 112 | let currentPack = split(valueOrElse(getString(stakingContract, bpKey), "0:0_0_0_0_0_0::"), ":") | |
113 | 113 | let resList = split(currentPack[bpIdxRes], "_") | |
114 | 114 | let currentRes = parseIntValue(resList[resType]) | |
115 | 115 | if ((amount > currentRes)) | |
116 | 116 | then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount))) | |
117 | 117 | else { | |
118 | 118 | let whKey = keyFactoryWarehouseByIdAndType(curLocation[locIdxId], resType) | |
119 | 119 | let w0 = valueOrElse(getInteger(whKey), 0) | |
120 | 120 | let r0 = if ((w0 > FACTORYMAXWAREHOUSE)) | |
121 | 121 | then 0 | |
122 | 122 | else if (((w0 + amount) > FACTORYMAXWAREHOUSE)) | |
123 | 123 | then (FACTORYMAXWAREHOUSE - w0) | |
124 | 124 | else amount | |
125 | 125 | let usdnReceived = (fraction(r0, ((2 * RESOURCEPRICEMIN) - fraction((w0 + (r0 / 2)), RESOURCEPRICEMIN, FACTORYMAXWAREHOUSE)), MULT8) + fraction((amount - r0), RESOURCEPRICEMIN, MULT8)) | |
126 | 126 | let bpRes = subRes(resList, resType, amount) | |
127 | 127 | let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":") | |
128 | 128 | let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil)) | |
129 | 129 | $Tuple2([IntegerEntry(whKey, (w0 + amount)), ScriptTransfer(i.caller, usdnReceived, usdnAssetId)], result) | |
130 | 130 | } | |
131 | 131 | } | |
132 | 132 | } | |
133 | 133 | } | |
134 | 134 | ||
135 | 135 | ||
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 ◑