1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let defaultDecimals = 1000000 |
---|
| 5 | + | |
---|
| 6 | + | let scale8 = 100000000 |
---|
| 7 | + | |
---|
| 8 | + | let scale8BigInt = toBigInt(100000000) |
---|
| 9 | + | |
---|
| 10 | + | let scale18 = toBigInt(1000000000000000000) |
---|
| 11 | + | |
---|
| 12 | + | let zeroBigInt = toBigInt(0) |
---|
| 13 | + | |
---|
| 14 | + | let oneBigInt = toBigInt(1) |
---|
| 15 | + | |
---|
| 16 | + | let slippage4D = toBigInt((scale8 - ((scale8 * 1) / scale8))) |
---|
| 17 | + | |
---|
| 18 | + | let Amult = "100" |
---|
| 19 | + | |
---|
| 20 | + | let Dconv = "1" |
---|
| 21 | + | |
---|
| 22 | + | let SEP = "__" |
---|
| 23 | + | |
---|
| 24 | + | let EMPTY = "" |
---|
| 25 | + | |
---|
| 26 | + | let PoolActive = 1 |
---|
| 27 | + | |
---|
| 28 | + | let PoolPutDis = 2 |
---|
| 29 | + | |
---|
| 30 | + | let PoolMatcherDis = 3 |
---|
| 31 | + | |
---|
| 32 | + | let PoolShutdown = 4 |
---|
| 33 | + | |
---|
| 34 | + | let idxPoolAddress = 1 |
---|
| 35 | + | |
---|
| 36 | + | let idxPoolSt = 2 |
---|
| 37 | + | |
---|
| 38 | + | let idxLPAsId = 3 |
---|
| 39 | + | |
---|
| 40 | + | let idxAmAsId = 4 |
---|
| 41 | + | |
---|
| 42 | + | let idxPrAsId = 5 |
---|
| 43 | + | |
---|
| 44 | + | let idxAmtAsDcm = 6 |
---|
| 45 | + | |
---|
| 46 | + | let idxPriceAsDcm = 7 |
---|
| 47 | + | |
---|
| 48 | + | let idxIAmtAsId = 8 |
---|
| 49 | + | |
---|
| 50 | + | let idxIPriceAsId = 9 |
---|
| 51 | + | |
---|
| 52 | + | let idxFactStakCntr = 1 |
---|
| 53 | + | |
---|
| 54 | + | let idxFactSlippCntr = 7 |
---|
| 55 | + | |
---|
| 56 | + | let idxFactGwxRewCntr = 10 |
---|
| 57 | + | |
---|
| 58 | + | let delay = "%s__delay" |
---|
| 59 | + | |
---|
| 60 | + | func t1 (origVal,origScaleMult) = fraction(toBigInt(origVal), scale18, toBigInt(origScaleMult)) |
---|
| 61 | + | |
---|
| 62 | + | |
---|
| 63 | + | func f1 (val,resultScaleMult) = toInt(fraction(val, toBigInt(resultScaleMult), scale18)) |
---|
| 64 | + | |
---|
| 65 | + | |
---|
| 66 | + | func ts (amt,resScale,curScale) = fraction(amt, resScale, curScale) |
---|
| 67 | + | |
---|
| 68 | + | |
---|
| 69 | + | func abs (val) = if ((zeroBigInt > val)) |
---|
| 70 | + | then -(val) |
---|
| 71 | + | else val |
---|
| 72 | + | |
---|
| 73 | + | |
---|
| 74 | + | func fc () = "%s__factoryContract" |
---|
| 75 | + | |
---|
| 76 | + | |
---|
| 77 | + | func mpk () = "%s__managerPublicKey" |
---|
| 78 | + | |
---|
| 79 | + | |
---|
| 80 | + | func pmpk () = "%s__pendingManagerPublicKey" |
---|
| 81 | + | |
---|
| 82 | + | |
---|
| 83 | + | func pl () = "%s%s__price__last" |
---|
| 84 | + | |
---|
| 85 | + | |
---|
| 86 | + | func ph (h,t) = makeString(["%s%s%d%d__price__history", toString(h), toString(t)], SEP) |
---|
| 87 | + | |
---|
| 88 | + | |
---|
| 89 | + | func pau (ua,txId) = ((("%s%s%s__P__" + ua) + "__") + txId) |
---|
| 90 | + | |
---|
| 91 | + | |
---|
| 92 | + | func gau (ua,txId) = ((("%s%s%s__G__" + ua) + "__") + txId) |
---|
| 93 | + | |
---|
| 94 | + | |
---|
| 95 | + | func aa () = "%s__amountAsset" |
---|
| 96 | + | |
---|
| 97 | + | |
---|
| 98 | + | func pa () = "%s__priceAsset" |
---|
| 99 | + | |
---|
| 100 | + | |
---|
| 101 | + | func amp () = "%s__amp" |
---|
| 102 | + | |
---|
| 103 | + | |
---|
| 104 | + | func ada () = "%s__addonAddr" |
---|
| 105 | + | |
---|
| 106 | + | |
---|
| 107 | + | func lgotc (caller) = makeString(["%s%s__lastGetOneTknCall", caller], SEP) |
---|
| 108 | + | |
---|
| 109 | + | |
---|
| 110 | + | func lsotc (caller) = makeString(["%s%s__lastPutOneTknCall", caller], SEP) |
---|
| 111 | + | |
---|
| 112 | + | |
---|
| 113 | + | func fcfg () = "%s__factoryConfig" |
---|
| 114 | + | |
---|
| 115 | + | |
---|
| 116 | + | func mtpk () = "%s%s__matcher__publicKey" |
---|
| 117 | + | |
---|
| 118 | + | |
---|
| 119 | + | func pc (iAmtAs,iPrAs) = (((("%d%d%s__" + iAmtAs) + "__") + iPrAs) + "__config") |
---|
| 120 | + | |
---|
| 121 | + | |
---|
| 122 | + | func mba (bAStr) = ("%s%s%s__mappings__baseAsset2internalId__" + bAStr) |
---|
| 123 | + | |
---|
| 124 | + | |
---|
| 125 | + | func aps () = "%s__shutdown" |
---|
| 126 | + | |
---|
| 127 | + | |
---|
| 128 | + | func keyAllowedLpStableScriptHash () = "%s__allowedLpStableScriptHash" |
---|
| 129 | + | |
---|
| 130 | + | |
---|
| 131 | + | func toe (orV,sendrV,matchV) = throw(((((("Failed: ordValid=" + toString(orV)) + " sndrValid=") + toString(sendrV)) + " mtchrValid=") + toString(matchV))) |
---|
| 132 | + | |
---|
| 133 | + | |
---|
| 134 | + | func str (val) = match val { |
---|
| 135 | + | case valStr: String => |
---|
| 136 | + | valStr |
---|
| 137 | + | case _ => |
---|
| 138 | + | throw("fail cast to String") |
---|
| 139 | + | } |
---|
| 140 | + | |
---|
| 141 | + | |
---|
| 142 | + | func strf (addr,key) = valueOrErrorMessage(getString(addr, key), makeString(["mandatory ", toString(addr), ".", key, " not defined"], "")) |
---|
| 143 | + | |
---|
| 144 | + | |
---|
| 145 | + | func intf (addr,key) = valueOrErrorMessage(getInteger(addr, key), makeString(["mandatory ", toString(addr), ".", key, " not defined"], "")) |
---|
| 146 | + | |
---|
| 147 | + | |
---|
| 148 | + | let fca = addressFromStringValue(strf(this, fc())) |
---|
| 149 | + | |
---|
| 150 | + | let A = strf(this, amp()) |
---|
| 151 | + | |
---|
| 152 | + | func igs () = valueOrElse(getBoolean(fca, aps()), false) |
---|
| 153 | + | |
---|
| 154 | + | |
---|
| 155 | + | func mp () = fromBase58String(strf(fca, mtpk())) |
---|
| 156 | + | |
---|
| 157 | + | |
---|
| 158 | + | func gpc () = { |
---|
| 159 | + | let amtAs = strf(this, aa()) |
---|
| 160 | + | let priceAs = strf(this, pa()) |
---|
| 161 | + | let iPriceAs = intf(fca, mba(priceAs)) |
---|
| 162 | + | let iAmtAs = intf(fca, mba(amtAs)) |
---|
| 163 | + | split(strf(fca, pc(toString(iAmtAs), toString(iPriceAs))), SEP) |
---|
| 164 | + | } |
---|
| 165 | + | |
---|
| 166 | + | |
---|
| 167 | + | func gfc () = split(strf(fca, fcfg()), SEP) |
---|
| 168 | + | |
---|
| 169 | + | |
---|
| 170 | + | 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) |
---|
| 171 | + | |
---|
| 172 | + | |
---|
| 173 | + | 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) |
---|
| 174 | + | |
---|
| 175 | + | |
---|
| 176 | + | func getAccBalance (assetId) = if ((assetId == "WAVES")) |
---|
| 177 | + | then wavesBalance(this).available |
---|
| 178 | + | else assetBalance(this, fromBase58String(assetId)) |
---|
| 179 | + | |
---|
| 180 | + | |
---|
| 181 | + | func cpbi (prAmtX18,amAmtX18) = fraction(prAmtX18, scale18, amAmtX18) |
---|
| 182 | + | |
---|
| 183 | + | |
---|
| 184 | + | func vad (A1,A2,slippage) = { |
---|
| 185 | + | let diff = fraction((A1 - A2), scale8BigInt, A2) |
---|
| 186 | + | let pass = ((slippage - abs(diff)) > zeroBigInt) |
---|
| 187 | + | if (!(pass)) |
---|
| 188 | + | then throw(("Big slpg: " + toString(diff))) |
---|
| 189 | + | else $Tuple2(pass, min([A1, A2])) |
---|
| 190 | + | } |
---|
| 191 | + | |
---|
| 192 | + | |
---|
| 193 | + | func vd (D1,D0,slpg) = { |
---|
| 194 | + | let diff = fraction(D0, scale8BigInt, D1) |
---|
| 195 | + | let fail = (slpg > diff) |
---|
| 196 | + | if (if (fail) |
---|
| 197 | + | then true |
---|
| 198 | + | else (D0 > D1)) |
---|
| 199 | + | then throw(((((((toString(D0) + " ") + toString(D1)) + " ") + toString(diff)) + " ") + toString(slpg))) |
---|
| 200 | + | else fail |
---|
| 201 | + | } |
---|
| 202 | + | |
---|
| 203 | + | |
---|
| 204 | + | func pcp (amAssetDcm,prAssetDcm,amAmt,prAmt) = { |
---|
| 205 | + | let amtAsAmtX18 = t1(amAmt, amAssetDcm) |
---|
| 206 | + | let prAsAmtX18 = t1(prAmt, prAssetDcm) |
---|
| 207 | + | cpbi(prAsAmtX18, amtAsAmtX18) |
---|
| 208 | + | } |
---|
| 209 | + | |
---|
| 210 | + | |
---|
| 211 | + | func calcPrices (amAmt,prAmt,lpAmt) = { |
---|
| 212 | + | let cfg = gpc() |
---|
| 213 | + | let amtAsDcm = parseIntValue(cfg[idxAmtAsDcm]) |
---|
| 214 | + | let prAsDcm = parseIntValue(cfg[idxPriceAsDcm]) |
---|
| 215 | + | let priceX18 = pcp(amtAsDcm, prAsDcm, amAmt, prAmt) |
---|
| 216 | + | let amAmtX18 = t1(amAmt, amtAsDcm) |
---|
| 217 | + | let prAmtX18 = t1(prAmt, prAsDcm) |
---|
| 218 | + | let lpAmtX18 = t1(lpAmt, scale8) |
---|
| 219 | + | let lpPrInAmAsX18 = cpbi(amAmtX18, lpAmtX18) |
---|
| 220 | + | let lpPrInPrAsX18 = cpbi(prAmtX18, lpAmtX18) |
---|
| 221 | + | [priceX18, lpPrInAmAsX18, lpPrInPrAsX18] |
---|
| 222 | + | } |
---|
| 223 | + | |
---|
| 224 | + | |
---|
| 225 | + | func calculatePrices (amAmt,prAmt,lpAmt) = { |
---|
| 226 | + | let p = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 227 | + | [f1(p[0], scale8), f1(p[1], scale8), f1(p[2], scale8)] |
---|
| 228 | + | } |
---|
| 229 | + | |
---|
| 230 | + | |
---|
| 231 | + | func ego (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 232 | + | let cfg = gpc() |
---|
| 233 | + | let lpId = cfg[idxLPAsId] |
---|
| 234 | + | let amId = cfg[idxAmAsId] |
---|
| 235 | + | let prId = cfg[idxPrAsId] |
---|
| 236 | + | let amDcm = parseIntValue(cfg[idxAmtAsDcm]) |
---|
| 237 | + | let prDcm = parseIntValue(cfg[idxPriceAsDcm]) |
---|
| 238 | + | let sts = cfg[idxPoolSt] |
---|
| 239 | + | let lpEmiss = valueOrErrorMessage(assetInfo(fromBase58String(lpId)), "Wrong LP id").quantity |
---|
| 240 | + | if ((lpId != pmtAssetId)) |
---|
| 241 | + | then throw("Wrong pmt asset") |
---|
| 242 | + | else { |
---|
| 243 | + | let amBalance = getAccBalance(amId) |
---|
| 244 | + | let prBalance = getAccBalance(prId) |
---|
| 245 | + | let amountBalanceDefaultDecimals = fraction(amBalance, defaultDecimals, amDcm) |
---|
| 246 | + | let priceBalanceDefaultDecimals = fraction(prBalance, defaultDecimals, prDcm) |
---|
| 247 | + | let amBalanceX18 = t1(amountBalanceDefaultDecimals, defaultDecimals) |
---|
| 248 | + | let prBalanceX18 = t1(priceBalanceDefaultDecimals, defaultDecimals) |
---|
| 249 | + | let curPriceX18 = cpbi(prBalanceX18, amBalanceX18) |
---|
| 250 | + | let curPrice = f1(curPriceX18, scale8) |
---|
| 251 | + | let pmtLpAmtX18 = t1(pmtLpAmt, scale8) |
---|
| 252 | + | let lpEmissX18 = t1(lpEmiss, scale8) |
---|
| 253 | + | let outAmAmtX18 = fraction(amBalanceX18, pmtLpAmtX18, lpEmissX18) |
---|
| 254 | + | let outPrAmtX18 = fraction(prBalanceX18, pmtLpAmtX18, lpEmissX18) |
---|
| 255 | + | let outAmountAmountDefaultDecimals = f1(outAmAmtX18, defaultDecimals) |
---|
| 256 | + | let outPriceAmountDefaultDecimals = f1(outPrAmtX18, defaultDecimals) |
---|
| 257 | + | let outAmAmt = fraction(outAmountAmountDefaultDecimals, amDcm, defaultDecimals) |
---|
| 258 | + | let outPrAmt = fraction(outPriceAmountDefaultDecimals, prDcm, defaultDecimals) |
---|
| 259 | + | let state = if ((txId58 == "")) |
---|
| 260 | + | then nil |
---|
| 261 | + | else [ScriptTransfer(userAddress, outAmAmt, if ((amId == "WAVES")) |
---|
| 262 | + | then unit |
---|
| 263 | + | else fromBase58String(amId)), ScriptTransfer(userAddress, outPrAmt, if ((prId == "WAVES")) |
---|
| 264 | + | then unit |
---|
| 265 | + | 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)] |
---|
| 266 | + | $Tuple10(outAmAmt, outPrAmt, amId, prId, amBalance, prBalance, lpEmiss, curPriceX18, sts, state) |
---|
| 267 | + | } |
---|
| 268 | + | } |
---|
| 269 | + | |
---|
| 270 | + | |
---|
| 271 | + | func epo (txId58,slippage,inAmAmt,inAmId,inPrAmt,inPrId,userAddress,isEval,emitLp,isOneAsset,pmtAmt,pmtId) = { |
---|
| 272 | + | let cfg = gpc() |
---|
| 273 | + | let lpId = fromBase58String(cfg[idxLPAsId]) |
---|
| 274 | + | let amIdStr = cfg[idxAmAsId] |
---|
| 275 | + | let prIdStr = cfg[idxPrAsId] |
---|
| 276 | + | let inAmIdStr = cfg[idxIAmtAsId] |
---|
| 277 | + | let inPrIdStr = cfg[idxIPriceAsId] |
---|
| 278 | + | let amtDcm = parseIntValue(cfg[idxAmtAsDcm]) |
---|
| 279 | + | let priceDcm = parseIntValue(cfg[idxPriceAsDcm]) |
---|
| 280 | + | let sts = cfg[idxPoolSt] |
---|
| 281 | + | let lpEm = valueOrErrorMessage(assetInfo(lpId), "Wr lp as").quantity |
---|
| 282 | + | let amBalance = if (isEval) |
---|
| 283 | + | then getAccBalance(amIdStr) |
---|
| 284 | + | else if (if (isOneAsset) |
---|
| 285 | + | then (pmtId == amIdStr) |
---|
| 286 | + | else false) |
---|
| 287 | + | then (getAccBalance(amIdStr) - pmtAmt) |
---|
| 288 | + | else if (isOneAsset) |
---|
| 289 | + | then getAccBalance(amIdStr) |
---|
| 290 | + | else (getAccBalance(amIdStr) - inAmAmt) |
---|
| 291 | + | let prBalance = if (isEval) |
---|
| 292 | + | then getAccBalance(prIdStr) |
---|
| 293 | + | else if (if (isOneAsset) |
---|
| 294 | + | then (pmtId == prIdStr) |
---|
| 295 | + | else false) |
---|
| 296 | + | then (getAccBalance(prIdStr) - pmtAmt) |
---|
| 297 | + | else if (isOneAsset) |
---|
| 298 | + | then getAccBalance(prIdStr) |
---|
| 299 | + | else (getAccBalance(prIdStr) - inPrAmt) |
---|
| 300 | + | let amountBalanceDefaultDecimals = fraction(amBalance, defaultDecimals, amtDcm) |
---|
| 301 | + | let priceBalanceDefaultDecimals = fraction(prBalance, defaultDecimals, priceDcm) |
---|
| 302 | + | let inAmountAssetAmountDefaultDecimals = fraction(inAmAmt, defaultDecimals, amtDcm) |
---|
| 303 | + | let inPriceAssetAmountDefaultDecimals = fraction(inPrAmt, defaultDecimals, priceDcm) |
---|
| 304 | + | let inAmAssetAmtX18 = t1(inAmountAssetAmountDefaultDecimals, defaultDecimals) |
---|
| 305 | + | let inPrAssetAmtX18 = t1(inPriceAssetAmountDefaultDecimals, defaultDecimals) |
---|
| 306 | + | let userPriceX18 = cpbi(inPrAssetAmtX18, inAmAssetAmtX18) |
---|
| 307 | + | let amBalanceX18 = t1(amountBalanceDefaultDecimals, defaultDecimals) |
---|
| 308 | + | let prBalanceX18 = t1(priceBalanceDefaultDecimals, defaultDecimals) |
---|
| 309 | + | let r = if ((lpEm == 0)) |
---|
| 310 | + | then { |
---|
| 311 | + | let curPriceX18 = zeroBigInt |
---|
| 312 | + | let slippageX18 = zeroBigInt |
---|
| 313 | + | let lpAmtX18 = pow((inAmAssetAmtX18 * inPrAssetAmtX18), 0, toBigInt(5), 1, 0, DOWN) |
---|
| 314 | + | $Tuple5(f1(lpAmtX18, scale8), f1(inAmAssetAmtX18, defaultDecimals), f1(inPrAssetAmtX18, defaultDecimals), cpbi((prBalanceX18 + inPrAssetAmtX18), (amBalanceX18 + inAmAssetAmtX18)), slippageX18) |
---|
| 315 | + | } |
---|
| 316 | + | else { |
---|
| 317 | + | let curPriceX18 = cpbi(prBalanceX18, amBalanceX18) |
---|
| 318 | + | let slippageRealX18 = fraction(abs((curPriceX18 - userPriceX18)), scale18, curPriceX18) |
---|
| 319 | + | let slippageX18 = t1(slippage, scale8) |
---|
| 320 | + | if (if ((curPriceX18 != zeroBigInt)) |
---|
| 321 | + | then (slippageRealX18 > slippageX18) |
---|
| 322 | + | else false) |
---|
| 323 | + | then throw(((("Price slippage " + toString(slippageRealX18)) + " > ") + toString(slippageX18))) |
---|
| 324 | + | else { |
---|
| 325 | + | let lpEmissionX18 = t1(lpEm, scale8) |
---|
| 326 | + | let prViaAmX18 = fraction(inAmAssetAmtX18, curPriceX18, scale18) |
---|
| 327 | + | let amViaPrX18 = fraction(inPrAssetAmtX18, scale18, curPriceX18) |
---|
| 328 | + | let expectedAmts = if ((prViaAmX18 > inPrAssetAmtX18)) |
---|
| 329 | + | then $Tuple2(amViaPrX18, inPrAssetAmtX18) |
---|
| 330 | + | else $Tuple2(inAmAssetAmtX18, prViaAmX18) |
---|
| 331 | + | let expAmtAssetAmtX18 = expectedAmts._1 |
---|
| 332 | + | let expPriceAssetAmtX18 = expectedAmts._2 |
---|
| 333 | + | let lpAmtX18 = fraction(lpEmissionX18, expPriceAssetAmtX18, prBalanceX18) |
---|
| 334 | + | $Tuple5(f1(lpAmtX18, scale8), f1(expAmtAssetAmtX18, defaultDecimals), f1(expPriceAssetAmtX18, defaultDecimals), curPriceX18, slippageX18) |
---|
| 335 | + | } |
---|
| 336 | + | } |
---|
| 337 | + | let calcLpAmt = r._1 |
---|
| 338 | + | let calcAmAssetPmt = fraction(r._2, amtDcm, defaultDecimals) |
---|
| 339 | + | let calcPrAssetPmt = fraction(r._3, priceDcm, defaultDecimals) |
---|
| 340 | + | let curPrice = f1(r._4, scale8) |
---|
| 341 | + | let slippageCalc = f1(r._5, scale8) |
---|
| 342 | + | if ((0 >= calcLpAmt)) |
---|
| 343 | + | then throw("LP <= 0") |
---|
| 344 | + | else { |
---|
| 345 | + | let emitLpAmt = if (!(emitLp)) |
---|
| 346 | + | then 0 |
---|
| 347 | + | else calcLpAmt |
---|
| 348 | + | let amDiff = (inAmAmt - calcAmAssetPmt) |
---|
| 349 | + | let prDiff = (inPrAmt - calcPrAssetPmt) |
---|
| 350 | + | let $t01664716992 = if (if (isOneAsset) |
---|
| 351 | + | then (pmtId == amIdStr) |
---|
| 352 | + | else false) |
---|
| 353 | + | then $Tuple2(pmtAmt, 0) |
---|
| 354 | + | else if (if (isOneAsset) |
---|
| 355 | + | then (pmtId == prIdStr) |
---|
| 356 | + | else false) |
---|
| 357 | + | then $Tuple2(0, pmtAmt) |
---|
| 358 | + | else $Tuple2(calcAmAssetPmt, calcPrAssetPmt) |
---|
| 359 | + | let writeAmAmt = $t01664716992._1 |
---|
| 360 | + | let writePrAmt = $t01664716992._2 |
---|
| 361 | + | 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))] |
---|
| 362 | + | $Tuple13(calcLpAmt, emitLpAmt, curPrice, amBalance, prBalance, lpEm, lpId, sts, commonState, amDiff, prDiff, inAmId, inPrId) |
---|
| 363 | + | } |
---|
| 364 | + | } |
---|
| 365 | + | |
---|
| 366 | + | |
---|
| 367 | + | func moa (order) = { |
---|
| 368 | + | let cfg = gpc() |
---|
| 369 | + | let amtAsId = cfg[idxAmAsId] |
---|
| 370 | + | let prAsId = cfg[idxPrAsId] |
---|
| 371 | + | let sts = parseIntValue(cfg[idxPoolSt]) |
---|
| 372 | + | let amtAsDcm = parseIntValue(cfg[idxAmtAsDcm]) |
---|
| 373 | + | let prAsDcm = parseIntValue(cfg[idxPriceAsDcm]) |
---|
| 374 | + | let accAmtAsBalance = getAccBalance(amtAsId) |
---|
| 375 | + | let accPrAsBalance = getAccBalance(prAsId) |
---|
| 376 | + | let curPriceX18 = if ((order.orderType == Buy)) |
---|
| 377 | + | then pcp(amtAsDcm, prAsDcm, (accAmtAsBalance + order.amount), accPrAsBalance) |
---|
| 378 | + | else pcp(amtAsDcm, prAsDcm, (accAmtAsBalance - order.amount), accPrAsBalance) |
---|
| 379 | + | let curPrice = f1(curPriceX18, scale8) |
---|
| 380 | + | if (if (if (igs()) |
---|
| 381 | + | then true |
---|
| 382 | + | else (sts == PoolMatcherDis)) |
---|
| 383 | + | then true |
---|
| 384 | + | else (sts == PoolShutdown)) |
---|
| 385 | + | then throw("Admin blocked") |
---|
| 386 | + | else { |
---|
| 387 | + | let orAmtAsset = order.assetPair.amountAsset |
---|
| 388 | + | let orAmtAsStr = if ((orAmtAsset == unit)) |
---|
| 389 | + | then "WAVES" |
---|
| 390 | + | else toBase58String(value(orAmtAsset)) |
---|
| 391 | + | let orPrAsset = order.assetPair.priceAsset |
---|
| 392 | + | let orPrAsStr = if ((orPrAsset == unit)) |
---|
| 393 | + | then "WAVES" |
---|
| 394 | + | else toBase58String(value(orPrAsset)) |
---|
| 395 | + | if (if ((orAmtAsStr != amtAsId)) |
---|
| 396 | + | then true |
---|
| 397 | + | else (orPrAsStr != prAsId)) |
---|
| 398 | + | then throw("Wr assets") |
---|
| 399 | + | else { |
---|
| 400 | + | let orderPrice = order.price |
---|
| 401 | + | let priceDcm = fraction(scale8, prAsDcm, amtAsDcm) |
---|
| 402 | + | let castOrderPrice = ts(orderPrice, scale8, priceDcm) |
---|
| 403 | + | let isOrderPriceValid = if ((order.orderType == Buy)) |
---|
| 404 | + | then (curPrice >= castOrderPrice) |
---|
| 405 | + | else (castOrderPrice >= curPrice) |
---|
| 406 | + | true |
---|
| 407 | + | } |
---|
| 408 | + | } |
---|
| 409 | + | } |
---|
| 410 | + | |
---|
| 411 | + | |
---|
| 412 | + | func cg (i) = if ((size(i.payments) != 1)) |
---|
| 413 | + | then throw("1 pmnt exp") |
---|
| 414 | + | else { |
---|
| 415 | + | let pmt = value(i.payments[0]) |
---|
| 416 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 417 | + | let pmtAmt = pmt.amount |
---|
| 418 | + | let r = ego(toBase58String(i.transactionId), toBase58String(pmtAssetId), pmtAmt, i.caller) |
---|
| 419 | + | let outAmAmt = r._1 |
---|
| 420 | + | let outPrAmt = r._2 |
---|
| 421 | + | let sts = parseIntValue(r._9) |
---|
| 422 | + | let state = r._10 |
---|
| 423 | + | if (if (igs()) |
---|
| 424 | + | then true |
---|
| 425 | + | else (sts == PoolShutdown)) |
---|
| 426 | + | then throw(("Admin blocked: " + toString(sts))) |
---|
| 427 | + | else $Tuple5(outAmAmt, outPrAmt, pmtAmt, pmtAssetId, state) |
---|
| 428 | + | } |
---|
| 429 | + | |
---|
| 430 | + | |
---|
| 431 | + | func cp (caller,txId,amAsPmt,prAsPmt,slippage,emitLp,isOneAsset,pmtAmt,pmtId) = { |
---|
| 432 | + | let r = epo(txId, slippage, value(amAsPmt).amount, value(amAsPmt).assetId, value(prAsPmt).amount, value(prAsPmt).assetId, caller, false, emitLp, isOneAsset, pmtAmt, pmtId) |
---|
| 433 | + | let sts = parseIntValue(r._8) |
---|
| 434 | + | if (if (if (igs()) |
---|
| 435 | + | then true |
---|
| 436 | + | else (sts == PoolPutDis)) |
---|
| 437 | + | then true |
---|
| 438 | + | else (sts == PoolShutdown)) |
---|
| 439 | + | then throw(("Blocked:" + toString(sts))) |
---|
| 440 | + | else r |
---|
| 441 | + | } |
---|
| 442 | + | |
---|
| 443 | + | |
---|
| 444 | + | func m () = match getString(mpk()) { |
---|
| 445 | + | case s: String => |
---|
| 446 | + | fromBase58String(s) |
---|
| 447 | + | case _: Unit => |
---|
| 448 | + | unit |
---|
| 449 | + | case _ => |
---|
| 450 | + | throw("Match error") |
---|
| 451 | + | } |
---|
| 452 | + | |
---|
| 453 | + | |
---|
| 454 | + | func pm () = match getString(pmpk()) { |
---|
| 455 | + | case s: String => |
---|
| 456 | + | fromBase58String(s) |
---|
| 457 | + | case _: Unit => |
---|
| 458 | + | unit |
---|
| 459 | + | case _ => |
---|
| 460 | + | throw("Match error") |
---|
| 461 | + | } |
---|
| 462 | + | |
---|
| 463 | + | |
---|
| 464 | + | let pd = throw("Permission denied") |
---|
| 465 | + | |
---|
| 466 | + | func mm (i) = match m() { |
---|
| 467 | + | case pk: ByteVector => |
---|
| 468 | + | if ((i.callerPublicKey == pk)) |
---|
| 469 | + | then true |
---|
| 470 | + | else pd |
---|
| 471 | + | case _: Unit => |
---|
| 472 | + | if ((i.caller == this)) |
---|
| 473 | + | then true |
---|
| 474 | + | else pd |
---|
| 475 | + | case _ => |
---|
| 476 | + | throw("Match error") |
---|
| 477 | + | } |
---|
| 478 | + | |
---|
| 479 | + | |
---|
| 480 | + | @Callable(i) |
---|
| 481 | + | func constructor (fc) = { |
---|
| 482 | + | let c = mm(i) |
---|
| 483 | + | if ((c == c)) |
---|
| 484 | + | then [StringEntry(fc(), fc)] |
---|
| 485 | + | else throw("Strict value is not equal to itself.") |
---|
| 486 | + | } |
---|
| 487 | + | |
---|
| 488 | + | |
---|
| 489 | + | |
---|
| 490 | + | @Callable(i) |
---|
| 491 | + | func setManager (pendingManagerPublicKey) = { |
---|
| 492 | + | let c = mm(i) |
---|
| 493 | + | if ((c == c)) |
---|
| 494 | + | then { |
---|
| 495 | + | let cm = fromBase58String(pendingManagerPublicKey) |
---|
| 496 | + | if ((cm == cm)) |
---|
| 497 | + | then [StringEntry(pmpk(), pendingManagerPublicKey)] |
---|
| 498 | + | else throw("Strict value is not equal to itself.") |
---|
| 499 | + | } |
---|
| 500 | + | else throw("Strict value is not equal to itself.") |
---|
| 501 | + | } |
---|
| 502 | + | |
---|
| 503 | + | |
---|
| 504 | + | |
---|
| 505 | + | @Callable(i) |
---|
| 506 | + | func confirmManager () = { |
---|
| 507 | + | let p = pm() |
---|
| 508 | + | let hpm = if (isDefined(p)) |
---|
| 509 | + | then true |
---|
| 510 | + | else throw("No pending manager") |
---|
| 511 | + | if ((hpm == hpm)) |
---|
| 512 | + | then { |
---|
| 513 | + | let cpm = if ((i.callerPublicKey == value(p))) |
---|
| 514 | + | then true |
---|
| 515 | + | else throw("You are not pending manager") |
---|
| 516 | + | if ((cpm == cpm)) |
---|
| 517 | + | then [StringEntry(mpk(), toBase58String(value(p))), DeleteEntry(pmpk())] |
---|
| 518 | + | else throw("Strict value is not equal to itself.") |
---|
| 519 | + | } |
---|
| 520 | + | else throw("Strict value is not equal to itself.") |
---|
| 521 | + | } |
---|
| 522 | + | |
---|
| 523 | + | |
---|
| 524 | + | |
---|
| 525 | + | @Callable(i) |
---|
| 526 | + | func put (slip,autoStake) = { |
---|
| 527 | + | let factCfg = gfc() |
---|
| 528 | + | let stakingCntr = valueOrErrorMessage(addressFromString(factCfg[idxFactStakCntr]), "Wr st addr") |
---|
| 529 | + | let slipCntr = valueOrErrorMessage(addressFromString(factCfg[idxFactSlippCntr]), "Wr sl addr") |
---|
| 530 | + | if ((0 > slip)) |
---|
| 531 | + | then throw("Wrong slippage") |
---|
| 532 | + | else if ((size(i.payments) != 2)) |
---|
| 533 | + | then throw("2 pmnts expd") |
---|
| 534 | + | else { |
---|
| 535 | + | 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, 0, "") |
---|
| 536 | + | let emitLpAmt = e._2 |
---|
| 537 | + | let lpAssetId = e._7 |
---|
| 538 | + | let state = e._9 |
---|
| 539 | + | let amDiff = e._10 |
---|
| 540 | + | let prDiff = e._11 |
---|
| 541 | + | let amId = e._12 |
---|
| 542 | + | let prId = e._13 |
---|
| 543 | + | let r = invoke(fca, "emit", [emitLpAmt], nil) |
---|
| 544 | + | if ((r == r)) |
---|
| 545 | + | then { |
---|
| 546 | + | let el = match r { |
---|
| 547 | + | case legacy: Address => |
---|
| 548 | + | invoke(legacy, "emit", [emitLpAmt], nil) |
---|
| 549 | + | case _ => |
---|
| 550 | + | unit |
---|
| 551 | + | } |
---|
| 552 | + | if ((el == el)) |
---|
| 553 | + | then { |
---|
| 554 | + | let sa = if ((amDiff > 0)) |
---|
| 555 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(amId, amDiff)]) |
---|
| 556 | + | else nil |
---|
| 557 | + | if ((sa == sa)) |
---|
| 558 | + | then { |
---|
| 559 | + | let sp = if ((prDiff > 0)) |
---|
| 560 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(prId, prDiff)]) |
---|
| 561 | + | else nil |
---|
| 562 | + | if ((sp == sp)) |
---|
| 563 | + | then { |
---|
| 564 | + | let lpTrnsfr = if (autoStake) |
---|
| 565 | + | then { |
---|
| 566 | + | let ss = invoke(stakingCntr, "stake", nil, [AttachedPayment(lpAssetId, emitLpAmt)]) |
---|
| 567 | + | if ((ss == ss)) |
---|
| 568 | + | then nil |
---|
| 569 | + | else throw("Strict value is not equal to itself.") |
---|
| 570 | + | } |
---|
| 571 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpAssetId)] |
---|
| 572 | + | (state ++ lpTrnsfr) |
---|
| 573 | + | } |
---|
| 574 | + | else throw("Strict value is not equal to itself.") |
---|
| 575 | + | } |
---|
| 576 | + | else throw("Strict value is not equal to itself.") |
---|
| 577 | + | } |
---|
| 578 | + | else throw("Strict value is not equal to itself.") |
---|
| 579 | + | } |
---|
| 580 | + | else throw("Strict value is not equal to itself.") |
---|
| 581 | + | } |
---|
| 582 | + | } |
---|
| 583 | + | |
---|
| 584 | + | |
---|
| 585 | + | |
---|
| 586 | + | @Callable(i) |
---|
| 587 | + | func putOneTkn (amAssetPart,prAssetPart,outLp,slippage,autoStake) = { |
---|
| 588 | + | let cfg = gfc() |
---|
| 589 | + | let stakingCntr = valueOrErrorMessage(addressFromString(cfg[idxFactStakCntr]), "Wr st addr") |
---|
| 590 | + | let slipCntr = valueOrErrorMessage(addressFromString(cfg[idxFactSlippCntr]), "Wr sl addr") |
---|
| 591 | + | let gwxCntr = valueOrErrorMessage(addressFromString(cfg[idxFactGwxRewCntr]), "Wr gwx addr") |
---|
| 592 | + | let poolCfg = gpc() |
---|
| 593 | + | let amId = poolCfg[idxAmAsId] |
---|
| 594 | + | let prId = poolCfg[idxPrAsId] |
---|
| 595 | + | let amDcm = parseIntValue(poolCfg[idxAmtAsDcm]) |
---|
| 596 | + | let prDcm = parseIntValue(poolCfg[idxPriceAsDcm]) |
---|
| 597 | + | let addon = valueOrElse(getString(this, ada()), "") |
---|
| 598 | + | let userAddress = if ((addon == toString(i.caller))) |
---|
| 599 | + | then i.originCaller |
---|
| 600 | + | else i.caller |
---|
| 601 | + | if (if (if (if ((0 >= slippage)) |
---|
| 602 | + | then true |
---|
| 603 | + | else (0 >= amAssetPart)) |
---|
| 604 | + | then true |
---|
| 605 | + | else (0 >= prAssetPart)) |
---|
| 606 | + | then true |
---|
| 607 | + | else (0 >= outLp)) |
---|
| 608 | + | then throw("Wrong params") |
---|
| 609 | + | else if ((size(i.payments) != 1)) |
---|
| 610 | + | then throw("1 pmnt expd") |
---|
| 611 | + | else { |
---|
| 612 | + | let pmt = value(i.payments[0]) |
---|
| 613 | + | let pmtAssetId = toBase58String(value(pmt.assetId)) |
---|
| 614 | + | let pmtAmt = pmt.amount |
---|
| 615 | + | if ((10000000 > pmtAmt)) |
---|
| 616 | + | then throw("Wrong pmt amt") |
---|
| 617 | + | else { |
---|
| 618 | + | let amBalance = getAccBalance(amId) |
---|
| 619 | + | let prBalance = getAccBalance(prId) |
---|
| 620 | + | let $t02564226218 = if ((pmtAssetId == amId)) |
---|
| 621 | + | then if (if ((pmtAmt > amBalance)) |
---|
| 622 | + | then true |
---|
| 623 | + | else (amAssetPart > pmtAmt)) |
---|
| 624 | + | then throw("invalid payment amount") |
---|
| 625 | + | else $Tuple6((amBalance - pmtAmt), prBalance, (pmtAmt - amAssetPart), prAssetPart, 0, 0) |
---|
| 626 | + | else if ((pmtAssetId == prId)) |
---|
| 627 | + | then if (if ((pmtAmt > prBalance)) |
---|
| 628 | + | then true |
---|
| 629 | + | else (prAssetPart > pmtAmt)) |
---|
| 630 | + | then throw("invalid payment amount") |
---|
| 631 | + | else $Tuple6(amBalance, (prBalance - pmtAmt), 0, 0, (pmtAmt - prAssetPart), amAssetPart) |
---|
| 632 | + | else throw("wrong pmtAssetId") |
---|
| 633 | + | let amBalanceNow = $t02564226218._1 |
---|
| 634 | + | let prBalanceNow = $t02564226218._2 |
---|
| 635 | + | let virtSwapInAm = $t02564226218._3 |
---|
| 636 | + | let virtSwapOutPr = $t02564226218._4 |
---|
| 637 | + | let virtSwapInPr = $t02564226218._5 |
---|
| 638 | + | let virtSwapOutAm = $t02564226218._6 |
---|
| 639 | + | let D0 = invoke(gwxCntr, "calcD", [toString(amBalanceNow), toString(prBalanceNow), A, Amult, Dconv], nil) |
---|
| 640 | + | let D1 = invoke(gwxCntr, "calcD", [toString(toBigInt(((amBalanceNow + virtSwapInAm) - virtSwapOutAm))), toString(toBigInt(((prBalanceNow + virtSwapInPr) - virtSwapOutPr))), A, Amult, Dconv], nil) |
---|
| 641 | + | let D0vsD1 = vd(parseBigIntValue(str(D1)), parseBigIntValue(str(D0)), slippage4D) |
---|
| 642 | + | if ((D0vsD1 == D0vsD1)) |
---|
| 643 | + | then { |
---|
| 644 | + | let estPut = cp(toString(i.caller), toBase58String(i.transactionId), AttachedPayment(fromBase58String(amId), amAssetPart), AttachedPayment(fromBase58String(prId), prAssetPart), slippage, true, true, pmtAmt, pmtAssetId) |
---|
| 645 | + | let estimLP = estPut._2 |
---|
| 646 | + | let lpAssetId = estPut._7 |
---|
| 647 | + | let state = estPut._9 |
---|
| 648 | + | let amDiff = estPut._10 |
---|
| 649 | + | let prDiff = estPut._11 |
---|
| 650 | + | let lpCalcRes = vad(toBigInt(estimLP), toBigInt(outLp), toBigInt(slippage)) |
---|
| 651 | + | let emitLpAmt = toInt(lpCalcRes._2) |
---|
| 652 | + | let e = invoke(fca, "emit", [emitLpAmt], nil) |
---|
| 653 | + | if ((e == e)) |
---|
| 654 | + | then { |
---|
| 655 | + | let el = match e { |
---|
| 656 | + | case legacy: Address => |
---|
| 657 | + | invoke(legacy, "emit", [emitLpAmt], nil) |
---|
| 658 | + | case _ => |
---|
| 659 | + | unit |
---|
| 660 | + | } |
---|
| 661 | + | if ((el == el)) |
---|
| 662 | + | then { |
---|
| 663 | + | let sa = if ((amDiff > 0)) |
---|
| 664 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(fromBase58String(amId), amDiff)]) |
---|
| 665 | + | else nil |
---|
| 666 | + | if ((sa == sa)) |
---|
| 667 | + | then { |
---|
| 668 | + | let sp = if ((prDiff > 0)) |
---|
| 669 | + | then invoke(slipCntr, "put", nil, [AttachedPayment(fromBase58String(prId), prDiff)]) |
---|
| 670 | + | else nil |
---|
| 671 | + | if ((sp == sp)) |
---|
| 672 | + | then { |
---|
| 673 | + | let lpTrnsfr = if (autoStake) |
---|
| 674 | + | then { |
---|
| 675 | + | let ss = invoke(stakingCntr, "stake", nil, [AttachedPayment(lpAssetId, emitLpAmt)]) |
---|
| 676 | + | if ((ss == ss)) |
---|
| 677 | + | then nil |
---|
| 678 | + | else throw("Strict value is not equal to itself.") |
---|
| 679 | + | } |
---|
| 680 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpAssetId)] |
---|
| 681 | + | (state ++ lpTrnsfr) |
---|
| 682 | + | } |
---|
| 683 | + | else throw("Strict value is not equal to itself.") |
---|
| 684 | + | } |
---|
| 685 | + | else throw("Strict value is not equal to itself.") |
---|
| 686 | + | } |
---|
| 687 | + | else throw("Strict value is not equal to itself.") |
---|
| 688 | + | } |
---|
| 689 | + | else throw("Strict value is not equal to itself.") |
---|
| 690 | + | } |
---|
| 691 | + | else throw("Strict value is not equal to itself.") |
---|
| 692 | + | } |
---|
| 693 | + | } |
---|
| 694 | + | } |
---|
| 695 | + | |
---|
| 696 | + | |
---|
| 697 | + | |
---|
| 698 | + | @Callable(i) |
---|
| 699 | + | func putForFree (maxSlpg) = if ((0 > maxSlpg)) |
---|
| 700 | + | then throw("Wrong slpg") |
---|
| 701 | + | else if ((size(i.payments) != 2)) |
---|
| 702 | + | then throw("2 pmnts expd") |
---|
| 703 | + | else { |
---|
| 704 | + | 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, 0, "") |
---|
| 705 | + | estPut._9 |
---|
| 706 | + | } |
---|
| 707 | + | |
---|
| 708 | + | |
---|
| 709 | + | |
---|
| 710 | + | @Callable(i) |
---|
| 711 | + | func get () = { |
---|
| 712 | + | let r = cg(i) |
---|
| 713 | + | let outAmtAmt = r._1 |
---|
| 714 | + | let outPrAmt = r._2 |
---|
| 715 | + | let pmtAmt = r._3 |
---|
| 716 | + | let pmtAssetId = r._4 |
---|
| 717 | + | let state = r._5 |
---|
| 718 | + | let b = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 719 | + | if ((b == b)) |
---|
| 720 | + | then state |
---|
| 721 | + | else throw("Strict value is not equal to itself.") |
---|
| 722 | + | } |
---|
| 723 | + | |
---|
| 724 | + | |
---|
| 725 | + | |
---|
| 726 | + | @Callable(i) |
---|
| 727 | + | func getOneTkn (exchResult,notUsed,outAmount,outAssetId,slippage) = if ((size(i.payments) != 1)) |
---|
| 728 | + | then throw("1 pmnt expd") |
---|
| 729 | + | else { |
---|
| 730 | + | let cfg = gpc() |
---|
| 731 | + | let lpId = cfg[idxLPAsId] |
---|
| 732 | + | let amId = cfg[idxAmAsId] |
---|
| 733 | + | let prId = cfg[idxPrAsId] |
---|
| 734 | + | let amDcm = parseIntValue(cfg[idxAmtAsDcm]) |
---|
| 735 | + | let prDcm = parseIntValue(cfg[idxPriceAsDcm]) |
---|
| 736 | + | let sts = cfg[idxPoolSt] |
---|
| 737 | + | let factCfg = gfc() |
---|
| 738 | + | let gwxCntr = valueOrErrorMessage(addressFromString(factCfg[idxFactGwxRewCntr]), "Wr sl addr") |
---|
| 739 | + | let pmt = value(i.payments[0]) |
---|
| 740 | + | let addon = valueOrElse(getString(this, ada()), "") |
---|
| 741 | + | let userAddress = if ((addon == toString(i.caller))) |
---|
| 742 | + | then i.originCaller |
---|
| 743 | + | else i.caller |
---|
| 744 | + | let txId58 = toBase58String(i.transactionId) |
---|
| 745 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 746 | + | let pmtAmt = pmt.amount |
---|
| 747 | + | if ((1000000000 > pmtAmt)) |
---|
| 748 | + | then throw("Min pmt 10 LP") |
---|
| 749 | + | else if (if (if ((0 > slippage)) |
---|
| 750 | + | then true |
---|
| 751 | + | else (0 > exchResult)) |
---|
| 752 | + | then true |
---|
| 753 | + | else (0 > outAmount)) |
---|
| 754 | + | then throw("Wrong params") |
---|
| 755 | + | else if ((lpId != toBase58String(pmtAssetId))) |
---|
| 756 | + | then throw("Wrong LP") |
---|
| 757 | + | else { |
---|
| 758 | + | let r = ego(toBase58String(i.transactionId), toBase58String(pmtAssetId), pmtAmt, i.caller) |
---|
| 759 | + | let estimAmAmt = r._1 |
---|
| 760 | + | let estimPrAmt = r._2 |
---|
| 761 | + | let amBalance = getAccBalance(amId) |
---|
| 762 | + | let prBalance = getAccBalance(prId) |
---|
| 763 | + | let $t03099731454 = if ((outAssetId == amId)) |
---|
| 764 | + | then $Tuple7((amBalance - estimAmAmt), (prBalance - estimPrAmt), exchResult, estimPrAmt, 0, 0, (estimAmAmt + exchResult)) |
---|
| 765 | + | else if ((outAssetId == prId)) |
---|
| 766 | + | then $Tuple7((amBalance - estimAmAmt), (prBalance - estimPrAmt), 0, 0, exchResult, estimAmAmt, (estimPrAmt + exchResult)) |
---|
| 767 | + | else throw("wrong outAssetId") |
---|
| 768 | + | let amBalanceNow = $t03099731454._1 |
---|
| 769 | + | let prBalanceNow = $t03099731454._2 |
---|
| 770 | + | let virtSwapInAm = $t03099731454._3 |
---|
| 771 | + | let virtSwapOutPr = $t03099731454._4 |
---|
| 772 | + | let virtSwapInPr = $t03099731454._5 |
---|
| 773 | + | let virtSwapOutAm = $t03099731454._6 |
---|
| 774 | + | let totalGet = $t03099731454._7 |
---|
| 775 | + | if (if ((0 > virtSwapInAm)) |
---|
| 776 | + | then true |
---|
| 777 | + | else (0 > virtSwapInPr)) |
---|
| 778 | + | then throw("Wrong calc") |
---|
| 779 | + | else { |
---|
| 780 | + | let D0 = invoke(gwxCntr, "calcD", [toString(amBalanceNow), toString(prBalanceNow), A, Amult, Dconv], nil) |
---|
| 781 | + | let D1 = invoke(gwxCntr, "calcD", [toString(((amBalanceNow - virtSwapInAm) + virtSwapOutAm)), toString(((prBalanceNow + virtSwapOutPr) - virtSwapInPr)), A, Amult, Dconv], nil) |
---|
| 782 | + | let D0vsD1 = vd(parseBigIntValue(str(D1)), parseBigIntValue(str(D0)), slippage4D) |
---|
| 783 | + | if ((D0vsD1 == D0vsD1)) |
---|
| 784 | + | then { |
---|
| 785 | + | let finalRes = vad(toBigInt(totalGet), toBigInt(outAmount), toBigInt(slippage)) |
---|
| 786 | + | if ((finalRes == finalRes)) |
---|
| 787 | + | then { |
---|
| 788 | + | let $t03215932263 = if ((outAssetId == amId)) |
---|
| 789 | + | then $Tuple2(toInt(finalRes._2), 0) |
---|
| 790 | + | else $Tuple2(0, toInt(finalRes._2)) |
---|
| 791 | + | let outAm = $t03215932263._1 |
---|
| 792 | + | let outPr = $t03215932263._2 |
---|
| 793 | + | let curPrX18 = cpbi(t1(prBalance, prDcm), t1(amBalance, amDcm)) |
---|
| 794 | + | let curPr = f1(curPrX18, scale8) |
---|
| 795 | + | let state = [ScriptTransfer(userAddress, (outAm + outPr), if ((outAssetId == "WAVES")) |
---|
| 796 | + | then unit |
---|
| 797 | + | else fromBase58String(outAssetId)), StringEntry(gau(toString(userAddress), txId58), dataGetActionInfo(outAm, outPr, pmtAmt, curPr, height, lastBlock.timestamp)), IntegerEntry(pl(), curPr), IntegerEntry(ph(height, lastBlock.timestamp), curPr)] |
---|
| 798 | + | if ((state == state)) |
---|
| 799 | + | then { |
---|
| 800 | + | let burn = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 801 | + | if ((burn == burn)) |
---|
| 802 | + | then state |
---|
| 803 | + | else throw("Strict value is not equal to itself.") |
---|
| 804 | + | } |
---|
| 805 | + | else throw("Strict value is not equal to itself.") |
---|
| 806 | + | } |
---|
| 807 | + | else throw("Strict value is not equal to itself.") |
---|
| 808 | + | } |
---|
| 809 | + | else throw("Strict value is not equal to itself.") |
---|
| 810 | + | } |
---|
| 811 | + | } |
---|
| 812 | + | } |
---|
| 813 | + | |
---|
| 814 | + | |
---|
| 815 | + | |
---|
| 816 | + | @Callable(i) |
---|
| 817 | + | func getNoLess (noLessThenAmtAsset,noLessThenPriceAsset) = { |
---|
| 818 | + | let r = cg(i) |
---|
| 819 | + | let outAmAmt = r._1 |
---|
| 820 | + | let outPrAmt = r._2 |
---|
| 821 | + | let pmtAmt = r._3 |
---|
| 822 | + | let pmtAssetId = r._4 |
---|
| 823 | + | let state = r._5 |
---|
| 824 | + | if ((noLessThenAmtAsset > outAmAmt)) |
---|
| 825 | + | then throw(((("Failed: " + toString(outAmAmt)) + " < ") + toString(noLessThenAmtAsset))) |
---|
| 826 | + | else if ((noLessThenPriceAsset > outPrAmt)) |
---|
| 827 | + | then throw(((("Failed: " + toString(outPrAmt)) + " < ") + toString(noLessThenPriceAsset))) |
---|
| 828 | + | else { |
---|
| 829 | + | let burnLPAssetOnFactory = invoke(fca, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 830 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 831 | + | then state |
---|
| 832 | + | else throw("Strict value is not equal to itself.") |
---|
| 833 | + | } |
---|
| 834 | + | } |
---|
| 835 | + | |
---|
| 836 | + | |
---|
| 837 | + | |
---|
| 838 | + | @Callable(i) |
---|
| 839 | + | func unstakeAndGet (amount) = { |
---|
| 840 | + | let checkPayments = if ((size(i.payments) != 0)) |
---|
| 841 | + | then throw("No pmnts expd") |
---|
| 842 | + | else true |
---|
| 843 | + | if ((checkPayments == checkPayments)) |
---|
| 844 | + | then { |
---|
| 845 | + | let cfg = gpc() |
---|
| 846 | + | let factoryCfg = gfc() |
---|
| 847 | + | let lpAssetId = fromBase58String(cfg[idxLPAsId]) |
---|
| 848 | + | let staking = valueOrErrorMessage(addressFromString(factoryCfg[idxFactStakCntr]), "Wr st addr") |
---|
| 849 | + | let unstakeInv = invoke(staking, "unstake", [toBase58String(lpAssetId), amount], nil) |
---|
| 850 | + | if ((unstakeInv == unstakeInv)) |
---|
| 851 | + | then { |
---|
| 852 | + | let r = ego(toBase58String(i.transactionId), toBase58String(lpAssetId), amount, i.caller) |
---|
| 853 | + | let sts = parseIntValue(r._9) |
---|
| 854 | + | let state = r._10 |
---|
| 855 | + | let v = if (if (igs()) |
---|
| 856 | + | then true |
---|
| 857 | + | else (sts == PoolShutdown)) |
---|
| 858 | + | then throw(("Blocked: " + toString(sts))) |
---|
| 859 | + | else true |
---|
| 860 | + | if ((v == v)) |
---|
| 861 | + | then { |
---|
| 862 | + | let burnA = invoke(fca, "burn", [amount], [AttachedPayment(lpAssetId, amount)]) |
---|
| 863 | + | if ((burnA == burnA)) |
---|
| 864 | + | then state |
---|
| 865 | + | else throw("Strict value is not equal to itself.") |
---|
| 866 | + | } |
---|
| 867 | + | else throw("Strict value is not equal to itself.") |
---|
| 868 | + | } |
---|
| 869 | + | else throw("Strict value is not equal to itself.") |
---|
| 870 | + | } |
---|
| 871 | + | else throw("Strict value is not equal to itself.") |
---|
| 872 | + | } |
---|
| 873 | + | |
---|
| 874 | + | |
---|
| 875 | + | |
---|
| 876 | + | @Callable(i) |
---|
| 877 | + | func activate (amtAsStr,prAsStr) = if ((toString(i.caller) != toString(fca))) |
---|
| 878 | + | then throw("denied") |
---|
| 879 | + | else $Tuple2([StringEntry(aa(), amtAsStr), StringEntry(pa(), prAsStr)], "success") |
---|
| 880 | + | |
---|
| 881 | + | |
---|
| 882 | + | |
---|
| 883 | + | @Callable(i) |
---|
| 884 | + | func setS (k,v) = if ((toString(i.caller) != strf(this, ada()))) |
---|
| 885 | + | then pd |
---|
| 886 | + | else [StringEntry(k, v)] |
---|
| 887 | + | |
---|
| 888 | + | |
---|
| 889 | + | |
---|
| 890 | + | @Callable(i) |
---|
| 891 | + | func setI (k,v) = if ((toString(i.caller) != strf(this, ada()))) |
---|
| 892 | + | then pd |
---|
| 893 | + | else [IntegerEntry(k, v)] |
---|
| 894 | + | |
---|
| 895 | + | |
---|
| 896 | + | |
---|
| 897 | + | @Callable(i) |
---|
| 898 | + | func getPoolConfigWrapperREADONLY () = $Tuple2(nil, gpc()) |
---|
| 899 | + | |
---|
| 900 | + | |
---|
| 901 | + | |
---|
| 902 | + | @Callable(i) |
---|
| 903 | + | func getAccBalanceWrapperREADONLY (assetId) = $Tuple2(nil, getAccBalance(assetId)) |
---|
| 904 | + | |
---|
| 905 | + | |
---|
| 906 | + | |
---|
| 907 | + | @Callable(i) |
---|
| 908 | + | func calcPricesWrapperREADONLY (amAmt,prAmt,lpAmt) = { |
---|
| 909 | + | let pr = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 910 | + | $Tuple2(nil, [toString(pr[0]), toString(pr[1]), toString(pr[2])]) |
---|
| 911 | + | } |
---|
| 912 | + | |
---|
| 913 | + | |
---|
| 914 | + | |
---|
| 915 | + | @Callable(i) |
---|
| 916 | + | func fromX18WrapperREADONLY (val,resScaleMult) = $Tuple2(nil, f1(parseBigIntValue(val), resScaleMult)) |
---|
| 917 | + | |
---|
| 918 | + | |
---|
| 919 | + | |
---|
| 920 | + | @Callable(i) |
---|
| 921 | + | func toX18WrapperREADONLY (origVal,origScaleMult) = $Tuple2(nil, toString(t1(origVal, origScaleMult))) |
---|
| 922 | + | |
---|
| 923 | + | |
---|
| 924 | + | |
---|
| 925 | + | @Callable(i) |
---|
| 926 | + | func calcPriceBigIntWrapperREADONLY (prAmtX18,amAmtX18) = $Tuple2(nil, toString(cpbi(parseBigIntValue(prAmtX18), parseBigIntValue(amAmtX18)))) |
---|
| 927 | + | |
---|
| 928 | + | |
---|
| 929 | + | |
---|
| 930 | + | @Callable(i) |
---|
| 931 | + | func estimatePutOperationWrapperREADONLY (txId58,slippage,inAmAmt,inAmId,inPrAmt,inPrId,usrAddr,isEval,emitLp) = $Tuple2(nil, epo(txId58, slippage, inAmAmt, inAmId, inPrAmt, inPrId, usrAddr, isEval, emitLp, false, 0, "")) |
---|
| 932 | + | |
---|
| 933 | + | |
---|
| 934 | + | |
---|
| 935 | + | @Callable(i) |
---|
| 936 | + | func estimateGetOperationWrapperREADONLY (txId58,pmtAsId,pmtLpAmt,usrAddr) = { |
---|
| 937 | + | let r = ego(txId58, pmtAsId, pmtLpAmt, addressFromStringValue(usrAddr)) |
---|
| 938 | + | $Tuple2(nil, $Tuple10(r._1, r._2, r._3, r._4, r._5, r._6, r._7, toString(r._8), r._9, r._10)) |
---|
| 939 | + | } |
---|
| 940 | + | |
---|
| 941 | + | |
---|
| 942 | + | @Verifier(tx) |
---|
| 943 | + | func verify () = { |
---|
| 944 | + | let targetPublicKey = match m() { |
---|
| 945 | + | case pk: ByteVector => |
---|
| 946 | + | pk |
---|
| 947 | + | case _: Unit => |
---|
| 948 | + | tx.senderPublicKey |
---|
| 949 | + | case _ => |
---|
| 950 | + | throw("Match error") |
---|
| 951 | + | } |
---|
| 952 | + | match tx { |
---|
| 953 | + | case order: Order => |
---|
| 954 | + | let matcherPub = mp() |
---|
| 955 | + | let orderValid = moa(order) |
---|
| 956 | + | let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey) |
---|
| 957 | + | let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub) |
---|
| 958 | + | if (if (if (orderValid) |
---|
| 959 | + | then senderValid |
---|
| 960 | + | else false) |
---|
| 961 | + | then matcherValid |
---|
| 962 | + | else false) |
---|
| 963 | + | then true |
---|
| 964 | + | else toe(orderValid, senderValid, matcherValid) |
---|
| 965 | + | case s: SetScriptTransaction => |
---|
| 966 | + | let newHash = blake2b256(value(s.script)) |
---|
| 967 | + | let allowedHash = fromBase64String(value(getString(fca, keyAllowedLpStableScriptHash()))) |
---|
| 968 | + | let currentHash = scriptHash(this) |
---|
| 969 | + | if (if ((allowedHash == newHash)) |
---|
| 970 | + | then (currentHash != newHash) |
---|
| 971 | + | else false) |
---|
| 972 | + | then true |
---|
| 973 | + | else sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 974 | + | case _ => |
---|
| 975 | + | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 976 | + | } |
---|
| 977 | + | } |
---|
| 978 | + | |
---|