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