1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let scale8 = 100000000 |
---|
| 5 | + | |
---|
| 6 | + | let scale8BigInt = toBigInt(100000000) |
---|
| 7 | + | |
---|
| 8 | + | let scale18 = toBigInt(1000000000000000000) |
---|
| 9 | + | |
---|
| 10 | + | let zeroBigInt = toBigInt(0) |
---|
| 11 | + | |
---|
| 12 | + | let big0 = toBigInt(0) |
---|
| 13 | + | |
---|
| 14 | + | let big1 = toBigInt(1) |
---|
| 15 | + | |
---|
| 16 | + | let big2 = toBigInt(2) |
---|
| 17 | + | |
---|
| 18 | + | let big3 = toBigInt(3) |
---|
| 19 | + | |
---|
| 20 | + | let big4 = toBigInt(4) |
---|
| 21 | + | |
---|
| 22 | + | let slippage4D = toBigInt((scale8 - ((scale8 * 1) / scale8))) |
---|
| 23 | + | |
---|
| 24 | + | let wavesString = "WAVES" |
---|
| 25 | + | |
---|
| 26 | + | let ampInitial = 50 |
---|
| 27 | + | |
---|
| 28 | + | let Amult = "100" |
---|
| 29 | + | |
---|
| 30 | + | let Dconv = "1" |
---|
| 31 | + | |
---|
| 32 | + | let SEP = "__" |
---|
| 33 | + | |
---|
| 34 | + | let EMPTY = "" |
---|
| 35 | + | |
---|
| 36 | + | let PoolActive = 1 |
---|
| 37 | + | |
---|
| 38 | + | let PoolPutDis = 2 |
---|
| 39 | + | |
---|
| 40 | + | let PoolMatcherDis = 3 |
---|
| 41 | + | |
---|
| 42 | + | let PoolShutdown = 4 |
---|
| 43 | + | |
---|
| 44 | + | let idxPoolAddress = 1 |
---|
| 45 | + | |
---|
| 46 | + | let idxPoolSt = 2 |
---|
| 47 | + | |
---|
| 48 | + | let idxLPAsId = 3 |
---|
| 49 | + | |
---|
| 50 | + | let idxAmAsId = 4 |
---|
| 51 | + | |
---|
| 52 | + | let idxPrAsId = 5 |
---|
| 53 | + | |
---|
| 54 | + | let idxAmtAsDcm = 6 |
---|
| 55 | + | |
---|
| 56 | + | let idxPriceAsDcm = 7 |
---|
| 57 | + | |
---|
| 58 | + | let idxIAmtAsId = 8 |
---|
| 59 | + | |
---|
| 60 | + | let idxIPriceAsId = 9 |
---|
| 61 | + | |
---|
| 62 | + | let idxFactStakCntr = 1 |
---|
| 63 | + | |
---|
| 64 | + | let idxFactoryRestCntr = 6 |
---|
| 65 | + | |
---|
| 66 | + | let idxFactSlippCntr = 7 |
---|
| 67 | + | |
---|
| 68 | + | let idxFactGwxRewCntr = 10 |
---|
| 69 | + | |
---|
| 70 | + | let feeDefault = fraction(10, scale8, 10000) |
---|
| 71 | + | |
---|
| 72 | + | func t1 (origVal,origScaleMult) = fraction(toBigInt(origVal), scale18, toBigInt(origScaleMult)) |
---|
| 73 | + | |
---|
| 74 | + | |
---|
| 75 | + | func t1BigInt (origVal,origScaleMult) = fraction(origVal, scale18, origScaleMult) |
---|
| 76 | + | |
---|
| 77 | + | |
---|
| 78 | + | func f1 (val,resultScaleMult) = toInt(fraction(val, toBigInt(resultScaleMult), scale18)) |
---|
| 79 | + | |
---|
| 80 | + | |
---|
| 81 | + | func fromX18Round (val,resultScaleMult,round) = toInt(fraction(val, toBigInt(resultScaleMult), scale18, round)) |
---|
| 82 | + | |
---|
| 83 | + | |
---|
| 84 | + | func t2 (origVal,origScaleMult) = fraction(origVal, scale18, toBigInt(origScaleMult)) |
---|
| 85 | + | |
---|
| 86 | + | |
---|
| 87 | + | func f2 (val,resultScaleMult) = fraction(val, toBigInt(resultScaleMult), scale18) |
---|
| 88 | + | |
---|
| 89 | + | |
---|
| 90 | + | func ts (amt,resScale,curScale) = fraction(amt, resScale, curScale) |
---|
| 91 | + | |
---|
| 92 | + | |
---|
| 93 | + | func abs (val) = if ((zeroBigInt > val)) |
---|
| 94 | + | then -(val) |
---|
| 95 | + | else val |
---|
| 96 | + | |
---|
| 97 | + | |
---|
| 98 | + | func absBigInt (val) = if ((zeroBigInt > val)) |
---|
| 99 | + | then -(val) |
---|
| 100 | + | else val |
---|
| 101 | + | |
---|
| 102 | + | |
---|
| 103 | + | func fc () = "%s__factoryContract" |
---|
| 104 | + | |
---|
| 105 | + | |
---|
| 106 | + | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
| 107 | + | |
---|
| 108 | + | |
---|
| 109 | + | func keyManagerVaultAddress () = "%s__managerVaultAddress" |
---|
| 110 | + | |
---|
| 111 | + | |
---|
| 112 | + | func pl () = "%s%s__price__last" |
---|
| 113 | + | |
---|
| 114 | + | |
---|
| 115 | + | func ph (h,t) = makeString(["%s%s%d%d__price__history", toString(h), toString(t)], SEP) |
---|
| 116 | + | |
---|
| 117 | + | |
---|
| 118 | + | func pau (ua,txId) = ((("%s%s%s__P__" + ua) + "__") + txId) |
---|
| 119 | + | |
---|
| 120 | + | |
---|
| 121 | + | func gau (ua,txId) = ((("%s%s%s__G__" + ua) + "__") + txId) |
---|
| 122 | + | |
---|
| 123 | + | |
---|
| 124 | + | func aa () = "%s__amountAsset" |
---|
| 125 | + | |
---|
| 126 | + | |
---|
| 127 | + | func pa () = "%s__priceAsset" |
---|
| 128 | + | |
---|
| 129 | + | |
---|
| 130 | + | func amp () = "%s__amp" |
---|
| 131 | + | |
---|
| 132 | + | |
---|
| 133 | + | func keyAmpHistory (heightBlocks) = ("%s%d__amp__" + toString(heightBlocks)) |
---|
| 134 | + | |
---|
| 135 | + | |
---|
| 136 | + | func keyChangeAmpLastCall () = "%s__changeAmpLastCall" |
---|
| 137 | + | |
---|
| 138 | + | |
---|
| 139 | + | let keyFee = "%s__fee" |
---|
| 140 | + | |
---|
| 141 | + | let fee = valueOrElse(getInteger(this, keyFee), feeDefault) |
---|
| 142 | + | |
---|
| 143 | + | let keyDLp = makeString(["%s", "dLp"], SEP) |
---|
| 144 | + | |
---|
| 145 | + | let keyDLpRefreshedHeight = makeString(["%s", "dLpRefreshedHeight"], SEP) |
---|
| 146 | + | |
---|
| 147 | + | let keyDLpRefreshDelay = makeString(["%s", "refreshDLpDelay"], SEP) |
---|
| 148 | + | |
---|
| 149 | + | let dLpRefreshDelayDefault = 30 |
---|
| 150 | + | |
---|
| 151 | + | let dLpRefreshDelay = valueOrElse(getInteger(this, keyDLpRefreshDelay), dLpRefreshDelayDefault) |
---|
| 152 | + | |
---|
| 153 | + | func fcfg () = "%s__factoryConfig" |
---|
| 154 | + | |
---|
| 155 | + | |
---|
| 156 | + | func mtpk () = "%s%s__matcher__publicKey" |
---|
| 157 | + | |
---|
| 158 | + | |
---|
| 159 | + | func pc (iAmtAs,iPrAs) = (((("%d%d%s__" + iAmtAs) + "__") + iPrAs) + "__config") |
---|
| 160 | + | |
---|
| 161 | + | |
---|
| 162 | + | func mba (bAStr) = ("%s%s%s__mappings__baseAsset2internalId__" + bAStr) |
---|
| 163 | + | |
---|
| 164 | + | |
---|
| 165 | + | func aps () = "%s__shutdown" |
---|
| 166 | + | |
---|
| 167 | + | |
---|
| 168 | + | func keyAllowedLpStableScriptHash () = "%s__allowedLpStableScriptHash" |
---|
| 169 | + | |
---|
| 170 | + | |
---|
| 171 | + | func keyFeeCollectorAddress () = "%s__feeCollectorAddress" |
---|
| 172 | + | |
---|
| 173 | + | |
---|
| 174 | + | func keySkipOrderValidation (poolAddress) = ("%s%s__skipOrderValidation__" + poolAddress) |
---|
| 175 | + | |
---|
| 176 | + | |
---|
| 177 | + | func throwOrderError (orderValid,orderValidInfo,senderValid,matcherValid) = throw((((((((("order validation failed: orderValid=" + toString(orderValid)) + " (") + orderValidInfo) + ")") + " senderValid=") + toString(senderValid)) + " matcherValid=") + toString(matcherValid))) |
---|
| 178 | + | |
---|
| 179 | + | |
---|
| 180 | + | func addressFromStringOrThis (addressString) = match addressFromString(addressString) { |
---|
| 181 | + | case a: Address => |
---|
| 182 | + | a |
---|
| 183 | + | case _ => |
---|
| 184 | + | this |
---|
| 185 | + | } |
---|
| 186 | + | |
---|
| 187 | + | |
---|
| 188 | + | func getManagerVaultAddressOrThis () = { |
---|
| 189 | + | let factoryAddress = match getString(fc()) { |
---|
| 190 | + | case fca: String => |
---|
| 191 | + | addressFromStringOrThis(fca) |
---|
| 192 | + | case _ => |
---|
| 193 | + | this |
---|
| 194 | + | } |
---|
| 195 | + | match getString(factoryAddress, keyManagerVaultAddress()) { |
---|
| 196 | + | case s: String => |
---|
| 197 | + | addressFromStringOrThis(s) |
---|
| 198 | + | case _ => |
---|
| 199 | + | this |
---|
| 200 | + | } |
---|
| 201 | + | } |
---|
| 202 | + | |
---|
| 203 | + | |
---|
| 204 | + | func strf (addr,key) = valueOrErrorMessage(getString(addr, key), makeString(["mandatory ", toString(addr), ".", key, " not defined"], "")) |
---|
| 205 | + | |
---|
| 206 | + | |
---|
| 207 | + | func intf (addr,key) = valueOrErrorMessage(getInteger(addr, key), makeString(["mandatory ", toString(addr), ".", key, " not defined"], "")) |
---|
| 208 | + | |
---|
| 209 | + | |
---|
| 210 | + | func throwErr (msg) = throw(makeString(["lp_stable.ride:", msg], " ")) |
---|
| 211 | + | |
---|
| 212 | + | |
---|
| 213 | + | func fmtErr (msg) = makeString(["lp_stable.ride:", msg], " ") |
---|
| 214 | + | |
---|
| 215 | + | |
---|
| 216 | + | let fca = addressFromStringValue(strf(this, fc())) |
---|
| 217 | + | |
---|
| 218 | + | let inFee = { |
---|
| 219 | + | let @ = invoke(fca, "getInFeeREADONLY", [toString(this)], nil) |
---|
| 220 | + | if ($isInstanceOf(@, "Int")) |
---|
| 221 | + | then @ |
---|
| 222 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 223 | + | } |
---|
| 224 | + | |
---|
| 225 | + | let outFee = { |
---|
| 226 | + | let @ = invoke(fca, "getOutFeeREADONLY", [toString(this)], nil) |
---|
| 227 | + | if ($isInstanceOf(@, "Int")) |
---|
| 228 | + | then @ |
---|
| 229 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 230 | + | } |
---|
| 231 | + | |
---|
| 232 | + | let A = strf(this, amp()) |
---|
| 233 | + | |
---|
| 234 | + | func igs () = valueOrElse(getBoolean(fca, aps()), false) |
---|
| 235 | + | |
---|
| 236 | + | |
---|
| 237 | + | func mp () = fromBase58String(strf(fca, mtpk())) |
---|
| 238 | + | |
---|
| 239 | + | |
---|
| 240 | + | let feeCollectorAddress = addressFromStringValue(strf(fca, keyFeeCollectorAddress())) |
---|
| 241 | + | |
---|
| 242 | + | func gpc () = { |
---|
| 243 | + | let amtAs = strf(this, aa()) |
---|
| 244 | + | let priceAs = strf(this, pa()) |
---|
| 245 | + | let iPriceAs = intf(fca, mba(priceAs)) |
---|
| 246 | + | let iAmtAs = intf(fca, mba(amtAs)) |
---|
| 247 | + | split(strf(fca, pc(toString(iAmtAs), toString(iPriceAs))), SEP) |
---|
| 248 | + | } |
---|
| 249 | + | |
---|
| 250 | + | |
---|
| 251 | + | func parseAssetId (input) = if ((input == wavesString)) |
---|
| 252 | + | then unit |
---|
| 253 | + | else fromBase58String(input) |
---|
| 254 | + | |
---|
| 255 | + | |
---|
| 256 | + | func assetIdToString (input) = if ((input == unit)) |
---|
| 257 | + | then wavesString |
---|
| 258 | + | else toBase58String(value(input)) |
---|
| 259 | + | |
---|
| 260 | + | |
---|
| 261 | + | func parsePoolConfig (poolConfig) = $Tuple7(addressFromStringValue(poolConfig[idxPoolAddress]), parseIntValue(poolConfig[idxPoolSt]), fromBase58String(poolConfig[idxLPAsId]), parseAssetId(poolConfig[idxAmAsId]), parseAssetId(poolConfig[idxPrAsId]), parseIntValue(poolConfig[idxAmtAsDcm]), parseIntValue(poolConfig[idxPriceAsDcm])) |
---|
| 262 | + | |
---|
| 263 | + | |
---|
| 264 | + | let poolConfigParsed = parsePoolConfig(gpc()) |
---|
| 265 | + | |
---|
| 266 | + | let $t086088794 = poolConfigParsed |
---|
| 267 | + | |
---|
| 268 | + | let cfgPoolAddress = $t086088794._1 |
---|
| 269 | + | |
---|
| 270 | + | let cfgPoolStatus = $t086088794._2 |
---|
| 271 | + | |
---|
| 272 | + | let cfgLpAssetId = $t086088794._3 |
---|
| 273 | + | |
---|
| 274 | + | let cfgAmountAssetId = $t086088794._4 |
---|
| 275 | + | |
---|
| 276 | + | let cfgPriceAssetId = $t086088794._5 |
---|
| 277 | + | |
---|
| 278 | + | let cfgAmountAssetDecimals = $t086088794._6 |
---|
| 279 | + | |
---|
| 280 | + | let cfgPriceAssetDecimals = $t086088794._7 |
---|
| 281 | + | |
---|
| 282 | + | func gfc () = split(strf(fca, fcfg()), SEP) |
---|
| 283 | + | |
---|
| 284 | + | |
---|
| 285 | + | let factoryConfig = gfc() |
---|
| 286 | + | |
---|
| 287 | + | let stakingContract = valueOrErrorMessage(addressFromString(factoryConfig[idxFactStakCntr]), "Invalid staking contract address") |
---|
| 288 | + | |
---|
| 289 | + | let slipageContract = valueOrErrorMessage(addressFromString(factoryConfig[idxFactSlippCntr]), "Invalid slipage contract address") |
---|
| 290 | + | |
---|
| 291 | + | let gwxContract = valueOrErrorMessage(addressFromString(factoryConfig[idxFactGwxRewCntr]), "Invalid gwx contract address") |
---|
| 292 | + | |
---|
| 293 | + | let restContract = valueOrErrorMessage(addressFromString(factoryConfig[idxFactoryRestCntr]), "Invalid gwx contract address") |
---|
| 294 | + | |
---|
| 295 | + | func dataPutActionInfo (inAmtAssetAmt,inPriceAssetAmt,outLpAmt,price,slipByUser,slippageReal,txHeight,txTimestamp,slipageAmAmt,slipagePrAmt) = makeString(["%d%d%d%d%d%d%d%d%d%d", toString(inAmtAssetAmt), toString(inPriceAssetAmt), toString(outLpAmt), toString(price), toString(slipByUser), toString(slippageReal), toString(txHeight), toString(txTimestamp), toString(slipageAmAmt), toString(slipagePrAmt)], SEP) |
---|
| 296 | + | |
---|
| 297 | + | |
---|
| 298 | + | 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) |
---|
| 299 | + | |
---|
| 300 | + | |
---|
| 301 | + | func getAccBalance (assetId) = if ((assetId == "WAVES")) |
---|
| 302 | + | then wavesBalance(this).available |
---|
| 303 | + | else assetBalance(this, fromBase58String(assetId)) |
---|
| 304 | + | |
---|
| 305 | + | |
---|
| 306 | + | func cpbi (prAmtX18,amAmtX18) = fraction(prAmtX18, scale18, amAmtX18) |
---|
| 307 | + | |
---|
| 308 | + | |
---|
| 309 | + | func cpbir (prAmtX18,amAmtX18,round) = fraction(prAmtX18, scale18, amAmtX18, round) |
---|
| 310 | + | |
---|
| 311 | + | |
---|
| 312 | + | func vad (A1,A2,slippage) = { |
---|
| 313 | + | let diff = fraction((A1 - A2), scale8BigInt, A2) |
---|
| 314 | + | let pass = ((slippage - abs(diff)) > zeroBigInt) |
---|
| 315 | + | if (!(pass)) |
---|
| 316 | + | then throw(("Big slpg: " + toString(diff))) |
---|
| 317 | + | else $Tuple2(pass, min([A1, A2])) |
---|
| 318 | + | } |
---|
| 319 | + | |
---|
| 320 | + | |
---|
| 321 | + | func vd (D1,D0,slpg) = { |
---|
| 322 | + | let diff = fraction(D0, scale8BigInt, D1) |
---|
| 323 | + | let fail = (slpg > diff) |
---|
| 324 | + | if (if (fail) |
---|
| 325 | + | then true |
---|
| 326 | + | else (D0 > D1)) |
---|
| 327 | + | then throw(((((((toString(D0) + " ") + toString(D1)) + " ") + toString(diff)) + " ") + toString(slpg))) |
---|
| 328 | + | else fail |
---|
| 329 | + | } |
---|
| 330 | + | |
---|
| 331 | + | |
---|
| 332 | + | func pcp (amAssetDcm,prAssetDcm,amAmt,prAmt) = { |
---|
| 333 | + | let amtAsAmtX18 = t1(amAmt, amAssetDcm) |
---|
| 334 | + | let prAsAmtX18 = t1(prAmt, prAssetDcm) |
---|
| 335 | + | cpbi(prAsAmtX18, amtAsAmtX18) |
---|
| 336 | + | } |
---|
| 337 | + | |
---|
| 338 | + | |
---|
| 339 | + | func calcPrices (amAmt,prAmt,lpAmt) = { |
---|
| 340 | + | let amtAsDcm = cfgAmountAssetDecimals |
---|
| 341 | + | let prAsDcm = cfgPriceAssetDecimals |
---|
| 342 | + | let priceX18 = pcp(amtAsDcm, prAsDcm, amAmt, prAmt) |
---|
| 343 | + | let amAmtX18 = t1(amAmt, amtAsDcm) |
---|
| 344 | + | let prAmtX18 = t1(prAmt, prAsDcm) |
---|
| 345 | + | let lpAmtX18 = t1(lpAmt, scale8) |
---|
| 346 | + | let lpPrInAmAsX18 = cpbi(amAmtX18, lpAmtX18) |
---|
| 347 | + | let lpPrInPrAsX18 = cpbi(prAmtX18, lpAmtX18) |
---|
| 348 | + | [priceX18, lpPrInAmAsX18, lpPrInPrAsX18] |
---|
| 349 | + | } |
---|
| 350 | + | |
---|
| 351 | + | |
---|
| 352 | + | func calculatePrices (amAmt,prAmt,lpAmt) = { |
---|
| 353 | + | let p = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 354 | + | [f1(p[0], scale8), f1(p[1], scale8), f1(p[2], scale8)] |
---|
| 355 | + | } |
---|
| 356 | + | |
---|
| 357 | + | |
---|
| 358 | + | func takeFee (amount,fee) = { |
---|
| 359 | + | let feeAmount = if ((fee == 0)) |
---|
| 360 | + | then 0 |
---|
| 361 | + | else fraction(amount, fee, scale8) |
---|
| 362 | + | $Tuple2((amount - feeAmount), feeAmount) |
---|
| 363 | + | } |
---|
| 364 | + | |
---|
| 365 | + | |
---|
| 366 | + | func getD (xp) = { |
---|
| 367 | + | let xp0 = xp[0] |
---|
| 368 | + | let xp1 = xp[1] |
---|
| 369 | + | let s = (xp0 + xp1) |
---|
| 370 | + | if ((s == big0)) |
---|
| 371 | + | then big0 |
---|
| 372 | + | else { |
---|
| 373 | + | let a = parseIntValue(A) |
---|
| 374 | + | let ann = (a * 2) |
---|
| 375 | + | let p = fraction(xp0, xp1, big1) |
---|
| 376 | + | let xp0_xp1_n_n = fraction(p, big4, big1) |
---|
| 377 | + | let ann_s = fraction(toBigInt(ann), s, big1) |
---|
| 378 | + | let ann_1 = toBigInt((ann - 1)) |
---|
| 379 | + | func calcDNext (d) = { |
---|
| 380 | + | let dd = fraction(d, d, big1) |
---|
| 381 | + | let ddd = fraction(dd, d, big1) |
---|
| 382 | + | let dp = fraction(ddd, big1, xp0_xp1_n_n) |
---|
| 383 | + | fraction((ann_s + fraction(dp, big2, big1)), d, (fraction(ann_1, d, big1) + fraction(big3, dp, big1))) |
---|
| 384 | + | } |
---|
| 385 | + | |
---|
| 386 | + | func calc (acc,i) = if (acc._2) |
---|
| 387 | + | then acc |
---|
| 388 | + | else { |
---|
| 389 | + | let d = acc._1 |
---|
| 390 | + | let dNext = calcDNext(d) |
---|
| 391 | + | let dDiffRaw = (dNext - value(d)) |
---|
| 392 | + | let dDiff = if ((big0 > dDiffRaw)) |
---|
| 393 | + | then -(dDiffRaw) |
---|
| 394 | + | else dDiffRaw |
---|
| 395 | + | if ((big1 >= dDiff)) |
---|
| 396 | + | then $Tuple2(dNext, true) |
---|
| 397 | + | else $Tuple2(dNext, false) |
---|
| 398 | + | } |
---|
| 399 | + | |
---|
| 400 | + | let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] |
---|
| 401 | + | let $t01329913347 = { |
---|
| 402 | + | let $l = arr |
---|
| 403 | + | let $s = size($l) |
---|
| 404 | + | let $acc0 = $Tuple2(s, false) |
---|
| 405 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 406 | + | then $a |
---|
| 407 | + | else calc($a, $l[$i]) |
---|
| 408 | + | |
---|
| 409 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 410 | + | then $a |
---|
| 411 | + | else throw("List size exceeds 17") |
---|
| 412 | + | |
---|
| 413 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17) |
---|
| 414 | + | } |
---|
| 415 | + | let d = $t01329913347._1 |
---|
| 416 | + | let found = $t01329913347._2 |
---|
| 417 | + | if (found) |
---|
| 418 | + | then d |
---|
| 419 | + | else throw(("D calculation error, D = " + toString(d))) |
---|
| 420 | + | } |
---|
| 421 | + | } |
---|
| 422 | + | |
---|
| 423 | + | |
---|
| 424 | + | func ego (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 425 | + | let lpId = cfgLpAssetId |
---|
| 426 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 427 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 428 | + | let amDcm = cfgAmountAssetDecimals |
---|
| 429 | + | let prDcm = cfgPriceAssetDecimals |
---|
| 430 | + | let sts = toString(cfgPoolStatus) |
---|
| 431 | + | let lpEmiss = valueOrErrorMessage(assetInfo(lpId), "Wrong LP id").quantity |
---|
| 432 | + | if ((toBase58String(lpId) != pmtAssetId)) |
---|
| 433 | + | then throw("Wrong pmt asset") |
---|
| 434 | + | else { |
---|
| 435 | + | let amBalance = getAccBalance(amId) |
---|
| 436 | + | let amBalanceX18 = t1(amBalance, amDcm) |
---|
| 437 | + | let prBalance = getAccBalance(prId) |
---|
| 438 | + | let prBalanceX18 = t1(prBalance, prDcm) |
---|
| 439 | + | let curPriceX18 = cpbi(prBalanceX18, amBalanceX18) |
---|
| 440 | + | let curPrice = f1(curPriceX18, scale8) |
---|
| 441 | + | let pmtLpAmtX18 = t1(pmtLpAmt, scale8) |
---|
| 442 | + | let lpEmissX18 = t1(lpEmiss, scale8) |
---|
| 443 | + | let outAmAmtX18 = fraction(amBalanceX18, pmtLpAmtX18, lpEmissX18) |
---|
| 444 | + | let outPrAmtX18 = fraction(prBalanceX18, pmtLpAmtX18, lpEmissX18) |
---|
| 445 | + | let outAmAmt = fromX18Round(outAmAmtX18, amDcm, FLOOR) |
---|
| 446 | + | let outPrAmt = fromX18Round(outPrAmtX18, prDcm, FLOOR) |
---|
| 447 | + | let state = if ((txId58 == "")) |
---|
| 448 | + | then nil |
---|
| 449 | + | else [ScriptTransfer(userAddress, outAmAmt, if ((amId == "WAVES")) |
---|
| 450 | + | then unit |
---|
| 451 | + | else fromBase58String(amId)), ScriptTransfer(userAddress, outPrAmt, if ((prId == "WAVES")) |
---|
| 452 | + | then unit |
---|
| 453 | + | else fromBase58String(prId)), StringEntry(gau(toString(userAddress), txId58), dataGetActionInfo(outAmAmt, outPrAmt, pmtLpAmt, curPrice, height, lastBlock.timestamp)), IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice)] |
---|
| 454 | + | $Tuple10(outAmAmt, outPrAmt, amId, prId, amBalance, prBalance, lpEmiss, curPriceX18, sts, state) |
---|
| 455 | + | } |
---|
| 456 | + | } |
---|
| 457 | + | |
---|
| 458 | + | |
---|
| 459 | + | func epo (txId58,slippage,inAmAmt,inAmId,inPrAmt,inPrId,userAddress,isEval,emitLp,isOneAsset,validateSlippage,pmtAmt,pmtId) = { |
---|
| 460 | + | let lpId = cfgLpAssetId |
---|
| 461 | + | let amIdStr = toBase58String(value(cfgAmountAssetId)) |
---|
| 462 | + | let prIdStr = toBase58String(value(cfgPriceAssetId)) |
---|
| 463 | + | let amtDcm = cfgAmountAssetDecimals |
---|
| 464 | + | let priceDcm = cfgPriceAssetDecimals |
---|
| 465 | + | let sts = toString(cfgPoolStatus) |
---|
| 466 | + | let lpEm = valueOrErrorMessage(assetInfo(lpId), "Wr lp as").quantity |
---|
| 467 | + | let amBalance = if (isEval) |
---|
| 468 | + | then getAccBalance(amIdStr) |
---|
| 469 | + | else if (if (isOneAsset) |
---|
| 470 | + | then (pmtId == amIdStr) |
---|
| 471 | + | else false) |
---|
| 472 | + | then (getAccBalance(amIdStr) - pmtAmt) |
---|
| 473 | + | else if (isOneAsset) |
---|
| 474 | + | then getAccBalance(amIdStr) |
---|
| 475 | + | else (getAccBalance(amIdStr) - inAmAmt) |
---|
| 476 | + | let prBalance = if (isEval) |
---|
| 477 | + | then getAccBalance(prIdStr) |
---|
| 478 | + | else if (if (isOneAsset) |
---|
| 479 | + | then (pmtId == prIdStr) |
---|
| 480 | + | else false) |
---|
| 481 | + | then (getAccBalance(prIdStr) - pmtAmt) |
---|
| 482 | + | else if (isOneAsset) |
---|
| 483 | + | then getAccBalance(prIdStr) |
---|
| 484 | + | else (getAccBalance(prIdStr) - inPrAmt) |
---|
| 485 | + | let inAmAssetAmtX18 = t1(inAmAmt, amtDcm) |
---|
| 486 | + | let inPrAssetAmtX18 = t1(inPrAmt, priceDcm) |
---|
| 487 | + | let userPriceX18 = cpbi(inPrAssetAmtX18, inAmAssetAmtX18) |
---|
| 488 | + | let amBalanceX18 = t1(amBalance, amtDcm) |
---|
| 489 | + | let prBalanceX18 = t1(prBalance, priceDcm) |
---|
| 490 | + | let D0 = getD([amBalanceX18, prBalanceX18]) |
---|
| 491 | + | let r = if ((lpEm == 0)) |
---|
| 492 | + | then { |
---|
| 493 | + | let D1 = getD([(amBalanceX18 + inAmAssetAmtX18), (prBalanceX18 + inPrAssetAmtX18)]) |
---|
| 494 | + | let checkD = if ((D1 > D0)) |
---|
| 495 | + | then true |
---|
| 496 | + | else throw("D1 should be greater than D0") |
---|
| 497 | + | if ((checkD == checkD)) |
---|
| 498 | + | then { |
---|
| 499 | + | let curPriceX18 = zeroBigInt |
---|
| 500 | + | let slippageX18 = zeroBigInt |
---|
| 501 | + | let lpAmtX18 = D1 |
---|
| 502 | + | $Tuple5(f1(lpAmtX18, scale8), f1(inAmAssetAmtX18, amtDcm), f1(inPrAssetAmtX18, priceDcm), cpbi((prBalanceX18 + inPrAssetAmtX18), (amBalanceX18 + inAmAssetAmtX18)), slippageX18) |
---|
| 503 | + | } |
---|
| 504 | + | else throw("Strict value is not equal to itself.") |
---|
| 505 | + | } |
---|
| 506 | + | else { |
---|
| 507 | + | let curPriceX18 = cpbi(prBalanceX18, amBalanceX18) |
---|
| 508 | + | let slippageRealX18 = fraction(abs((curPriceX18 - userPriceX18)), scale18, curPriceX18) |
---|
| 509 | + | let slippageX18 = t1(slippage, scale8) |
---|
| 510 | + | if (if (if (validateSlippage) |
---|
| 511 | + | then (curPriceX18 != zeroBigInt) |
---|
| 512 | + | else false) |
---|
| 513 | + | then (slippageRealX18 > slippageX18) |
---|
| 514 | + | else false) |
---|
| 515 | + | then throw(((("Price slippage " + toString(slippageRealX18)) + " > ") + toString(slippageX18))) |
---|
| 516 | + | else { |
---|
| 517 | + | let lpEmissionX18 = t1(lpEm, scale8) |
---|
| 518 | + | let prViaAmX18 = fraction(inAmAssetAmtX18, cpbir(prBalanceX18, amBalanceX18, CEILING), scale18, CEILING) |
---|
| 519 | + | let amViaPrX18 = fraction(inPrAssetAmtX18, scale18, cpbir(prBalanceX18, amBalanceX18, FLOOR), CEILING) |
---|
| 520 | + | let expectedAmts = if ((prViaAmX18 > inPrAssetAmtX18)) |
---|
| 521 | + | then $Tuple2(amViaPrX18, inPrAssetAmtX18) |
---|
| 522 | + | else $Tuple2(inAmAssetAmtX18, prViaAmX18) |
---|
| 523 | + | let expAmtAssetAmtX18 = expectedAmts._1 |
---|
| 524 | + | let expPriceAssetAmtX18 = expectedAmts._2 |
---|
| 525 | + | let D1 = getD([(amBalanceX18 + expAmtAssetAmtX18), (prBalanceX18 + expPriceAssetAmtX18)]) |
---|
| 526 | + | let checkD = if ((D1 > D0)) |
---|
| 527 | + | then true |
---|
| 528 | + | else throw("D1 should be greater than D0") |
---|
| 529 | + | if ((checkD == checkD)) |
---|
| 530 | + | then { |
---|
| 531 | + | let lpAmtX18 = fraction(lpEmissionX18, (D1 - D0), D0) |
---|
| 532 | + | $Tuple5(fromX18Round(lpAmtX18, scale8, FLOOR), fromX18Round(expAmtAssetAmtX18, amtDcm, CEILING), fromX18Round(expPriceAssetAmtX18, priceDcm, CEILING), curPriceX18, slippageX18) |
---|
| 533 | + | } |
---|
| 534 | + | else throw("Strict value is not equal to itself.") |
---|
| 535 | + | } |
---|
| 536 | + | } |
---|
| 537 | + | let calcLpAmt = r._1 |
---|
| 538 | + | let calcAmAssetPmt = r._2 |
---|
| 539 | + | let calcPrAssetPmt = r._3 |
---|
| 540 | + | let curPrice = f1(r._4, scale8) |
---|
| 541 | + | let slippageCalc = f1(r._5, scale8) |
---|
| 542 | + | if ((0 >= calcLpAmt)) |
---|
| 543 | + | then throw("LP <= 0") |
---|
| 544 | + | else { |
---|
| 545 | + | let emitLpAmt = if (!(emitLp)) |
---|
| 546 | + | then 0 |
---|
| 547 | + | else calcLpAmt |
---|
| 548 | + | let amDiff = (inAmAmt - calcAmAssetPmt) |
---|
| 549 | + | let prDiff = (inPrAmt - calcPrAssetPmt) |
---|
| 550 | + | let $t02110921454 = if (if (isOneAsset) |
---|
| 551 | + | then (pmtId == amIdStr) |
---|
| 552 | + | else false) |
---|
| 553 | + | then $Tuple2(pmtAmt, 0) |
---|
| 554 | + | else if (if (isOneAsset) |
---|
| 555 | + | then (pmtId == prIdStr) |
---|
| 556 | + | else false) |
---|
| 557 | + | then $Tuple2(0, pmtAmt) |
---|
| 558 | + | else $Tuple2(calcAmAssetPmt, calcPrAssetPmt) |
---|
| 559 | + | let writeAmAmt = $t02110921454._1 |
---|
| 560 | + | let writePrAmt = $t02110921454._2 |
---|
| 561 | + | let commonState = [IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice), StringEntry(pau(userAddress, txId58), dataPutActionInfo(writeAmAmt, writePrAmt, emitLpAmt, curPrice, slippage, slippageCalc, height, lastBlock.timestamp, amDiff, prDiff))] |
---|
| 562 | + | $Tuple13(calcLpAmt, emitLpAmt, curPrice, amBalance, prBalance, lpEm, lpId, sts, commonState, amDiff, prDiff, inAmId, inPrId) |
---|
| 563 | + | } |
---|
| 564 | + | } |
---|
| 565 | + | |
---|
| 566 | + | |
---|
| 567 | + | func getYD (xp,i,D) = { |
---|
| 568 | + | let n = big2 |
---|
| 569 | + | let x = xp[if ((i == 0)) |
---|
| 570 | + | then 1 |
---|
| 571 | + | else 0] |
---|
| 572 | + | let aPrecision = parseBigIntValue(Amult) |
---|
| 573 | + | let a = (parseBigIntValue(A) * aPrecision) |
---|
| 574 | + | let s = x |
---|
| 575 | + | let ann = (a * n) |
---|
| 576 | + | let c = (((((D * D) / (x * n)) * D) * aPrecision) / (ann * n)) |
---|
| 577 | + | let b = ((s + ((D * aPrecision) / ann)) - D) |
---|
| 578 | + | func calc (acc,cur) = { |
---|
| 579 | + | let $t02264922669 = acc |
---|
| 580 | + | let y = $t02264922669._1 |
---|
| 581 | + | let found = $t02264922669._2 |
---|
| 582 | + | if ((found != unit)) |
---|
| 583 | + | then acc |
---|
| 584 | + | else { |
---|
| 585 | + | let yNext = (((y * y) + c) / ((big2 * y) + b)) |
---|
| 586 | + | let yDiff = absBigInt((yNext - value(y))) |
---|
| 587 | + | if ((big1 >= yDiff)) |
---|
| 588 | + | then $Tuple2(yNext, cur) |
---|
| 589 | + | else $Tuple2(yNext, unit) |
---|
| 590 | + | } |
---|
| 591 | + | } |
---|
| 592 | + | |
---|
| 593 | + | let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
---|
| 594 | + | let $t02297623023 = { |
---|
| 595 | + | let $l = arr |
---|
| 596 | + | let $s = size($l) |
---|
| 597 | + | let $acc0 = $Tuple2(D, unit) |
---|
| 598 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 599 | + | then $a |
---|
| 600 | + | else calc($a, $l[$i]) |
---|
| 601 | + | |
---|
| 602 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 603 | + | then $a |
---|
| 604 | + | else throw("List size exceeds 15") |
---|
| 605 | + | |
---|
| 606 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15) |
---|
| 607 | + | } |
---|
| 608 | + | let y = $t02297623023._1 |
---|
| 609 | + | let found = $t02297623023._2 |
---|
| 610 | + | if ((found != unit)) |
---|
| 611 | + | then y |
---|
| 612 | + | else throw(("Y calculation error, Y = " + toString(y))) |
---|
| 613 | + | } |
---|
| 614 | + | |
---|
| 615 | + | |
---|
| 616 | + | func calcDLp (amountBalance,priceBalance,lpEmission) = { |
---|
| 617 | + | let updatedDLp = fraction(getD([t1BigInt(amountBalance, toBigInt(cfgAmountAssetDecimals)), t1BigInt(priceBalance, toBigInt(cfgPriceAssetDecimals))]), scale18, lpEmission) |
---|
| 618 | + | if ((lpEmission == big0)) |
---|
| 619 | + | then big0 |
---|
| 620 | + | else updatedDLp |
---|
| 621 | + | } |
---|
| 622 | + | |
---|
| 623 | + | |
---|
| 624 | + | func calcCurrentDLp (amountAssetDelta,priceAssetDelta,lpAssetEmissionDelta) = { |
---|
| 625 | + | let amountAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgAmountAssetId))) - amountAssetDelta) |
---|
| 626 | + | let priceAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgPriceAssetId))) - priceAssetDelta) |
---|
| 627 | + | let lpAssetEmission = (toBigInt(value(assetInfo(cfgLpAssetId)).quantity) - lpAssetEmissionDelta) |
---|
| 628 | + | let currentDLp = calcDLp(amountAssetBalance, priceAssetBalance, lpAssetEmission) |
---|
| 629 | + | currentDLp |
---|
| 630 | + | } |
---|
| 631 | + | |
---|
| 632 | + | |
---|
| 633 | + | func refreshDLpInternal (amountAssetBalanceDelta,priceAssetBalanceDelta,lpAssetEmissionDelta) = { |
---|
| 634 | + | let amountAssetBalance = (getAccBalance(assetIdToString(cfgAmountAssetId)) + amountAssetBalanceDelta) |
---|
| 635 | + | let priceAssetBalance = (getAccBalance(assetIdToString(cfgPriceAssetId)) + priceAssetBalanceDelta) |
---|
| 636 | + | let lpAssetEmission = (value(assetInfo(cfgLpAssetId)).quantity + lpAssetEmissionDelta) |
---|
| 637 | + | let updatedDLp = calcDLp(toBigInt(amountAssetBalance), toBigInt(priceAssetBalance), toBigInt(lpAssetEmission)) |
---|
| 638 | + | let actions = [IntegerEntry(keyDLpRefreshedHeight, height), StringEntry(keyDLp, toString(updatedDLp))] |
---|
| 639 | + | $Tuple2(actions, updatedDLp) |
---|
| 640 | + | } |
---|
| 641 | + | |
---|
| 642 | + | |
---|
| 643 | + | func validateUpdatedDLp (oldDLp,updatedDLp) = if ((updatedDLp >= oldDLp)) |
---|
| 644 | + | then true |
---|
| 645 | + | else throwErr("updated DLp lower than current DLp") |
---|
| 646 | + | |
---|
| 647 | + | |
---|
| 648 | + | func validateMatcherOrderAllowed (order) = { |
---|
| 649 | + | let amountAssetBalance = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 650 | + | let priceAssetBalance = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 651 | + | let amountAssetAmount = order.amount |
---|
| 652 | + | let priceAssetAmount = fraction(order.amount, order.price, scale8, FLOOR) |
---|
| 653 | + | let $t02522125433 = if ((order.orderType == Buy)) |
---|
| 654 | + | then $Tuple2(amountAssetAmount, -(priceAssetAmount)) |
---|
| 655 | + | else $Tuple2(-(amountAssetAmount), priceAssetAmount) |
---|
| 656 | + | let amountAssetBalanceDelta = $t02522125433._1 |
---|
| 657 | + | let priceAssetBalanceDelta = $t02522125433._2 |
---|
| 658 | + | if (if (if (igs()) |
---|
| 659 | + | then true |
---|
| 660 | + | else (cfgPoolStatus == PoolMatcherDis)) |
---|
| 661 | + | then true |
---|
| 662 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 663 | + | then throw("Admin blocked") |
---|
| 664 | + | else if (if ((order.assetPair.amountAsset != cfgAmountAssetId)) |
---|
| 665 | + | then true |
---|
| 666 | + | else (order.assetPair.priceAsset != cfgPriceAssetId)) |
---|
| 667 | + | then throw("Wr assets") |
---|
| 668 | + | else { |
---|
| 669 | + | let dLp = parseBigIntValue(valueOrElse(getString(this, keyDLp), "0")) |
---|
| 670 | + | let $t02577525875 = refreshDLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 671 | + | let unusedActions = $t02577525875._1 |
---|
| 672 | + | let dLpNew = $t02577525875._2 |
---|
| 673 | + | let isOrderValid = (dLpNew >= dLp) |
---|
| 674 | + | let info = makeString(["dLp=", toString(dLp), " dLpNew=", toString(dLpNew), " amountAssetBalance=", toString(amountAssetBalance), " priceAssetBalance=", toString(priceAssetBalance), " amountAssetBalanceDelta=", toString(amountAssetBalanceDelta), " priceAssetBalanceDelta=", toString(priceAssetBalanceDelta), " height=", toString(height)], "") |
---|
| 675 | + | $Tuple2(isOrderValid, info) |
---|
| 676 | + | } |
---|
| 677 | + | } |
---|
| 678 | + | |
---|
| 679 | + | |
---|
| 680 | + | func cg (i) = if ((size(i.payments) != 1)) |
---|
| 681 | + | then throw("1 pmnt exp") |
---|
| 682 | + | else { |
---|
| 683 | + | let pmt = value(i.payments[0]) |
---|
| 684 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 685 | + | let pmtAmt = pmt.amount |
---|
| 686 | + | let r = ego(toBase58String(i.transactionId), toBase58String(pmtAssetId), pmtAmt, i.caller) |
---|
| 687 | + | let outAmAmt = r._1 |
---|
| 688 | + | let outPrAmt = r._2 |
---|
| 689 | + | let sts = parseIntValue(r._9) |
---|
| 690 | + | let state = r._10 |
---|
| 691 | + | if (if (igs()) |
---|
| 692 | + | then true |
---|
| 693 | + | else (sts == PoolShutdown)) |
---|
| 694 | + | then throw(("Admin blocked: " + toString(sts))) |
---|
| 695 | + | else $Tuple5(outAmAmt, outPrAmt, pmtAmt, pmtAssetId, state) |
---|
| 696 | + | } |
---|
| 697 | + | |
---|
| 698 | + | |
---|
| 699 | + | func cp (caller,txId,amAsPmt,prAsPmt,slippage,emitLp,isOneAsset,validateSlippage,pmtAmt,pmtId) = { |
---|
| 700 | + | let r = epo(txId, slippage, value(amAsPmt).amount, value(amAsPmt).assetId, value(prAsPmt).amount, value(prAsPmt).assetId, caller, (txId == ""), emitLp, isOneAsset, validateSlippage, pmtAmt, pmtId) |
---|
| 701 | + | let sts = parseIntValue(r._8) |
---|
| 702 | + | if (if (if (igs()) |
---|
| 703 | + | then true |
---|
| 704 | + | else (sts == PoolPutDis)) |
---|
| 705 | + | then true |
---|
| 706 | + | else (sts == PoolShutdown)) |
---|
| 707 | + | then throw(("Blocked:" + toString(sts))) |
---|
| 708 | + | else r |
---|
| 709 | + | } |
---|
| 710 | + | |
---|
| 711 | + | |
---|
| 712 | + | func calcPutOneTkn (pmtAmtRaw,pmtAssetId,userAddress,txId,withTakeFee) = { |
---|
| 713 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 714 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 715 | + | let lpId = cfgLpAssetId |
---|
| 716 | + | let amtDcm = cfgAmountAssetDecimals |
---|
| 717 | + | let priceDcm = cfgPriceAssetDecimals |
---|
| 718 | + | let lpAssetEmission = toBigInt(valueOrErrorMessage(assetInfo(lpId), "invalid lp asset").quantity) |
---|
| 719 | + | let chechEmission = if ((lpAssetEmission > big0)) |
---|
| 720 | + | then true |
---|
| 721 | + | else throw("initial deposit requires all coins") |
---|
| 722 | + | if ((chechEmission == chechEmission)) |
---|
| 723 | + | then { |
---|
| 724 | + | let amBalance = getAccBalance(amId) |
---|
| 725 | + | let prBalance = getAccBalance(prId) |
---|
| 726 | + | let $t02853128993 = if ((txId == "")) |
---|
| 727 | + | then $Tuple2(amBalance, prBalance) |
---|
| 728 | + | else if ((pmtAssetId == amId)) |
---|
| 729 | + | then if ((pmtAmtRaw > amBalance)) |
---|
| 730 | + | then throw("invalid payment amount") |
---|
| 731 | + | else $Tuple2((amBalance - pmtAmtRaw), prBalance) |
---|
| 732 | + | else if ((pmtAssetId == prId)) |
---|
| 733 | + | then if ((pmtAmtRaw > prBalance)) |
---|
| 734 | + | then throw("invalid payment amount") |
---|
| 735 | + | else $Tuple2(amBalance, (prBalance - pmtAmtRaw)) |
---|
| 736 | + | else throw("wrong pmtAssetId") |
---|
| 737 | + | let amBalanceOld = $t02853128993._1 |
---|
| 738 | + | let prBalanceOld = $t02853128993._2 |
---|
| 739 | + | let $t02899929175 = if ((pmtAssetId == amId)) |
---|
| 740 | + | then $Tuple2(pmtAmtRaw, 0) |
---|
| 741 | + | else if ((pmtAssetId == prId)) |
---|
| 742 | + | then $Tuple2(0, pmtAmtRaw) |
---|
| 743 | + | else throw("invalid payment") |
---|
| 744 | + | let amAmountRaw = $t02899929175._1 |
---|
| 745 | + | let prAmountRaw = $t02899929175._2 |
---|
| 746 | + | let $t02917929433 = if (withTakeFee) |
---|
| 747 | + | then $Tuple3(takeFee(amAmountRaw, inFee)._1, takeFee(prAmountRaw, inFee)._1, takeFee(pmtAmtRaw, inFee)._2) |
---|
| 748 | + | else $Tuple3(amAmountRaw, prAmountRaw, 0) |
---|
| 749 | + | let amAmount = $t02917929433._1 |
---|
| 750 | + | let prAmount = $t02917929433._2 |
---|
| 751 | + | let feeAmount = $t02917929433._3 |
---|
| 752 | + | let amBalanceNew = (amBalanceOld + amAmount) |
---|
| 753 | + | let prBalanceNew = (prBalanceOld + prAmount) |
---|
| 754 | + | let D0 = getD([t1(amBalanceOld, cfgAmountAssetDecimals), t1(prBalanceOld, cfgPriceAssetDecimals)]) |
---|
| 755 | + | let D1 = getD([t1(amBalanceNew, cfgAmountAssetDecimals), t1(prBalanceNew, cfgPriceAssetDecimals)]) |
---|
| 756 | + | let checkD = if ((D1 > D0)) |
---|
| 757 | + | then true |
---|
| 758 | + | else throw() |
---|
| 759 | + | if ((checkD == checkD)) |
---|
| 760 | + | then { |
---|
| 761 | + | let lpAmount = fraction(lpAssetEmission, (D1 - D0), D0, FLOOR) |
---|
| 762 | + | let curPrice = f1(cpbi(t1(prBalanceNew, priceDcm), t1(amBalanceNew, amtDcm)), scale8) |
---|
| 763 | + | let commonState = [IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice), StringEntry(pau(userAddress, txId), dataPutActionInfo(amAmountRaw, prAmountRaw, toInt(lpAmount), curPrice, 0, 0, height, lastBlock.timestamp, 0, 0))] |
---|
| 764 | + | let poolProportion = fraction(prBalanceOld, scale8, amBalanceOld) |
---|
| 765 | + | let amountAssetPart = fraction(pmtAmtRaw, scale8, (poolProportion + scale8)) |
---|
| 766 | + | let priceAssetPart = (pmtAmtRaw - amountAssetPart) |
---|
| 767 | + | let lpAmtBoth = fraction(lpAssetEmission, toBigInt(priceAssetPart), toBigInt(prBalanceOld)) |
---|
| 768 | + | let bonus = toInt(fraction((lpAmount - lpAmtBoth), scale8BigInt, lpAmtBoth)) |
---|
| 769 | + | $Tuple4(toInt(lpAmount), commonState, feeAmount, bonus) |
---|
| 770 | + | } |
---|
| 771 | + | else throw("Strict value is not equal to itself.") |
---|
| 772 | + | } |
---|
| 773 | + | else throw("Strict value is not equal to itself.") |
---|
| 774 | + | } |
---|
| 775 | + | |
---|
| 776 | + | |
---|
| 777 | + | func getOneTknV2Internal (outAssetId,minOutAmount,payments,caller,originCaller,transactionId) = { |
---|
| 778 | + | let lpId = toBase58String(value(cfgLpAssetId)) |
---|
| 779 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 780 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 781 | + | let amDecimals = cfgAmountAssetDecimals |
---|
| 782 | + | let prDecimals = cfgPriceAssetDecimals |
---|
| 783 | + | let poolStatus = cfgPoolStatus |
---|
| 784 | + | let userAddress = if ((caller == restContract)) |
---|
| 785 | + | then originCaller |
---|
| 786 | + | else caller |
---|
| 787 | + | let pmt = value(payments[0]) |
---|
| 788 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 789 | + | let pmtAmt = pmt.amount |
---|
| 790 | + | let currentDLp = calcCurrentDLp(big0, big0, big0) |
---|
| 791 | + | if ((currentDLp == currentDLp)) |
---|
| 792 | + | then { |
---|
| 793 | + | let txId58 = toBase58String(transactionId) |
---|
| 794 | + | if ((lpId != toBase58String(pmtAssetId))) |
---|
| 795 | + | then throw("Wrong LP") |
---|
| 796 | + | else { |
---|
| 797 | + | let amBalance = getAccBalance(amId) |
---|
| 798 | + | let prBalance = getAccBalance(prId) |
---|
| 799 | + | let $t03154531656 = { |
---|
| 800 | + | let @ = invoke(this, "getOneTknV2READONLY", [outAssetId, pmtAmt], nil) |
---|
| 801 | + | if ($isInstanceOf(@, "(Int, Int)")) |
---|
| 802 | + | then @ |
---|
| 803 | + | else throw(($getType(@) + " couldn't be cast to (Int, Int)")) |
---|
| 804 | + | } |
---|
| 805 | + | if (($t03154531656 == $t03154531656)) |
---|
| 806 | + | then { |
---|
| 807 | + | let feeAmount = $t03154531656._2 |
---|
| 808 | + | let totalGet = $t03154531656._1 |
---|
| 809 | + | let totalAmount = if (if ((minOutAmount > 0)) |
---|
| 810 | + | then (minOutAmount > totalGet) |
---|
| 811 | + | else false) |
---|
| 812 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 813 | + | else totalGet |
---|
| 814 | + | let $t03184632153 = if ((outAssetId == amId)) |
---|
| 815 | + | then $Tuple4(totalAmount, 0, ((amBalance - totalAmount) - feeAmount), prBalance) |
---|
| 816 | + | else if ((outAssetId == prId)) |
---|
| 817 | + | then $Tuple4(0, totalAmount, amBalance, ((prBalance - totalAmount) - feeAmount)) |
---|
| 818 | + | else throw("invalid out asset id") |
---|
| 819 | + | let outAm = $t03184632153._1 |
---|
| 820 | + | let outPr = $t03184632153._2 |
---|
| 821 | + | let amBalanceNew = $t03184632153._3 |
---|
| 822 | + | let prBalanceNew = $t03184632153._4 |
---|
| 823 | + | let curPrX18 = cpbi(t1(prBalanceNew, prDecimals), t1(amBalanceNew, amDecimals)) |
---|
| 824 | + | let curPr = f1(curPrX18, scale8) |
---|
| 825 | + | let outAssetIdOrWaves = if ((outAssetId == "WAVES")) |
---|
| 826 | + | then unit |
---|
| 827 | + | else fromBase58String(outAssetId) |
---|
| 828 | + | let sendFeeToMatcher = if ((feeAmount > 0)) |
---|
| 829 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, outAssetIdOrWaves)] |
---|
| 830 | + | else nil |
---|
| 831 | + | let state = ([ScriptTransfer(userAddress, totalAmount, outAssetIdOrWaves), StringEntry(gau(toString(userAddress), txId58), dataGetActionInfo(outAm, outPr, pmtAmt, curPr, height, lastBlock.timestamp)), IntegerEntry(pl(), curPr), IntegerEntry(ph(height, lastBlock.timestamp), curPr)] ++ sendFeeToMatcher) |
---|
| 832 | + | if ((state == state)) |
---|
| 833 | + | then { |
---|
| 834 | + | let burn = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 835 | + | if ((burn == burn)) |
---|
| 836 | + | then { |
---|
| 837 | + | let $t03293833288 = { |
---|
| 838 | + | let feeAmountForCalc = if ((this == feeCollectorAddress)) |
---|
| 839 | + | then 0 |
---|
| 840 | + | else feeAmount |
---|
| 841 | + | let outInAmountAsset = if ((parseAssetId(outAssetId) == cfgAmountAssetId)) |
---|
| 842 | + | then true |
---|
| 843 | + | else false |
---|
| 844 | + | if (outInAmountAsset) |
---|
| 845 | + | then $Tuple2(-((totalGet + feeAmountForCalc)), 0) |
---|
| 846 | + | else $Tuple2(0, -((totalGet + feeAmountForCalc))) |
---|
| 847 | + | } |
---|
| 848 | + | let amountAssetBalanceDelta = $t03293833288._1 |
---|
| 849 | + | let priceAssetBalanceDelta = $t03293833288._2 |
---|
| 850 | + | let $t03329133399 = refreshDLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 851 | + | let refreshDLpActions = $t03329133399._1 |
---|
| 852 | + | let updatedDLp = $t03329133399._2 |
---|
| 853 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 854 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 855 | + | then $Tuple2((state ++ refreshDLpActions), totalAmount) |
---|
| 856 | + | else throw("Strict value is not equal to itself.") |
---|
| 857 | + | } |
---|
| 858 | + | else throw("Strict value is not equal to itself.") |
---|
| 859 | + | } |
---|
| 860 | + | else throw("Strict value is not equal to itself.") |
---|
| 861 | + | } |
---|
| 862 | + | else throw("Strict value is not equal to itself.") |
---|
| 863 | + | } |
---|
| 864 | + | } |
---|
| 865 | + | else throw("Strict value is not equal to itself.") |
---|
| 866 | + | } |
---|
| 867 | + | |
---|
| 868 | + | |
---|
| 869 | + | func managerPublicKeyOrUnit () = { |
---|
| 870 | + | let managerVaultAddress = getManagerVaultAddressOrThis() |
---|
| 871 | + | match getString(managerVaultAddress, keyManagerPublicKey()) { |
---|
| 872 | + | case s: String => |
---|
| 873 | + | fromBase58String(s) |
---|
| 874 | + | case _: Unit => |
---|
| 875 | + | unit |
---|
| 876 | + | case _ => |
---|
| 877 | + | throw("Match error") |
---|
| 878 | + | } |
---|
| 879 | + | } |
---|
| 880 | + | |
---|
| 881 | + | |
---|
| 882 | + | let pd = throw("Permission denied") |
---|
| 883 | + | |
---|
| 884 | + | func isManager (i) = match managerPublicKeyOrUnit() { |
---|
| 885 | + | case pk: ByteVector => |
---|
| 886 | + | (i.callerPublicKey == pk) |
---|
| 887 | + | case _: Unit => |
---|
| 888 | + | (i.caller == this) |
---|
| 889 | + | case _ => |
---|
| 890 | + | throw("Match error") |
---|
| 891 | + | } |
---|
| 892 | + | |
---|
| 893 | + | |
---|
| 894 | + | func mustManager (i) = match managerPublicKeyOrUnit() { |
---|
| 895 | + | case pk: ByteVector => |
---|
| 896 | + | if ((i.callerPublicKey == pk)) |
---|
| 897 | + | then true |
---|
| 898 | + | else pd |
---|
| 899 | + | case _: Unit => |
---|
| 900 | + | if ((i.caller == this)) |
---|
| 901 | + | then true |
---|
| 902 | + | else pd |
---|
| 903 | + | case _ => |
---|
| 904 | + | throw("Match error") |
---|
| 905 | + | } |
---|
| 906 | + | |
---|
| 907 | + | |
---|
| 908 | + | func getY (isReverse,D,poolAmountInBalance) = { |
---|
| 909 | + | let poolConfig = gpc() |
---|
| 910 | + | let amId = poolConfig[idxAmAsId] |
---|
| 911 | + | let prId = poolConfig[idxPrAsId] |
---|
| 912 | + | let n = big2 |
---|
| 913 | + | let aPrecision = parseBigIntValue(Amult) |
---|
| 914 | + | let a = (parseBigIntValue(A) * aPrecision) |
---|
| 915 | + | let xp = if ((isReverse == false)) |
---|
| 916 | + | then [(toBigInt(getAccBalance(amId)) + poolAmountInBalance), toBigInt(getAccBalance(prId))] |
---|
| 917 | + | else [(toBigInt(getAccBalance(prId)) + poolAmountInBalance), toBigInt(getAccBalance(amId))] |
---|
| 918 | + | let x = xp[0] |
---|
| 919 | + | let s = x |
---|
| 920 | + | let ann = (a * n) |
---|
| 921 | + | let c = (((((D * D) / (x * n)) * D) * aPrecision) / (ann * n)) |
---|
| 922 | + | let b = ((s + ((D * aPrecision) / ann)) - D) |
---|
| 923 | + | func calc (acc,cur) = { |
---|
| 924 | + | let $t03488634906 = acc |
---|
| 925 | + | let y = $t03488634906._1 |
---|
| 926 | + | let found = $t03488634906._2 |
---|
| 927 | + | if ((found != unit)) |
---|
| 928 | + | then acc |
---|
| 929 | + | else { |
---|
| 930 | + | let yNext = (((y * y) + c) / ((big2 * y) + b)) |
---|
| 931 | + | let yDiff = absBigInt((yNext - value(y))) |
---|
| 932 | + | if ((big1 >= yDiff)) |
---|
| 933 | + | then $Tuple2(yNext, cur) |
---|
| 934 | + | else $Tuple2(yNext, unit) |
---|
| 935 | + | } |
---|
| 936 | + | } |
---|
| 937 | + | |
---|
| 938 | + | let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
---|
| 939 | + | let $t03523735284 = { |
---|
| 940 | + | let $l = arr |
---|
| 941 | + | let $s = size($l) |
---|
| 942 | + | let $acc0 = $Tuple2(D, unit) |
---|
| 943 | + | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
| 944 | + | then $a |
---|
| 945 | + | else calc($a, $l[$i]) |
---|
| 946 | + | |
---|
| 947 | + | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
| 948 | + | then $a |
---|
| 949 | + | else throw("List size exceeds 15") |
---|
| 950 | + | |
---|
| 951 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15) |
---|
| 952 | + | } |
---|
| 953 | + | let y = $t03523735284._1 |
---|
| 954 | + | let found = $t03523735284._2 |
---|
| 955 | + | if ((found != unit)) |
---|
| 956 | + | then y |
---|
| 957 | + | else throw(("Y calculation error, Y = " + toString(y))) |
---|
| 958 | + | } |
---|
| 959 | + | |
---|
| 960 | + | |
---|
| 961 | + | func skipOrderValidation () = valueOrElse(getBoolean(fca, keySkipOrderValidation(toString(this))), false) |
---|
| 962 | + | |
---|
| 963 | + | |
---|
| 964 | + | @Callable(i) |
---|
| 965 | + | func calculateAmountOutForSwapREADONLY (cleanAmountIn,isReverse,feePoolAmount) = { |
---|
| 966 | + | let $t03563636060 = if ((isReverse == false)) |
---|
| 967 | + | then { |
---|
| 968 | + | let assetOut = strf(this, pa()) |
---|
| 969 | + | let poolAmountInBalance = (toBigInt(getAccBalance(strf(this, aa()))) + toBigInt(cleanAmountIn)) |
---|
| 970 | + | $Tuple2(assetOut, poolAmountInBalance) |
---|
| 971 | + | } |
---|
| 972 | + | else { |
---|
| 973 | + | let assetOut = strf(this, aa()) |
---|
| 974 | + | let poolAmountInBalance = (toBigInt(getAccBalance(strf(this, pa()))) + toBigInt(cleanAmountIn)) |
---|
| 975 | + | $Tuple2(assetOut, poolAmountInBalance) |
---|
| 976 | + | } |
---|
| 977 | + | let assetOut = $t03563636060._1 |
---|
| 978 | + | let poolAmountInBalance = $t03563636060._2 |
---|
| 979 | + | let poolConfig = gpc() |
---|
| 980 | + | let amId = poolConfig[idxAmAsId] |
---|
| 981 | + | let prId = poolConfig[idxPrAsId] |
---|
| 982 | + | let xp = [toBigInt(getAccBalance(amId)), toBigInt(getAccBalance(prId))] |
---|
| 983 | + | let D = getD(xp) |
---|
| 984 | + | let y = getY(isReverse, D, toBigInt(cleanAmountIn)) |
---|
| 985 | + | let dy = ((toBigInt(getAccBalance(assetOut)) - y) - toBigInt(1)) |
---|
| 986 | + | let totalGetRaw = max([0, toInt(dy)]) |
---|
| 987 | + | let newXp = if ((isReverse == false)) |
---|
| 988 | + | then [((toBigInt(getAccBalance(amId)) + toBigInt(cleanAmountIn)) + toBigInt(feePoolAmount)), (toBigInt(getAccBalance(prId)) - dy)] |
---|
| 989 | + | else [(toBigInt(getAccBalance(amId)) - dy), ((toBigInt(getAccBalance(prId)) + toBigInt(cleanAmountIn)) + toBigInt(feePoolAmount))] |
---|
| 990 | + | let newD = getD(newXp) |
---|
| 991 | + | let checkD = if ((newD >= D)) |
---|
| 992 | + | then true |
---|
| 993 | + | else throw(makeString(["new D is fewer error", toString(D), toString(newD)], "__")) |
---|
| 994 | + | if ((checkD == checkD)) |
---|
| 995 | + | then $Tuple2(nil, totalGetRaw) |
---|
| 996 | + | else throw("Strict value is not equal to itself.") |
---|
| 997 | + | } |
---|
| 998 | + | |
---|
| 999 | + | |
---|
| 1000 | + | |
---|
| 1001 | + | @Callable(i) |
---|
| 1002 | + | func calculateAmountOutForSwapAndSendTokens (cleanAmountIn,isReverse,amountOutMin,addressTo,feePoolAmount) = { |
---|
| 1003 | + | let swapContact = { |
---|
| 1004 | + | let @ = invoke(fca, "getSwapContractREADONLY", nil, nil) |
---|
| 1005 | + | if ($isInstanceOf(@, "String")) |
---|
| 1006 | + | then @ |
---|
| 1007 | + | else throw(($getType(@) + " couldn't be cast to String")) |
---|
| 1008 | + | } |
---|
| 1009 | + | let checks = [if ((value(i.payments[0]).amount >= cleanAmountIn)) |
---|
| 1010 | + | then true |
---|
| 1011 | + | else throwErr("Wrong amount"), if ((i.caller == addressFromStringValue(swapContact))) |
---|
| 1012 | + | then true |
---|
| 1013 | + | else throwErr("Permission denied")] |
---|
| 1014 | + | if ((checks == checks)) |
---|
| 1015 | + | then { |
---|
| 1016 | + | let pmt = value(i.payments[0]) |
---|
| 1017 | + | let assetIn = assetIdToString(pmt.assetId) |
---|
| 1018 | + | let $t03749637890 = if ((isReverse == false)) |
---|
| 1019 | + | then { |
---|
| 1020 | + | let assetOut = strf(this, pa()) |
---|
| 1021 | + | let poolAmountInBalance = (getAccBalance(assetIn) - value(i.payments[0]).amount) |
---|
| 1022 | + | $Tuple2(assetOut, poolAmountInBalance) |
---|
| 1023 | + | } |
---|
| 1024 | + | else { |
---|
| 1025 | + | let assetOut = strf(this, aa()) |
---|
| 1026 | + | let poolAmountInBalance = (getAccBalance(assetIn) - value(i.payments[0]).amount) |
---|
| 1027 | + | $Tuple2(assetOut, poolAmountInBalance) |
---|
| 1028 | + | } |
---|
| 1029 | + | let assetOut = $t03749637890._1 |
---|
| 1030 | + | let poolAmountInBalance = $t03749637890._2 |
---|
| 1031 | + | let poolConfig = gpc() |
---|
| 1032 | + | let amId = poolConfig[idxAmAsId] |
---|
| 1033 | + | let prId = poolConfig[idxPrAsId] |
---|
| 1034 | + | let xp = if ((isReverse == false)) |
---|
| 1035 | + | then [(toBigInt(getAccBalance(amId)) - toBigInt(value(i.payments[0]).amount)), toBigInt(getAccBalance(prId))] |
---|
| 1036 | + | else [toBigInt(getAccBalance(amId)), (toBigInt(getAccBalance(prId)) - toBigInt(value(i.payments[0]).amount))] |
---|
| 1037 | + | let D = getD(xp) |
---|
| 1038 | + | let y = getY(isReverse, D, toBigInt(0)) |
---|
| 1039 | + | let dy = ((toBigInt(getAccBalance(assetOut)) - y) - toBigInt(1)) |
---|
| 1040 | + | let totalGetRaw = max([0, toInt(dy)]) |
---|
| 1041 | + | let checkMin = if ((totalGetRaw >= amountOutMin)) |
---|
| 1042 | + | then true |
---|
| 1043 | + | else throw("Exchange result is fewer coins than expected") |
---|
| 1044 | + | if ((checkMin == checkMin)) |
---|
| 1045 | + | then { |
---|
| 1046 | + | let newXp = if ((isReverse == false)) |
---|
| 1047 | + | then [(toBigInt(getAccBalance(amId)) + toBigInt(feePoolAmount)), (toBigInt(getAccBalance(prId)) - dy)] |
---|
| 1048 | + | else [(toBigInt(getAccBalance(amId)) - dy), (toBigInt(getAccBalance(prId)) + toBigInt(feePoolAmount))] |
---|
| 1049 | + | let newD = getD(newXp) |
---|
| 1050 | + | let checkD = if ((newD >= D)) |
---|
| 1051 | + | then true |
---|
| 1052 | + | else throw("new D is fewer error") |
---|
| 1053 | + | if ((checkD == checkD)) |
---|
| 1054 | + | then $Tuple2([ScriptTransfer(addressFromStringValue(addressTo), totalGetRaw, parseAssetId(assetOut))], totalGetRaw) |
---|
| 1055 | + | else throw("Strict value is not equal to itself.") |
---|
| 1056 | + | } |
---|
| 1057 | + | else throw("Strict value is not equal to itself.") |
---|
| 1058 | + | } |
---|
| 1059 | + | else throw("Strict value is not equal to itself.") |
---|
| 1060 | + | } |
---|
| 1061 | + | |
---|
| 1062 | + | |
---|
| 1063 | + | |
---|
| 1064 | + | @Callable(i) |
---|
| 1065 | + | func constructor (fc) = { |
---|
| 1066 | + | let c = mustManager(i) |
---|
| 1067 | + | if ((c == c)) |
---|
| 1068 | + | then [StringEntry(fc(), fc)] |
---|
| 1069 | + | else throw("Strict value is not equal to itself.") |
---|
| 1070 | + | } |
---|
| 1071 | + | |
---|
| 1072 | + | |
---|
| 1073 | + | |
---|
| 1074 | + | @Callable(i) |
---|
| 1075 | + | func put (slip,autoStake) = { |
---|
| 1076 | + | let factCfg = gfc() |
---|
| 1077 | + | let stakingCntr = valueOrErrorMessage(addressFromString(factCfg[idxFactStakCntr]), "Wr st addr") |
---|
| 1078 | + | let slipCntr = valueOrErrorMessage(addressFromString(factCfg[idxFactSlippCntr]), "Wr sl addr") |
---|
| 1079 | + | if ((0 > slip)) |
---|
| 1080 | + | then throw("Wrong slippage") |
---|
| 1081 | + | else if ((size(i.payments) != 2)) |
---|
| 1082 | + | then throw("2 pmnts expd") |
---|
| 1083 | + | else { |
---|
| 1084 | + | let amAssetPmt = toBigInt(value(i.payments[0]).amount) |
---|
| 1085 | + | let prAssetPmt = toBigInt(value(i.payments[1]).amount) |
---|
| 1086 | + | let amountAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgAmountAssetId))) - amAssetPmt) |
---|
| 1087 | + | if ((amountAssetBalance == amountAssetBalance)) |
---|
| 1088 | + | then { |
---|
| 1089 | + | let priceAssetBalance = (toBigInt(getAccBalance(assetIdToString(cfgPriceAssetId))) - prAssetPmt) |
---|
| 1090 | + | if ((priceAssetBalance == priceAssetBalance)) |
---|
| 1091 | + | then { |
---|
| 1092 | + | let lpAssetEmission = toBigInt(value(assetInfo(cfgLpAssetId)).quantity) |
---|
| 1093 | + | if ((lpAssetEmission == lpAssetEmission)) |
---|
| 1094 | + | then { |
---|
| 1095 | + | let currentDLp = calcCurrentDLp(amAssetPmt, prAssetPmt, toBigInt(0)) |
---|
| 1096 | + | if ((currentDLp == currentDLp)) |
---|
| 1097 | + | then { |
---|
| 1098 | + | let e = cp(toString(i.caller), toBase58String(i.transactionId), AttachedPayment(value(i.payments[0]).assetId, value(i.payments[0]).amount), i.payments[1], slip, true, false, true, 0, "") |
---|
| 1099 | + | let emitLpAmt = e._2 |
---|
| 1100 | + | let lpAssetId = e._7 |
---|
| 1101 | + | let state = e._9 |
---|
| 1102 | + | let amDiff = e._10 |
---|
| 1103 | + | let prDiff = e._11 |
---|
| 1104 | + | let amId = e._12 |
---|
| 1105 | + | let prId = e._13 |
---|
| 1106 | + | let r = invoke(fca, "emit", [emitLpAmt], nil) |
---|
| 1107 | + | if ((r == r)) |
---|
| 1108 | + | then { |
---|
| 1109 | + | let el = match r { |
---|
| 1110 | + | case legacy: Address => |
---|
| 1111 | + | invoke(legacy, "emit", [emitLpAmt], nil) |
---|
| 1112 | + | case _ => |
---|
| 1113 | + | unit |
---|
| 1114 | + | } |
---|
| 1115 | + | if ((el == el)) |
---|
| 1116 | + | then { |
---|
| 1117 | + | let sa = if ((amDiff > 0)) |
---|
| 1118 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(amId, amDiff)]) |
---|
| 1119 | + | else nil |
---|
| 1120 | + | if ((sa == sa)) |
---|
| 1121 | + | then { |
---|
| 1122 | + | let sp = if ((prDiff > 0)) |
---|
| 1123 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(prId, prDiff)]) |
---|
| 1124 | + | else nil |
---|
| 1125 | + | if ((sp == sp)) |
---|
| 1126 | + | then { |
---|
| 1127 | + | let lpTrnsfr = if (autoStake) |
---|
| 1128 | + | then { |
---|
| 1129 | + | let ss = invoke(stakingCntr, "stake", nil, [AttachedPayment(lpAssetId, emitLpAmt)]) |
---|
| 1130 | + | if ((ss == ss)) |
---|
| 1131 | + | then nil |
---|
| 1132 | + | else throw("Strict value is not equal to itself.") |
---|
| 1133 | + | } |
---|
| 1134 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpAssetId)] |
---|
| 1135 | + | let $t04203442176 = refreshDLpInternal(0, 0, 0) |
---|
| 1136 | + | let refreshDLpActions = $t04203442176._1 |
---|
| 1137 | + | let updatedDLp = $t04203442176._2 |
---|
| 1138 | + | let check = if ((updatedDLp >= currentDLp)) |
---|
| 1139 | + | then true |
---|
| 1140 | + | else throwErr(makeString(["updated DLp lower than current DLp", toString(amountAssetBalance), toString(priceAssetBalance), toString(lpAssetEmission), toString(currentDLp), toString(updatedDLp), toString(amDiff), toString(prDiff)], " ")) |
---|
| 1141 | + | if ((check == check)) |
---|
| 1142 | + | then { |
---|
| 1143 | + | let lpAssetEmissionAfter = value(assetInfo(cfgLpAssetId)).quantity |
---|
| 1144 | + | if ((lpAssetEmissionAfter == lpAssetEmissionAfter)) |
---|
| 1145 | + | then ((state ++ lpTrnsfr) ++ refreshDLpActions) |
---|
| 1146 | + | else throw("Strict value is not equal to itself.") |
---|
| 1147 | + | } |
---|
| 1148 | + | else throw("Strict value is not equal to itself.") |
---|
| 1149 | + | } |
---|
| 1150 | + | else throw("Strict value is not equal to itself.") |
---|
| 1151 | + | } |
---|
| 1152 | + | else throw("Strict value is not equal to itself.") |
---|
| 1153 | + | } |
---|
| 1154 | + | else throw("Strict value is not equal to itself.") |
---|
| 1155 | + | } |
---|
| 1156 | + | else throw("Strict value is not equal to itself.") |
---|
| 1157 | + | } |
---|
| 1158 | + | else throw("Strict value is not equal to itself.") |
---|
| 1159 | + | } |
---|
| 1160 | + | else throw("Strict value is not equal to itself.") |
---|
| 1161 | + | } |
---|
| 1162 | + | else throw("Strict value is not equal to itself.") |
---|
| 1163 | + | } |
---|
| 1164 | + | else throw("Strict value is not equal to itself.") |
---|
| 1165 | + | } |
---|
| 1166 | + | } |
---|
| 1167 | + | |
---|
| 1168 | + | |
---|
| 1169 | + | |
---|
| 1170 | + | @Callable(i) |
---|
| 1171 | + | func putOneTknV2 (minOutAmount,autoStake) = { |
---|
| 1172 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1173 | + | let @ = invoke(fca, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1174 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1175 | + | then @ |
---|
| 1176 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1177 | + | } |
---|
| 1178 | + | let isPutDisabled = if (if (if (igs()) |
---|
| 1179 | + | then true |
---|
| 1180 | + | else (cfgPoolStatus == PoolPutDis)) |
---|
| 1181 | + | then true |
---|
| 1182 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1183 | + | then true |
---|
| 1184 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1185 | + | let checks = [if (if (!(isPutDisabled)) |
---|
| 1186 | + | then true |
---|
| 1187 | + | else isManager(i)) |
---|
| 1188 | + | then true |
---|
| 1189 | + | else throwErr("put operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 1190 | + | then true |
---|
| 1191 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 1192 | + | if ((checks == checks)) |
---|
| 1193 | + | then { |
---|
| 1194 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 1195 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 1196 | + | let lpId = cfgLpAssetId |
---|
| 1197 | + | let amDecimals = cfgAmountAssetDecimals |
---|
| 1198 | + | let prDecimals = cfgPriceAssetDecimals |
---|
| 1199 | + | let userAddress = if ((i.caller == this)) |
---|
| 1200 | + | then i.originCaller |
---|
| 1201 | + | else i.caller |
---|
| 1202 | + | let pmt = value(i.payments[0]) |
---|
| 1203 | + | let pmtAssetId = toBase58String(value(pmt.assetId)) |
---|
| 1204 | + | let pmtAmt = pmt.amount |
---|
| 1205 | + | let currentDLp = if ((pmt.assetId == cfgAmountAssetId)) |
---|
| 1206 | + | then calcCurrentDLp(toBigInt(pmtAmt), toBigInt(0), toBigInt(0)) |
---|
| 1207 | + | else calcCurrentDLp(toBigInt(0), toBigInt(pmtAmt), toBigInt(0)) |
---|
| 1208 | + | if ((currentDLp == currentDLp)) |
---|
| 1209 | + | then { |
---|
| 1210 | + | let $t04381743975 = calcPutOneTkn(pmtAmt, pmtAssetId, toString(userAddress), toBase58String(i.transactionId), true) |
---|
| 1211 | + | if (($t04381743975 == $t04381743975)) |
---|
| 1212 | + | then { |
---|
| 1213 | + | let feeAmount = $t04381743975._3 |
---|
| 1214 | + | let state = $t04381743975._2 |
---|
| 1215 | + | let estimLP = $t04381743975._1 |
---|
| 1216 | + | let emitLpAmt = if (if ((minOutAmount > 0)) |
---|
| 1217 | + | then (minOutAmount > estimLP) |
---|
| 1218 | + | else false) |
---|
| 1219 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 1220 | + | else estimLP |
---|
| 1221 | + | let e = invoke(fca, "emit", [emitLpAmt], nil) |
---|
| 1222 | + | if ((e == e)) |
---|
| 1223 | + | then { |
---|
| 1224 | + | let el = match e { |
---|
| 1225 | + | case legacy: Address => |
---|
| 1226 | + | invoke(legacy, "emit", [emitLpAmt], nil) |
---|
| 1227 | + | case _ => |
---|
| 1228 | + | unit |
---|
| 1229 | + | } |
---|
| 1230 | + | if ((el == el)) |
---|
| 1231 | + | then { |
---|
| 1232 | + | let lpTrnsfr = if (autoStake) |
---|
| 1233 | + | then { |
---|
| 1234 | + | let ss = invoke(stakingContract, "stakeFor", [toString(i.caller)], [AttachedPayment(lpId, emitLpAmt)]) |
---|
| 1235 | + | if ((ss == ss)) |
---|
| 1236 | + | then nil |
---|
| 1237 | + | else throw("Strict value is not equal to itself.") |
---|
| 1238 | + | } |
---|
| 1239 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpId)] |
---|
| 1240 | + | let sendFeeToMatcher = if ((feeAmount > 0)) |
---|
| 1241 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, fromBase58String(pmtAssetId))] |
---|
| 1242 | + | else nil |
---|
| 1243 | + | let $t04486045209 = if ((this == feeCollectorAddress)) |
---|
| 1244 | + | then $Tuple2(0, 0) |
---|
| 1245 | + | else { |
---|
| 1246 | + | let paymentInAmountAsset = if ((pmt.assetId == cfgAmountAssetId)) |
---|
| 1247 | + | then true |
---|
| 1248 | + | else false |
---|
| 1249 | + | if (paymentInAmountAsset) |
---|
| 1250 | + | then $Tuple2(-(feeAmount), 0) |
---|
| 1251 | + | else $Tuple2(0, -(feeAmount)) |
---|
| 1252 | + | } |
---|
| 1253 | + | let amountAssetBalanceDelta = $t04486045209._1 |
---|
| 1254 | + | let priceAssetBalanceDelta = $t04486045209._2 |
---|
| 1255 | + | let $t04521245320 = refreshDLpInternal(amountAssetBalanceDelta, priceAssetBalanceDelta, 0) |
---|
| 1256 | + | let refreshDLpActions = $t04521245320._1 |
---|
| 1257 | + | let updatedDLp = $t04521245320._2 |
---|
| 1258 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1259 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1260 | + | then $Tuple2((((state ++ lpTrnsfr) ++ sendFeeToMatcher) ++ refreshDLpActions), emitLpAmt) |
---|
| 1261 | + | else throw("Strict value is not equal to itself.") |
---|
| 1262 | + | } |
---|
| 1263 | + | else throw("Strict value is not equal to itself.") |
---|
| 1264 | + | } |
---|
| 1265 | + | else throw("Strict value is not equal to itself.") |
---|
| 1266 | + | } |
---|
| 1267 | + | else throw("Strict value is not equal to itself.") |
---|
| 1268 | + | } |
---|
| 1269 | + | else throw("Strict value is not equal to itself.") |
---|
| 1270 | + | } |
---|
| 1271 | + | else throw("Strict value is not equal to itself.") |
---|
| 1272 | + | } |
---|
| 1273 | + | |
---|
| 1274 | + | |
---|
| 1275 | + | |
---|
| 1276 | + | @Callable(i) |
---|
| 1277 | + | func putForFree (maxSlpg) = if ((0 > maxSlpg)) |
---|
| 1278 | + | then throw("Wrong slpg") |
---|
| 1279 | + | else if ((size(i.payments) != 2)) |
---|
| 1280 | + | then throw("2 pmnts expd") |
---|
| 1281 | + | else { |
---|
| 1282 | + | let estPut = cp(toString(i.caller), toBase58String(i.transactionId), AttachedPayment(value(i.payments[0]).assetId, value(i.payments[0]).amount), i.payments[1], maxSlpg, false, false, true, 0, "") |
---|
| 1283 | + | let state = estPut._9 |
---|
| 1284 | + | let amAssetPmt = toBigInt(value(i.payments[0]).amount) |
---|
| 1285 | + | let prAssetPmt = toBigInt(value(i.payments[1]).amount) |
---|
| 1286 | + | let currentDLp = calcCurrentDLp(amAssetPmt, prAssetPmt, toBigInt(0)) |
---|
| 1287 | + | if ((currentDLp == currentDLp)) |
---|
| 1288 | + | then { |
---|
| 1289 | + | let $t04635046415 = refreshDLpInternal(0, 0, 0) |
---|
| 1290 | + | let refreshDLpActions = $t04635046415._1 |
---|
| 1291 | + | let updatedDLp = $t04635046415._2 |
---|
| 1292 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1293 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1294 | + | then (state ++ refreshDLpActions) |
---|
| 1295 | + | else throw("Strict value is not equal to itself.") |
---|
| 1296 | + | } |
---|
| 1297 | + | else throw("Strict value is not equal to itself.") |
---|
| 1298 | + | } |
---|
| 1299 | + | |
---|
| 1300 | + | |
---|
| 1301 | + | |
---|
| 1302 | + | @Callable(i) |
---|
| 1303 | + | func get () = { |
---|
| 1304 | + | let currentDLp = calcCurrentDLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1305 | + | if ((currentDLp == currentDLp)) |
---|
| 1306 | + | then { |
---|
| 1307 | + | let r = cg(i) |
---|
| 1308 | + | let outAmtAmt = r._1 |
---|
| 1309 | + | let outPrAmt = r._2 |
---|
| 1310 | + | let pmtAmt = r._3 |
---|
| 1311 | + | let pmtAssetId = r._4 |
---|
| 1312 | + | let state = r._5 |
---|
| 1313 | + | let b = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 1314 | + | if ((b == b)) |
---|
| 1315 | + | then { |
---|
| 1316 | + | let $t04758847670 = refreshDLpInternal(-(outAmtAmt), -(outPrAmt), 0) |
---|
| 1317 | + | let refreshDLpActions = $t04758847670._1 |
---|
| 1318 | + | let updatedDLp = $t04758847670._2 |
---|
| 1319 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1320 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1321 | + | then (state ++ refreshDLpActions) |
---|
| 1322 | + | else throw("Strict value is not equal to itself.") |
---|
| 1323 | + | } |
---|
| 1324 | + | else throw("Strict value is not equal to itself.") |
---|
| 1325 | + | } |
---|
| 1326 | + | else throw("Strict value is not equal to itself.") |
---|
| 1327 | + | } |
---|
| 1328 | + | |
---|
| 1329 | + | |
---|
| 1330 | + | |
---|
| 1331 | + | @Callable(i) |
---|
| 1332 | + | func getOneTknV2 (outAssetId,minOutAmount) = { |
---|
| 1333 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1334 | + | let @ = invoke(fca, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1335 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1336 | + | then @ |
---|
| 1337 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1338 | + | } |
---|
| 1339 | + | let isGetDisabled = if (if (igs()) |
---|
| 1340 | + | then true |
---|
| 1341 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1342 | + | then true |
---|
| 1343 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1344 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 1345 | + | then true |
---|
| 1346 | + | else isManager(i)) |
---|
| 1347 | + | then true |
---|
| 1348 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 1349 | + | then true |
---|
| 1350 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 1351 | + | if ((checks == checks)) |
---|
| 1352 | + | then { |
---|
| 1353 | + | let $t04828848443 = getOneTknV2Internal(outAssetId, minOutAmount, i.payments, i.caller, i.originCaller, i.transactionId) |
---|
| 1354 | + | let state = $t04828848443._1 |
---|
| 1355 | + | let totalAmount = $t04828848443._2 |
---|
| 1356 | + | $Tuple2(state, totalAmount) |
---|
| 1357 | + | } |
---|
| 1358 | + | else throw("Strict value is not equal to itself.") |
---|
| 1359 | + | } |
---|
| 1360 | + | |
---|
| 1361 | + | |
---|
| 1362 | + | |
---|
| 1363 | + | @Callable(i) |
---|
| 1364 | + | func refreshDLp () = { |
---|
| 1365 | + | let lastRefreshedBlockHeight = valueOrElse(getInteger(keyDLpRefreshedHeight), 0) |
---|
| 1366 | + | let checkLastRefreshedBlockHeight = if (((height - lastRefreshedBlockHeight) >= dLpRefreshDelay)) |
---|
| 1367 | + | then unit |
---|
| 1368 | + | else throwErr(makeString([toString(dLpRefreshDelay), " blocks have not passed since the previous call"], "")) |
---|
| 1369 | + | if ((checkLastRefreshedBlockHeight == checkLastRefreshedBlockHeight)) |
---|
| 1370 | + | then { |
---|
| 1371 | + | let dLp = valueOrErrorMessage(parseBigInt(valueOrElse(getString(this, keyDLp), "0")), fmtErr("invalid dLp")) |
---|
| 1372 | + | let $t04896749031 = refreshDLpInternal(0, 0, 0) |
---|
| 1373 | + | let dLpUpdateActions = $t04896749031._1 |
---|
| 1374 | + | let updatedDLp = $t04896749031._2 |
---|
| 1375 | + | let actions = if ((dLp != updatedDLp)) |
---|
| 1376 | + | then dLpUpdateActions |
---|
| 1377 | + | else throwErr("nothing to refresh") |
---|
| 1378 | + | $Tuple2(actions, toString(updatedDLp)) |
---|
| 1379 | + | } |
---|
| 1380 | + | else throw("Strict value is not equal to itself.") |
---|
| 1381 | + | } |
---|
| 1382 | + | |
---|
| 1383 | + | |
---|
| 1384 | + | |
---|
| 1385 | + | @Callable(i) |
---|
| 1386 | + | func getOneTknV2READONLY (outAssetId,lpAssetAmount) = { |
---|
| 1387 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 1388 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 1389 | + | let lpId = toBase58String(value(cfgLpAssetId)) |
---|
| 1390 | + | let xp = [toBigInt(getAccBalance(amId)), toBigInt(getAccBalance(prId))] |
---|
| 1391 | + | let lpEmission = toBigInt(valueOrErrorMessage(assetInfo(fromBase58String(lpId)), "invalid lp asset").quantity) |
---|
| 1392 | + | let D0 = getD(xp) |
---|
| 1393 | + | let D1 = (D0 - fraction(toBigInt(lpAssetAmount), D0, lpEmission)) |
---|
| 1394 | + | let index = if ((outAssetId == amId)) |
---|
| 1395 | + | then 0 |
---|
| 1396 | + | else if ((outAssetId == prId)) |
---|
| 1397 | + | then 1 |
---|
| 1398 | + | else throw("invalid out asset id") |
---|
| 1399 | + | let newY = getYD(xp, index, D1) |
---|
| 1400 | + | let dy = (xp[index] - newY) |
---|
| 1401 | + | let totalGetRaw = max([0, toInt((dy - big1))]) |
---|
| 1402 | + | let $t05004150096 = takeFee(totalGetRaw, outFee) |
---|
| 1403 | + | let totalGet = $t05004150096._1 |
---|
| 1404 | + | let feeAmount = $t05004150096._2 |
---|
| 1405 | + | $Tuple2(nil, $Tuple2(totalGet, feeAmount)) |
---|
| 1406 | + | } |
---|
| 1407 | + | |
---|
| 1408 | + | |
---|
| 1409 | + | |
---|
| 1410 | + | @Callable(i) |
---|
| 1411 | + | func getOneTknV2WithBonusREADONLY (outAssetId,lpAssetAmount) = { |
---|
| 1412 | + | let amId = toBase58String(value(cfgAmountAssetId)) |
---|
| 1413 | + | let prId = toBase58String(value(cfgPriceAssetId)) |
---|
| 1414 | + | let lpId = toBase58String(value(cfgLpAssetId)) |
---|
| 1415 | + | let amBalance = getAccBalance(amId) |
---|
| 1416 | + | let prBalance = getAccBalance(prId) |
---|
| 1417 | + | let $t05047150586 = { |
---|
| 1418 | + | let @ = invoke(this, "getOneTknV2READONLY", [outAssetId, lpAssetAmount], nil) |
---|
| 1419 | + | if ($isInstanceOf(@, "(Int, Int)")) |
---|
| 1420 | + | then @ |
---|
| 1421 | + | else throw(($getType(@) + " couldn't be cast to (Int, Int)")) |
---|
| 1422 | + | } |
---|
| 1423 | + | let totalGet = $t05047150586._1 |
---|
| 1424 | + | let feeAmount = $t05047150586._2 |
---|
| 1425 | + | let r = ego("", lpId, lpAssetAmount, this) |
---|
| 1426 | + | let outAmAmt = r._1 |
---|
| 1427 | + | let outPrAmt = r._2 |
---|
| 1428 | + | let sumOfGetAssets = (outAmAmt + outPrAmt) |
---|
| 1429 | + | let bonus = if ((sumOfGetAssets == 0)) |
---|
| 1430 | + | then if ((totalGet == 0)) |
---|
| 1431 | + | then 0 |
---|
| 1432 | + | else throw("bonus calculation error") |
---|
| 1433 | + | else fraction((totalGet - sumOfGetAssets), scale8, sumOfGetAssets) |
---|
| 1434 | + | $Tuple2(nil, $Tuple3(totalGet, feeAmount, bonus)) |
---|
| 1435 | + | } |
---|
| 1436 | + | |
---|
| 1437 | + | |
---|
| 1438 | + | |
---|
| 1439 | + | @Callable(i) |
---|
| 1440 | + | func getNoLess (noLessThenAmtAsset,noLessThenPriceAsset) = { |
---|
| 1441 | + | let r = cg(i) |
---|
| 1442 | + | let outAmAmt = r._1 |
---|
| 1443 | + | let outPrAmt = r._2 |
---|
| 1444 | + | let pmtAmt = r._3 |
---|
| 1445 | + | let pmtAssetId = r._4 |
---|
| 1446 | + | let state = r._5 |
---|
| 1447 | + | if ((noLessThenAmtAsset > outAmAmt)) |
---|
| 1448 | + | then throw(((("Failed: " + toString(outAmAmt)) + " < ") + toString(noLessThenAmtAsset))) |
---|
| 1449 | + | else if ((noLessThenPriceAsset > outPrAmt)) |
---|
| 1450 | + | then throw(((("Failed: " + toString(outPrAmt)) + " < ") + toString(noLessThenPriceAsset))) |
---|
| 1451 | + | else { |
---|
| 1452 | + | let currentDLp = calcCurrentDLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1453 | + | if ((currentDLp == currentDLp)) |
---|
| 1454 | + | then { |
---|
| 1455 | + | let burnLPAssetOnFactory = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 1456 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1457 | + | then { |
---|
| 1458 | + | let $t05175251833 = refreshDLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1459 | + | let refreshDLpActions = $t05175251833._1 |
---|
| 1460 | + | let updatedDLp = $t05175251833._2 |
---|
| 1461 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1462 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1463 | + | then (state ++ refreshDLpActions) |
---|
| 1464 | + | else throw("Strict value is not equal to itself.") |
---|
| 1465 | + | } |
---|
| 1466 | + | else throw("Strict value is not equal to itself.") |
---|
| 1467 | + | } |
---|
| 1468 | + | else throw("Strict value is not equal to itself.") |
---|
| 1469 | + | } |
---|
| 1470 | + | } |
---|
| 1471 | + | |
---|
| 1472 | + | |
---|
| 1473 | + | |
---|
| 1474 | + | @Callable(i) |
---|
| 1475 | + | func unstakeAndGet (amount) = { |
---|
| 1476 | + | let checkPayments = if ((size(i.payments) != 0)) |
---|
| 1477 | + | then throw("No pmnts expd") |
---|
| 1478 | + | else true |
---|
| 1479 | + | if ((checkPayments == checkPayments)) |
---|
| 1480 | + | then { |
---|
| 1481 | + | let factoryCfg = gfc() |
---|
| 1482 | + | let lpAssetId = cfgLpAssetId |
---|
| 1483 | + | let staking = valueOrErrorMessage(addressFromString(factoryCfg[idxFactStakCntr]), "Wr st addr") |
---|
| 1484 | + | let currentDLp = calcCurrentDLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1485 | + | if ((currentDLp == currentDLp)) |
---|
| 1486 | + | then { |
---|
| 1487 | + | let unstakeInv = invoke(staking, "unstake", [toBase58String(lpAssetId), amount], nil) |
---|
| 1488 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1489 | + | then { |
---|
| 1490 | + | let r = ego(toBase58String(i.transactionId), toBase58String(lpAssetId), amount, i.caller) |
---|
| 1491 | + | let outAmAmt = r._1 |
---|
| 1492 | + | let outPrAmt = r._2 |
---|
| 1493 | + | let sts = parseIntValue(r._9) |
---|
| 1494 | + | let state = r._10 |
---|
| 1495 | + | let v = if (if (igs()) |
---|
| 1496 | + | then true |
---|
| 1497 | + | else (sts == PoolShutdown)) |
---|
| 1498 | + | then throw(("Blocked: " + toString(sts))) |
---|
| 1499 | + | else true |
---|
| 1500 | + | if ((v == v)) |
---|
| 1501 | + | then { |
---|
| 1502 | + | let burnA = invoke(fca, "burn", [amount], [AttachedPayment(lpAssetId, amount)]) |
---|
| 1503 | + | if ((burnA == burnA)) |
---|
| 1504 | + | then { |
---|
| 1505 | + | let $t05286052941 = refreshDLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1506 | + | let refreshDLpActions = $t05286052941._1 |
---|
| 1507 | + | let updatedDLp = $t05286052941._2 |
---|
| 1508 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1509 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1510 | + | then (state ++ refreshDLpActions) |
---|
| 1511 | + | else throw("Strict value is not equal to itself.") |
---|
| 1512 | + | } |
---|
| 1513 | + | else throw("Strict value is not equal to itself.") |
---|
| 1514 | + | } |
---|
| 1515 | + | else throw("Strict value is not equal to itself.") |
---|
| 1516 | + | } |
---|
| 1517 | + | else throw("Strict value is not equal to itself.") |
---|
| 1518 | + | } |
---|
| 1519 | + | else throw("Strict value is not equal to itself.") |
---|
| 1520 | + | } |
---|
| 1521 | + | else throw("Strict value is not equal to itself.") |
---|
| 1522 | + | } |
---|
| 1523 | + | |
---|
| 1524 | + | |
---|
| 1525 | + | |
---|
| 1526 | + | @Callable(i) |
---|
| 1527 | + | func unstakeAndGetNoLess (unstakeAmount,noLessThenAmountAsset,noLessThenPriceAsset) = { |
---|
| 1528 | + | let isGetDisabled = if (igs()) |
---|
| 1529 | + | then true |
---|
| 1530 | + | else (cfgPoolStatus == PoolShutdown) |
---|
| 1531 | + | let checks = [if (!(isGetDisabled)) |
---|
| 1532 | + | then true |
---|
| 1533 | + | else throw("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 1534 | + | then true |
---|
| 1535 | + | else throw("no payments are expected")] |
---|
| 1536 | + | if ((checks == checks)) |
---|
| 1537 | + | then { |
---|
| 1538 | + | let currentDLp = calcCurrentDLp(toBigInt(0), toBigInt(0), toBigInt(0)) |
---|
| 1539 | + | if ((currentDLp == currentDLp)) |
---|
| 1540 | + | then { |
---|
| 1541 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(cfgLpAssetId), unstakeAmount], nil) |
---|
| 1542 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1543 | + | then { |
---|
| 1544 | + | let res = ego(toBase58String(i.transactionId), toBase58String(cfgLpAssetId), unstakeAmount, i.caller) |
---|
| 1545 | + | let outAmAmt = res._1 |
---|
| 1546 | + | let outPrAmt = res._2 |
---|
| 1547 | + | let state = res._10 |
---|
| 1548 | + | let checkAmounts = [if ((outAmAmt >= noLessThenAmountAsset)) |
---|
| 1549 | + | then true |
---|
| 1550 | + | else throw(makeString(["amount asset amount to receive is less than ", toString(noLessThenAmountAsset)], "")), if ((outPrAmt >= noLessThenPriceAsset)) |
---|
| 1551 | + | then true |
---|
| 1552 | + | else throw(makeString(["price asset amount to receive is less than ", toString(noLessThenPriceAsset)], ""))] |
---|
| 1553 | + | if ((checkAmounts == checkAmounts)) |
---|
| 1554 | + | then { |
---|
| 1555 | + | let burnLPAssetOnFactory = invoke(fca, "burn", [unstakeAmount], [AttachedPayment(cfgLpAssetId, unstakeAmount)]) |
---|
| 1556 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1557 | + | then { |
---|
| 1558 | + | let $t05419254273 = refreshDLpInternal(-(outAmAmt), -(outPrAmt), 0) |
---|
| 1559 | + | let refreshDLpActions = $t05419254273._1 |
---|
| 1560 | + | let updatedDLp = $t05419254273._2 |
---|
| 1561 | + | let isUpdatedDLpValid = validateUpdatedDLp(currentDLp, updatedDLp) |
---|
| 1562 | + | if ((isUpdatedDLpValid == isUpdatedDLpValid)) |
---|
| 1563 | + | then (state ++ refreshDLpActions) |
---|
| 1564 | + | else throw("Strict value is not equal to itself.") |
---|
| 1565 | + | } |
---|
| 1566 | + | else throw("Strict value is not equal to itself.") |
---|
| 1567 | + | } |
---|
| 1568 | + | else throw("Strict value is not equal to itself.") |
---|
| 1569 | + | } |
---|
| 1570 | + | else throw("Strict value is not equal to itself.") |
---|
| 1571 | + | } |
---|
| 1572 | + | else throw("Strict value is not equal to itself.") |
---|
| 1573 | + | } |
---|
| 1574 | + | else throw("Strict value is not equal to itself.") |
---|
| 1575 | + | } |
---|
| 1576 | + | |
---|
| 1577 | + | |
---|
| 1578 | + | |
---|
| 1579 | + | @Callable(i) |
---|
| 1580 | + | func unstakeAndGetOneTknV2 (unstakeAmount,outAssetId,minOutAmount) = { |
---|
| 1581 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 1582 | + | let @ = invoke(fca, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 1583 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 1584 | + | then @ |
---|
| 1585 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 1586 | + | } |
---|
| 1587 | + | let isGetDisabled = if (if (igs()) |
---|
| 1588 | + | then true |
---|
| 1589 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 1590 | + | then true |
---|
| 1591 | + | else isPoolOneTokenOperationsDisabled |
---|
| 1592 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 1593 | + | then true |
---|
| 1594 | + | else isManager(i)) |
---|
| 1595 | + | then true |
---|
| 1596 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 1597 | + | then true |
---|
| 1598 | + | else throwErr("no payments are expected")] |
---|
| 1599 | + | if ((checks == checks)) |
---|
| 1600 | + | then { |
---|
| 1601 | + | let factoryCfg = gfc() |
---|
| 1602 | + | let lpAssetId = cfgLpAssetId |
---|
| 1603 | + | let staking = valueOrErrorMessage(addressFromString(factoryCfg[idxFactStakCntr]), "Wr st addr") |
---|
| 1604 | + | let userAddress = i.caller |
---|
| 1605 | + | let lpAssetRecipientAddress = this |
---|
| 1606 | + | let unstakeInv = invoke(staking, "unstakeINTERNAL", [lpAssetId, unstakeAmount, userAddress.bytes, lpAssetRecipientAddress.bytes], nil) |
---|
| 1607 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1608 | + | then { |
---|
| 1609 | + | let $t05529555483 = getOneTknV2Internal(outAssetId, minOutAmount, [AttachedPayment(lpAssetId, unstakeAmount)], i.caller, i.originCaller, i.transactionId) |
---|
| 1610 | + | let state = $t05529555483._1 |
---|
| 1611 | + | let totalAmount = $t05529555483._2 |
---|
| 1612 | + | $Tuple2(state, totalAmount) |
---|
| 1613 | + | } |
---|
| 1614 | + | else throw("Strict value is not equal to itself.") |
---|
| 1615 | + | } |
---|
| 1616 | + | else throw("Strict value is not equal to itself.") |
---|
| 1617 | + | } |
---|
| 1618 | + | |
---|
| 1619 | + | |
---|
| 1620 | + | |
---|
| 1621 | + | @Callable(i) |
---|
| 1622 | + | func putOneTknV2WithBonusREADONLY (paymentAmountRaw,paymentAssetId) = { |
---|
| 1623 | + | let $t05561155714 = calcPutOneTkn(paymentAmountRaw, paymentAssetId, "", "", true) |
---|
| 1624 | + | let lpAmount = $t05561155714._1 |
---|
| 1625 | + | let state = $t05561155714._2 |
---|
| 1626 | + | let feeAmount = $t05561155714._3 |
---|
| 1627 | + | let bonus = $t05561155714._4 |
---|
| 1628 | + | $Tuple2(nil, $Tuple3(lpAmount, feeAmount, bonus)) |
---|
| 1629 | + | } |
---|
| 1630 | + | |
---|
| 1631 | + | |
---|
| 1632 | + | |
---|
| 1633 | + | @Callable(i) |
---|
| 1634 | + | func putOneTknV2WithoutTakeFeeREADONLY (paymentAmountRaw,paymentAssetId) = { |
---|
| 1635 | + | let $t05586255966 = calcPutOneTkn(paymentAmountRaw, paymentAssetId, "", "", false) |
---|
| 1636 | + | let lpAmount = $t05586255966._1 |
---|
| 1637 | + | let state = $t05586255966._2 |
---|
| 1638 | + | let feeAmount = $t05586255966._3 |
---|
| 1639 | + | let bonus = $t05586255966._4 |
---|
| 1640 | + | $Tuple2(nil, $Tuple3(lpAmount, feeAmount, bonus)) |
---|
| 1641 | + | } |
---|
| 1642 | + | |
---|
| 1643 | + | |
---|
| 1644 | + | |
---|
| 1645 | + | @Callable(i) |
---|
| 1646 | + | func activate (amtAsStr,prAsStr) = if ((toString(i.caller) != toString(fca))) |
---|
| 1647 | + | then throw("denied") |
---|
| 1648 | + | else $Tuple2([StringEntry(aa(), amtAsStr), StringEntry(pa(), prAsStr), StringEntry(amp(), toString(ampInitial)), StringEntry(keyAmpHistory(height), toString(ampInitial))], "success") |
---|
| 1649 | + | |
---|
| 1650 | + | |
---|
| 1651 | + | |
---|
| 1652 | + | @Callable(i) |
---|
| 1653 | + | func getPoolConfigWrapperREADONLY () = $Tuple2(nil, gpc()) |
---|
| 1654 | + | |
---|
| 1655 | + | |
---|
| 1656 | + | |
---|
| 1657 | + | @Callable(i) |
---|
| 1658 | + | func getAccBalanceWrapperREADONLY (assetId) = $Tuple2(nil, getAccBalance(assetId)) |
---|
| 1659 | + | |
---|
| 1660 | + | |
---|
| 1661 | + | |
---|
| 1662 | + | @Callable(i) |
---|
| 1663 | + | func calcPricesWrapperREADONLY (amAmt,prAmt,lpAmt) = { |
---|
| 1664 | + | let pr = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 1665 | + | $Tuple2(nil, [toString(pr[0]), toString(pr[1]), toString(pr[2])]) |
---|
| 1666 | + | } |
---|
| 1667 | + | |
---|
| 1668 | + | |
---|
| 1669 | + | |
---|
| 1670 | + | @Callable(i) |
---|
| 1671 | + | func fromX18WrapperREADONLY (val,resScaleMult) = $Tuple2(nil, f1(parseBigIntValue(val), resScaleMult)) |
---|
| 1672 | + | |
---|
| 1673 | + | |
---|
| 1674 | + | |
---|
| 1675 | + | @Callable(i) |
---|
| 1676 | + | func toX18WrapperREADONLY (origVal,origScaleMult) = $Tuple2(nil, toString(t1(origVal, origScaleMult))) |
---|
| 1677 | + | |
---|
| 1678 | + | |
---|
| 1679 | + | |
---|
| 1680 | + | @Callable(i) |
---|
| 1681 | + | func calcPriceBigIntWrapperREADONLY (prAmtX18,amAmtX18) = $Tuple2(nil, toString(cpbi(parseBigIntValue(prAmtX18), parseBigIntValue(amAmtX18)))) |
---|
| 1682 | + | |
---|
| 1683 | + | |
---|
| 1684 | + | |
---|
| 1685 | + | @Callable(i) |
---|
| 1686 | + | func estimatePutOperationWrapperREADONLY (txId58,slippage,inAmAmt,inAmId,inPrAmt,inPrId,usrAddr,isEval,emitLp) = $Tuple2(nil, epo(txId58, slippage, inAmAmt, inAmId, inPrAmt, inPrId, usrAddr, isEval, emitLp, true, false, 0, "")) |
---|
| 1687 | + | |
---|
| 1688 | + | |
---|
| 1689 | + | |
---|
| 1690 | + | @Callable(i) |
---|
| 1691 | + | func estimateGetOperationWrapperREADONLY (txId58,pmtAsId,pmtLpAmt,usrAddr) = { |
---|
| 1692 | + | let r = ego(txId58, pmtAsId, pmtLpAmt, addressFromStringValue(usrAddr)) |
---|
| 1693 | + | $Tuple2(nil, $Tuple10(r._1, r._2, r._3, r._4, r._5, r._6, r._7, toString(r._8), r._9, r._10)) |
---|
| 1694 | + | } |
---|
| 1695 | + | |
---|
| 1696 | + | |
---|
| 1697 | + | |
---|
| 1698 | + | @Callable(i) |
---|
| 1699 | + | func changeAmp () = { |
---|
| 1700 | + | let cfg = invoke(fca, "getChangeAmpConfigREADONLY", [toString(this)], nil) |
---|
| 1701 | + | let $t05844658645 = match cfg { |
---|
| 1702 | + | case list: List[Any] => |
---|
| 1703 | + | $Tuple3({ |
---|
| 1704 | + | let @ = list[0] |
---|
| 1705 | + | if ($isInstanceOf(@, "Int")) |
---|
| 1706 | + | then @ |
---|
| 1707 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 1708 | + | }, { |
---|
| 1709 | + | let @ = list[1] |
---|
| 1710 | + | if ($isInstanceOf(@, "Int")) |
---|
| 1711 | + | then @ |
---|
| 1712 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 1713 | + | }, { |
---|
| 1714 | + | let @ = list[2] |
---|
| 1715 | + | if ($isInstanceOf(@, "Int")) |
---|
| 1716 | + | then @ |
---|
| 1717 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 1718 | + | }) |
---|
| 1719 | + | case _ => |
---|
| 1720 | + | throwErr("invalid entry type") |
---|
| 1721 | + | } |
---|
| 1722 | + | let delay = $t05844658645._1 |
---|
| 1723 | + | let delta = $t05844658645._2 |
---|
| 1724 | + | let target = $t05844658645._3 |
---|
| 1725 | + | let curAmp = parseIntValue(getStringValue(amp())) |
---|
| 1726 | + | let newAmpRaw = (curAmp + delta) |
---|
| 1727 | + | let newAmp = if ((0 > delta)) |
---|
| 1728 | + | then if ((target > newAmpRaw)) |
---|
| 1729 | + | then target |
---|
| 1730 | + | else newAmpRaw |
---|
| 1731 | + | else if ((newAmpRaw > target)) |
---|
| 1732 | + | then target |
---|
| 1733 | + | else newAmpRaw |
---|
| 1734 | + | let lastCall = valueOrElse(getInteger(keyChangeAmpLastCall()), 0) |
---|
| 1735 | + | let wait = (lastCall + delay) |
---|
| 1736 | + | let checks = [if ((height > wait)) |
---|
| 1737 | + | then true |
---|
| 1738 | + | else throwErr("try again in few blocks"), if ((curAmp != newAmp)) |
---|
| 1739 | + | then true |
---|
| 1740 | + | else throwErr("already reached target")] |
---|
| 1741 | + | if ((checks == checks)) |
---|
| 1742 | + | then [IntegerEntry(keyChangeAmpLastCall(), height), StringEntry(amp(), toString(newAmp)), StringEntry(keyAmpHistory(height), toString(newAmp))] |
---|
| 1743 | + | else throw("Strict value is not equal to itself.") |
---|
| 1744 | + | } |
---|
| 1745 | + | |
---|
| 1746 | + | |
---|
| 1747 | + | @Verifier(tx) |
---|
| 1748 | + | func verify () = { |
---|
| 1749 | + | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
| 1750 | + | case pk: ByteVector => |
---|
| 1751 | + | pk |
---|
| 1752 | + | case _: Unit => |
---|
| 1753 | + | tx.senderPublicKey |
---|
| 1754 | + | case _ => |
---|
| 1755 | + | throw("Match error") |
---|
| 1756 | + | } |
---|
| 1757 | + | match tx { |
---|
| 1758 | + | case order: Order => |
---|
| 1759 | + | let matcherPub = mp() |
---|
| 1760 | + | let $t05957859695 = if (skipOrderValidation()) |
---|
| 1761 | + | then $Tuple2(true, "") |
---|
| 1762 | + | else validateMatcherOrderAllowed(order) |
---|
| 1763 | + | let orderValid = $t05957859695._1 |
---|
| 1764 | + | let orderValidInfo = $t05957859695._2 |
---|
| 1765 | + | let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey) |
---|
| 1766 | + | let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub) |
---|
| 1767 | + | if (if (if (orderValid) |
---|
| 1768 | + | then senderValid |
---|
| 1769 | + | else false) |
---|
| 1770 | + | then matcherValid |
---|
| 1771 | + | else false) |
---|
| 1772 | + | then true |
---|
| 1773 | + | else throwOrderError(orderValid, orderValidInfo, senderValid, matcherValid) |
---|
| 1774 | + | case s: SetScriptTransaction => |
---|
| 1775 | + | if (sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey)) |
---|
| 1776 | + | then true |
---|
| 1777 | + | else { |
---|
| 1778 | + | let newHash = blake2b256(value(s.script)) |
---|
| 1779 | + | let allowedHash = fromBase64String(value(getString(fca, keyAllowedLpStableScriptHash()))) |
---|
| 1780 | + | let currentHash = scriptHash(this) |
---|
| 1781 | + | if ((allowedHash == newHash)) |
---|
| 1782 | + | then (currentHash != newHash) |
---|
| 1783 | + | else false |
---|
| 1784 | + | } |
---|
| 1785 | + | case _ => |
---|
| 1786 | + | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 1787 | + | } |
---|
| 1788 | + | } |
---|
| 1789 | + | |
---|