1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let lPdecimals = 8 |
---|
| 5 | + | |
---|
| 6 | + | let scale8 = 100000000 |
---|
| 7 | + | |
---|
| 8 | + | let scale8BigInt = toBigInt(100000000) |
---|
| 9 | + | |
---|
| 10 | + | let scale18 = toBigInt(1000000000000000000) |
---|
| 11 | + | |
---|
| 12 | + | let zeroBigInt = toBigInt(0) |
---|
| 13 | + | |
---|
| 14 | + | let big0 = toBigInt(0) |
---|
| 15 | + | |
---|
| 16 | + | let big1 = toBigInt(1) |
---|
| 17 | + | |
---|
| 18 | + | let big2 = toBigInt(2) |
---|
| 19 | + | |
---|
| 20 | + | let wavesString = "WAVES" |
---|
| 21 | + | |
---|
| 22 | + | let SEP = "__" |
---|
| 23 | + | |
---|
| 24 | + | let PoolActive = 1 |
---|
| 25 | + | |
---|
| 26 | + | let PoolPutDisabled = 2 |
---|
| 27 | + | |
---|
| 28 | + | let PoolMatcherDisabled = 3 |
---|
| 29 | + | |
---|
| 30 | + | let PoolShutdown = 4 |
---|
| 31 | + | |
---|
| 32 | + | let idxPoolAddress = 1 |
---|
| 33 | + | |
---|
| 34 | + | let idxPoolStatus = 2 |
---|
| 35 | + | |
---|
| 36 | + | let idxPoolLPAssetId = 3 |
---|
| 37 | + | |
---|
| 38 | + | let idxAmtAssetId = 4 |
---|
| 39 | + | |
---|
| 40 | + | let idxPriceAssetId = 5 |
---|
| 41 | + | |
---|
| 42 | + | let idxAmtAssetDcm = 6 |
---|
| 43 | + | |
---|
| 44 | + | let idxPriceAssetDcm = 7 |
---|
| 45 | + | |
---|
| 46 | + | let idxIAmtAssetId = 8 |
---|
| 47 | + | |
---|
| 48 | + | let idxIPriceAssetId = 9 |
---|
| 49 | + | |
---|
| 50 | + | let idxLPAssetDcm = 10 |
---|
| 51 | + | |
---|
| 52 | + | let idxPoolAmtAssetAmt = 1 |
---|
| 53 | + | |
---|
| 54 | + | let idxPoolPriceAssetAmt = 2 |
---|
| 55 | + | |
---|
| 56 | + | let idxPoolLPAssetAmt = 3 |
---|
| 57 | + | |
---|
| 58 | + | let idxFactoryStakingContract = 1 |
---|
| 59 | + | |
---|
| 60 | + | let idxFactorySlippageContract = 7 |
---|
| 61 | + | |
---|
| 62 | + | func toX18 (origVal,origScaleMult) = fraction(toBigInt(origVal), scale18, toBigInt(origScaleMult)) |
---|
| 63 | + | |
---|
| 64 | + | |
---|
| 65 | + | func toX18BigInt (origVal,origScaleMult) = fraction(origVal, scale18, origScaleMult) |
---|
| 66 | + | |
---|
| 67 | + | |
---|
| 68 | + | func fromX18 (val,resultScaleMult) = toInt(fraction(val, toBigInt(resultScaleMult), scale18)) |
---|
| 69 | + | |
---|
| 70 | + | |
---|
| 71 | + | func fromX18Round (val,resultScaleMult,round) = toInt(fraction(val, toBigInt(resultScaleMult), scale18, round)) |
---|
| 72 | + | |
---|
| 73 | + | |
---|
| 74 | + | func toScale (amt,resScale,curScale) = fraction(amt, resScale, curScale) |
---|
| 75 | + | |
---|
| 76 | + | |
---|
| 77 | + | func abs (val) = if ((0 > val)) |
---|
| 78 | + | then -(val) |
---|
| 79 | + | else val |
---|
| 80 | + | |
---|
| 81 | + | |
---|
| 82 | + | func absBigInt (val) = if ((zeroBigInt > val)) |
---|
| 83 | + | then -(val) |
---|
| 84 | + | else val |
---|
| 85 | + | |
---|
| 86 | + | |
---|
| 87 | + | func swapContract () = "%s__swapContract" |
---|
| 88 | + | |
---|
| 89 | + | |
---|
| 90 | + | func fc () = "%s__factoryContract" |
---|
| 91 | + | |
---|
| 92 | + | |
---|
| 93 | + | func mpk () = "%s__managerPublicKey" |
---|
| 94 | + | |
---|
| 95 | + | |
---|
| 96 | + | func pmpk () = "%s__pendingManagerPublicKey" |
---|
| 97 | + | |
---|
| 98 | + | |
---|
| 99 | + | func pl () = "%s%s__price__last" |
---|
| 100 | + | |
---|
| 101 | + | |
---|
| 102 | + | func ph (h,timestamp) = makeString(["%s%s%d%d__price__history", toString(h), toString(timestamp)], SEP) |
---|
| 103 | + | |
---|
| 104 | + | |
---|
| 105 | + | func pau (userAddress,txId) = ((("%s%s%s__P__" + userAddress) + "__") + txId) |
---|
| 106 | + | |
---|
| 107 | + | |
---|
| 108 | + | func gau (userAddress,txId) = ((("%s%s%s__G__" + userAddress) + "__") + txId) |
---|
| 109 | + | |
---|
| 110 | + | |
---|
| 111 | + | func aa () = "%s__amountAsset" |
---|
| 112 | + | |
---|
| 113 | + | |
---|
| 114 | + | func pa () = "%s__priceAsset" |
---|
| 115 | + | |
---|
| 116 | + | |
---|
| 117 | + | let keyFee = "%s__fee" |
---|
| 118 | + | |
---|
| 119 | + | let feeDefault = fraction(10, scale8, 10000) |
---|
| 120 | + | |
---|
| 121 | + | let fee = valueOrElse(getInteger(this, keyFee), feeDefault) |
---|
| 122 | + | |
---|
| 123 | + | let keyKLp = makeString(["%s", "kLp"], SEP) |
---|
| 124 | + | |
---|
| 125 | + | let keyKLpRefreshedHeight = makeString(["%s", "kLpRefreshedHeight"], SEP) |
---|
| 126 | + | |
---|
| 127 | + | let keyKLpRefreshDelay = makeString(["%s", "refreshKLpDelay"], SEP) |
---|
| 128 | + | |
---|
| 129 | + | let kLpRefreshDelayDefault = 30 |
---|
| 130 | + | |
---|
| 131 | + | let kLpRefreshDelay = valueOrElse(getInteger(this, keyKLpRefreshDelay), kLpRefreshDelayDefault) |
---|
| 132 | + | |
---|
| 133 | + | func keyAdditionalBalance (assetId) = makeString(["%s%s", "stakedBalance", assetId], SEP) |
---|
| 134 | + | |
---|
| 135 | + | |
---|
| 136 | + | func keyStakingAssetBalance (assetId) = makeString(["%s%s", "shareAssetBalance", assetId], SEP) |
---|
| 137 | + | |
---|
| 138 | + | |
---|
| 139 | + | func getAdditionalBalanceOrZero (assetId) = valueOrElse(getInteger(this, keyAdditionalBalance(assetId)), 0) |
---|
| 140 | + | |
---|
| 141 | + | |
---|
| 142 | + | func getStakingAssetBalanceOrZero (assetId) = valueOrElse(getInteger(this, keyStakingAssetBalance(assetId)), 0) |
---|
| 143 | + | |
---|
| 144 | + | |
---|
| 145 | + | func keyFactoryConfig () = "%s__factoryConfig" |
---|
| 146 | + | |
---|
| 147 | + | |
---|
| 148 | + | func keyMatcherPub () = "%s%s__matcher__publicKey" |
---|
| 149 | + | |
---|
| 150 | + | |
---|
| 151 | + | func keyMappingPoolContractAddressToPoolAssets (poolContractAddress) = (("%s%s%s__" + poolContractAddress) + "__mappings__poolContract2LpAsset") |
---|
| 152 | + | |
---|
| 153 | + | |
---|
| 154 | + | func keyPoolConfig (iAmtAsset,iPriceAsset) = (((("%d%d%s__" + iAmtAsset) + "__") + iPriceAsset) + "__config") |
---|
| 155 | + | |
---|
| 156 | + | |
---|
| 157 | + | func keyMappingsBaseAsset2internalId (baseAssetStr) = ("%s%s%s__mappings__baseAsset2internalId__" + baseAssetStr) |
---|
| 158 | + | |
---|
| 159 | + | |
---|
| 160 | + | func keyAllPoolsShutdown () = "%s__shutdown" |
---|
| 161 | + | |
---|
| 162 | + | |
---|
| 163 | + | func keyPoolWeight (contractAddress) = ("%s%s__poolWeight__" + contractAddress) |
---|
| 164 | + | |
---|
| 165 | + | |
---|
| 166 | + | func keyAllowedLpScriptHash () = "%s__allowedLpScriptHash" |
---|
| 167 | + | |
---|
| 168 | + | |
---|
| 169 | + | let keyFeeCollectorAddress = "%s__feeCollectorAddress" |
---|
| 170 | + | |
---|
| 171 | + | func keySkipOrderValidation (poolAddress) = ("%s%s__skipOrderValidation__" + poolAddress) |
---|
| 172 | + | |
---|
| 173 | + | |
---|
| 174 | + | func throwOrderError (orderValid,orderValidInfo,senderValid,matcherValid) = throw((((((((("order validation failed: orderValid=" + toString(orderValid)) + " (") + orderValidInfo) + ")") + " senderValid=") + toString(senderValid)) + " matcherValid=") + toString(matcherValid))) |
---|
| 175 | + | |
---|
| 176 | + | |
---|
| 177 | + | func getStringOrFail (address,key) = valueOrErrorMessage(getString(address, key), makeString(["mandatory ", toString(address), ".", key, " is not defined"], "")) |
---|
| 178 | + | |
---|
| 179 | + | |
---|
| 180 | + | func getIntOrFail (address,key) = valueOrErrorMessage(getInteger(address, key), makeString(["mandatory ", toString(address), ".", key, " is not defined"], "")) |
---|
| 181 | + | |
---|
| 182 | + | |
---|
| 183 | + | func throwErr (msg) = throw(makeString(["lp.ride:", msg], " ")) |
---|
| 184 | + | |
---|
| 185 | + | |
---|
| 186 | + | func fmtErr (msg) = makeString(["lp.ride:", msg], " ") |
---|
| 187 | + | |
---|
| 188 | + | |
---|
| 189 | + | let factoryContract = addressFromStringValue(getStringOrFail(this, fc())) |
---|
| 190 | + | |
---|
| 191 | + | let feeCollectorAddress = addressFromStringValue(getStringOrFail(factoryContract, keyFeeCollectorAddress)) |
---|
| 192 | + | |
---|
| 193 | + | let inFee = { |
---|
| 194 | + | let @ = invoke(factoryContract, "getInFeeREADONLY", [toString(this)], nil) |
---|
| 195 | + | if ($isInstanceOf(@, "Int")) |
---|
| 196 | + | then @ |
---|
| 197 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 198 | + | } |
---|
| 199 | + | |
---|
| 200 | + | let outFee = { |
---|
| 201 | + | let @ = invoke(factoryContract, "getOutFeeREADONLY", [toString(this)], nil) |
---|
| 202 | + | if ($isInstanceOf(@, "Int")) |
---|
| 203 | + | then @ |
---|
| 204 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 205 | + | } |
---|
| 206 | + | |
---|
| 207 | + | func isGlobalShutdown () = valueOrElse(getBoolean(factoryContract, keyAllPoolsShutdown()), false) |
---|
| 208 | + | |
---|
| 209 | + | |
---|
| 210 | + | func getMatcherPubOrFail () = fromBase58String(getStringOrFail(factoryContract, keyMatcherPub())) |
---|
| 211 | + | |
---|
| 212 | + | |
---|
| 213 | + | func getPoolConfig () = { |
---|
| 214 | + | let amtAsset = getStringOrFail(this, aa()) |
---|
| 215 | + | let priceAsset = getStringOrFail(this, pa()) |
---|
| 216 | + | let iPriceAsset = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(priceAsset)) |
---|
| 217 | + | let iAmtAsset = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(amtAsset)) |
---|
| 218 | + | split(getStringOrFail(factoryContract, keyPoolConfig(toString(iAmtAsset), toString(iPriceAsset))), SEP) |
---|
| 219 | + | } |
---|
| 220 | + | |
---|
| 221 | + | |
---|
| 222 | + | func parseAssetId (input) = if ((input == wavesString)) |
---|
| 223 | + | then unit |
---|
| 224 | + | else fromBase58String(input) |
---|
| 225 | + | |
---|
| 226 | + | |
---|
| 227 | + | func assetIdToString (input) = if ((input == unit)) |
---|
| 228 | + | then wavesString |
---|
| 229 | + | else toBase58String(value(input)) |
---|
| 230 | + | |
---|
| 231 | + | |
---|
| 232 | + | func parsePoolConfig (poolConfig) = $Tuple7(addressFromStringValue(poolConfig[idxPoolAddress]), parseIntValue(poolConfig[idxPoolStatus]), fromBase58String(poolConfig[idxPoolLPAssetId]), parseAssetId(poolConfig[idxAmtAssetId]), parseAssetId(poolConfig[idxPriceAssetId]), parseIntValue(poolConfig[idxAmtAssetDcm]), parseIntValue(poolConfig[idxPriceAssetDcm])) |
---|
| 233 | + | |
---|
| 234 | + | |
---|
| 235 | + | let poolConfigParsed = parsePoolConfig(getPoolConfig()) |
---|
| 236 | + | |
---|
| 237 | + | let $t090589224 = poolConfigParsed |
---|
| 238 | + | |
---|
| 239 | + | let cfgPoolAddress = $t090589224._1 |
---|
| 240 | + | |
---|
| 241 | + | let cfgPoolStatus = $t090589224._2 |
---|
| 242 | + | |
---|
| 243 | + | let cfgLpAssetId = $t090589224._3 |
---|
| 244 | + | |
---|
| 245 | + | let cfgAmountAssetId = $t090589224._4 |
---|
| 246 | + | |
---|
| 247 | + | let cfgPriceAssetId = $t090589224._5 |
---|
| 248 | + | |
---|
| 249 | + | let cfgAmountAssetDecimals = $t090589224._6 |
---|
| 250 | + | |
---|
| 251 | + | let cfgPriceAssetDecimals = $t090589224._7 |
---|
| 252 | + | |
---|
| 253 | + | func getFactoryConfig () = split(getStringOrFail(factoryContract, keyFactoryConfig()), SEP) |
---|
| 254 | + | |
---|
| 255 | + | |
---|
| 256 | + | let stakingContract = valueOrErrorMessage(addressFromString(getFactoryConfig()[idxFactoryStakingContract]), "incorrect staking address") |
---|
| 257 | + | |
---|
| 258 | + | let slippageContract = valueOrErrorMessage(addressFromString(getFactoryConfig()[idxFactorySlippageContract]), "incorrect staking address") |
---|
| 259 | + | |
---|
| 260 | + | func dataPutActionInfo (inAmtAssetAmt,inPriceAssetAmt,outLpAmt,price,slippageTolerancePassedByUser,slippageToleranceReal,txHeight,txTimestamp,slipageAmtAssetAmt,slipagePriceAssetAmt) = makeString(["%d%d%d%d%d%d%d%d%d%d", toString(inAmtAssetAmt), toString(inPriceAssetAmt), toString(outLpAmt), toString(price), toString(slippageTolerancePassedByUser), toString(slippageToleranceReal), toString(txHeight), toString(txTimestamp), toString(slipageAmtAssetAmt), toString(slipagePriceAssetAmt)], SEP) |
---|
| 261 | + | |
---|
| 262 | + | |
---|
| 263 | + | func dataGetActionInfo (outAmtAssetAmt,outPriceAssetAmt,inLpAmt,price,txHeight,txTimestamp) = makeString(["%d%d%d%d%d%d", toString(outAmtAssetAmt), toString(outPriceAssetAmt), toString(inLpAmt), toString(price), toString(txHeight), toString(txTimestamp)], SEP) |
---|
| 264 | + | |
---|
| 265 | + | |
---|
| 266 | + | func getAccBalance (assetId) = { |
---|
| 267 | + | let balanceOnPool = if ((assetId == "WAVES")) |
---|
| 268 | + | then wavesBalance(this).available |
---|
| 269 | + | else assetBalance(this, fromBase58String(assetId)) |
---|
| 270 | + | let totalBalance = ((balanceOnPool + getAdditionalBalanceOrZero(assetId)) - getStakingAssetBalanceOrZero(assetId)) |
---|
| 271 | + | max([0, totalBalance]) |
---|
| 272 | + | } |
---|
| 273 | + | |
---|
| 274 | + | |
---|
| 275 | + | func calcPriceBigInt (prAmtX18,amAmtX18) = fraction(prAmtX18, scale18, amAmtX18) |
---|
| 276 | + | |
---|
| 277 | + | |
---|
| 278 | + | func calcPriceBigIntRound (prAmtX18,amAmtX18,round) = fraction(prAmtX18, scale18, amAmtX18, round) |
---|
| 279 | + | |
---|
| 280 | + | |
---|
| 281 | + | func getRate (proxy) = { |
---|
| 282 | + | let inv = invoke(proxy, "getRate", nil, nil) |
---|
| 283 | + | if ((inv == inv)) |
---|
| 284 | + | then match inv { |
---|
| 285 | + | case r: Int => |
---|
| 286 | + | r |
---|
| 287 | + | case _ => |
---|
| 288 | + | throwErr("proxy.getRate() unexpected value") |
---|
| 289 | + | } |
---|
| 290 | + | else throw("Strict value is not equal to itself.") |
---|
| 291 | + | } |
---|
| 292 | + | |
---|
| 293 | + | |
---|
| 294 | + | func deposit (assetId,amount,stakingAssetId,proxy) = { |
---|
| 295 | + | let currentAdditionalBalance = getAdditionalBalanceOrZero(assetId) |
---|
| 296 | + | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
| 297 | + | then { |
---|
| 298 | + | let currentStakingAssetBalance = getStakingAssetBalanceOrZero(stakingAssetId) |
---|
| 299 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
| 300 | + | then { |
---|
| 301 | + | let asset = parseAssetId(assetId) |
---|
| 302 | + | if ((amount > 0)) |
---|
| 303 | + | then { |
---|
| 304 | + | let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(asset, amount)]) |
---|
| 305 | + | if ((depositInvoke == depositInvoke)) |
---|
| 306 | + | then match depositInvoke { |
---|
| 307 | + | case receivedStakingAsset: Int => |
---|
| 308 | + | let newAdditionalBalance = (currentAdditionalBalance + amount) |
---|
| 309 | + | let newStakingAssetBalance = (currentStakingAssetBalance + receivedStakingAsset) |
---|
| 310 | + | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newStakingAssetBalance)] |
---|
| 311 | + | case _ => |
---|
| 312 | + | nil |
---|
| 313 | + | } |
---|
| 314 | + | else throw("Strict value is not equal to itself.") |
---|
| 315 | + | } |
---|
| 316 | + | else nil |
---|
| 317 | + | } |
---|
| 318 | + | else throw("Strict value is not equal to itself.") |
---|
| 319 | + | } |
---|
| 320 | + | else throw("Strict value is not equal to itself.") |
---|
| 321 | + | } |
---|
| 322 | + | |
---|
| 323 | + | |
---|
| 324 | + | func withdraw (assetId,amount,stakingAssetId,proxy,proxyRateMul,profitAddress) = { |
---|
| 325 | + | let currentAdditionalBalance = getAdditionalBalanceOrZero(assetId) |
---|
| 326 | + | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
| 327 | + | then { |
---|
| 328 | + | let currentStakingAssetBalance = getStakingAssetBalanceOrZero(stakingAssetId) |
---|
| 329 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
| 330 | + | then { |
---|
| 331 | + | let currentProxyRate = getRate(proxy) |
---|
| 332 | + | if ((currentProxyRate == currentProxyRate)) |
---|
| 333 | + | then { |
---|
| 334 | + | let oldRate = fraction(proxyRateMul, currentAdditionalBalance, currentStakingAssetBalance) |
---|
| 335 | + | let stakingAsset = parseAssetId(stakingAssetId) |
---|
| 336 | + | let oldSendStakingAmount = fraction(proxyRateMul, amount, oldRate) |
---|
| 337 | + | let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate) |
---|
| 338 | + | let profitAmount = max([0, (oldSendStakingAmount - sendStakingAssetAmount)]) |
---|
| 339 | + | if ((sendStakingAssetAmount > 0)) |
---|
| 340 | + | then { |
---|
| 341 | + | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(stakingAsset, sendStakingAssetAmount)]) |
---|
| 342 | + | if ((withdrawInvoke == withdrawInvoke)) |
---|
| 343 | + | then match withdrawInvoke { |
---|
| 344 | + | case receivedAssets: Int => |
---|
| 345 | + | let newAdditionalBalance = (currentAdditionalBalance - receivedAssets) |
---|
| 346 | + | let newStakingAssetBalance = ((currentStakingAssetBalance - sendStakingAssetAmount) - profitAmount) |
---|
| 347 | + | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newStakingAssetBalance), ScriptTransfer(profitAddress, profitAmount, parseAssetId(stakingAssetId))] |
---|
| 348 | + | case _ => |
---|
| 349 | + | nil |
---|
| 350 | + | } |
---|
| 351 | + | else throw("Strict value is not equal to itself.") |
---|
| 352 | + | } |
---|
| 353 | + | else nil |
---|
| 354 | + | } |
---|
| 355 | + | else throw("Strict value is not equal to itself.") |
---|
| 356 | + | } |
---|
| 357 | + | else throw("Strict value is not equal to itself.") |
---|
| 358 | + | } |
---|
| 359 | + | else throw("Strict value is not equal to itself.") |
---|
| 360 | + | } |
---|
| 361 | + | |
---|
| 362 | + | |
---|
| 363 | + | func getLeaseProxyConfig (assetId) = match invoke(factoryContract, "getPoolLeaseConfigREADONLY", [toString(this), assetId], nil) { |
---|
| 364 | + | case a: (Boolean, Int, Int, String, String, Int, String) => |
---|
| 365 | + | a |
---|
| 366 | + | case _ => |
---|
| 367 | + | throwErr((("[" + assetId) + "] getLeaseProxyConfig() error")) |
---|
| 368 | + | } |
---|
| 369 | + | |
---|
| 370 | + | |
---|
| 371 | + | func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul,profitAddress) = { |
---|
| 372 | + | let currentAdditionalBalance = getAdditionalBalanceOrZero(assetId) |
---|
| 373 | + | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
| 374 | + | then { |
---|
| 375 | + | let currentStakingAssetBalance = getStakingAssetBalanceOrZero(stakingAssetId) |
---|
| 376 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
| 377 | + | then { |
---|
| 378 | + | let leasableTotalBalance = max([0, (getAccBalance(assetId) - minBalance)]) |
---|
| 379 | + | let targetAdditionalBalance = fraction(targetRatio, leasableTotalBalance, 100) |
---|
| 380 | + | let diff = (currentAdditionalBalance - targetAdditionalBalance) |
---|
| 381 | + | if ((diff == 0)) |
---|
| 382 | + | then nil |
---|
| 383 | + | else if ((0 > diff)) |
---|
| 384 | + | then { |
---|
| 385 | + | let sendAssetAmount = -(diff) |
---|
| 386 | + | deposit(assetId, sendAssetAmount, stakingAssetId, proxy) |
---|
| 387 | + | } |
---|
| 388 | + | else { |
---|
| 389 | + | let getAssetAmount = diff |
---|
| 390 | + | withdraw(assetId, getAssetAmount, stakingAssetId, proxy, proxyRateMul, profitAddress) |
---|
| 391 | + | } |
---|
| 392 | + | } |
---|
| 393 | + | else throw("Strict value is not equal to itself.") |
---|
| 394 | + | } |
---|
| 395 | + | else throw("Strict value is not equal to itself.") |
---|
| 396 | + | } |
---|
| 397 | + | |
---|
| 398 | + | |
---|
| 399 | + | func rebalanceAsset (assetId) = { |
---|
| 400 | + | let $t01546015596 = getLeaseProxyConfig(assetId) |
---|
| 401 | + | let isLeasable = $t01546015596._1 |
---|
| 402 | + | let leasedRatio = $t01546015596._2 |
---|
| 403 | + | let minBalance = $t01546015596._3 |
---|
| 404 | + | let proxyAddress = $t01546015596._4 |
---|
| 405 | + | let proxyAssetId = $t01546015596._5 |
---|
| 406 | + | let proxyRateMul = $t01546015596._6 |
---|
| 407 | + | let stakingProfitAddress = $t01546015596._7 |
---|
| 408 | + | if (isLeasable) |
---|
| 409 | + | then rebalanceInternal(leasedRatio, assetId, proxyAssetId, minBalance, addressFromStringValue(proxyAddress), proxyRateMul, addressFromStringValue(stakingProfitAddress)) |
---|
| 410 | + | else nil |
---|
| 411 | + | } |
---|
| 412 | + | |
---|
| 413 | + | |
---|
| 414 | + | func withdrawAndRebalanceAsset (assetId,getAmount) = { |
---|
| 415 | + | let $t01599516131 = getLeaseProxyConfig(assetId) |
---|
| 416 | + | let isLeasable = $t01599516131._1 |
---|
| 417 | + | let leasedRatio = $t01599516131._2 |
---|
| 418 | + | let minBalance = $t01599516131._3 |
---|
| 419 | + | let proxyAddress = $t01599516131._4 |
---|
| 420 | + | let proxyAssetId = $t01599516131._5 |
---|
| 421 | + | let proxyRateMul = $t01599516131._6 |
---|
| 422 | + | let stakingProfitAddress = $t01599516131._7 |
---|
| 423 | + | if (isLeasable) |
---|
| 424 | + | then { |
---|
| 425 | + | let newTotalLeasableBalance = max([0, ((getAccBalance(assetId) - getAmount) - minBalance)]) |
---|
| 426 | + | if ((newTotalLeasableBalance == newTotalLeasableBalance)) |
---|
| 427 | + | then { |
---|
| 428 | + | let newAdditionalBalance = fraction(leasedRatio, newTotalLeasableBalance, 100) |
---|
| 429 | + | if ((newAdditionalBalance == newAdditionalBalance)) |
---|
| 430 | + | then { |
---|
| 431 | + | let withdrawAmount = (getAdditionalBalanceOrZero(assetId) - newAdditionalBalance) |
---|
| 432 | + | if ((withdrawAmount == withdrawAmount)) |
---|
| 433 | + | then if ((0 > withdrawAmount)) |
---|
| 434 | + | then deposit(assetId, -(withdrawAmount), proxyAssetId, addressFromStringValue(proxyAddress)) |
---|
| 435 | + | else withdraw(assetId, withdrawAmount, proxyAssetId, addressFromStringValue(proxyAddress), proxyRateMul, addressFromStringValue(stakingProfitAddress)) |
---|
| 436 | + | else throw("Strict value is not equal to itself.") |
---|
| 437 | + | } |
---|
| 438 | + | else throw("Strict value is not equal to itself.") |
---|
| 439 | + | } |
---|
| 440 | + | else throw("Strict value is not equal to itself.") |
---|
| 441 | + | } |
---|
| 442 | + | else nil |
---|
| 443 | + | } |
---|
| 444 | + | |
---|
| 445 | + | |
---|
| 446 | + | func withdrawAndRebalanceAll (amountAssetOutAmount,priceAssetOutAmount) = { |
---|
| 447 | + | let AmAmtWithdrawState = withdrawAndRebalanceAsset(getStringOrFail(this, aa()), amountAssetOutAmount) |
---|
| 448 | + | let PrAmtWithdrawState = withdrawAndRebalanceAsset(getStringOrFail(this, pa()), priceAssetOutAmount) |
---|
| 449 | + | (AmAmtWithdrawState ++ PrAmtWithdrawState) |
---|
| 450 | + | } |
---|
| 451 | + | |
---|
| 452 | + | |
---|
| 453 | + | func privateCalcPrice (amAssetDcm,prAssetDcm,amAmt,prAmt) = { |
---|
| 454 | + | let amtAssetAmtX18 = toX18(amAmt, amAssetDcm) |
---|
| 455 | + | let priceAssetAmtX18 = toX18(prAmt, prAssetDcm) |
---|
| 456 | + | calcPriceBigInt(priceAssetAmtX18, amtAssetAmtX18) |
---|
| 457 | + | } |
---|
| 458 | + | |
---|
| 459 | + | |
---|
| 460 | + | func calcPrices (amAmt,prAmt,lpAmt) = { |
---|
| 461 | + | let cfg = getPoolConfig() |
---|
| 462 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 463 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 464 | + | let priceX18 = privateCalcPrice(amtAssetDcm, priceAssetDcm, amAmt, prAmt) |
---|
| 465 | + | let amAmtX18 = toX18(amAmt, amtAssetDcm) |
---|
| 466 | + | let prAmtX18 = toX18(prAmt, priceAssetDcm) |
---|
| 467 | + | let lpAmtX18 = toX18(lpAmt, scale8) |
---|
| 468 | + | let lpPriceInAmAssetX18 = calcPriceBigInt(amAmtX18, lpAmtX18) |
---|
| 469 | + | let lpPriceInPrAssetX18 = calcPriceBigInt(prAmtX18, lpAmtX18) |
---|
| 470 | + | [priceX18, lpPriceInAmAssetX18, lpPriceInPrAssetX18] |
---|
| 471 | + | } |
---|
| 472 | + | |
---|
| 473 | + | |
---|
| 474 | + | func calculatePrices (amAmt,prAmt,lpAmt) = { |
---|
| 475 | + | let prices = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 476 | + | [fromX18(prices[0], scale8), fromX18(prices[1], scale8), fromX18(prices[2], scale8)] |
---|
| 477 | + | } |
---|
| 478 | + | |
---|
| 479 | + | |
---|
| 480 | + | func estimateGetOperation (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 481 | + | let cfg = getPoolConfig() |
---|
| 482 | + | let lpAssetId = cfg[idxPoolLPAssetId] |
---|
| 483 | + | let amAssetId = cfg[idxAmtAssetId] |
---|
| 484 | + | let prAssetId = cfg[idxPriceAssetId] |
---|
| 485 | + | let amAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 486 | + | let prAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 487 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 488 | + | let lpEmission = valueOrErrorMessage(assetInfo(fromBase58String(lpAssetId)), (("Asset " + lpAssetId) + " doesn't exist")).quantity |
---|
| 489 | + | if ((lpAssetId != pmtAssetId)) |
---|
| 490 | + | then throw("Invalid asset passed.") |
---|
| 491 | + | else { |
---|
| 492 | + | let amBalance = getAccBalance(amAssetId) |
---|
| 493 | + | let amBalanceX18 = toX18(amBalance, amAssetDcm) |
---|
| 494 | + | let prBalance = getAccBalance(prAssetId) |
---|
| 495 | + | let prBalanceX18 = toX18(prBalance, prAssetDcm) |
---|
| 496 | + | let curPriceX18 = calcPriceBigInt(prBalanceX18, amBalanceX18) |
---|
| 497 | + | let curPrice = fromX18(curPriceX18, scale8) |
---|
| 498 | + | let pmtLpAmtX18 = toX18(pmtLpAmt, scale8) |
---|
| 499 | + | let lpEmissionX18 = toX18(lpEmission, scale8) |
---|
| 500 | + | let outAmAmtX18 = fraction(amBalanceX18, pmtLpAmtX18, lpEmissionX18) |
---|
| 501 | + | let outPrAmtX18 = fraction(prBalanceX18, pmtLpAmtX18, lpEmissionX18) |
---|
| 502 | + | let outAmAmt = fromX18Round(outAmAmtX18, amAssetDcm, FLOOR) |
---|
| 503 | + | let outPrAmt = fromX18Round(outPrAmtX18, prAssetDcm, FLOOR) |
---|
| 504 | + | let state = if ((txId58 == "")) |
---|
| 505 | + | then nil |
---|
| 506 | + | else [ScriptTransfer(userAddress, outAmAmt, if ((amAssetId == "WAVES")) |
---|
| 507 | + | then unit |
---|
| 508 | + | else fromBase58String(amAssetId)), ScriptTransfer(userAddress, outPrAmt, if ((prAssetId == "WAVES")) |
---|
| 509 | + | then unit |
---|
| 510 | + | else fromBase58String(prAssetId)), StringEntry(gau(toString(userAddress), txId58), dataGetActionInfo(outAmAmt, outPrAmt, pmtLpAmt, curPrice, height, lastBlock.timestamp)), IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice)] |
---|
| 511 | + | $Tuple10(outAmAmt, outPrAmt, amAssetId, prAssetId, amBalance, prBalance, lpEmission, curPriceX18, poolStatus, state) |
---|
| 512 | + | } |
---|
| 513 | + | } |
---|
| 514 | + | |
---|
| 515 | + | |
---|
| 516 | + | func estimatePutOperation (txId58,slippageTolerance,inAmAssetAmt,inAmAssetId,inPrAssetAmt,inPrAssetId,userAddress,isEvaluate,emitLp) = { |
---|
| 517 | + | let cfg = getPoolConfig() |
---|
| 518 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 519 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 520 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 521 | + | let iAmtAssetId = cfg[idxIAmtAssetId] |
---|
| 522 | + | let iPriceAssetId = cfg[idxIPriceAssetId] |
---|
| 523 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 524 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 525 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 526 | + | let lpEmission = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 527 | + | let inAmAssetIdStr = toBase58String(valueOrElse(inAmAssetId, fromBase58String("WAVES"))) |
---|
| 528 | + | let inPrAssetIdStr = toBase58String(valueOrElse(inPrAssetId, fromBase58String("WAVES"))) |
---|
| 529 | + | if (if ((amAssetIdStr != inAmAssetIdStr)) |
---|
| 530 | + | then true |
---|
| 531 | + | else (prAssetIdStr != inPrAssetIdStr)) |
---|
| 532 | + | then throw("Invalid amt or price asset passed.") |
---|
| 533 | + | else { |
---|
| 534 | + | let amBalance = if (isEvaluate) |
---|
| 535 | + | then getAccBalance(amAssetIdStr) |
---|
| 536 | + | else (getAccBalance(amAssetIdStr) - inAmAssetAmt) |
---|
| 537 | + | let prBalance = if (isEvaluate) |
---|
| 538 | + | then getAccBalance(prAssetIdStr) |
---|
| 539 | + | else (getAccBalance(prAssetIdStr) - inPrAssetAmt) |
---|
| 540 | + | let inAmAssetAmtX18 = toX18(inAmAssetAmt, amtAssetDcm) |
---|
| 541 | + | let inPrAssetAmtX18 = toX18(inPrAssetAmt, priceAssetDcm) |
---|
| 542 | + | let userPriceX18 = calcPriceBigInt(inPrAssetAmtX18, inAmAssetAmtX18) |
---|
| 543 | + | let amBalanceX18 = toX18(amBalance, amtAssetDcm) |
---|
| 544 | + | let prBalanceX18 = toX18(prBalance, priceAssetDcm) |
---|
| 545 | + | let res = if ((lpEmission == 0)) |
---|
| 546 | + | then { |
---|
| 547 | + | let curPriceX18 = zeroBigInt |
---|
| 548 | + | let slippageX18 = zeroBigInt |
---|
| 549 | + | let lpAmtX18 = pow((inAmAssetAmtX18 * inPrAssetAmtX18), 0, toBigInt(5), 1, 0, DOWN) |
---|
| 550 | + | $Tuple5(fromX18(lpAmtX18, scale8), fromX18(inAmAssetAmtX18, amtAssetDcm), fromX18(inPrAssetAmtX18, priceAssetDcm), calcPriceBigInt((prBalanceX18 + inPrAssetAmtX18), (amBalanceX18 + inAmAssetAmtX18)), slippageX18) |
---|
| 551 | + | } |
---|
| 552 | + | else { |
---|
| 553 | + | let curPriceX18 = calcPriceBigInt(prBalanceX18, amBalanceX18) |
---|
| 554 | + | let slippageX18 = fraction(absBigInt((curPriceX18 - userPriceX18)), scale18, curPriceX18) |
---|
| 555 | + | let slippageToleranceX18 = toX18(slippageTolerance, scale8) |
---|
| 556 | + | if (if ((curPriceX18 != zeroBigInt)) |
---|
| 557 | + | then (slippageX18 > slippageToleranceX18) |
---|
| 558 | + | else false) |
---|
| 559 | + | then throw(((("Price slippage " + toString(slippageX18)) + " exceeded the passed limit of ") + toString(slippageToleranceX18))) |
---|
| 560 | + | else { |
---|
| 561 | + | let lpEmissionX18 = toX18(lpEmission, scale8) |
---|
| 562 | + | let prViaAmX18 = fraction(inAmAssetAmtX18, calcPriceBigIntRound(prBalanceX18, amBalanceX18, CEILING), scale18, CEILING) |
---|
| 563 | + | let amViaPrX18 = fraction(inPrAssetAmtX18, scale18, calcPriceBigIntRound(prBalanceX18, amBalanceX18, FLOOR), CEILING) |
---|
| 564 | + | let expectedAmts = if ((prViaAmX18 > inPrAssetAmtX18)) |
---|
| 565 | + | then $Tuple2(amViaPrX18, inPrAssetAmtX18) |
---|
| 566 | + | else $Tuple2(inAmAssetAmtX18, prViaAmX18) |
---|
| 567 | + | let expAmtAssetAmtX18 = expectedAmts._1 |
---|
| 568 | + | let expPriceAssetAmtX18 = expectedAmts._2 |
---|
| 569 | + | let lpAmtX18 = fraction(lpEmissionX18, expPriceAssetAmtX18, prBalanceX18, FLOOR) |
---|
| 570 | + | $Tuple5(fromX18Round(lpAmtX18, scale8, FLOOR), fromX18Round(expAmtAssetAmtX18, amtAssetDcm, CEILING), fromX18Round(expPriceAssetAmtX18, priceAssetDcm, CEILING), curPriceX18, slippageX18) |
---|
| 571 | + | } |
---|
| 572 | + | } |
---|
| 573 | + | let calcLpAmt = res._1 |
---|
| 574 | + | let calcAmAssetPmt = res._2 |
---|
| 575 | + | let calcPrAssetPmt = res._3 |
---|
| 576 | + | let curPrice = fromX18(res._4, scale8) |
---|
| 577 | + | let slippageCalc = fromX18(res._5, scale8) |
---|
| 578 | + | if ((0 >= calcLpAmt)) |
---|
| 579 | + | then throw("Invalid calculations. LP calculated is less than zero.") |
---|
| 580 | + | else { |
---|
| 581 | + | let emitLpAmt = if (!(emitLp)) |
---|
| 582 | + | then 0 |
---|
| 583 | + | else calcLpAmt |
---|
| 584 | + | let amDiff = (inAmAssetAmt - calcAmAssetPmt) |
---|
| 585 | + | let prDiff = (inPrAssetAmt - calcPrAssetPmt) |
---|
| 586 | + | let commonState = [IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice), StringEntry(pau(userAddress, txId58), dataPutActionInfo(calcAmAssetPmt, calcPrAssetPmt, emitLpAmt, curPrice, slippageTolerance, slippageCalc, height, lastBlock.timestamp, amDiff, prDiff))] |
---|
| 587 | + | $Tuple13(calcLpAmt, emitLpAmt, curPrice, amBalance, prBalance, lpEmission, lpAssetId, poolStatus, commonState, amDiff, prDiff, inAmAssetId, inPrAssetId) |
---|
| 588 | + | } |
---|
| 589 | + | } |
---|
| 590 | + | } |
---|
| 591 | + | |
---|
| 592 | + | |
---|
| 593 | + | func calcKLp (amountBalance,priceBalance,lpEmission) = { |
---|
| 594 | + | let amountBalanceX18 = toX18BigInt(amountBalance, toBigInt(cfgAmountAssetDecimals)) |
---|
| 595 | + | let priceBalanceX18 = toX18BigInt(priceBalance, toBigInt(cfgPriceAssetDecimals)) |
---|
| 596 | + | let updatedKLp = fraction(pow((amountBalanceX18 * priceBalanceX18), 0, toBigInt(5), 1, 18, DOWN), big1, lpEmission) |
---|
| 597 | + | if ((lpEmission == big0)) |
---|
| 598 | + | then big0 |
---|
| 599 | + | else updatedKLp |
---|
| 600 | + | } |
---|
| 601 | + | |
---|
| 602 | + | |
---|
| 603 | + | func calcCurrentKLp (amountAssetDelta,priceAssetDelta,lpAssetEmissionDelta) = { |
---|
| 604 | + | let amountAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgAmountAssetId))) - amountAssetDelta) |
---|
| 605 | + | let priceAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgPriceAssetId))) - priceAssetDelta) |
---|
| 606 | + | let lpAssetEmission = (toBigInt(value(assetInfo(cfgLpAssetId)).quantity) - lpAssetEmissionDelta) |
---|
| 607 | + | let currentKLp = calcKLp(amountAssetBalance, priceAssetBalance, lpAssetEmission) |
---|
| 608 | + | currentKLp |
---|
| 609 | + | } |
---|
| 610 | + | |
---|
| 611 | + | |
---|
| 612 | + | func refreshKLpInternal (amountAssetBalanceDelta,priceAssetBalanceDelta,lpAssetEmissionDelta) = { |
---|
| 613 | + | let amountAssetBalance = (getAccBalance(assetIdToString(cfgAmountAssetId)) + amountAssetBalanceDelta) |
---|
| 614 | + | let priceAssetBalance = (getAccBalance(assetIdToString(cfgPriceAssetId)) + priceAssetBalanceDelta) |
---|
| 615 | + | let lpAssetEmission = (value(assetInfo(cfgLpAssetId)).quantity + lpAssetEmissionDelta) |
---|
| 616 | + | let updatedKLp = calcKLp(toBigInt(amountAssetBalance), toBigInt(priceAssetBalance), toBigInt(lpAssetEmission)) |
---|
| 617 | + | let actions = [IntegerEntry(keyKLpRefreshedHeight, height), StringEntry(keyKLp, toString(updatedKLp))] |
---|
| 618 | + | $Tuple2(actions, updatedKLp) |
---|
| 619 | + | } |
---|
| 620 | + | |
---|
| 621 | + | |
---|
| 622 | + | func skipOrderValidation () = valueOrElse(getBoolean(factoryContract, keySkipOrderValidation(toString(this))), false) |
---|
| 623 | + | |
---|
| 624 | + | |
---|
| 625 | + | func validateUpdatedKLp (oldKLp,updatedKLp) = if ((updatedKLp >= oldKLp)) |
---|
| 626 | + | then true |
---|
| 627 | + | else throwErr(makeString(["updated KLp lower than current KLp", toString(oldKLp), toString(updatedKLp)], " ")) |
---|
| 628 | + | |
---|
| 629 | + | |
---|
| 630 | + | func validateMatcherOrderAllowed (order) = { |
---|
| 631 | + | let amountAssetBalance = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 632 | + | let priceAssetBalance = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 633 | + | let amountAssetAmount = order.amount |
---|
| 634 | + | let priceAssetAmount = fraction(order.amount, order.price, scale8, FLOOR) |
---|
| 635 | + | let $t02852028732 = if ((order.orderType == Buy)) |
---|
| 636 | + | then $Tuple2(amountAssetAmount, -(priceAssetAmount)) |
---|
| 637 | + | else $Tuple2(-(amountAssetAmount), priceAssetAmount) |
---|
| 638 | + | let amountAssetBalanceDelta = $t02852028732._1 |
---|
| 639 | + | let priceAssetBalanceDelta = $t02852028732._2 |
---|
| 640 | + | if (if (if (isGlobalShutdown()) |
---|
| 641 | + | then true |
---|
| 642 | + | else (cfgPoolStatus == PoolMatcherDisabled)) |
---|
| 643 | + | then true |
---|
| 644 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 645 | + | then throw("Exchange operations disabled") |
---|
| 646 | + | else if (if ((order.assetPair.amountAsset != cfgAmountAssetId)) |
---|
| 647 | + | then true |
---|
| 648 | + | else (order.assetPair.priceAsset != cfgPriceAssetId)) |
---|
| 649 | + | then throw("Wrong order assets.") |
---|
| 650 | + | else { |
---|
| 651 | + | let kLp = valueOrErrorMessage(parseBigInt(valueOrElse(getString(this, keyKLp), "0")), fmtErr("invalid kLp")) |
---|
| 652 | + | let $t02917229272 = refreshKLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 653 | + | let unusedActions = $t02917229272._1 |
---|
| 654 | + | let kLpNew = $t02917229272._2 |
---|
| 655 | + | let isOrderValid = (kLpNew >= kLp) |
---|
| 656 | + | let info = makeString(["kLp=", toString(kLp), " kLpNew=", toString(kLpNew), " amountAssetBalance=", toString(amountAssetBalance), " priceAssetBalance=", toString(priceAssetBalance), " amountAssetBalanceDelta=", toString(amountAssetBalanceDelta), " priceAssetBalanceDelta=", toString(priceAssetBalanceDelta), " height=", toString(height)], "") |
---|
| 657 | + | $Tuple2(isOrderValid, info) |
---|
| 658 | + | } |
---|
| 659 | + | } |
---|
| 660 | + | |
---|
| 661 | + | |
---|
| 662 | + | func commonGet (i) = if ((size(i.payments) != 1)) |
---|
| 663 | + | then throw("exactly 1 payment is expected") |
---|
| 664 | + | else { |
---|
| 665 | + | let pmt = value(i.payments[0]) |
---|
| 666 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 667 | + | let pmtAmt = pmt.amount |
---|
| 668 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(pmtAssetId), pmtAmt, i.caller) |
---|
| 669 | + | let outAmAmt = res._1 |
---|
| 670 | + | let outPrAmt = res._2 |
---|
| 671 | + | let poolStatus = parseIntValue(res._9) |
---|
| 672 | + | let state = res._10 |
---|
| 673 | + | if (if (isGlobalShutdown()) |
---|
| 674 | + | then true |
---|
| 675 | + | else (poolStatus == PoolShutdown)) |
---|
| 676 | + | then throw(("Get operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 677 | + | else $Tuple5(outAmAmt, outPrAmt, pmtAmt, pmtAssetId, state) |
---|
| 678 | + | } |
---|
| 679 | + | |
---|
| 680 | + | |
---|
| 681 | + | func commonPut (i,slippageTolerance,emitLp) = if ((size(i.payments) != 2)) |
---|
| 682 | + | then throw("exactly 2 payments are expected") |
---|
| 683 | + | else { |
---|
| 684 | + | let amAssetPmt = value(i.payments[0]) |
---|
| 685 | + | let prAssetPmt = value(i.payments[1]) |
---|
| 686 | + | let estPut = estimatePutOperation(toBase58String(i.transactionId), slippageTolerance, amAssetPmt.amount, amAssetPmt.assetId, prAssetPmt.amount, prAssetPmt.assetId, toString(i.caller), false, emitLp) |
---|
| 687 | + | let poolStatus = parseIntValue(estPut._8) |
---|
| 688 | + | if (if (if (isGlobalShutdown()) |
---|
| 689 | + | then true |
---|
| 690 | + | else (poolStatus == PoolPutDisabled)) |
---|
| 691 | + | then true |
---|
| 692 | + | else (poolStatus == PoolShutdown)) |
---|
| 693 | + | then throw(("Put operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 694 | + | else estPut |
---|
| 695 | + | } |
---|
| 696 | + | |
---|
| 697 | + | |
---|
| 698 | + | func emit (amount) = { |
---|
| 699 | + | let emitInv = invoke(factoryContract, "emit", [amount], nil) |
---|
| 700 | + | if ((emitInv == emitInv)) |
---|
| 701 | + | then { |
---|
| 702 | + | let emitInvLegacy = match emitInv { |
---|
| 703 | + | case legacyFactoryContract: Address => |
---|
| 704 | + | invoke(legacyFactoryContract, "emit", [amount], nil) |
---|
| 705 | + | case _ => |
---|
| 706 | + | unit |
---|
| 707 | + | } |
---|
| 708 | + | if ((emitInvLegacy == emitInvLegacy)) |
---|
| 709 | + | then amount |
---|
| 710 | + | else throw("Strict value is not equal to itself.") |
---|
| 711 | + | } |
---|
| 712 | + | else throw("Strict value is not equal to itself.") |
---|
| 713 | + | } |
---|
| 714 | + | |
---|
| 715 | + | |
---|
| 716 | + | func takeFee (amount,fee) = { |
---|
| 717 | + | let feeAmount = if ((fee == 0)) |
---|
| 718 | + | then 0 |
---|
| 719 | + | else fraction(amount, fee, scale8) |
---|
| 720 | + | $Tuple2((amount - feeAmount), feeAmount) |
---|
| 721 | + | } |
---|
| 722 | + | |
---|
| 723 | + | |
---|
| 724 | + | func calcPutOneToken (paymentAmountRaw,paymentAssetId,userAddress,txId) = { |
---|
| 725 | + | let isEval = (txId == unit) |
---|
| 726 | + | let amountBalanceRaw = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 727 | + | let priceBalanceRaw = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 728 | + | let paymentInAmountAsset = if ((paymentAssetId == cfgAmountAssetId)) |
---|
| 729 | + | then true |
---|
| 730 | + | else if ((paymentAssetId == cfgPriceAssetId)) |
---|
| 731 | + | then false |
---|
| 732 | + | else throwErr("invalid asset") |
---|
| 733 | + | let $t03238532678 = if (isEval) |
---|
| 734 | + | then $Tuple2(amountBalanceRaw, priceBalanceRaw) |
---|
| 735 | + | else if (paymentInAmountAsset) |
---|
| 736 | + | then $Tuple2((amountBalanceRaw - paymentAmountRaw), priceBalanceRaw) |
---|
| 737 | + | else $Tuple2(amountBalanceRaw, (priceBalanceRaw - paymentAmountRaw)) |
---|
| 738 | + | let amountBalanceOld = $t03238532678._1 |
---|
| 739 | + | let priceBalanceOld = $t03238532678._2 |
---|
| 740 | + | let $t03268232831 = if (paymentInAmountAsset) |
---|
| 741 | + | then $Tuple2(paymentAmountRaw, 0) |
---|
| 742 | + | else $Tuple2(0, paymentAmountRaw) |
---|
| 743 | + | let amountAssetAmountRaw = $t03268232831._1 |
---|
| 744 | + | let priceAssetAmountRaw = $t03268232831._2 |
---|
| 745 | + | let amountAssetAmount = takeFee(amountAssetAmountRaw, inFee)._1 |
---|
| 746 | + | let priceAssetAmount = takeFee(priceAssetAmountRaw, inFee)._1 |
---|
| 747 | + | let $t03296333027 = takeFee(paymentAmountRaw, inFee) |
---|
| 748 | + | let paymentAmount = $t03296333027._1 |
---|
| 749 | + | let feeAmount = $t03296333027._2 |
---|
| 750 | + | let amountBalanceNew = (amountBalanceOld + amountAssetAmount) |
---|
| 751 | + | let priceBalanceNew = (priceBalanceOld + priceAssetAmount) |
---|
| 752 | + | let priceNewX18 = calcPriceBigInt(toX18(priceBalanceNew, cfgPriceAssetDecimals), toX18(amountBalanceNew, cfgAmountAssetDecimals)) |
---|
| 753 | + | let priceNew = fromX18(priceNewX18, scale8) |
---|
| 754 | + | let paymentBalance = if (paymentInAmountAsset) |
---|
| 755 | + | then amountBalanceOld |
---|
| 756 | + | else priceBalanceOld |
---|
| 757 | + | let paymentBalanceBigInt = toBigInt(paymentBalance) |
---|
| 758 | + | let supplyBigInt = toBigInt(valueOrErrorMessage(assetInfo(cfgLpAssetId), (("asset " + toBase58String(cfgLpAssetId)) + " doesn't exist")).quantity) |
---|
| 759 | + | let chechSupply = if ((supplyBigInt > big0)) |
---|
| 760 | + | then true |
---|
| 761 | + | else throwErr("initial deposit requires all coins") |
---|
| 762 | + | if ((chechSupply == chechSupply)) |
---|
| 763 | + | then { |
---|
| 764 | + | let depositBigInt = toBigInt(paymentAmount) |
---|
| 765 | + | let issueAmount = max([0, toInt(((supplyBigInt * (sqrtBigInt((scale18 + ((depositBigInt * scale18) / paymentBalanceBigInt)), 18, 18, DOWN) - scale18)) / scale18))]) |
---|
| 766 | + | let commonState = if (isEval) |
---|
| 767 | + | then nil |
---|
| 768 | + | else [IntegerEntry(pl(), priceNew), IntegerEntry(ph(height, lastBlock.timestamp), priceNew), StringEntry(pau(toString(value(userAddress)), toBase58String(value(txId))), dataPutActionInfo(amountAssetAmountRaw, priceAssetAmountRaw, issueAmount, priceNew, 0, 0, height, lastBlock.timestamp, 0, 0))] |
---|
| 769 | + | let priceOldX18 = calcPriceBigInt(toX18(priceBalanceOld, cfgPriceAssetDecimals), toX18(amountBalanceOld, cfgAmountAssetDecimals)) |
---|
| 770 | + | let priceOld = fromX18(priceOldX18, scale8) |
---|
| 771 | + | let loss = { |
---|
| 772 | + | let $t03470834875 = if (paymentInAmountAsset) |
---|
| 773 | + | then $Tuple2(amountAssetAmountRaw, amountBalanceOld) |
---|
| 774 | + | else $Tuple2(priceAssetAmountRaw, priceBalanceOld) |
---|
| 775 | + | let amount = $t03470834875._1 |
---|
| 776 | + | let balance = $t03470834875._2 |
---|
| 777 | + | let issueAmountBoth = toInt(fraction(supplyBigInt, toBigInt((amount / 2)), toBigInt(balance))) |
---|
| 778 | + | fraction((issueAmount - issueAmountBoth), scale8, issueAmountBoth) |
---|
| 779 | + | } |
---|
| 780 | + | $Tuple5(issueAmount, commonState, feeAmount, loss, paymentInAmountAsset) |
---|
| 781 | + | } |
---|
| 782 | + | else throw("Strict value is not equal to itself.") |
---|
| 783 | + | } |
---|
| 784 | + | |
---|
| 785 | + | |
---|
| 786 | + | func calcGetOneToken (outAssetId,paymentAmount,paymentAssetId,userAddress,txId) = { |
---|
| 787 | + | let isEval = (txId == unit) |
---|
| 788 | + | let cfg = getPoolConfig() |
---|
| 789 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 790 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 791 | + | let checks = [if ((paymentAssetId == cfgLpAssetId)) |
---|
| 792 | + | then true |
---|
| 793 | + | else throwErr("invalid lp asset")] |
---|
| 794 | + | if ((checks == checks)) |
---|
| 795 | + | then { |
---|
| 796 | + | let outInAmountAsset = if ((outAssetId == cfgAmountAssetId)) |
---|
| 797 | + | then true |
---|
| 798 | + | else if ((outAssetId == cfgPriceAssetId)) |
---|
| 799 | + | then false |
---|
| 800 | + | else throwErr("invalid asset") |
---|
| 801 | + | let balanceBigInt = if (outInAmountAsset) |
---|
| 802 | + | then toBigInt(getAccBalance(assetIdToString(cfgAmountAssetId))) |
---|
| 803 | + | else toBigInt(getAccBalance(assetIdToString(cfgPriceAssetId))) |
---|
| 804 | + | let outInAmountAssetDecimals = if (outInAmountAsset) |
---|
| 805 | + | then amtAssetDcm |
---|
| 806 | + | else priceAssetDcm |
---|
| 807 | + | let amBalanceOld = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 808 | + | let prBalanceOld = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 809 | + | let outBalance = if (outInAmountAsset) |
---|
| 810 | + | then amBalanceOld |
---|
| 811 | + | else prBalanceOld |
---|
| 812 | + | let outBalanceBigInt = toBigInt(outBalance) |
---|
| 813 | + | let supplyBigInt = toBigInt(valueOrErrorMessage(assetInfo(cfgLpAssetId), (("asset " + toBase58String(cfgLpAssetId)) + " doesn't exist")).quantity) |
---|
| 814 | + | let redeemedBigInt = toBigInt(paymentAmount) |
---|
| 815 | + | let amountRaw = max([0, toInt(((balanceBigInt * (scale18 - pow((scale18 - ((redeemedBigInt * scale18) / supplyBigInt)), 18, big2, 0, 18, DOWN))) / scale18))]) |
---|
| 816 | + | let $t03695337009 = takeFee(amountRaw, outFee) |
---|
| 817 | + | let totalAmount = $t03695337009._1 |
---|
| 818 | + | let feeAmount = $t03695337009._2 |
---|
| 819 | + | let $t03701337239 = if (outInAmountAsset) |
---|
| 820 | + | then $Tuple4(totalAmount, 0, (amBalanceOld - amountRaw), prBalanceOld) |
---|
| 821 | + | else $Tuple4(0, totalAmount, amBalanceOld, (prBalanceOld - amountRaw)) |
---|
| 822 | + | let outAmAmount = $t03701337239._1 |
---|
| 823 | + | let outPrAmount = $t03701337239._2 |
---|
| 824 | + | let amBalanceNew = $t03701337239._3 |
---|
| 825 | + | let prBalanceNew = $t03701337239._4 |
---|
| 826 | + | let priceNewX18 = calcPriceBigInt(toX18(prBalanceNew, cfgPriceAssetDecimals), toX18(amBalanceNew, cfgAmountAssetDecimals)) |
---|
| 827 | + | let priceNew = fromX18(priceNewX18, scale8) |
---|
| 828 | + | let commonState = if (isEval) |
---|
| 829 | + | then nil |
---|
| 830 | + | else [StringEntry(gau(toString(value(userAddress)), toBase58String(value(txId))), dataGetActionInfo(outAmAmount, outPrAmount, paymentAmount, priceNew, height, lastBlock.timestamp)), IntegerEntry(pl(), priceNew), IntegerEntry(ph(height, lastBlock.timestamp), priceNew)] |
---|
| 831 | + | let priceOldX18 = calcPriceBigInt(toX18(prBalanceOld, cfgPriceAssetDecimals), toX18(amBalanceOld, cfgAmountAssetDecimals)) |
---|
| 832 | + | let priceOld = fromX18(priceOldX18, scale8) |
---|
| 833 | + | let loss = { |
---|
| 834 | + | let amountBothInPaymentAsset = (toInt(fraction(balanceBigInt, redeemedBigInt, supplyBigInt)) * 2) |
---|
| 835 | + | fraction((totalAmount - amountBothInPaymentAsset), scale8, amountBothInPaymentAsset) |
---|
| 836 | + | } |
---|
| 837 | + | $Tuple5(totalAmount, commonState, feeAmount, loss, outInAmountAsset) |
---|
| 838 | + | } |
---|
| 839 | + | else throw("Strict value is not equal to itself.") |
---|
| 840 | + | } |
---|
| 841 | + | |
---|
| 842 | + | |
---|
| 843 | + | func managerPublicKeyOrUnit () = match getString(mpk()) { |
---|
| 844 | + | case s: String => |
---|
| 845 | + | fromBase58String(s) |
---|
| 846 | + | case _: Unit => |
---|
| 847 | + | unit |
---|
| 848 | + | case _ => |
---|
| 849 | + | throw("Match error") |
---|
| 850 | + | } |
---|
| 851 | + | |
---|
| 852 | + | |
---|
| 853 | + | func pendingManagerPublicKeyOrUnit () = match getString(pmpk()) { |
---|
| 854 | + | case s: String => |
---|
| 855 | + | fromBase58String(s) |
---|
| 856 | + | case _: Unit => |
---|
| 857 | + | unit |
---|
| 858 | + | case _ => |
---|
| 859 | + | throw("Match error") |
---|
| 860 | + | } |
---|
| 861 | + | |
---|
| 862 | + | |
---|
| 863 | + | func isManager (i) = match managerPublicKeyOrUnit() { |
---|
| 864 | + | case pk: ByteVector => |
---|
| 865 | + | (i.callerPublicKey == pk) |
---|
| 866 | + | case _: Unit => |
---|
| 867 | + | (i.caller == this) |
---|
| 868 | + | case _ => |
---|
| 869 | + | throw("Match error") |
---|
| 870 | + | } |
---|
| 871 | + | |
---|
| 872 | + | |
---|
| 873 | + | func mustManager (i) = { |
---|
| 874 | + | let pd = throw("Permission denied") |
---|
| 875 | + | match managerPublicKeyOrUnit() { |
---|
| 876 | + | case pk: ByteVector => |
---|
| 877 | + | if ((i.callerPublicKey == pk)) |
---|
| 878 | + | then true |
---|
| 879 | + | else pd |
---|
| 880 | + | case _: Unit => |
---|
| 881 | + | if ((i.caller == this)) |
---|
| 882 | + | then true |
---|
| 883 | + | else pd |
---|
| 884 | + | case _ => |
---|
| 885 | + | throw("Match error") |
---|
| 886 | + | } |
---|
| 887 | + | } |
---|
| 888 | + | |
---|
| 889 | + | |
---|
| 890 | + | @Callable(i) |
---|
| 891 | + | func rebalance () = (rebalanceAsset(getStringOrFail(this, aa())) ++ rebalanceAsset(getStringOrFail(this, pa()))) |
---|
| 892 | + | |
---|
| 893 | + | |
---|
| 894 | + | |
---|
| 895 | + | @Callable(i) |
---|
| 896 | + | func calculateAmountOutForSwapREADONLY (cleanAmountIn,isReverse,feePoolAmount) = { |
---|
| 897 | + | let $t03912539430 = if ((isReverse == false)) |
---|
| 898 | + | then { |
---|
| 899 | + | let assetOut = getStringOrFail(this, pa()) |
---|
| 900 | + | let assetIn = getStringOrFail(this, aa()) |
---|
| 901 | + | $Tuple2(assetOut, assetIn) |
---|
| 902 | + | } |
---|
| 903 | + | else { |
---|
| 904 | + | let assetOut = getStringOrFail(this, aa()) |
---|
| 905 | + | let assetIn = getStringOrFail(this, pa()) |
---|
| 906 | + | $Tuple2(assetOut, assetIn) |
---|
| 907 | + | } |
---|
| 908 | + | let assetOut = $t03912539430._1 |
---|
| 909 | + | let assetIn = $t03912539430._2 |
---|
| 910 | + | let poolAssetInBalance = getAccBalance(assetIn) |
---|
| 911 | + | let poolAssetOutBalance = getAccBalance(assetOut) |
---|
| 912 | + | let amountOut = fraction(poolAssetOutBalance, cleanAmountIn, (poolAssetInBalance + cleanAmountIn)) |
---|
| 913 | + | let oldK = (toBigInt(poolAssetInBalance) * toBigInt(poolAssetOutBalance)) |
---|
| 914 | + | let newK = (((toBigInt(getAccBalance(assetIn)) + toBigInt(cleanAmountIn)) + toBigInt(feePoolAmount)) * (toBigInt(getAccBalance(assetOut)) - toBigInt(amountOut))) |
---|
| 915 | + | let checkK = if ((newK >= oldK)) |
---|
| 916 | + | then true |
---|
| 917 | + | else throw("new K is fewer error") |
---|
| 918 | + | if ((checkK == checkK)) |
---|
| 919 | + | then $Tuple2(nil, amountOut) |
---|
| 920 | + | else throw("Strict value is not equal to itself.") |
---|
| 921 | + | } |
---|
| 922 | + | |
---|
| 923 | + | |
---|
| 924 | + | |
---|
| 925 | + | @Callable(i) |
---|
| 926 | + | func calculateAmountOutForSwapAndSendTokens (cleanAmountIn,isReverse,amountOutMin,addressTo,feePoolAmount) = { |
---|
| 927 | + | let swapContact = { |
---|
| 928 | + | let @ = invoke(factoryContract, "getSwapContractREADONLY", nil, nil) |
---|
| 929 | + | if ($isInstanceOf(@, "String")) |
---|
| 930 | + | then @ |
---|
| 931 | + | else throw(($getType(@) + " couldn't be cast to String")) |
---|
| 932 | + | } |
---|
| 933 | + | let checks = [if ((value(i.payments[0]).amount >= cleanAmountIn)) |
---|
| 934 | + | then true |
---|
| 935 | + | else throwErr("Wrong amount"), if ((i.caller == addressFromStringValue(swapContact))) |
---|
| 936 | + | then true |
---|
| 937 | + | else throwErr("Permission denied")] |
---|
| 938 | + | if ((checks == checks)) |
---|
| 939 | + | then { |
---|
| 940 | + | let pmt = value(i.payments[0]) |
---|
| 941 | + | let assetIn = assetIdToString(pmt.assetId) |
---|
| 942 | + | let assetOut = if ((isReverse == false)) |
---|
| 943 | + | then getStringOrFail(this, pa()) |
---|
| 944 | + | else getStringOrFail(this, aa()) |
---|
| 945 | + | let poolAssetInBalance = (getAccBalance(assetIn) - value(i.payments[0]).amount) |
---|
| 946 | + | let poolAssetOutBalance = getAccBalance(assetOut) |
---|
| 947 | + | let amountOut = fraction(poolAssetOutBalance, cleanAmountIn, (poolAssetInBalance + cleanAmountIn)) |
---|
| 948 | + | let oldK = (toBigInt(poolAssetInBalance) * toBigInt(poolAssetOutBalance)) |
---|
| 949 | + | let newK = ((toBigInt(getAccBalance(assetIn)) + toBigInt(feePoolAmount)) * (toBigInt(getAccBalance(assetOut)) - toBigInt(amountOut))) |
---|
| 950 | + | let checkK = if ((newK >= oldK)) |
---|
| 951 | + | then true |
---|
| 952 | + | else throw("new K is fewer error") |
---|
| 953 | + | if ((checkK == checkK)) |
---|
| 954 | + | then { |
---|
| 955 | + | let checkMin = if ((amountOut >= amountOutMin)) |
---|
| 956 | + | then true |
---|
| 957 | + | else throw("Exchange result is fewer coins than expected") |
---|
| 958 | + | if ((checkMin == checkMin)) |
---|
| 959 | + | then { |
---|
| 960 | + | let rebalanceState = rebalanceAsset(assetIn) |
---|
| 961 | + | if ((rebalanceState == rebalanceState)) |
---|
| 962 | + | then { |
---|
| 963 | + | let withdrawState = withdrawAndRebalanceAsset(assetOut, amountOut) |
---|
| 964 | + | if ((withdrawState == withdrawState)) |
---|
| 965 | + | then $Tuple2(((withdrawState ++ rebalanceState) ++ [ScriptTransfer(addressFromStringValue(addressTo), amountOut, parseAssetId(assetOut))]), amountOut) |
---|
| 966 | + | else throw("Strict value is not equal to itself.") |
---|
| 967 | + | } |
---|
| 968 | + | else throw("Strict value is not equal to itself.") |
---|
| 969 | + | } |
---|
| 970 | + | else throw("Strict value is not equal to itself.") |
---|
| 971 | + | } |
---|
| 972 | + | else throw("Strict value is not equal to itself.") |
---|
| 973 | + | } |
---|
| 974 | + | else throw("Strict value is not equal to itself.") |
---|
| 975 | + | } |
---|
| 976 | + | |
---|
| 977 | + | |
---|
| 978 | + | |
---|
| 979 | + | @Callable(i) |
---|
| 980 | + | func setManager (pendingManagerPublicKey) = { |
---|
| 981 | + | let checkCaller = mustManager(i) |
---|
| 982 | + | if ((checkCaller == checkCaller)) |
---|
| 983 | + | then { |
---|
| 984 | + | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
| 985 | + | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
| 986 | + | then [StringEntry(pmpk(), pendingManagerPublicKey)] |
---|
| 987 | + | else throw("Strict value is not equal to itself.") |
---|
| 988 | + | } |
---|
| 989 | + | else throw("Strict value is not equal to itself.") |
---|
| 990 | + | } |
---|
| 991 | + | |
---|
| 992 | + | |
---|
| 993 | + | |
---|
| 994 | + | @Callable(i) |
---|
| 995 | + | func confirmManager () = { |
---|
| 996 | + | let pm = pendingManagerPublicKeyOrUnit() |
---|
| 997 | + | let hasPM = if (isDefined(pm)) |
---|
| 998 | + | then true |
---|
| 999 | + | else throw("No pending manager") |
---|
| 1000 | + | if ((hasPM == hasPM)) |
---|
| 1001 | + | then { |
---|
| 1002 | + | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
| 1003 | + | then true |
---|
| 1004 | + | else throw("You are not pending manager") |
---|
| 1005 | + | if ((checkPM == checkPM)) |
---|
| 1006 | + | then [StringEntry(mpk(), toBase58String(value(pm))), DeleteEntry(pmpk())] |
---|
| 1007 | + | else throw("Strict value is not equal to itself.") |
---|
| 1008 | + | } |
---|
| 1009 | + | else throw("Strict value is not equal to itself.") |
---|
| 1010 | + | } |
---|
| 1011 | + | |
---|
| 1012 | + | |
---|
| 1013 | + | |
---|
| 1014 | + | @Callable(i) |
---|
| 1015 | + | func put (slippageTolerance,shouldAutoStake) = if ((0 > slippageTolerance)) |
---|
| 1016 | + | then throw("Invalid slippageTolerance passed") |
---|
| 1017 | + | else { |
---|
| 1018 | + | let estPut = commonPut(i, slippageTolerance, true) |
---|
| 1019 | + | let emitLpAmt = estPut._2 |
---|
| 1020 | + | let lpAssetId = estPut._7 |
---|
| 1021 | + | let state = estPut._9 |
---|
| 1022 | + | let amDiff = estPut._10 |
---|
| 1023 | + | let prDiff = estPut._11 |
---|
| 1024 | + | let amId = estPut._12 |
---|
| 1025 | + | let prId = estPut._13 |
---|
| 1026 | + | let amAssetPmt = toBigInt(value(i.payments[0]).amount) |
---|
| 1027 | + | let prAssetPmt = toBigInt(value(i.payments[1]).amount) |
---|
| 1028 | + | let currentKLp = calcCurrentKLp(amAssetPmt, prAssetPmt, toBigInt(0)) |
---|
| 1029 | + | if ((currentKLp == currentKLp)) |
---|
| 1030 | + | then { |
---|
| 1031 | + | let emitInv = invoke(factoryContract, "emit", [emitLpAmt], nil) |
---|
| 1032 | + | if ((emitInv == emitInv)) |
---|
| 1033 | + | then { |
---|
| 1034 | + | let emitInvLegacy = match emitInv { |
---|
| 1035 | + | case legacyFactoryContract: Address => |
---|
| 1036 | + | invoke(legacyFactoryContract, "emit", [emitLpAmt], nil) |
---|
| 1037 | + | case _ => |
---|
| 1038 | + | unit |
---|
| 1039 | + | } |
---|
| 1040 | + | if ((emitInvLegacy == emitInvLegacy)) |
---|
| 1041 | + | then { |
---|
| 1042 | + | let slippageAInv = if ((amDiff > 0)) |
---|
| 1043 | + | then invoke(slippageContract, "put", nil, [AttachedPayment(amId, amDiff)]) |
---|
| 1044 | + | else nil |
---|
| 1045 | + | if ((slippageAInv == slippageAInv)) |
---|
| 1046 | + | then { |
---|
| 1047 | + | let slippagePInv = if ((prDiff > 0)) |
---|
| 1048 | + | then invoke(slippageContract, "put", nil, [AttachedPayment(prId, prDiff)]) |
---|
| 1049 | + | else nil |
---|
| 1050 | + | if ((slippagePInv == slippagePInv)) |
---|
| 1051 | + | then { |
---|
| 1052 | + | let lpTransfer = if (shouldAutoStake) |
---|
| 1053 | + | then { |
---|
| 1054 | + | let slpStakeInv = invoke(stakingContract, "stake", nil, [AttachedPayment(lpAssetId, emitLpAmt)]) |
---|
| 1055 | + | if ((slpStakeInv == slpStakeInv)) |
---|
| 1056 | + | then nil |
---|
| 1057 | + | else throw("Strict value is not equal to itself.") |
---|
| 1058 | + | } |
---|
| 1059 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpAssetId)] |
---|
| 1060 | + | let $t04447644938 = refreshKLpInternal(0, 0, 0) |
---|
| 1061 | + | if (($t04447644938 == $t04447644938)) |
---|
| 1062 | + | then { |
---|
| 1063 | + | let updatedKLp = $t04447644938._2 |
---|
| 1064 | + | let refreshKLpActions = $t04447644938._1 |
---|
| 1065 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1066 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1067 | + | then { |
---|
| 1068 | + | let reb = invoke(this, "rebalance", nil, nil) |
---|
| 1069 | + | if ((reb == reb)) |
---|
| 1070 | + | then ((state ++ lpTransfer) ++ refreshKLpActions) |
---|
| 1071 | + | else throw("Strict value is not equal to itself.") |
---|
| 1072 | + | } |
---|
| 1073 | + | else throw("Strict value is not equal to itself.") |
---|
| 1074 | + | } |
---|
| 1075 | + | else throw("Strict value is not equal to itself.") |
---|
| 1076 | + | } |
---|
| 1077 | + | else throw("Strict value is not equal to itself.") |
---|
| 1078 | + | } |
---|
| 1079 | + | else throw("Strict value is not equal to itself.") |
---|
| 1080 | + | } |
---|
| 1081 | + | else throw("Strict value is not equal to itself.") |
---|
| 1082 | + | } |
---|
| 1083 | + | else throw("Strict value is not equal to itself.") |
---|
| 1084 | + | } |
---|
| 1085 | + | else throw("Strict value is not equal to itself.") |
---|
| 1086 | + | } |
---|
| 1087 | + | |
---|
| 1088 | + | |
---|
| 1089 | + | |
---|
| 1090 | + | @Callable(i) |
---|
| 1091 | + | func putForFree (maxSlippage) = if ((0 > maxSlippage)) |
---|
| 1092 | + | then throw("Invalid value passed") |
---|
| 1093 | + | else { |
---|
| 1094 | + | let estPut = commonPut(i, maxSlippage, false) |
---|
| 1095 | + | let state = estPut._9 |
---|
| 1096 | + | let amAssetPmt = toBigInt(value(i.payments[0]).amount) |
---|
| 1097 | + | let prAssetPmt = toBigInt(value(i.payments[1]).amount) |
---|
| 1098 | + | let currentKLp = calcCurrentKLp(amAssetPmt, prAssetPmt, toBigInt(0)) |
---|
| 1099 | + | if ((currentKLp == currentKLp)) |
---|
| 1100 | + | then { |
---|
| 1101 | + | let $t04555045615 = refreshKLpInternal(0, 0, 0) |
---|
| 1102 | + | let refreshKLpActions = $t04555045615._1 |
---|
| 1103 | + | let updatedKLp = $t04555045615._2 |
---|
| 1104 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1105 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1106 | + | then (state ++ refreshKLpActions) |
---|
| 1107 | + | else throw("Strict value is not equal to itself.") |
---|
| 1108 | + | } |
---|
| 1109 | + | else throw("Strict value is not equal to itself.") |
---|
| 1110 | + | } |
---|
| 1111 | + | |
---|
| 1112 | + | |
---|
| 1113 | + | |
---|
| 1114 | + | @Callable(i) |
---|
| 1115 | + | func putOneTkn (minOutAmount,autoStake) = { |
---|
| 1116 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1117 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1118 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1119 | + | then @ |
---|
| 1120 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1121 | + | } |
---|
| 1122 | + | let isPutDisabled = if (if (if (isGlobalShutdown()) |
---|
| 1123 | + | then true |
---|
| 1124 | + | else (cfgPoolStatus == PoolPutDisabled)) |
---|
| 1125 | + | then true |
---|
| 1126 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1127 | + | then true |
---|
| 1128 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1129 | + | let checks = [if (if (!(isPutDisabled)) |
---|
| 1130 | + | then true |
---|
| 1131 | + | else isManager(i)) |
---|
| 1132 | + | then true |
---|
| 1133 | + | else throwErr("put operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 1134 | + | then true |
---|
| 1135 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 1136 | + | if ((checks == checks)) |
---|
| 1137 | + | then { |
---|
| 1138 | + | let payment = i.payments[0] |
---|
| 1139 | + | let paymentAssetId = payment.assetId |
---|
| 1140 | + | let paymentAmountRaw = payment.amount |
---|
| 1141 | + | let currentKLp = if ((paymentAssetId == cfgAmountAssetId)) |
---|
| 1142 | + | then calcCurrentKLp(toBigInt(paymentAmountRaw), toBigInt(0), toBigInt(0)) |
---|
| 1143 | + | else if ((paymentAssetId == cfgPriceAssetId)) |
---|
| 1144 | + | then calcCurrentKLp(toBigInt(0), toBigInt(paymentAmountRaw), toBigInt(0)) |
---|
| 1145 | + | else throwErr("payment asset is not supported") |
---|
| 1146 | + | if ((currentKLp == currentKLp)) |
---|
| 1147 | + | then { |
---|
| 1148 | + | let userAddress = i.caller |
---|
| 1149 | + | let txId = i.transactionId |
---|
| 1150 | + | let $t04680346955 = calcPutOneToken(paymentAmountRaw, paymentAssetId, userAddress, txId) |
---|
| 1151 | + | if (($t04680346955 == $t04680346955)) |
---|
| 1152 | + | then { |
---|
| 1153 | + | let paymentInAmountAsset = $t04680346955._5 |
---|
| 1154 | + | let bonus = $t04680346955._4 |
---|
| 1155 | + | let feeAmount = $t04680346955._3 |
---|
| 1156 | + | let commonState = $t04680346955._2 |
---|
| 1157 | + | let emitAmountEstimated = $t04680346955._1 |
---|
| 1158 | + | let emitAmount = if (if ((minOutAmount > 0)) |
---|
| 1159 | + | then (minOutAmount > emitAmountEstimated) |
---|
| 1160 | + | else false) |
---|
| 1161 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 1162 | + | else emitAmountEstimated |
---|
| 1163 | + | let emitInv = emit(emitAmount) |
---|
| 1164 | + | if ((emitInv == emitInv)) |
---|
| 1165 | + | then { |
---|
| 1166 | + | let lpTransfer = if (autoStake) |
---|
| 1167 | + | then { |
---|
| 1168 | + | let stakeInv = invoke(stakingContract, "stake", nil, [AttachedPayment(cfgLpAssetId, emitAmount)]) |
---|
| 1169 | + | if ((stakeInv == stakeInv)) |
---|
| 1170 | + | then nil |
---|
| 1171 | + | else throw("Strict value is not equal to itself.") |
---|
| 1172 | + | } |
---|
| 1173 | + | else [ScriptTransfer(i.caller, emitAmount, cfgLpAssetId)] |
---|
| 1174 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 1175 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, paymentAssetId)] |
---|
| 1176 | + | else nil |
---|
| 1177 | + | let $t04754147738 = if ((this == feeCollectorAddress)) |
---|
| 1178 | + | then $Tuple2(0, 0) |
---|
| 1179 | + | else if (paymentInAmountAsset) |
---|
| 1180 | + | then $Tuple2(-(feeAmount), 0) |
---|
| 1181 | + | else $Tuple2(0, -(feeAmount)) |
---|
| 1182 | + | let amountAssetBalanceDelta = $t04754147738._1 |
---|
| 1183 | + | let priceAssetBalanceDelta = $t04754147738._2 |
---|
| 1184 | + | let $t04774147849 = refreshKLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 1185 | + | let refreshKLpActions = $t04774147849._1 |
---|
| 1186 | + | let updatedKLp = $t04774147849._2 |
---|
| 1187 | + | let kLp = value(getString(keyKLp)) |
---|
| 1188 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1189 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1190 | + | then { |
---|
| 1191 | + | let reb = invoke(this, "rebalance", nil, nil) |
---|
| 1192 | + | if ((reb == reb)) |
---|
| 1193 | + | then $Tuple2((((commonState ++ lpTransfer) ++ sendFee) ++ refreshKLpActions), emitAmount) |
---|
| 1194 | + | else throw("Strict value is not equal to itself.") |
---|
| 1195 | + | } |
---|
| 1196 | + | else throw("Strict value is not equal to itself.") |
---|
| 1197 | + | } |
---|
| 1198 | + | else throw("Strict value is not equal to itself.") |
---|
| 1199 | + | } |
---|
| 1200 | + | else throw("Strict value is not equal to itself.") |
---|
| 1201 | + | } |
---|
| 1202 | + | else throw("Strict value is not equal to itself.") |
---|
| 1203 | + | } |
---|
| 1204 | + | else throw("Strict value is not equal to itself.") |
---|
| 1205 | + | } |
---|
| 1206 | + | |
---|
| 1207 | + | |
---|
| 1208 | + | |
---|
| 1209 | + | @Callable(i) |
---|
| 1210 | + | func putOneTknREADONLY (paymentAssetId,paymentAmountRaw) = { |
---|
| 1211 | + | let $t04820448361 = calcPutOneToken(paymentAmountRaw, parseAssetId(paymentAssetId), unit, unit) |
---|
| 1212 | + | let emitAmountEstimated = $t04820448361._1 |
---|
| 1213 | + | let commonState = $t04820448361._2 |
---|
| 1214 | + | let feeAmount = $t04820448361._3 |
---|
| 1215 | + | let bonus = $t04820448361._4 |
---|
| 1216 | + | let paymentInAmountAsset = $t04820448361._5 |
---|
| 1217 | + | $Tuple2(nil, $Tuple3(emitAmountEstimated, feeAmount, bonus)) |
---|
| 1218 | + | } |
---|
| 1219 | + | |
---|
| 1220 | + | |
---|
| 1221 | + | |
---|
| 1222 | + | @Callable(i) |
---|
| 1223 | + | func getOneTkn (outAssetIdStr,minOutAmount) = { |
---|
| 1224 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1225 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1226 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1227 | + | then @ |
---|
| 1228 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1229 | + | } |
---|
| 1230 | + | let isGetDisabled = if (if (isGlobalShutdown()) |
---|
| 1231 | + | then true |
---|
| 1232 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1233 | + | then true |
---|
| 1234 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1235 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 1236 | + | then true |
---|
| 1237 | + | else isManager(i)) |
---|
| 1238 | + | then true |
---|
| 1239 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 1240 | + | then true |
---|
| 1241 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 1242 | + | if ((checks == checks)) |
---|
| 1243 | + | then { |
---|
| 1244 | + | let outAssetId = parseAssetId(outAssetIdStr) |
---|
| 1245 | + | let payment = i.payments[0] |
---|
| 1246 | + | let paymentAssetId = payment.assetId |
---|
| 1247 | + | let paymentAmount = payment.amount |
---|
| 1248 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1249 | + | if ((currentKLp == currentKLp)) |
---|
| 1250 | + | then { |
---|
| 1251 | + | let userAddress = i.caller |
---|
| 1252 | + | let txId = i.transactionId |
---|
| 1253 | + | let $t04924649399 = calcGetOneToken(outAssetId, paymentAmount, paymentAssetId, userAddress, txId) |
---|
| 1254 | + | if (($t04924649399 == $t04924649399)) |
---|
| 1255 | + | then { |
---|
| 1256 | + | let outInAmountAsset = $t04924649399._5 |
---|
| 1257 | + | let bonus = $t04924649399._4 |
---|
| 1258 | + | let feeAmount = $t04924649399._3 |
---|
| 1259 | + | let commonState = $t04924649399._2 |
---|
| 1260 | + | let amountEstimated = $t04924649399._1 |
---|
| 1261 | + | let amount = if (if ((minOutAmount > 0)) |
---|
| 1262 | + | then (minOutAmount > amountEstimated) |
---|
| 1263 | + | else false) |
---|
| 1264 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 1265 | + | else amountEstimated |
---|
| 1266 | + | let burnInv = invoke(factoryContract, "burn", [paymentAmount], [AttachedPayment(paymentAssetId, paymentAmount)]) |
---|
| 1267 | + | if ((burnInv == burnInv)) |
---|
| 1268 | + | then { |
---|
| 1269 | + | let withdrawState = withdrawAndRebalanceAsset(outAssetIdStr, (amount + max([0, feeAmount]))) |
---|
| 1270 | + | let assetTransfer = [ScriptTransfer(userAddress, amount, outAssetId)] |
---|
| 1271 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 1272 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, outAssetId)] |
---|
| 1273 | + | else nil |
---|
| 1274 | + | let $t05006350310 = { |
---|
| 1275 | + | let feeAmountForCalc = if ((this == feeCollectorAddress)) |
---|
| 1276 | + | then 0 |
---|
| 1277 | + | else feeAmount |
---|
| 1278 | + | if (outInAmountAsset) |
---|
| 1279 | + | then $Tuple2(-((amount + feeAmountForCalc)), 0) |
---|
| 1280 | + | else $Tuple2(0, -((amount + feeAmountForCalc))) |
---|
| 1281 | + | } |
---|
| 1282 | + | let amountAssetBalanceDelta = $t05006350310._1 |
---|
| 1283 | + | let priceAssetBalanceDelta = $t05006350310._2 |
---|
| 1284 | + | let $t05031350421 = refreshKLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 1285 | + | let refreshKLpActions = $t05031350421._1 |
---|
| 1286 | + | let updatedKLp = $t05031350421._2 |
---|
| 1287 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1288 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1289 | + | then $Tuple2(((((commonState ++ withdrawState) ++ assetTransfer) ++ sendFee) ++ refreshKLpActions), amount) |
---|
| 1290 | + | else throw("Strict value is not equal to itself.") |
---|
| 1291 | + | } |
---|
| 1292 | + | else throw("Strict value is not equal to itself.") |
---|
| 1293 | + | } |
---|
| 1294 | + | else throw("Strict value is not equal to itself.") |
---|
| 1295 | + | } |
---|
| 1296 | + | else throw("Strict value is not equal to itself.") |
---|
| 1297 | + | } |
---|
| 1298 | + | else throw("Strict value is not equal to itself.") |
---|
| 1299 | + | } |
---|
| 1300 | + | |
---|
| 1301 | + | |
---|
| 1302 | + | |
---|
| 1303 | + | @Callable(i) |
---|
| 1304 | + | func getOneTknREADONLY (outAssetId,paymentAmount) = { |
---|
| 1305 | + | let $t05069950855 = calcGetOneToken(parseAssetId(outAssetId), paymentAmount, cfgLpAssetId, unit, unit) |
---|
| 1306 | + | let amountEstimated = $t05069950855._1 |
---|
| 1307 | + | let commonState = $t05069950855._2 |
---|
| 1308 | + | let feeAmount = $t05069950855._3 |
---|
| 1309 | + | let bonus = $t05069950855._4 |
---|
| 1310 | + | let outInAmountAsset = $t05069950855._5 |
---|
| 1311 | + | $Tuple2(nil, $Tuple3(amountEstimated, feeAmount, bonus)) |
---|
| 1312 | + | } |
---|
| 1313 | + | |
---|
| 1314 | + | |
---|
| 1315 | + | |
---|
| 1316 | + | @Callable(i) |
---|
| 1317 | + | func unstakeAndGetOneTkn (unstakeAmount,outAssetIdStr,minOutAmount) = { |
---|
| 1318 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1319 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1320 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1321 | + | then @ |
---|
| 1322 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1323 | + | } |
---|
| 1324 | + | let isGetDisabled = if (if (isGlobalShutdown()) |
---|
| 1325 | + | then true |
---|
| 1326 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1327 | + | then true |
---|
| 1328 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1329 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 1330 | + | then true |
---|
| 1331 | + | else isManager(i)) |
---|
| 1332 | + | then true |
---|
| 1333 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 1334 | + | then true |
---|
| 1335 | + | else throwErr("no payments are expected")] |
---|
| 1336 | + | if ((checks == checks)) |
---|
| 1337 | + | then { |
---|
| 1338 | + | let outAssetId = parseAssetId(outAssetIdStr) |
---|
| 1339 | + | let userAddress = i.caller |
---|
| 1340 | + | let txId = i.transactionId |
---|
| 1341 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1342 | + | if ((currentKLp == currentKLp)) |
---|
| 1343 | + | then { |
---|
| 1344 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(cfgLpAssetId), unstakeAmount], nil) |
---|
| 1345 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1346 | + | then { |
---|
| 1347 | + | let $t05176051911 = calcGetOneToken(outAssetId, unstakeAmount, cfgLpAssetId, userAddress, txId) |
---|
| 1348 | + | if (($t05176051911 == $t05176051911)) |
---|
| 1349 | + | then { |
---|
| 1350 | + | let outInAmountAsset = $t05176051911._5 |
---|
| 1351 | + | let bonus = $t05176051911._4 |
---|
| 1352 | + | let feeAmount = $t05176051911._3 |
---|
| 1353 | + | let commonState = $t05176051911._2 |
---|
| 1354 | + | let amountEstimated = $t05176051911._1 |
---|
| 1355 | + | let amount = if (if ((minOutAmount > 0)) |
---|
| 1356 | + | then (minOutAmount > amountEstimated) |
---|
| 1357 | + | else false) |
---|
| 1358 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 1359 | + | else amountEstimated |
---|
| 1360 | + | let burnInv = invoke(factoryContract, "burn", [unstakeAmount], [AttachedPayment(cfgLpAssetId, unstakeAmount)]) |
---|
| 1361 | + | if ((burnInv == burnInv)) |
---|
| 1362 | + | then { |
---|
| 1363 | + | let withdrawState = withdrawAndRebalanceAsset(outAssetIdStr, (amount + max([0, feeAmount]))) |
---|
| 1364 | + | let assetTransfer = [ScriptTransfer(i.caller, amount, outAssetId)] |
---|
| 1365 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 1366 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, outAssetId)] |
---|
| 1367 | + | else nil |
---|
| 1368 | + | let $t05257052817 = { |
---|
| 1369 | + | let feeAmountForCalc = if ((this == feeCollectorAddress)) |
---|
| 1370 | + | then 0 |
---|
| 1371 | + | else feeAmount |
---|
| 1372 | + | if (outInAmountAsset) |
---|
| 1373 | + | then $Tuple2(-((amount + feeAmountForCalc)), 0) |
---|
| 1374 | + | else $Tuple2(0, -((amount + feeAmountForCalc))) |
---|
| 1375 | + | } |
---|
| 1376 | + | let amountAssetBalanceDelta = $t05257052817._1 |
---|
| 1377 | + | let priceAssetBalanceDelta = $t05257052817._2 |
---|
| 1378 | + | let $t05282052928 = refreshKLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 1379 | + | let refreshKLpActions = $t05282052928._1 |
---|
| 1380 | + | let updatedKLp = $t05282052928._2 |
---|
| 1381 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1382 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1383 | + | then $Tuple2(((((commonState ++ withdrawState) ++ assetTransfer) ++ sendFee) ++ refreshKLpActions), amount) |
---|
| 1384 | + | else throw("Strict value is not equal to itself.") |
---|
| 1385 | + | } |
---|
| 1386 | + | else throw("Strict value is not equal to itself.") |
---|
| 1387 | + | } |
---|
| 1388 | + | else throw("Strict value is not equal to itself.") |
---|
| 1389 | + | } |
---|
| 1390 | + | else throw("Strict value is not equal to itself.") |
---|
| 1391 | + | } |
---|
| 1392 | + | else throw("Strict value is not equal to itself.") |
---|
| 1393 | + | } |
---|
| 1394 | + | else throw("Strict value is not equal to itself.") |
---|
| 1395 | + | } |
---|
| 1396 | + | |
---|
| 1397 | + | |
---|
| 1398 | + | |
---|
| 1399 | + | @Callable(i) |
---|
| 1400 | + | func get () = { |
---|
| 1401 | + | let res = commonGet(i) |
---|
| 1402 | + | let outAmAmt = res._1 |
---|
| 1403 | + | let outPrAmt = res._2 |
---|
| 1404 | + | let pmtAmt = res._3 |
---|
| 1405 | + | let pmtAssetId = res._4 |
---|
| 1406 | + | let state = res._5 |
---|
| 1407 | + | let withdrawState = withdrawAndRebalanceAll(outAmAmt, outPrAmt) |
---|
| 1408 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1409 | + | if ((currentKLp == currentKLp)) |
---|
| 1410 | + | then { |
---|
| 1411 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 1412 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1413 | + | then { |
---|
| 1414 | + | let $t05402354104 = refreshKLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1415 | + | let refreshKLpActions = $t05402354104._1 |
---|
| 1416 | + | let updatedKLp = $t05402354104._2 |
---|
| 1417 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1418 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1419 | + | then ((withdrawState ++ state) ++ refreshKLpActions) |
---|
| 1420 | + | else throw("Strict value is not equal to itself.") |
---|
| 1421 | + | } |
---|
| 1422 | + | else throw("Strict value is not equal to itself.") |
---|
| 1423 | + | } |
---|
| 1424 | + | else throw("Strict value is not equal to itself.") |
---|
| 1425 | + | } |
---|
| 1426 | + | |
---|
| 1427 | + | |
---|
| 1428 | + | |
---|
| 1429 | + | @Callable(i) |
---|
| 1430 | + | func getNoLess (noLessThenAmtAsset,noLessThenPriceAsset) = { |
---|
| 1431 | + | let res = commonGet(i) |
---|
| 1432 | + | let outAmAmt = res._1 |
---|
| 1433 | + | let outPrAmt = res._2 |
---|
| 1434 | + | let pmtAmt = res._3 |
---|
| 1435 | + | let pmtAssetId = res._4 |
---|
| 1436 | + | let state = res._5 |
---|
| 1437 | + | if ((noLessThenAmtAsset > outAmAmt)) |
---|
| 1438 | + | then throw(((("noLessThenAmtAsset failed: " + toString(outAmAmt)) + " < ") + toString(noLessThenAmtAsset))) |
---|
| 1439 | + | else if ((noLessThenPriceAsset > outPrAmt)) |
---|
| 1440 | + | then throw(((("noLessThenPriceAsset failed: " + toString(outPrAmt)) + " < ") + toString(noLessThenPriceAsset))) |
---|
| 1441 | + | else { |
---|
| 1442 | + | let withdrawState = withdrawAndRebalanceAll(outAmAmt, outPrAmt) |
---|
| 1443 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1444 | + | if ((currentKLp == currentKLp)) |
---|
| 1445 | + | then { |
---|
| 1446 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 1447 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1448 | + | then { |
---|
| 1449 | + | let $t05519955280 = refreshKLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1450 | + | let refreshKLpActions = $t05519955280._1 |
---|
| 1451 | + | let updatedKLp = $t05519955280._2 |
---|
| 1452 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1453 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1454 | + | then ((withdrawState ++ state) ++ refreshKLpActions) |
---|
| 1455 | + | else throw("Strict value is not equal to itself.") |
---|
| 1456 | + | } |
---|
| 1457 | + | else throw("Strict value is not equal to itself.") |
---|
| 1458 | + | } |
---|
| 1459 | + | else throw("Strict value is not equal to itself.") |
---|
| 1460 | + | } |
---|
| 1461 | + | } |
---|
| 1462 | + | |
---|
| 1463 | + | |
---|
| 1464 | + | |
---|
| 1465 | + | @Callable(i) |
---|
| 1466 | + | func unstakeAndGet (amount) = { |
---|
| 1467 | + | let checkPayments = if ((size(i.payments) != 0)) |
---|
| 1468 | + | then throw("No payments are expected") |
---|
| 1469 | + | else true |
---|
| 1470 | + | if ((checkPayments == checkPayments)) |
---|
| 1471 | + | then { |
---|
| 1472 | + | let cfg = getPoolConfig() |
---|
| 1473 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1474 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1475 | + | if ((currentKLp == currentKLp)) |
---|
| 1476 | + | then { |
---|
| 1477 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(lpAssetId), amount], nil) |
---|
| 1478 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1479 | + | then { |
---|
| 1480 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(lpAssetId), amount, i.caller) |
---|
| 1481 | + | let outAmAmt = res._1 |
---|
| 1482 | + | let outPrAmt = res._2 |
---|
| 1483 | + | let poolStatus = parseIntValue(res._9) |
---|
| 1484 | + | let state = res._10 |
---|
| 1485 | + | let withdrawState = withdrawAndRebalanceAll(outAmAmt, outPrAmt) |
---|
| 1486 | + | let checkPoolStatus = if (if (isGlobalShutdown()) |
---|
| 1487 | + | then true |
---|
| 1488 | + | else (poolStatus == PoolShutdown)) |
---|
| 1489 | + | then throw(("Get operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 1490 | + | else true |
---|
| 1491 | + | if ((checkPoolStatus == checkPoolStatus)) |
---|
| 1492 | + | then { |
---|
| 1493 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [amount], [AttachedPayment(lpAssetId, amount)]) |
---|
| 1494 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1495 | + | then { |
---|
| 1496 | + | let $t05655256633 = refreshKLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1497 | + | let refreshKLpActions = $t05655256633._1 |
---|
| 1498 | + | let updatedKLp = $t05655256633._2 |
---|
| 1499 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1500 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1501 | + | then ((withdrawState ++ state) ++ refreshKLpActions) |
---|
| 1502 | + | else throw("Strict value is not equal to itself.") |
---|
| 1503 | + | } |
---|
| 1504 | + | else throw("Strict value is not equal to itself.") |
---|
| 1505 | + | } |
---|
| 1506 | + | else throw("Strict value is not equal to itself.") |
---|
| 1507 | + | } |
---|
| 1508 | + | else throw("Strict value is not equal to itself.") |
---|
| 1509 | + | } |
---|
| 1510 | + | else throw("Strict value is not equal to itself.") |
---|
| 1511 | + | } |
---|
| 1512 | + | else throw("Strict value is not equal to itself.") |
---|
| 1513 | + | } |
---|
| 1514 | + | |
---|
| 1515 | + | |
---|
| 1516 | + | |
---|
| 1517 | + | @Callable(i) |
---|
| 1518 | + | func unstakeAndGetNoLess (unstakeAmount,noLessThenAmountAsset,noLessThenPriceAsset) = { |
---|
| 1519 | + | let isGetDisabled = if (isGlobalShutdown()) |
---|
| 1520 | + | then true |
---|
| 1521 | + | else (cfgPoolStatus == PoolShutdown) |
---|
| 1522 | + | let checks = [if (!(isGetDisabled)) |
---|
| 1523 | + | then true |
---|
| 1524 | + | else throw("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 1525 | + | then true |
---|
| 1526 | + | else throw("no payments are expected")] |
---|
| 1527 | + | if ((checks == checks)) |
---|
| 1528 | + | then { |
---|
| 1529 | + | let currentKLp = calcCurrentKLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1530 | + | if ((currentKLp == currentKLp)) |
---|
| 1531 | + | then { |
---|
| 1532 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(cfgLpAssetId), unstakeAmount], nil) |
---|
| 1533 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1534 | + | then { |
---|
| 1535 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(cfgLpAssetId), unstakeAmount, i.caller) |
---|
| 1536 | + | let outAmAmt = res._1 |
---|
| 1537 | + | let outPrAmt = res._2 |
---|
| 1538 | + | let state = res._10 |
---|
| 1539 | + | let withdrawState = withdrawAndRebalanceAll(outAmAmt, outPrAmt) |
---|
| 1540 | + | let checkAmounts = [if ((outAmAmt >= noLessThenAmountAsset)) |
---|
| 1541 | + | then true |
---|
| 1542 | + | else throw(makeString(["amount asset amount to receive is less than ", toString(noLessThenAmountAsset)], "")), if ((outPrAmt >= noLessThenPriceAsset)) |
---|
| 1543 | + | then true |
---|
| 1544 | + | else throw(makeString(["price asset amount to receive is less than ", toString(noLessThenPriceAsset)], ""))] |
---|
| 1545 | + | if ((checkAmounts == checkAmounts)) |
---|
| 1546 | + | then { |
---|
| 1547 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [unstakeAmount], [AttachedPayment(cfgLpAssetId, unstakeAmount)]) |
---|
| 1548 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1549 | + | then { |
---|
| 1550 | + | let $t05807458155 = refreshKLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1551 | + | let refreshKLpActions = $t05807458155._1 |
---|
| 1552 | + | let updatedKLp = $t05807458155._2 |
---|
| 1553 | + | let isUpdatedKLpValid = validateUpdatedKLp(currentKLp, updatedKLp) |
---|
| 1554 | + | if ((isUpdatedKLpValid == isUpdatedKLpValid)) |
---|
| 1555 | + | then ((withdrawState ++ state) ++ refreshKLpActions) |
---|
| 1556 | + | else throw("Strict value is not equal to itself.") |
---|
| 1557 | + | } |
---|
| 1558 | + | else throw("Strict value is not equal to itself.") |
---|
| 1559 | + | } |
---|
| 1560 | + | else throw("Strict value is not equal to itself.") |
---|
| 1561 | + | } |
---|
| 1562 | + | else throw("Strict value is not equal to itself.") |
---|
| 1563 | + | } |
---|
| 1564 | + | else throw("Strict value is not equal to itself.") |
---|
| 1565 | + | } |
---|
| 1566 | + | else throw("Strict value is not equal to itself.") |
---|
| 1567 | + | } |
---|
| 1568 | + | |
---|
| 1569 | + | |
---|
| 1570 | + | |
---|
| 1571 | + | @Callable(i) |
---|
| 1572 | + | func activate (amtAssetStr,priceAssetStr) = if ((toString(i.caller) != toString(factoryContract))) |
---|
| 1573 | + | then throw("permissions denied") |
---|
| 1574 | + | else $Tuple2([StringEntry(aa(), amtAssetStr), StringEntry(pa(), priceAssetStr)], "success") |
---|
| 1575 | + | |
---|
| 1576 | + | |
---|
| 1577 | + | |
---|
| 1578 | + | @Callable(i) |
---|
| 1579 | + | func refreshKLp () = { |
---|
| 1580 | + | let lastRefreshedBlockHeight = valueOrElse(getInteger(keyKLpRefreshedHeight), 0) |
---|
| 1581 | + | let checkLastRefreshedBlockHeight = if (((height - lastRefreshedBlockHeight) >= kLpRefreshDelay)) |
---|
| 1582 | + | then unit |
---|
| 1583 | + | else throwErr(makeString([toString(kLpRefreshDelay), " blocks have not passed since the previous call"], "")) |
---|
| 1584 | + | if ((checkLastRefreshedBlockHeight == checkLastRefreshedBlockHeight)) |
---|
| 1585 | + | then { |
---|
| 1586 | + | let kLp = valueOrErrorMessage(parseBigInt(valueOrElse(getString(this, keyKLp), "0")), fmtErr("invalid kLp")) |
---|
| 1587 | + | let $t05935959423 = refreshKLpInternal(0, 0, 0) |
---|
| 1588 | + | let kLpUpdateActions = $t05935959423._1 |
---|
| 1589 | + | let updatedKLp = $t05935959423._2 |
---|
| 1590 | + | let actions = if ((kLp != updatedKLp)) |
---|
| 1591 | + | then kLpUpdateActions |
---|
| 1592 | + | else throwErr("nothing to refresh") |
---|
| 1593 | + | $Tuple2(actions, toString(updatedKLp)) |
---|
| 1594 | + | } |
---|
| 1595 | + | else throw("Strict value is not equal to itself.") |
---|
| 1596 | + | } |
---|
| 1597 | + | |
---|
| 1598 | + | |
---|
| 1599 | + | |
---|
| 1600 | + | @Callable(i) |
---|
| 1601 | + | func getPoolConfigWrapperREADONLY () = $Tuple2(nil, getPoolConfig()) |
---|
| 1602 | + | |
---|
| 1603 | + | |
---|
| 1604 | + | |
---|
| 1605 | + | @Callable(i) |
---|
| 1606 | + | func getAccBalanceWrapperREADONLY (assetId) = $Tuple2(nil, getAccBalance(assetId)) |
---|
| 1607 | + | |
---|
| 1608 | + | |
---|
| 1609 | + | |
---|
| 1610 | + | @Callable(i) |
---|
| 1611 | + | func calcPricesWrapperREADONLY (amAmt,prAmt,lpAmt) = { |
---|
| 1612 | + | let prices = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 1613 | + | $Tuple2(nil, [toString(prices[0]), toString(prices[1]), toString(prices[2])]) |
---|
| 1614 | + | } |
---|
| 1615 | + | |
---|
| 1616 | + | |
---|
| 1617 | + | |
---|
| 1618 | + | @Callable(i) |
---|
| 1619 | + | func toX18WrapperREADONLY (origVal,origScaleMult) = $Tuple2(nil, toString(toX18(origVal, origScaleMult))) |
---|
| 1620 | + | |
---|
| 1621 | + | |
---|
| 1622 | + | |
---|
| 1623 | + | @Callable(i) |
---|
| 1624 | + | func fromX18WrapperREADONLY (val,resultScaleMult) = $Tuple2(nil, fromX18(parseBigIntValue(val), resultScaleMult)) |
---|
| 1625 | + | |
---|
| 1626 | + | |
---|
| 1627 | + | |
---|
| 1628 | + | @Callable(i) |
---|
| 1629 | + | func calcPriceBigIntWrapperREADONLY (prAmtX18,amAmtX18) = $Tuple2(nil, toString(calcPriceBigInt(parseBigIntValue(prAmtX18), parseBigIntValue(amAmtX18)))) |
---|
| 1630 | + | |
---|
| 1631 | + | |
---|
| 1632 | + | |
---|
| 1633 | + | @Callable(i) |
---|
| 1634 | + | func estimatePutOperationWrapperREADONLY (txId58,slippageTolerance,inAmAssetAmt,inAmAssetId,inPrAssetAmt,inPrAssetId,userAddress,isEvaluate,emitLp) = $Tuple2(nil, estimatePutOperation(txId58, slippageTolerance, inAmAssetAmt, inAmAssetId, inPrAssetAmt, inPrAssetId, userAddress, isEvaluate, emitLp)) |
---|
| 1635 | + | |
---|
| 1636 | + | |
---|
| 1637 | + | |
---|
| 1638 | + | @Callable(i) |
---|
| 1639 | + | func estimateGetOperationWrapperREADONLY (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 1640 | + | let res = estimateGetOperation(txId58, pmtAssetId, pmtLpAmt, addressFromStringValue(userAddress)) |
---|
| 1641 | + | $Tuple2(nil, $Tuple10(res._1, res._2, res._3, res._4, res._5, res._6, res._7, toString(res._8), res._9, res._10)) |
---|
| 1642 | + | } |
---|
| 1643 | + | |
---|
| 1644 | + | |
---|
| 1645 | + | |
---|
| 1646 | + | @Callable(i) |
---|
| 1647 | + | func statsREADONLY () = { |
---|
| 1648 | + | let cfg = getPoolConfig() |
---|
| 1649 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1650 | + | let amtAssetId = cfg[idxAmtAssetId] |
---|
| 1651 | + | let priceAssetId = cfg[idxPriceAssetId] |
---|
| 1652 | + | let iAmtAssetId = cfg[idxIAmtAssetId] |
---|
| 1653 | + | let iPriceAssetId = cfg[idxIPriceAssetId] |
---|
| 1654 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1655 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1656 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1657 | + | let accAmtAssetBalance = getAccBalance(amtAssetId) |
---|
| 1658 | + | let accPriceAssetBalance = getAccBalance(priceAssetId) |
---|
| 1659 | + | let pricesList = if ((poolLPBalance == 0)) |
---|
| 1660 | + | then [zeroBigInt, zeroBigInt, zeroBigInt] |
---|
| 1661 | + | else calcPrices(accAmtAssetBalance, accPriceAssetBalance, poolLPBalance) |
---|
| 1662 | + | let curPrice = 0 |
---|
| 1663 | + | let lpAmtAssetShare = fromX18(pricesList[1], scale8) |
---|
| 1664 | + | let lpPriceAssetShare = fromX18(pricesList[2], scale8) |
---|
| 1665 | + | let poolWeight = value(getInteger(factoryContract, keyPoolWeight(toString(this)))) |
---|
| 1666 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d", toString(accAmtAssetBalance), toString(accPriceAssetBalance), toString(poolLPBalance), toString(curPrice), toString(lpAmtAssetShare), toString(lpPriceAssetShare), toString(poolWeight)], SEP)) |
---|
| 1667 | + | } |
---|
| 1668 | + | |
---|
| 1669 | + | |
---|
| 1670 | + | |
---|
| 1671 | + | @Callable(i) |
---|
| 1672 | + | func evaluatePutByAmountAssetREADONLY (inAmAssetAmt) = { |
---|
| 1673 | + | let cfg = getPoolConfig() |
---|
| 1674 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1675 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 1676 | + | let amAssetId = fromBase58String(amAssetIdStr) |
---|
| 1677 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 1678 | + | let prAssetId = fromBase58String(prAssetIdStr) |
---|
| 1679 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1680 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1681 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 1682 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1683 | + | let accAmtAssetBalance = getAccBalance(amAssetIdStr) |
---|
| 1684 | + | let accPriceAssetBalance = getAccBalance(prAssetIdStr) |
---|
| 1685 | + | let amtAssetAmtX18 = toX18(accAmtAssetBalance, amtAssetDcm) |
---|
| 1686 | + | let priceAssetAmtX18 = toX18(accPriceAssetBalance, priceAssetDcm) |
---|
| 1687 | + | let curPriceX18 = if ((poolLPBalance == 0)) |
---|
| 1688 | + | then zeroBigInt |
---|
| 1689 | + | else calcPriceBigInt(priceAssetAmtX18, amtAssetAmtX18) |
---|
| 1690 | + | let inAmAssetAmtX18 = toX18(inAmAssetAmt, amtAssetDcm) |
---|
| 1691 | + | let inPrAssetAmtX18 = fraction(inAmAssetAmtX18, curPriceX18, scale18) |
---|
| 1692 | + | let inPrAssetAmt = fromX18(inPrAssetAmtX18, priceAssetDcm) |
---|
| 1693 | + | let estPut = estimatePutOperation("", 500000, inAmAssetAmt, amAssetId, inPrAssetAmt, prAssetId, "", true, false) |
---|
| 1694 | + | let calcLpAmt = estPut._1 |
---|
| 1695 | + | let curPriceCalc = estPut._3 |
---|
| 1696 | + | let amBalance = estPut._4 |
---|
| 1697 | + | let prBalance = estPut._5 |
---|
| 1698 | + | let lpEmission = estPut._6 |
---|
| 1699 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d%d", toString(calcLpAmt), toString(fromX18(curPriceX18, scale8)), toString(amBalance), toString(prBalance), toString(lpEmission), poolStatus, toString(inAmAssetAmt), toString(inPrAssetAmt)], SEP)) |
---|
| 1700 | + | } |
---|
| 1701 | + | |
---|
| 1702 | + | |
---|
| 1703 | + | |
---|
| 1704 | + | @Callable(i) |
---|
| 1705 | + | func evaluatePutByPriceAssetREADONLY (inPrAssetAmt) = { |
---|
| 1706 | + | let cfg = getPoolConfig() |
---|
| 1707 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1708 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 1709 | + | let amAssetId = fromBase58String(amAssetIdStr) |
---|
| 1710 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 1711 | + | let prAssetId = fromBase58String(prAssetIdStr) |
---|
| 1712 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1713 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1714 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 1715 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1716 | + | let amBalanceRaw = getAccBalance(amAssetIdStr) |
---|
| 1717 | + | let prBalanceRaw = getAccBalance(prAssetIdStr) |
---|
| 1718 | + | let amBalanceRawX18 = toX18(amBalanceRaw, amtAssetDcm) |
---|
| 1719 | + | let prBalanceRawX18 = toX18(prBalanceRaw, priceAssetDcm) |
---|
| 1720 | + | let curPriceX18 = if ((poolLPBalance == 0)) |
---|
| 1721 | + | then zeroBigInt |
---|
| 1722 | + | else calcPriceBigInt(prBalanceRawX18, amBalanceRawX18) |
---|
| 1723 | + | let inPrAssetAmtX18 = toX18(inPrAssetAmt, priceAssetDcm) |
---|
| 1724 | + | let inAmAssetAmtX18 = fraction(inPrAssetAmtX18, scale18, curPriceX18) |
---|
| 1725 | + | let inAmAssetAmt = fromX18(inAmAssetAmtX18, amtAssetDcm) |
---|
| 1726 | + | let estPut = estimatePutOperation("", 500000, inAmAssetAmt, amAssetId, inPrAssetAmt, prAssetId, "", true, false) |
---|
| 1727 | + | let calcLpAmt = estPut._1 |
---|
| 1728 | + | let curPriceCalc = estPut._3 |
---|
| 1729 | + | let amBalance = estPut._4 |
---|
| 1730 | + | let prBalance = estPut._5 |
---|
| 1731 | + | let lpEmission = estPut._6 |
---|
| 1732 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d%d", toString(calcLpAmt), toString(fromX18(curPriceX18, scale8)), toString(amBalance), toString(prBalance), toString(lpEmission), poolStatus, toString(inAmAssetAmt), toString(inPrAssetAmt)], SEP)) |
---|
| 1733 | + | } |
---|
| 1734 | + | |
---|
| 1735 | + | |
---|
| 1736 | + | |
---|
| 1737 | + | @Callable(i) |
---|
| 1738 | + | func evaluateGetREADONLY (paymentLpAssetId,paymentLpAmt) = { |
---|
| 1739 | + | let res = estimateGetOperation("", paymentLpAssetId, paymentLpAmt, this) |
---|
| 1740 | + | let outAmAmt = res._1 |
---|
| 1741 | + | let outPrAmt = res._2 |
---|
| 1742 | + | let amBalance = res._5 |
---|
| 1743 | + | let prBalance = res._6 |
---|
| 1744 | + | let lpEmission = res._7 |
---|
| 1745 | + | let curPrice = res._8 |
---|
| 1746 | + | let poolStatus = parseIntValue(res._9) |
---|
| 1747 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d", toString(outAmAmt), toString(outPrAmt), toString(amBalance), toString(prBalance), toString(lpEmission), toString(curPrice), toString(poolStatus)], SEP)) |
---|
| 1748 | + | } |
---|
| 1749 | + | |
---|
| 1750 | + | |
---|
| 1751 | + | @Verifier(tx) |
---|
| 1752 | + | func verify () = { |
---|
| 1753 | + | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
| 1754 | + | case pk: ByteVector => |
---|
| 1755 | + | pk |
---|
| 1756 | + | case _: Unit => |
---|
| 1757 | + | tx.senderPublicKey |
---|
| 1758 | + | case _ => |
---|
| 1759 | + | throw("Match error") |
---|
| 1760 | + | } |
---|
| 1761 | + | match tx { |
---|
| 1762 | + | case order: Order => |
---|
| 1763 | + | let matcherPub = getMatcherPubOrFail() |
---|
| 1764 | + | let $t06808568202 = if (skipOrderValidation()) |
---|
| 1765 | + | then $Tuple2(true, "") |
---|
| 1766 | + | else validateMatcherOrderAllowed(order) |
---|
| 1767 | + | let orderValid = $t06808568202._1 |
---|
| 1768 | + | let orderValidInfo = $t06808568202._2 |
---|
| 1769 | + | let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey) |
---|
| 1770 | + | let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub) |
---|
| 1771 | + | if (if (if (orderValid) |
---|
| 1772 | + | then senderValid |
---|
| 1773 | + | else false) |
---|
| 1774 | + | then matcherValid |
---|
| 1775 | + | else false) |
---|
| 1776 | + | then true |
---|
| 1777 | + | else throwOrderError(orderValid, orderValidInfo, senderValid, matcherValid) |
---|
| 1778 | + | case s: SetScriptTransaction => |
---|
| 1779 | + | if (sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey)) |
---|
| 1780 | + | then true |
---|
| 1781 | + | else { |
---|
| 1782 | + | let newHash = blake2b256(value(s.script)) |
---|
| 1783 | + | let allowedHash = fromBase64String(value(getString(factoryContract, keyAllowedLpScriptHash()))) |
---|
| 1784 | + | let currentHash = scriptHash(this) |
---|
| 1785 | + | if ((allowedHash == newHash)) |
---|
| 1786 | + | then (currentHash != newHash) |
---|
| 1787 | + | else false |
---|
| 1788 | + | } |
---|
| 1789 | + | case _ => |
---|
| 1790 | + | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 1791 | + | } |
---|
| 1792 | + | } |
---|
| 1793 | + | |
---|