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