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/eDymXbbhPuAjvPWCmVqcHRjfJL2mzYHWKAyN4AC2JhY2tFbmRBZGRyCQERQGV4dHJOYXRpdmUoMTA2MikBAiMzTjVTcFgyMVIzUjc1UW80ZWIzTXdGRnZXN1RVenlodmF2dgAPc3Rha2luZ0NvbnRyYWN0CQERQGV4dHJOYXRpdmUoMTA2MikBAiMzTkRDeUJHNXE4NUp1YVJpaWdVZUV0YWlueWpDUVQzWHBabQAPREVGQVVMVExPQ0FUSU9OAg9BZnJpY2FfRl9BZnJpY2EABk5VTVJFUwAGABNGQUNUT1JZTUFYV0FSRUhPVVNFAIDIr6AlABBSRVNPVVJDRVBSSUNFTUlOANXWCQAIcmVzVHlwZXMJAMwIAgIDT2lsCQDMCAICA09yZQkAzAgCAgRXb29kCQDMCAICBFNhbmQJAMwIAgIEQ2xheQkAzAgCAgdPcmdhbmljBQNuaWwACmNvbnRpbmVudHMJAMwIAgIIQW1lcmljYXMJAMwIAgIGRXVyb3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dlZAJfMQkBDlNjcmlwdFRyYW5zZmVyAwgFAWkGY2FsbGVyCAUGbWVyZ2VkAl8zBQt1c2RuQXNzZXRJZAUGcmVzdWx0ABUayFQ=", "height": 2350342, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: 3HUQWiH2Qw9cgm4iUUWmokNPHEGpyQMSzrpx84CzALBL Next: J1yu5iTdahLA9Uo5ZUwXDVsxwhffx9hVUUZ7M8LdLYZp Diff:
Old | New | Differences | |
---|---|---|---|
92 | 92 | } | |
93 | 93 | ||
94 | 94 | ||
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 | + | ||
95 | 111 | @Callable(i) | |
96 | - | func sellResource (resType,amount) = if (if ((0 > resType)) | |
112 | + | func sellResource (resType,amount,minPrice) = if (if ((0 > resType)) | |
97 | 113 | then true | |
98 | 114 | else (resType >= NUMRES)) | |
99 | 115 | then throw(("Unknown resource: " + toString(resType))) | |
108 | 124 | if ((curLocation[locIdxType] != "F")) | |
109 | 125 | then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType])) | |
110 | 126 | 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::"), ":") | |
113 | 128 | let resList = split(currentPack[bpIdxRes], "_") | |
114 | 129 | let currentRes = parseIntValue(resList[resType]) | |
115 | 130 | if ((amount > currentRes)) | |
116 | 131 | then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount))) | |
117 | 132 | 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) | |
126 | 134 | let bpRes = subRes(resList, resType, amount) | |
127 | 135 | let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":") | |
128 | 136 | let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil)) | |
129 | - | $Tuple2([ | |
137 | + | $Tuple2([b._1, ScriptTransfer(i.caller, b._2, usdnAssetId)], result) | |
130 | 138 | } | |
131 | 139 | } | |
132 | 140 | } | |
135 | 143 | ||
136 | 144 | ||
137 | 145 | @Callable(i) | |
138 | - | func sellResources (resTypes) = { | |
146 | + | func sellResources (resTypes,minPrices) = { | |
139 | 147 | let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked") | |
140 | 148 | if ((size(i.payments) != 0)) | |
141 | 149 | then throw("sellResources doesn't require any payments") | |
144 | 152 | if ((curLocation[locIdxType] != "F")) | |
145 | 153 | then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType])) | |
146 | 154 | else { | |
147 | - | let | |
148 | - | let currentPack = split(valueOrElse(getString(stakingContract, | |
155 | + | let locId = curLocation[locIdxId] | |
156 | + | let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":") | |
149 | 157 | 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 | + | } | |
164 | 167 | ||
165 | 168 | let merged = { | |
166 | 169 | let $l = [0, 1, 2, 3, 4, 5] |
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 | + | 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 | + | ||
95 | 111 | @Callable(i) | |
96 | - | func sellResource (resType,amount) = if (if ((0 > resType)) | |
112 | + | func sellResource (resType,amount,minPrice) = if (if ((0 > resType)) | |
97 | 113 | then true | |
98 | 114 | else (resType >= NUMRES)) | |
99 | 115 | then throw(("Unknown resource: " + toString(resType))) | |
100 | 116 | else if ((0 >= amount)) | |
101 | 117 | then throw(("Amount should be positive! " + toString(amount))) | |
102 | 118 | else { | |
103 | 119 | let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked") | |
104 | 120 | if ((size(i.payments) != 0)) | |
105 | 121 | then throw("sellResources doesn't require any payments") | |
106 | 122 | else { | |
107 | 123 | let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_") | |
108 | 124 | if ((curLocation[locIdxType] != "F")) | |
109 | 125 | then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType])) | |
110 | 126 | 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::"), ":") | |
113 | 128 | let resList = split(currentPack[bpIdxRes], "_") | |
114 | 129 | let currentRes = parseIntValue(resList[resType]) | |
115 | 130 | if ((amount > currentRes)) | |
116 | 131 | then throw(((((("You have " + toString(currentRes)) + " of ") + resTypes[resType]) + " in backpack, but tried to sell ") + toString(amount))) | |
117 | 132 | 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) | |
126 | 134 | let bpRes = subRes(resList, resType, amount) | |
127 | 135 | let newPack = makeString([currentPack[bpIdxLevel], bpRes, currentPack[bpIdxMat], currentPack[bpIdxProd]], ":") | |
128 | 136 | let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil)) | |
129 | - | $Tuple2([ | |
137 | + | $Tuple2([b._1, ScriptTransfer(i.caller, b._2, usdnAssetId)], result) | |
130 | 138 | } | |
131 | 139 | } | |
132 | 140 | } | |
133 | 141 | } | |
134 | 142 | ||
135 | 143 | ||
136 | 144 | ||
137 | 145 | @Callable(i) | |
138 | - | func sellResources (resTypes) = { | |
146 | + | func sellResources (resTypes,minPrices) = { | |
139 | 147 | let duckAssetId = valueOrErrorMessage(getString(stakingContract, keyStakedDuckByOwner(toString(i.caller))), "You don't have a duck staked") | |
140 | 148 | if ((size(i.payments) != 0)) | |
141 | 149 | then throw("sellResources doesn't require any payments") | |
142 | 150 | else { | |
143 | 151 | let curLocation = split(valueOrElse(getString(stakingContract, keyDuckLocation(duckAssetId)), DEFAULTLOCATION), "_") | |
144 | 152 | if ((curLocation[locIdxType] != "F")) | |
145 | 153 | then throw(("Duck location type should be Factory, but is " + curLocation[locIdxType])) | |
146 | 154 | else { | |
147 | - | let | |
148 | - | let currentPack = split(valueOrElse(getString(stakingContract, | |
155 | + | let locId = curLocation[locIdxId] | |
156 | + | let currentPack = split(valueOrElse(getString(stakingContract, keyBackpackByDuck(duckAssetId)), "0:0_0_0_0_0_0::"), ":") | |
149 | 157 | 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 | + | } | |
164 | 167 | ||
165 | 168 | let merged = { | |
166 | 169 | let $l = [0, 1, 2, 3, 4, 5] | |
167 | 170 | let $s = size($l) | |
168 | 171 | let $acc0 = $Tuple3(nil, nil, 0) | |
169 | 172 | func $f0_1 ($a,$i) = if (($i >= $s)) | |
170 | 173 | then $a | |
171 | 174 | else adder($a, $l[$i]) | |
172 | 175 | ||
173 | 176 | func $f0_2 ($a,$i) = if (($i >= $s)) | |
174 | 177 | then $a | |
175 | 178 | else throw("List size exceeds 6") | |
176 | 179 | ||
177 | 180 | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6) | |
178 | 181 | } | |
179 | 182 | let newPack = makeString([currentPack[bpIdxLevel], makeString(merged._2, "_"), currentPack[bpIdxMat], currentPack[bpIdxProd]], ":") | |
180 | 183 | let result = asString(invoke(stakingContract, "updateBackpack", [duckAssetId, newPack], nil)) | |
181 | 184 | $Tuple2((merged._1 :+ ScriptTransfer(i.caller, merged._3, usdnAssetId)), result) | |
182 | 185 | } | |
183 | 186 | } | |
184 | 187 | } | |
185 | 188 | ||
186 | 189 |
github/deemru/w8io/169f3d6 52.95 ms ◑