7 | 7 | | |
---|
8 | 8 | | |
---|
9 | 9 | | func throwErr (msg) = throw(wrapErr(msg)) |
---|
10 | 10 | | |
---|
11 | 11 | | |
---|
12 | 12 | | func getStringOrFail (key) = valueOrErrorMessage(getString(this, key), ("No data for this.key=" + key)) |
---|
13 | 13 | | |
---|
14 | 14 | | |
---|
15 | 15 | | func getBooleanOrFail (key) = valueOrErrorMessage(getBoolean(this, key), ("No data for this.key=" + key)) |
---|
16 | 16 | | |
---|
17 | 17 | | |
---|
18 | 18 | | func failExecuteGet (msg,baseAssetStr,userAddressStr,getTxIdStr) = throw(((((((msg + ": baseAssetStr=") + baseAssetStr) + " userAddressStr=") + userAddressStr) + " getTxIdStr=") + getTxIdStr)) |
---|
19 | 19 | | |
---|
20 | 20 | | |
---|
21 | 21 | | func keyAssetCfg (baseAssetStr) = ("%s%s%s__config__asset__" + baseAssetStr) |
---|
22 | 22 | | |
---|
23 | 23 | | |
---|
24 | 24 | | func keyNextInternalAssetId () = "%s__nextInternalAssetId" |
---|
25 | 25 | | |
---|
26 | 26 | | |
---|
27 | 27 | | func keyPriceLast (internalBasetAssetStr) = ("%s%s%d__price__last__" + internalBasetAssetStr) |
---|
28 | 28 | | |
---|
29 | 29 | | |
---|
30 | 30 | | func keyTopUpLastHeight (internalBasetAssetStr,sender) = makeString(["%s%s%s%d%s__topup__last__height", internalBasetAssetStr, sender], SEP) |
---|
31 | 31 | | |
---|
32 | 32 | | |
---|
33 | 33 | | func keyPriceHistory (internalBasetAssetStr,h,timestamp) = makeString(["%s%s%d%d%d__price__history", internalBasetAssetStr, toString(h), toString(timestamp)], SEP) |
---|
34 | 34 | | |
---|
35 | 35 | | |
---|
36 | 36 | | func keyTotalLocked (internalBasetAssetStr) = ("%s%s%d__total__locked__" + internalBasetAssetStr) |
---|
37 | 37 | | |
---|
38 | 38 | | |
---|
39 | 39 | | func keyTotalLockedByUser (internalBaseAssetStr,userAddressStr) = makeString(["%s%s%d%s__total__locked", internalBaseAssetStr, userAddressStr], SEP) |
---|
40 | 40 | | |
---|
41 | 41 | | |
---|
42 | 42 | | func keyMappingsInternal2baseAssetId (internalBaseAsset) = ("%s%s%d__mappings__internal2baseAssetId__" + toString(internalBaseAsset)) |
---|
43 | 43 | | |
---|
44 | 44 | | |
---|
45 | 45 | | func keyMappingsBaseAsset2internalId (baseAssetStr) = ("%s%s%s__mappings__baseAsset2internalId__" + baseAssetStr) |
---|
46 | 46 | | |
---|
47 | 47 | | |
---|
48 | 48 | | func keyMappingsShare2baseAssetId (shareAssetStr) = ("%s%s%s__mappings__share2baseAssetId__" + shareAssetStr) |
---|
49 | 49 | | |
---|
50 | 50 | | |
---|
51 | 51 | | func keyMappingsBaseAsset2shareId (baseAssetStr) = ("%s%s%s__mappings__baseAsset2shareId__" + baseAssetStr) |
---|
52 | 52 | | |
---|
53 | 53 | | |
---|
54 | 54 | | func keyShutdownPutOperation (internalBaseAssetStr) = ("%s%s%d__shutdown__put__" + internalBaseAssetStr) |
---|
55 | 55 | | |
---|
56 | 56 | | |
---|
57 | 57 | | func keyShutdown (internalBaseAssetStr) = ("%s%d__shutdown__" + internalBaseAssetStr) |
---|
58 | 58 | | |
---|
59 | 59 | | |
---|
60 | 60 | | func keyShutdownManager (internalBaseAssetStr) = ("%s%s%d__shutdown__manager__" + internalBaseAssetStr) |
---|
61 | 61 | | |
---|
62 | 62 | | |
---|
63 | 63 | | let IdxCfgShareAssetId = 1 |
---|
64 | 64 | | |
---|
65 | 65 | | let IdxCfgInternalBaseAsset = 2 |
---|
66 | 66 | | |
---|
67 | 67 | | let IdxCfgDecimalsMultBothAssets = 3 |
---|
68 | 68 | | |
---|
69 | 69 | | let IdxCfgDecimalsMultPrice = 4 |
---|
70 | 70 | | |
---|
71 | 71 | | let IdxCfgGetDelayBlocks = 5 |
---|
72 | 72 | | |
---|
73 | 73 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
74 | 74 | | |
---|
75 | 75 | | |
---|
76 | 76 | | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
77 | 77 | | |
---|
78 | 78 | | |
---|
79 | 79 | | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
80 | 80 | | case s: String => |
---|
81 | 81 | | fromBase58String(s) |
---|
82 | 82 | | case _: Unit => |
---|
83 | 83 | | unit |
---|
84 | 84 | | case _ => |
---|
85 | 85 | | throw("Match error") |
---|
86 | 86 | | } |
---|
87 | 87 | | |
---|
88 | 88 | | |
---|
89 | 89 | | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
90 | 90 | | case s: String => |
---|
91 | 91 | | fromBase58String(s) |
---|
92 | 92 | | case _: Unit => |
---|
93 | 93 | | unit |
---|
94 | 94 | | case _ => |
---|
95 | 95 | | throw("Match error") |
---|
96 | 96 | | } |
---|
97 | 97 | | |
---|
98 | 98 | | |
---|
99 | 99 | | let permissionDeniedError = throw("Permission denied") |
---|
100 | 100 | | |
---|
101 | 101 | | func mustThis (i) = if ((i.caller == this)) |
---|
102 | 102 | | then true |
---|
103 | 103 | | else permissionDeniedError |
---|
104 | 104 | | |
---|
105 | 105 | | |
---|
106 | 106 | | func mustManager (i) = match managerPublicKeyOrUnit() { |
---|
107 | 107 | | case pk: ByteVector => |
---|
108 | 108 | | if ((i.callerPublicKey == pk)) |
---|
109 | 109 | | then true |
---|
110 | 110 | | else permissionDeniedError |
---|
111 | 111 | | case _: Unit => |
---|
112 | 112 | | mustThis(i) |
---|
113 | 113 | | case _ => |
---|
114 | 114 | | throw("Match error") |
---|
115 | 115 | | } |
---|
116 | 116 | | |
---|
117 | 117 | | |
---|
118 | 118 | | func dataAssetCfg (shareAssetStr,internalBaseAssetStr,decimalsMultBothAssets,decimalsMultPrice,getDelayInBlocks) = makeString(["%s%d%d%d%d", shareAssetStr, internalBaseAssetStr, toString(decimalsMultBothAssets), toString(decimalsMultPrice), toString(getDelayInBlocks)], SEP) |
---|
119 | 119 | | |
---|
120 | 120 | | |
---|
121 | 121 | | let IdxTotalLockedShare = 1 |
---|
122 | 122 | | |
---|
123 | 123 | | let IdxTotalLockedBase = 2 |
---|
124 | 124 | | |
---|
125 | 125 | | func dataTotalLocked (shareAssetAmount,baseAssetAmount) = makeString(["%d%d", toString(shareAssetAmount), toString(baseAssetAmount)], SEP) |
---|
126 | 126 | | |
---|
127 | 127 | | |
---|
128 | 128 | | func readTotalLocked (key) = { |
---|
129 | 129 | | let totalLockedArray = split(valueOrElse(getString(this, key), dataTotalLocked(0, 0)), SEP) |
---|
130 | 130 | | [-1, parseIntValue(totalLockedArray[IdxTotalLockedShare]), parseIntValue(totalLockedArray[IdxTotalLockedBase])] |
---|
131 | 131 | | } |
---|
132 | 132 | | |
---|
133 | 133 | | |
---|
134 | 134 | | func keyOperation (operationType,internalBaseAssetStr,userAddress,txId) = makeString(["%s%d%s%s", operationType, internalBaseAssetStr, userAddress, txId], SEP) |
---|
135 | 135 | | |
---|
136 | 136 | | |
---|
137 | 137 | | let IdxOperStatus = 1 |
---|
138 | 138 | | |
---|
139 | 139 | | let IdxOperInAmount = 2 |
---|
140 | 140 | | |
---|
141 | 141 | | let IdxOperPrice = 3 |
---|
142 | 142 | | |
---|
143 | 143 | | let IdxOperOutAmount = 4 |
---|
144 | 144 | | |
---|
145 | 145 | | let IdxOperStartHeight = 5 |
---|
146 | 146 | | |
---|
147 | 147 | | let IdxOperStartTimestamp = 6 |
---|
148 | 148 | | |
---|
149 | 149 | | let IdxOperEndHeight = 7 |
---|
150 | 150 | | |
---|
151 | 151 | | let IdxOperEndTimestamp = 8 |
---|
152 | 152 | | |
---|
153 | 153 | | func privateDataOperationAllStrings (status,inAssetAmount,price,outAssetAmount,startHeight,startTimestamp,endHeight,endTimestamp) = makeString(["%s%d%d%d%d%d%d%d", status, inAssetAmount, price, outAssetAmount, startHeight, startTimestamp, endHeight, endTimestamp], SEP) |
---|
154 | 154 | | |
---|
155 | 155 | | |
---|
156 | 156 | | func dataOperation (status,inAssetAmount,price,outAssetAmount,startHeight,startTimestamp,endHeight,endTimestamp) = privateDataOperationAllStrings(status, toString(inAssetAmount), toString(price), toString(outAssetAmount), toString(startHeight), toString(startTimestamp), toString(endHeight), toString(endTimestamp)) |
---|
157 | 157 | | |
---|
158 | 158 | | |
---|
159 | 159 | | func dataOperationExecutionUpdate (currOperArray,newStatus,newEndTimestamp) = privateDataOperationAllStrings(newStatus, currOperArray[IdxOperInAmount], currOperArray[IdxOperPrice], currOperArray[IdxOperOutAmount], currOperArray[IdxOperStartHeight], currOperArray[IdxOperStartTimestamp], currOperArray[IdxOperEndHeight], toString(newEndTimestamp)) |
---|
160 | 160 | | |
---|
161 | 161 | | |
---|
162 | 162 | | func readAssetCfgOrFail (baseAssetStr) = { |
---|
163 | 163 | | let key = keyAssetCfg(baseAssetStr) |
---|
164 | 164 | | split(getStringOrFail(key), SEP) |
---|
165 | 165 | | } |
---|
166 | 166 | | |
---|
167 | 167 | | |
---|
168 | 168 | | func incrementTotalLocked (key,shareAssetAmount,baseAssetAmount) = { |
---|
169 | 169 | | let dataArray = readTotalLocked(key) |
---|
170 | 170 | | StringEntry(key, dataTotalLocked((dataArray[IdxTotalLockedShare] + shareAssetAmount), (dataArray[IdxTotalLockedBase] + baseAssetAmount))) |
---|
171 | 171 | | } |
---|
172 | 172 | | |
---|
173 | 173 | | |
---|
174 | 174 | | func decrementTotalLocked (key,shareAssetAmount,baseAssetAmount) = { |
---|
175 | 175 | | let dataArray = readTotalLocked(key) |
---|
176 | 176 | | StringEntry(key, dataTotalLocked((dataArray[IdxTotalLockedShare] - shareAssetAmount), (dataArray[IdxTotalLockedBase] - baseAssetAmount))) |
---|
177 | 177 | | } |
---|
178 | 178 | | |
---|
179 | 179 | | |
---|
180 | 180 | | func genericCalcPrice (internalBaseAssetStr,baseAssetId,topUpBaseAmount,shareAssetId,decimalsMultBothAssets,decimalsMultPrice) = { |
---|
181 | 181 | | let totalLockedArray = readTotalLocked(keyTotalLocked(internalBaseAssetStr)) |
---|
182 | 182 | | let totalLockedBaseAmount = totalLockedArray[IdxTotalLockedBase] |
---|
183 | 183 | | let baseAssetBalance = assetBalance(this, baseAssetId) |
---|
184 | 184 | | let baseAssetBalanceConsideringLock = ((baseAssetBalance - totalLockedBaseAmount) + topUpBaseAmount) |
---|
185 | 185 | | if ((0 > baseAssetBalanceConsideringLock)) |
---|
186 | 186 | | then throw(((("baseAssetBalanceConsideringLock < 0: baseAssetBalance=" + toString(baseAssetBalance)) + " baseAssetBalanceConsideringLock=") + toString(baseAssetBalanceConsideringLock))) |
---|
187 | 187 | | else { |
---|
188 | 188 | | let shareEmission = value(assetInfo(shareAssetId)).quantity |
---|
189 | 189 | | let price = if ((shareEmission == 0)) |
---|
190 | 190 | | then (1 * decimalsMultPrice) |
---|
191 | 191 | | else fraction(baseAssetBalanceConsideringLock, decimalsMultPrice, shareEmission) |
---|
192 | 192 | | $Tuple5(price, baseAssetBalance, totalLockedBaseAmount, baseAssetBalanceConsideringLock, shareEmission) |
---|
193 | 193 | | } |
---|
194 | 194 | | } |
---|
195 | 195 | | |
---|
196 | 196 | | |
---|
197 | 197 | | func calcPrice (internalBaseAssetStr,baseAssetId,shareAssetId,decimalsMultBothAssets,decimalsMultPrice) = genericCalcPrice(internalBaseAssetStr, baseAssetId, 0, shareAssetId, decimalsMultBothAssets, decimalsMultPrice) |
---|
198 | 198 | | |
---|
199 | 199 | | |
---|
200 | 200 | | func privateCurrentSysParamsREST (baseAssetStr) = { |
---|
201 | 201 | | let baseAssetId = fromBase58String(baseAssetStr) |
---|
202 | 202 | | let cfgArray = readAssetCfgOrFail(baseAssetStr) |
---|
203 | 203 | | let shareAssetStr = cfgArray[IdxCfgShareAssetId] |
---|
204 | 204 | | let shareAssetId = fromBase58String(shareAssetStr) |
---|
205 | 205 | | let decimalsMultBothAssets = parseIntValue(cfgArray[IdxCfgDecimalsMultBothAssets]) |
---|
206 | 206 | | let decimalsMultPrice = parseIntValue(cfgArray[IdxCfgDecimalsMultPrice]) |
---|
207 | 207 | | let internalBaseAssetStr = cfgArray[IdxCfgInternalBaseAsset] |
---|
208 | 208 | | let sysState = calcPrice(internalBaseAssetStr, baseAssetId, shareAssetId, decimalsMultBothAssets, decimalsMultPrice) |
---|
209 | 209 | | $Tuple6(IntegerEntry("price", sysState._1), IntegerEntry("decimalsMultPrice", decimalsMultPrice), IntegerEntry("baseAssetBalance", sysState._2), IntegerEntry("totalLockedBaseAmount", sysState._3), IntegerEntry("baseAssetBalanceConsideringLock", sysState._4), IntegerEntry("shareEmission", sysState._5)) |
---|
210 | 210 | | } |
---|
211 | 211 | | |
---|
212 | 212 | | |
---|
213 | 213 | | @Callable(i) |
---|
214 | 214 | | func adminRegisterAsset (baseAssetStr,shareAssetName,shareAssetDescr,getDelayinBlocks,shutdownManagerAddress) = { |
---|
215 | 215 | | let baseAssetId = fromBase58String(baseAssetStr) |
---|
216 | 216 | | let decimals = value(assetInfo(baseAssetId)).decimals |
---|
217 | 217 | | let checkCaller = mustManager(i) |
---|
218 | 218 | | if ((checkCaller == checkCaller)) |
---|
219 | 219 | | then if ((toString(addressFromStringValue(shutdownManagerAddress)) != shutdownManagerAddress)) |
---|
220 | 220 | | then throw("invalid shutdownManagerAddress") |
---|
221 | 221 | | else if ((0 > getDelayinBlocks)) |
---|
222 | 222 | | then throw(("invalid getDelayinBlocks=" + toString(getDelayinBlocks))) |
---|
223 | 223 | | else { |
---|
224 | 224 | | let shareAssetIssueAction = Issue(shareAssetName, shareAssetDescr, 1, decimals, true) |
---|
225 | 225 | | let shareAssetId = calculateAssetId(shareAssetIssueAction) |
---|
226 | 226 | | let shareAssetStr = toBase58String(shareAssetId) |
---|
227 | 227 | | let decimalsMultPrice = ((100 * 1000) * 1000) |
---|
228 | 228 | | let decimalsMultBothAssets = pow(10, 0, decimals, 0, 0, DOWN) |
---|
229 | 229 | | let startPrice = (1 * decimalsMultPrice) |
---|
230 | 230 | | let internalBaseAssettId = valueOrElse(getInteger(this, keyNextInternalAssetId()), 0) |
---|
231 | 231 | | let internalBaseAssetStr = toString(internalBaseAssettId) |
---|
232 | 232 | | [StringEntry(keyAssetCfg(baseAssetStr), dataAssetCfg(shareAssetStr, internalBaseAssetStr, decimalsMultBothAssets, decimalsMultPrice, getDelayinBlocks)), StringEntry(keyMappingsInternal2baseAssetId(internalBaseAssettId), baseAssetStr), StringEntry(keyMappingsBaseAsset2internalId(baseAssetStr), internalBaseAssetStr), StringEntry(keyMappingsShare2baseAssetId(shareAssetStr), baseAssetStr), StringEntry(keyMappingsBaseAsset2shareId(baseAssetStr), shareAssetStr), BooleanEntry(keyShutdownPutOperation(internalBaseAssetStr), false), StringEntry(keyShutdownManager(internalBaseAssetStr), shutdownManagerAddress), IntegerEntry(keyNextInternalAssetId(), (internalBaseAssettId + 1)), IntegerEntry(keyPriceLast(internalBaseAssetStr), startPrice), IntegerEntry(keyPriceHistory(internalBaseAssetStr, height, lastBlock.timestamp), startPrice), shareAssetIssueAction, Burn(shareAssetId, 1)] |
---|
233 | 233 | | } |
---|
234 | 234 | | else throw("Strict value is not equal to itself.") |
---|
235 | 235 | | } |
---|
236 | 236 | | |
---|
237 | 237 | | |
---|
238 | 238 | | |
---|
239 | 239 | | @Callable(i) |
---|
240 | 240 | | func shutdownPut (internalBaseAssetId) = { |
---|
241 | 241 | | let internalBaseAssetIdStr = toString(internalBaseAssetId) |
---|
242 | 242 | | let baseAssetIdStr = getStringOrFail(keyMappingsInternal2baseAssetId(internalBaseAssetId)) |
---|
243 | 243 | | let shutdownManagerAddress = getStringOrFail(keyShutdownManager(internalBaseAssetIdStr)) |
---|
244 | 244 | | if ((1 > size(baseAssetIdStr))) |
---|
245 | 245 | | then throw("invalid internalBaseAssetId") |
---|
246 | 246 | | else if ((toString(i.caller) != shutdownManagerAddress)) |
---|
247 | 247 | | then throw("access denied") |
---|
248 | 248 | | else [BooleanEntry(keyShutdownPutOperation(toString(internalBaseAssetId)), true)] |
---|
249 | 249 | | } |
---|
250 | 250 | | |
---|
251 | 251 | | |
---|
252 | 252 | | |
---|
253 | 253 | | @Callable(i) |
---|
254 | 254 | | func put () = { |
---|
255 | 255 | | let pmt = value(i.payments[0]) |
---|
256 | 256 | | let baseAssetId = value(pmt.assetId) |
---|
257 | 257 | | let baseAssetStr = toBase58String(baseAssetId) |
---|
258 | 258 | | let userAddressStr = toString(i.caller) |
---|
259 | 259 | | let cfgArray = readAssetCfgOrFail(baseAssetStr) |
---|
260 | 260 | | let shareAssetStr = cfgArray[IdxCfgShareAssetId] |
---|
261 | 261 | | let shareAssetId = fromBase58String(shareAssetStr) |
---|
262 | 262 | | let decimalsMultBothAssets = parseIntValue(cfgArray[IdxCfgDecimalsMultBothAssets]) |
---|
263 | 263 | | let decimalsMultPrice = parseIntValue(cfgArray[IdxCfgDecimalsMultPrice]) |
---|
264 | 264 | | let internalBaseAssetStr = cfgArray[IdxCfgInternalBaseAsset] |
---|
265 | 265 | | let isPutBlocked = getBooleanOrFail(keyShutdownPutOperation(internalBaseAssetStr)) |
---|
266 | 266 | | let shutdown = valueOrElse(getBoolean(keyShutdown(internalBaseAssetStr)), false) |
---|
267 | 267 | | if (if (isPutBlocked) |
---|
268 | 268 | | then true |
---|
269 | 269 | | else shutdown) |
---|
270 | 270 | | then throw("put operation is blocked") |
---|
271 | 271 | | else { |
---|
272 | 272 | | let price = calcPrice(internalBaseAssetStr, baseAssetId, shareAssetId, decimalsMultBothAssets, decimalsMultPrice)._1 |
---|
273 | 273 | | let shareAssetAmount = fraction(pmt.amount, decimalsMultPrice, price) |
---|
274 | 274 | | [Reissue(shareAssetId, shareAssetAmount, true), ScriptTransfer(i.caller, shareAssetAmount, shareAssetId), StringEntry(keyOperation("P", internalBaseAssetStr, userAddressStr, toBase58String(i.transactionId)), dataOperation("FINISHED", pmt.amount, price, shareAssetAmount, height, lastBlock.timestamp, height, lastBlock.timestamp))] |
---|
275 | 275 | | } |
---|
276 | 276 | | } |
---|
277 | 277 | | |
---|
278 | 278 | | |
---|
279 | 279 | | |
---|
280 | 280 | | @Callable(i) |
---|
281 | 281 | | func submitGetRequest () = { |
---|
282 | 282 | | let pmt = value(i.payments[0]) |
---|
283 | 283 | | let shareAssetId = value(pmt.assetId) |
---|
284 | 284 | | let shareAssetStr = toBase58String(shareAssetId) |
---|
285 | 285 | | let callerPubStr = toBase58String(i.callerPublicKey) |
---|
286 | 286 | | let userAddress = i.caller |
---|
287 | 287 | | let userAddressStr = toString(userAddress) |
---|
288 | 288 | | let shareAssetAmount = pmt.amount |
---|
289 | 289 | | let baseAssetStr = getStringOrFail(keyMappingsShare2baseAssetId(shareAssetStr)) |
---|
290 | 290 | | let baseAssetId = fromBase58String(baseAssetStr) |
---|
291 | 291 | | let cfgArray = readAssetCfgOrFail(baseAssetStr) |
---|
292 | 292 | | let decimalsMultBothAssets = parseIntValue(cfgArray[IdxCfgDecimalsMultBothAssets]) |
---|
293 | 293 | | let decimalsMultPrice = parseIntValue(cfgArray[IdxCfgDecimalsMultPrice]) |
---|
294 | 294 | | let getDelayBlocks = parseIntValue(cfgArray[IdxCfgGetDelayBlocks]) |
---|
295 | 295 | | let internalBaseAssetStr = cfgArray[IdxCfgInternalBaseAsset] |
---|
296 | 296 | | let shutdown = valueOrElse(getBoolean(keyShutdown(internalBaseAssetStr)), false) |
---|
297 | 297 | | if (shutdown) |
---|
298 | 298 | | then throw("operation is blocked") |
---|
299 | 299 | | else { |
---|
300 | 300 | | let price = calcPrice(internalBaseAssetStr, baseAssetId, shareAssetId, decimalsMultBothAssets, decimalsMultPrice)._1 |
---|
301 | 301 | | let baseAssetAmount = fraction(shareAssetAmount, price, decimalsMultPrice) |
---|
302 | 302 | | let operationKey = keyOperation("G", internalBaseAssetStr, userAddressStr, toBase58String(i.transactionId)) |
---|
303 | 303 | | let operationData = dataOperation("PENDING", shareAssetAmount, price, baseAssetAmount, height, lastBlock.timestamp, (height + getDelayBlocks), 0) |
---|
304 | 304 | | [Burn(shareAssetId, shareAssetAmount), StringEntry(operationKey, operationData), incrementTotalLocked(keyTotalLocked(internalBaseAssetStr), shareAssetAmount, baseAssetAmount), incrementTotalLocked(keyTotalLockedByUser(internalBaseAssetStr, userAddressStr), shareAssetAmount, baseAssetAmount)] |
---|
305 | 305 | | } |
---|
306 | 306 | | } |
---|
307 | 307 | | |
---|
308 | 308 | | |
---|
309 | 309 | | |
---|
310 | 310 | | @Callable(i) |
---|
311 | 311 | | func executeGetRequest (baseAssetStr,userAddressStr,getTxIdStr) = { |
---|
312 | 312 | | let userAddress = addressFromStringValue(userAddressStr) |
---|
313 | 313 | | let assetCfgArray = readAssetCfgOrFail(baseAssetStr) |
---|
314 | 314 | | let shareAssetId = fromBase58String(assetCfgArray[IdxCfgShareAssetId]) |
---|
315 | 315 | | let baseAssetId = fromBase58String(baseAssetStr) |
---|
316 | 316 | | let internalBaseAssetStr = assetCfgArray[IdxCfgInternalBaseAsset] |
---|
317 | 317 | | let shutdown = valueOrElse(getBoolean(keyShutdown(internalBaseAssetStr)), false) |
---|
318 | 318 | | if (shutdown) |
---|
319 | 319 | | then throw("operation is blocked") |
---|
320 | 320 | | else { |
---|
321 | 321 | | let operationKey = keyOperation("G", internalBaseAssetStr, userAddressStr, getTxIdStr) |
---|
322 | 322 | | let operationArray = split(getStringOrFail(operationKey), SEP) |
---|
323 | 323 | | let status = operationArray[IdxOperStatus] |
---|
324 | 324 | | let endHeight = parseIntValue(operationArray[IdxOperEndHeight]) |
---|
325 | 325 | | let inShareAmount = parseIntValue(operationArray[IdxOperInAmount]) |
---|
326 | 326 | | let outBaseAmount = parseIntValue(operationArray[IdxOperOutAmount]) |
---|
327 | 327 | | if ((status != "PENDING")) |
---|
328 | 328 | | then failExecuteGet("Status is not PENDING", baseAssetStr, userAddressStr, getTxIdStr) |
---|
329 | 329 | | else if ((endHeight > height)) |
---|
330 | 330 | | then failExecuteGet(((("EndHeight[" + toString(endHeight)) + "] > ") + toString(height)), baseAssetStr, userAddressStr, getTxIdStr) |
---|
331 | 331 | | else [ScriptTransfer(userAddress, outBaseAmount, baseAssetId), StringEntry(operationKey, dataOperationExecutionUpdate(operationArray, "FINISHED", lastBlock.timestamp)), decrementTotalLocked(keyTotalLocked(internalBaseAssetStr), inShareAmount, outBaseAmount), decrementTotalLocked(keyTotalLockedByUser(internalBaseAssetStr, userAddressStr), inShareAmount, outBaseAmount)] |
---|
332 | 332 | | } |
---|
333 | 333 | | } |
---|
334 | 334 | | |
---|
335 | 335 | | |
---|
336 | 336 | | |
---|
337 | 337 | | @Callable(i) |
---|
338 | 338 | | func topUpBalance (baseAssetStr) = { |
---|
339 | 339 | | let assetCfgArray = readAssetCfgOrFail(baseAssetStr) |
---|
340 | 340 | | let pmt = value(i.payments[0]) |
---|
341 | 341 | | let pmtAssetId = value(pmt.assetId) |
---|
342 | 342 | | let pmtAssetStr = toBase58String(pmtAssetId) |
---|
343 | 343 | | let shareAssetId = fromBase58String(assetCfgArray[IdxCfgShareAssetId]) |
---|
344 | 344 | | let decimalsMultBothAssets = parseIntValue(assetCfgArray[IdxCfgDecimalsMultBothAssets]) |
---|
345 | 345 | | let decimalsMultPrice = parseIntValue(assetCfgArray[IdxCfgDecimalsMultPrice]) |
---|
346 | 346 | | let internalBaseAssetStr = assetCfgArray[IdxCfgInternalBaseAsset] |
---|
347 | 347 | | let topUpLastHeightKEY = keyTopUpLastHeight(internalBaseAssetStr, toString(i.caller)) |
---|
348 | 348 | | let topUpLastHeight = valueOrElse(getInteger(this, topUpLastHeightKEY), 0) |
---|
349 | 349 | | let shutdown = valueOrElse(getBoolean(keyShutdown(internalBaseAssetStr)), false) |
---|
350 | 350 | | if (shutdown) |
---|
351 | 351 | | then throw("operation is blocked") |
---|
352 | 352 | | else if ((baseAssetStr != pmtAssetStr)) |
---|
353 | 353 | | then throw("attached payment's asset id is NOT matched passed baseAssetStr") |
---|
354 | 354 | | else if ((size(i.payments) > 1)) |
---|
355 | 355 | | then throw("only one payment can be attached") |
---|
356 | 356 | | else if ((100 > (height - topUpLastHeight))) |
---|
357 | 357 | | then throw("1 topUp per 100 blocks from the same address is allowed") |
---|
358 | 358 | | else { |
---|
359 | 359 | | let price = genericCalcPrice(internalBaseAssetStr, pmtAssetId, pmt.amount, shareAssetId, decimalsMultBothAssets, decimalsMultPrice)._1 |
---|
360 | 360 | | [IntegerEntry(keyPriceLast(internalBaseAssetStr), price), IntegerEntry(keyPriceHistory(internalBaseAssetStr, height, lastBlock.timestamp), price), IntegerEntry(topUpLastHeightKEY, height)] |
---|
361 | 361 | | } |
---|
362 | 362 | | } |
---|
363 | 363 | | |
---|
364 | 364 | | |
---|
365 | 365 | | |
---|
366 | 366 | | @Callable(i) |
---|
367 | 367 | | func emit (assetId,amount) = { |
---|
368 | 368 | | let lpStakingPoolsContract = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(makeString(["%s", "lpStakingPoolsContract"], SEP)), wrapErr("lp_staking_pools contract address is undefined"))), wrapErr("invalid lp_staking_pools contract address")) |
---|
369 | 369 | | let checks = [if ((i.caller == lpStakingPoolsContract)) |
---|
370 | 370 | | then true |
---|
371 | 371 | | else throwErr("permission denied"), if ((amount > 0)) |
---|
372 | 372 | | then true |
---|
373 | 373 | | else throwErr("invalid amount to emit"), if ((getString(keyMappingsShare2baseAssetId(toBase58String(assetId))) != unit)) |
---|
374 | 374 | | then true |
---|
375 | 375 | | else throwErr("invalid assetId")] |
---|
376 | 376 | | if ((checks == checks)) |
---|
377 | 377 | | then { |
---|
378 | 378 | | let isReissuable = true |
---|
379 | 379 | | $Tuple2([Reissue(assetId, amount, isReissuable), ScriptTransfer(lpStakingPoolsContract, amount, assetId)], amount) |
---|
380 | 380 | | } |
---|
381 | 381 | | else throw("Strict value is not equal to itself.") |
---|
382 | 382 | | } |
---|
383 | 383 | | |
---|
384 | 384 | | |
---|
385 | 385 | | |
---|
386 | 386 | | @Callable(i) |
---|
387 | 387 | | func currentSysParamsREST (baseAssetStr) = { |
---|
388 | 388 | | let sysStateTuple = privateCurrentSysParamsREST(baseAssetStr) |
---|
389 | 389 | | let price = sysStateTuple._1.value |
---|
390 | 390 | | let decimalsMultPrice = sysStateTuple._2.value |
---|
391 | 391 | | let baseAssetBalance = sysStateTuple._3.value |
---|
392 | 392 | | let totalLockedBaseAmount = sysStateTuple._4.value |
---|
393 | 393 | | let baseAssetBalanceConsideringLock = sysStateTuple._5.value |
---|
394 | 394 | | let shareEmission = sysStateTuple._6.value |
---|
395 | 395 | | let restData = makeString(["startCurrentSysParamsREST", toString(price), toString(decimalsMultPrice), toString(baseAssetBalance), toString(totalLockedBaseAmount), toString(baseAssetBalanceConsideringLock), toString(shareEmission), "endCurrentSysParamsREST"], SEP) |
---|
396 | 396 | | throw(restData) |
---|
397 | 397 | | } |
---|
398 | 398 | | |
---|
399 | 399 | | |
---|
400 | 400 | | |
---|
401 | 401 | | @Callable(i) |
---|
402 | 402 | | func setManager (pendingManagerPublicKey) = { |
---|
403 | 403 | | let checkCaller = mustManager(i) |
---|
404 | 404 | | if ((checkCaller == checkCaller)) |
---|
405 | 405 | | then { |
---|
406 | 406 | | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
407 | 407 | | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
408 | 408 | | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] |
---|
409 | 409 | | else throw("Strict value is not equal to itself.") |
---|
410 | 410 | | } |
---|
411 | 411 | | else throw("Strict value is not equal to itself.") |
---|
412 | 412 | | } |
---|
413 | 413 | | |
---|
414 | 414 | | |
---|
415 | 415 | | |
---|
416 | 416 | | @Callable(i) |
---|
417 | 417 | | func confirmManager () = { |
---|
418 | 418 | | let pm = pendingManagerPublicKeyOrUnit() |
---|
419 | 419 | | let hasPM = if (isDefined(pm)) |
---|
420 | 420 | | then true |
---|
421 | 421 | | else throwErr("no pending manager") |
---|
422 | 422 | | if ((hasPM == hasPM)) |
---|
423 | 423 | | then { |
---|
424 | 424 | | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
425 | 425 | | then true |
---|
426 | 426 | | else throwErr("you are not pending manager") |
---|
427 | 427 | | if ((checkPM == checkPM)) |
---|
428 | 428 | | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] |
---|
429 | 429 | | else throw("Strict value is not equal to itself.") |
---|
430 | 430 | | } |
---|
431 | 431 | | else throw("Strict value is not equal to itself.") |
---|
432 | 432 | | } |
---|
433 | 433 | | |
---|
434 | 434 | | |
---|
435 | 435 | | @Verifier(tx) |
---|
436 | 436 | | func verify () = { |
---|
437 | 437 | | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
438 | 438 | | case pk: ByteVector => |
---|
439 | 439 | | pk |
---|
440 | 440 | | case _: Unit => |
---|
441 | 441 | | tx.senderPublicKey |
---|
442 | 442 | | case _ => |
---|
443 | 443 | | throw("Match error") |
---|
444 | 444 | | } |
---|
445 | 445 | | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
446 | 446 | | } |
---|
447 | 447 | | |
---|