1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let lPdecimals = 8 |
---|
| 5 | + | |
---|
| 6 | + | let scale8 = 100000000 |
---|
| 7 | + | |
---|
| 8 | + | let scale8BigInt = toBigInt(100000000) |
---|
| 9 | + | |
---|
| 10 | + | let scale18 = toBigInt(1000000000000000000) |
---|
| 11 | + | |
---|
| 12 | + | let zeroBigInt = toBigInt(0) |
---|
| 13 | + | |
---|
| 14 | + | let big0 = toBigInt(0) |
---|
| 15 | + | |
---|
| 16 | + | let big1 = toBigInt(1) |
---|
| 17 | + | |
---|
| 18 | + | let big2 = toBigInt(2) |
---|
| 19 | + | |
---|
| 20 | + | let wavesString = "WAVES" |
---|
| 21 | + | |
---|
| 22 | + | let SEP = "__" |
---|
| 23 | + | |
---|
| 24 | + | let PoolActive = 1 |
---|
| 25 | + | |
---|
| 26 | + | let PoolPutDisabled = 2 |
---|
| 27 | + | |
---|
| 28 | + | let PoolMatcherDisabled = 3 |
---|
| 29 | + | |
---|
| 30 | + | let PoolShutdown = 4 |
---|
| 31 | + | |
---|
| 32 | + | let idxPoolAddress = 1 |
---|
| 33 | + | |
---|
| 34 | + | let idxPoolStatus = 2 |
---|
| 35 | + | |
---|
| 36 | + | let idxPoolLPAssetId = 3 |
---|
| 37 | + | |
---|
| 38 | + | let idxAmtAssetId = 4 |
---|
| 39 | + | |
---|
| 40 | + | let idxPriceAssetId = 5 |
---|
| 41 | + | |
---|
| 42 | + | let idxAmtAssetDcm = 6 |
---|
| 43 | + | |
---|
| 44 | + | let idxPriceAssetDcm = 7 |
---|
| 45 | + | |
---|
| 46 | + | let idxIAmtAssetId = 8 |
---|
| 47 | + | |
---|
| 48 | + | let idxIPriceAssetId = 9 |
---|
| 49 | + | |
---|
| 50 | + | let idxLPAssetDcm = 10 |
---|
| 51 | + | |
---|
| 52 | + | let idxPoolAmtAssetAmt = 1 |
---|
| 53 | + | |
---|
| 54 | + | let idxPoolPriceAssetAmt = 2 |
---|
| 55 | + | |
---|
| 56 | + | let idxPoolLPAssetAmt = 3 |
---|
| 57 | + | |
---|
| 58 | + | let idxFactoryStakingContract = 1 |
---|
| 59 | + | |
---|
| 60 | + | let idxFactorySlippageContract = 7 |
---|
| 61 | + | |
---|
| 62 | + | func toX18 (origVal,origScaleMult) = fraction(toBigInt(origVal), scale18, toBigInt(origScaleMult)) |
---|
| 63 | + | |
---|
| 64 | + | |
---|
| 65 | + | func fromX18 (val,resultScaleMult) = toInt(fraction(val, toBigInt(resultScaleMult), scale18)) |
---|
| 66 | + | |
---|
| 67 | + | |
---|
| 68 | + | func toScale (amt,resScale,curScale) = fraction(amt, resScale, curScale) |
---|
| 69 | + | |
---|
| 70 | + | |
---|
| 71 | + | func abs (val) = if ((0 > val)) |
---|
| 72 | + | then -(val) |
---|
| 73 | + | else val |
---|
| 74 | + | |
---|
| 75 | + | |
---|
| 76 | + | func absBigInt (val) = if ((zeroBigInt > val)) |
---|
| 77 | + | then -(val) |
---|
| 78 | + | else val |
---|
| 79 | + | |
---|
| 80 | + | |
---|
| 81 | + | func fc () = "%s__factoryContract" |
---|
| 82 | + | |
---|
| 83 | + | |
---|
| 84 | + | func mpk () = "%s__managerPublicKey" |
---|
| 85 | + | |
---|
| 86 | + | |
---|
| 87 | + | func pmpk () = "%s__pendingManagerPublicKey" |
---|
| 88 | + | |
---|
| 89 | + | |
---|
| 90 | + | func pl () = "%s%s__price__last" |
---|
| 91 | + | |
---|
| 92 | + | |
---|
| 93 | + | func ph (h,timestamp) = makeString(["%s%s%d%d__price__history", toString(h), toString(timestamp)], SEP) |
---|
| 94 | + | |
---|
| 95 | + | |
---|
| 96 | + | func pau (userAddress,txId) = ((("%s%s%s__P__" + userAddress) + "__") + txId) |
---|
| 97 | + | |
---|
| 98 | + | |
---|
| 99 | + | func gau (userAddress,txId) = ((("%s%s%s__G__" + userAddress) + "__") + txId) |
---|
| 100 | + | |
---|
| 101 | + | |
---|
| 102 | + | func aa () = "%s__amountAsset" |
---|
| 103 | + | |
---|
| 104 | + | |
---|
| 105 | + | func pa () = "%s__priceAsset" |
---|
| 106 | + | |
---|
| 107 | + | |
---|
| 108 | + | func keyFactoryConfig () = "%s__factoryConfig" |
---|
| 109 | + | |
---|
| 110 | + | |
---|
| 111 | + | func keyMatcherPub () = "%s%s__matcher__publicKey" |
---|
| 112 | + | |
---|
| 113 | + | |
---|
| 114 | + | func keyMappingPoolContractAddressToPoolAssets (poolContractAddress) = (("%s%s%s__" + poolContractAddress) + "__mappings__poolContract2LpAsset") |
---|
| 115 | + | |
---|
| 116 | + | |
---|
| 117 | + | func keyPoolConfig (iAmtAsset,iPriceAsset) = (((("%d%d%s__" + iAmtAsset) + "__") + iPriceAsset) + "__config") |
---|
| 118 | + | |
---|
| 119 | + | |
---|
| 120 | + | func keyMappingsBaseAsset2internalId (baseAssetStr) = ("%s%s%s__mappings__baseAsset2internalId__" + baseAssetStr) |
---|
| 121 | + | |
---|
| 122 | + | |
---|
| 123 | + | func keyAllPoolsShutdown () = "%s__shutdown" |
---|
| 124 | + | |
---|
| 125 | + | |
---|
| 126 | + | func keyPoolWeight (contractAddress) = ("%s%s__poolWeight__" + contractAddress) |
---|
| 127 | + | |
---|
| 128 | + | |
---|
| 129 | + | func keyAllowedLpScriptHash () = "%s__allowedLpScriptHash" |
---|
| 130 | + | |
---|
| 131 | + | |
---|
| 132 | + | let keyFeeCollectorAddress = "%s__feeCollectorAddress" |
---|
| 133 | + | |
---|
| 134 | + | func throwOrderError (orderValid,senderValid,matcherValid) = throw(((((("order validation failed: orderValid=" + toString(orderValid)) + " senderValid=") + toString(senderValid)) + " matcherValid=") + toString(matcherValid))) |
---|
| 135 | + | |
---|
| 136 | + | |
---|
| 137 | + | func getStringOrFail (address,key) = valueOrErrorMessage(getString(address, key), makeString(["mandatory ", toString(address), ".", key, " is not defined"], "")) |
---|
| 138 | + | |
---|
| 139 | + | |
---|
| 140 | + | func getIntOrFail (address,key) = valueOrErrorMessage(getInteger(address, key), makeString(["mandatory ", toString(address), ".", key, " is not defined"], "")) |
---|
| 141 | + | |
---|
| 142 | + | |
---|
| 143 | + | func throwErr (msg) = throw(makeString(["lp.ride:", msg], " ")) |
---|
| 144 | + | |
---|
| 145 | + | |
---|
| 146 | + | let factoryContract = addressFromStringValue(getStringOrFail(this, fc())) |
---|
| 147 | + | |
---|
| 148 | + | let feeCollectorAddress = addressFromStringValue(getStringOrFail(factoryContract, keyFeeCollectorAddress)) |
---|
| 149 | + | |
---|
| 150 | + | let inFee = { |
---|
| 151 | + | let @ = invoke(factoryContract, "getInFeeREADONLY", [toString(this)], nil) |
---|
| 152 | + | if ($isInstanceOf(@, "Int")) |
---|
| 153 | + | then @ |
---|
| 154 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 155 | + | } |
---|
| 156 | + | |
---|
| 157 | + | let outFee = { |
---|
| 158 | + | let @ = invoke(factoryContract, "getOutFeeREADONLY", [toString(this)], nil) |
---|
| 159 | + | if ($isInstanceOf(@, "Int")) |
---|
| 160 | + | then @ |
---|
| 161 | + | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
| 162 | + | } |
---|
| 163 | + | |
---|
| 164 | + | func isGlobalShutdown () = valueOrElse(getBoolean(factoryContract, keyAllPoolsShutdown()), false) |
---|
| 165 | + | |
---|
| 166 | + | |
---|
| 167 | + | func getMatcherPubOrFail () = fromBase58String(getStringOrFail(factoryContract, keyMatcherPub())) |
---|
| 168 | + | |
---|
| 169 | + | |
---|
| 170 | + | func getPoolConfig () = { |
---|
| 171 | + | let amtAsset = getStringOrFail(this, aa()) |
---|
| 172 | + | let priceAsset = getStringOrFail(this, pa()) |
---|
| 173 | + | let iPriceAsset = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(priceAsset)) |
---|
| 174 | + | let iAmtAsset = getIntOrFail(factoryContract, keyMappingsBaseAsset2internalId(amtAsset)) |
---|
| 175 | + | split(getStringOrFail(factoryContract, keyPoolConfig(toString(iAmtAsset), toString(iPriceAsset))), SEP) |
---|
| 176 | + | } |
---|
| 177 | + | |
---|
| 178 | + | |
---|
| 179 | + | func parseAssetId (input) = if ((input == wavesString)) |
---|
| 180 | + | then unit |
---|
| 181 | + | else fromBase58String(input) |
---|
| 182 | + | |
---|
| 183 | + | |
---|
| 184 | + | func assetIdToString (input) = if ((input == unit)) |
---|
| 185 | + | then wavesString |
---|
| 186 | + | else toBase58String(value(input)) |
---|
| 187 | + | |
---|
| 188 | + | |
---|
| 189 | + | func parsePoolConfig (poolConfig) = $Tuple7(addressFromStringValue(poolConfig[idxPoolAddress]), parseIntValue(poolConfig[idxPoolStatus]), fromBase58String(poolConfig[idxPoolLPAssetId]), parseAssetId(poolConfig[idxAmtAssetId]), parseAssetId(poolConfig[idxPriceAssetId]), parseIntValue(poolConfig[idxAmtAssetDcm]), parseIntValue(poolConfig[idxPriceAssetDcm])) |
---|
| 190 | + | |
---|
| 191 | + | |
---|
| 192 | + | let poolConfigParsed = parsePoolConfig(getPoolConfig()) |
---|
| 193 | + | |
---|
| 194 | + | let $t076017767 = poolConfigParsed |
---|
| 195 | + | |
---|
| 196 | + | let cfgPoolAddress = $t076017767._1 |
---|
| 197 | + | |
---|
| 198 | + | let cfgPoolStatus = $t076017767._2 |
---|
| 199 | + | |
---|
| 200 | + | let cfgLpAssetId = $t076017767._3 |
---|
| 201 | + | |
---|
| 202 | + | let cfgAmountAssetId = $t076017767._4 |
---|
| 203 | + | |
---|
| 204 | + | let cfgPriceAssetId = $t076017767._5 |
---|
| 205 | + | |
---|
| 206 | + | let cfgAmountAssetDecimals = $t076017767._6 |
---|
| 207 | + | |
---|
| 208 | + | let cfgPriceAssetDecimals = $t076017767._7 |
---|
| 209 | + | |
---|
| 210 | + | func getFactoryConfig () = split(getStringOrFail(factoryContract, keyFactoryConfig()), SEP) |
---|
| 211 | + | |
---|
| 212 | + | |
---|
| 213 | + | let stakingContract = valueOrErrorMessage(addressFromString(getFactoryConfig()[idxFactoryStakingContract]), "incorrect staking address") |
---|
| 214 | + | |
---|
| 215 | + | let slippageContract = valueOrErrorMessage(addressFromString(getFactoryConfig()[idxFactorySlippageContract]), "incorrect staking address") |
---|
| 216 | + | |
---|
| 217 | + | func dataPutActionInfo (inAmtAssetAmt,inPriceAssetAmt,outLpAmt,price,slippageTolerancePassedByUser,slippageToleranceReal,txHeight,txTimestamp,slipageAmtAssetAmt,slipagePriceAssetAmt) = makeString(["%d%d%d%d%d%d%d%d%d%d", toString(inAmtAssetAmt), toString(inPriceAssetAmt), toString(outLpAmt), toString(price), toString(slippageTolerancePassedByUser), toString(slippageToleranceReal), toString(txHeight), toString(txTimestamp), toString(slipageAmtAssetAmt), toString(slipagePriceAssetAmt)], SEP) |
---|
| 218 | + | |
---|
| 219 | + | |
---|
| 220 | + | 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) |
---|
| 221 | + | |
---|
| 222 | + | |
---|
| 223 | + | func getAccBalance (assetId) = if ((assetId == "WAVES")) |
---|
| 224 | + | then wavesBalance(this).available |
---|
| 225 | + | else assetBalance(this, fromBase58String(assetId)) |
---|
| 226 | + | |
---|
| 227 | + | |
---|
| 228 | + | func calcPriceBigInt (prAmtX18,amAmtX18) = fraction(prAmtX18, scale18, amAmtX18) |
---|
| 229 | + | |
---|
| 230 | + | |
---|
| 231 | + | func privateCalcPrice (amAssetDcm,prAssetDcm,amAmt,prAmt) = { |
---|
| 232 | + | let amtAssetAmtX18 = toX18(amAmt, amAssetDcm) |
---|
| 233 | + | let priceAssetAmtX18 = toX18(prAmt, prAssetDcm) |
---|
| 234 | + | calcPriceBigInt(priceAssetAmtX18, amtAssetAmtX18) |
---|
| 235 | + | } |
---|
| 236 | + | |
---|
| 237 | + | |
---|
| 238 | + | func calcPrices (amAmt,prAmt,lpAmt) = { |
---|
| 239 | + | let cfg = getPoolConfig() |
---|
| 240 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 241 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 242 | + | let priceX18 = privateCalcPrice(amtAssetDcm, priceAssetDcm, amAmt, prAmt) |
---|
| 243 | + | let amAmtX18 = toX18(amAmt, amtAssetDcm) |
---|
| 244 | + | let prAmtX18 = toX18(prAmt, priceAssetDcm) |
---|
| 245 | + | let lpAmtX18 = toX18(lpAmt, scale8) |
---|
| 246 | + | let lpPriceInAmAssetX18 = calcPriceBigInt(amAmtX18, lpAmtX18) |
---|
| 247 | + | let lpPriceInPrAssetX18 = calcPriceBigInt(prAmtX18, lpAmtX18) |
---|
| 248 | + | [priceX18, lpPriceInAmAssetX18, lpPriceInPrAssetX18] |
---|
| 249 | + | } |
---|
| 250 | + | |
---|
| 251 | + | |
---|
| 252 | + | func calculatePrices (amAmt,prAmt,lpAmt) = { |
---|
| 253 | + | let prices = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 254 | + | [fromX18(prices[0], scale8), fromX18(prices[1], scale8), fromX18(prices[2], scale8)] |
---|
| 255 | + | } |
---|
| 256 | + | |
---|
| 257 | + | |
---|
| 258 | + | func estimateGetOperation (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 259 | + | let cfg = getPoolConfig() |
---|
| 260 | + | let lpAssetId = cfg[idxPoolLPAssetId] |
---|
| 261 | + | let amAssetId = cfg[idxAmtAssetId] |
---|
| 262 | + | let prAssetId = cfg[idxPriceAssetId] |
---|
| 263 | + | let amAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 264 | + | let prAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 265 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 266 | + | let lpEmission = valueOrErrorMessage(assetInfo(fromBase58String(lpAssetId)), (("Asset " + lpAssetId) + " doesn't exist")).quantity |
---|
| 267 | + | if ((lpAssetId != pmtAssetId)) |
---|
| 268 | + | then throw("Invalid asset passed.") |
---|
| 269 | + | else { |
---|
| 270 | + | let amBalance = getAccBalance(amAssetId) |
---|
| 271 | + | let amBalanceX18 = toX18(amBalance, amAssetDcm) |
---|
| 272 | + | let prBalance = getAccBalance(prAssetId) |
---|
| 273 | + | let prBalanceX18 = toX18(prBalance, prAssetDcm) |
---|
| 274 | + | let curPriceX18 = calcPriceBigInt(prBalanceX18, amBalanceX18) |
---|
| 275 | + | let curPrice = fromX18(curPriceX18, scale8) |
---|
| 276 | + | let pmtLpAmtX18 = toX18(pmtLpAmt, scale8) |
---|
| 277 | + | let lpEmissionX18 = toX18(lpEmission, scale8) |
---|
| 278 | + | let outAmAmtX18 = fraction(amBalanceX18, pmtLpAmtX18, lpEmissionX18) |
---|
| 279 | + | let outPrAmtX18 = fraction(prBalanceX18, pmtLpAmtX18, lpEmissionX18) |
---|
| 280 | + | let outAmAmt = fromX18(outAmAmtX18, amAssetDcm) |
---|
| 281 | + | let outPrAmt = fromX18(outPrAmtX18, prAssetDcm) |
---|
| 282 | + | let state = if ((txId58 == "")) |
---|
| 283 | + | then nil |
---|
| 284 | + | else [ScriptTransfer(userAddress, outAmAmt, if ((amAssetId == "WAVES")) |
---|
| 285 | + | then unit |
---|
| 286 | + | else fromBase58String(amAssetId)), ScriptTransfer(userAddress, outPrAmt, if ((prAssetId == "WAVES")) |
---|
| 287 | + | then unit |
---|
| 288 | + | else fromBase58String(prAssetId)), StringEntry(gau(toString(userAddress), txId58), dataGetActionInfo(outAmAmt, outPrAmt, pmtLpAmt, curPrice, height, lastBlock.timestamp)), IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice)] |
---|
| 289 | + | $Tuple10(outAmAmt, outPrAmt, amAssetId, prAssetId, amBalance, prBalance, lpEmission, curPriceX18, poolStatus, state) |
---|
| 290 | + | } |
---|
| 291 | + | } |
---|
| 292 | + | |
---|
| 293 | + | |
---|
| 294 | + | func estimatePutOperation (txId58,slippageTolerance,inAmAssetAmt,inAmAssetId,inPrAssetAmt,inPrAssetId,userAddress,isEvaluate,emitLp) = { |
---|
| 295 | + | let cfg = getPoolConfig() |
---|
| 296 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 297 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 298 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 299 | + | let iAmtAssetId = cfg[idxIAmtAssetId] |
---|
| 300 | + | let iPriceAssetId = cfg[idxIPriceAssetId] |
---|
| 301 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 302 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 303 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 304 | + | let lpEmission = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 305 | + | let inAmAssetIdStr = toBase58String(valueOrElse(inAmAssetId, fromBase58String("WAVES"))) |
---|
| 306 | + | let inPrAssetIdStr = toBase58String(valueOrElse(inPrAssetId, fromBase58String("WAVES"))) |
---|
| 307 | + | if (if ((amAssetIdStr != inAmAssetIdStr)) |
---|
| 308 | + | then true |
---|
| 309 | + | else (prAssetIdStr != inPrAssetIdStr)) |
---|
| 310 | + | then throw("Invalid amt or price asset passed.") |
---|
| 311 | + | else { |
---|
| 312 | + | let amBalance = if (isEvaluate) |
---|
| 313 | + | then getAccBalance(amAssetIdStr) |
---|
| 314 | + | else (getAccBalance(amAssetIdStr) - inAmAssetAmt) |
---|
| 315 | + | let prBalance = if (isEvaluate) |
---|
| 316 | + | then getAccBalance(prAssetIdStr) |
---|
| 317 | + | else (getAccBalance(prAssetIdStr) - inPrAssetAmt) |
---|
| 318 | + | let inAmAssetAmtX18 = toX18(inAmAssetAmt, amtAssetDcm) |
---|
| 319 | + | let inPrAssetAmtX18 = toX18(inPrAssetAmt, priceAssetDcm) |
---|
| 320 | + | let userPriceX18 = calcPriceBigInt(inPrAssetAmtX18, inAmAssetAmtX18) |
---|
| 321 | + | let amBalanceX18 = toX18(amBalance, amtAssetDcm) |
---|
| 322 | + | let prBalanceX18 = toX18(prBalance, priceAssetDcm) |
---|
| 323 | + | let res = if ((lpEmission == 0)) |
---|
| 324 | + | then { |
---|
| 325 | + | let curPriceX18 = zeroBigInt |
---|
| 326 | + | let slippageX18 = zeroBigInt |
---|
| 327 | + | let lpAmtX18 = pow((inAmAssetAmtX18 * inPrAssetAmtX18), 0, toBigInt(5), 1, 0, DOWN) |
---|
| 328 | + | $Tuple5(fromX18(lpAmtX18, scale8), fromX18(inAmAssetAmtX18, amtAssetDcm), fromX18(inPrAssetAmtX18, priceAssetDcm), calcPriceBigInt((prBalanceX18 + inPrAssetAmtX18), (amBalanceX18 + inAmAssetAmtX18)), slippageX18) |
---|
| 329 | + | } |
---|
| 330 | + | else { |
---|
| 331 | + | let curPriceX18 = calcPriceBigInt(prBalanceX18, amBalanceX18) |
---|
| 332 | + | let slippageX18 = fraction(absBigInt((curPriceX18 - userPriceX18)), scale18, curPriceX18) |
---|
| 333 | + | let slippageToleranceX18 = toX18(slippageTolerance, scale8) |
---|
| 334 | + | if (if ((curPriceX18 != zeroBigInt)) |
---|
| 335 | + | then (slippageX18 > slippageToleranceX18) |
---|
| 336 | + | else false) |
---|
| 337 | + | then throw(((("Price slippage " + toString(slippageX18)) + " exceeded the passed limit of ") + toString(slippageToleranceX18))) |
---|
| 338 | + | else { |
---|
| 339 | + | let lpEmissionX18 = toX18(lpEmission, scale8) |
---|
| 340 | + | let prViaAmX18 = fraction(inAmAssetAmtX18, curPriceX18, scale18) |
---|
| 341 | + | let amViaPrX18 = fraction(inPrAssetAmtX18, scale18, curPriceX18) |
---|
| 342 | + | let expectedAmts = if ((prViaAmX18 > inPrAssetAmtX18)) |
---|
| 343 | + | then $Tuple2(amViaPrX18, inPrAssetAmtX18) |
---|
| 344 | + | else $Tuple2(inAmAssetAmtX18, prViaAmX18) |
---|
| 345 | + | let expAmtAssetAmtX18 = expectedAmts._1 |
---|
| 346 | + | let expPriceAssetAmtX18 = expectedAmts._2 |
---|
| 347 | + | let lpAmtX18 = fraction(lpEmissionX18, expPriceAssetAmtX18, prBalanceX18) |
---|
| 348 | + | $Tuple5(fromX18(lpAmtX18, scale8), fromX18(expAmtAssetAmtX18, amtAssetDcm), fromX18(expPriceAssetAmtX18, priceAssetDcm), curPriceX18, slippageX18) |
---|
| 349 | + | } |
---|
| 350 | + | } |
---|
| 351 | + | let calcLpAmt = res._1 |
---|
| 352 | + | let calcAmAssetPmt = res._2 |
---|
| 353 | + | let calcPrAssetPmt = res._3 |
---|
| 354 | + | let curPrice = fromX18(res._4, scale8) |
---|
| 355 | + | let slippageCalc = fromX18(res._5, scale8) |
---|
| 356 | + | if ((0 >= calcLpAmt)) |
---|
| 357 | + | then throw("Invalid calculations. LP calculated is less than zero.") |
---|
| 358 | + | else { |
---|
| 359 | + | let emitLpAmt = if (!(emitLp)) |
---|
| 360 | + | then 0 |
---|
| 361 | + | else calcLpAmt |
---|
| 362 | + | let amDiff = (inAmAssetAmt - calcAmAssetPmt) |
---|
| 363 | + | let prDiff = (inPrAssetAmt - calcPrAssetPmt) |
---|
| 364 | + | let commonState = [IntegerEntry(pl(), curPrice), IntegerEntry(ph(height, lastBlock.timestamp), curPrice), StringEntry(pau(userAddress, txId58), dataPutActionInfo(calcAmAssetPmt, calcPrAssetPmt, emitLpAmt, curPrice, slippageTolerance, slippageCalc, height, lastBlock.timestamp, amDiff, prDiff))] |
---|
| 365 | + | $Tuple13(calcLpAmt, emitLpAmt, curPrice, amBalance, prBalance, lpEmission, lpAssetId, poolStatus, commonState, amDiff, prDiff, inAmAssetId, inPrAssetId) |
---|
| 366 | + | } |
---|
| 367 | + | } |
---|
| 368 | + | } |
---|
| 369 | + | |
---|
| 370 | + | |
---|
| 371 | + | func validateMatcherOrderAllowed (order) = { |
---|
| 372 | + | let cfg = getPoolConfig() |
---|
| 373 | + | let amtAssetId = cfg[idxAmtAssetId] |
---|
| 374 | + | let priceAssetId = cfg[idxPriceAssetId] |
---|
| 375 | + | let poolStatus = parseIntValue(cfg[idxPoolStatus]) |
---|
| 376 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 377 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 378 | + | let accAmtAssetBalance = getAccBalance(amtAssetId) |
---|
| 379 | + | let accPriceAssetBalance = getAccBalance(priceAssetId) |
---|
| 380 | + | let curPriceX18 = if ((order.orderType == Buy)) |
---|
| 381 | + | then privateCalcPrice(amtAssetDcm, priceAssetDcm, (accAmtAssetBalance + order.amount), accPriceAssetBalance) |
---|
| 382 | + | else privateCalcPrice(amtAssetDcm, priceAssetDcm, (accAmtAssetBalance - order.amount), accPriceAssetBalance) |
---|
| 383 | + | let curPrice = fromX18(curPriceX18, scale8) |
---|
| 384 | + | if (if (if (isGlobalShutdown()) |
---|
| 385 | + | then true |
---|
| 386 | + | else (poolStatus == PoolMatcherDisabled)) |
---|
| 387 | + | then true |
---|
| 388 | + | else (poolStatus == PoolShutdown)) |
---|
| 389 | + | then throw("Exchange operations disabled") |
---|
| 390 | + | else { |
---|
| 391 | + | let orderAmtAsset = order.assetPair.amountAsset |
---|
| 392 | + | let orderAmtAssetStr = if ((orderAmtAsset == unit)) |
---|
| 393 | + | then "WAVES" |
---|
| 394 | + | else toBase58String(value(orderAmtAsset)) |
---|
| 395 | + | let orderPriceAsset = order.assetPair.priceAsset |
---|
| 396 | + | let orderPriceAssetStr = if ((orderPriceAsset == unit)) |
---|
| 397 | + | then "WAVES" |
---|
| 398 | + | else toBase58String(value(orderPriceAsset)) |
---|
| 399 | + | if (if ((orderAmtAssetStr != amtAssetId)) |
---|
| 400 | + | then true |
---|
| 401 | + | else (orderPriceAssetStr != priceAssetId)) |
---|
| 402 | + | then throw("Wrong order assets.") |
---|
| 403 | + | else { |
---|
| 404 | + | let orderPrice = order.price |
---|
| 405 | + | let priceDcm = fraction(scale8, priceAssetDcm, amtAssetDcm) |
---|
| 406 | + | let castedOrderPrice = toScale(orderPrice, scale8, priceDcm) |
---|
| 407 | + | let isOrderPriceValid = if ((order.orderType == Buy)) |
---|
| 408 | + | then (curPrice >= castedOrderPrice) |
---|
| 409 | + | else (castedOrderPrice >= curPrice) |
---|
| 410 | + | true |
---|
| 411 | + | } |
---|
| 412 | + | } |
---|
| 413 | + | } |
---|
| 414 | + | |
---|
| 415 | + | |
---|
| 416 | + | func commonGet (i) = if ((size(i.payments) != 1)) |
---|
| 417 | + | then throw("exactly 1 payment is expected") |
---|
| 418 | + | else { |
---|
| 419 | + | let pmt = value(i.payments[0]) |
---|
| 420 | + | let pmtAssetId = value(pmt.assetId) |
---|
| 421 | + | let pmtAmt = pmt.amount |
---|
| 422 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(pmtAssetId), pmtAmt, i.caller) |
---|
| 423 | + | let outAmAmt = res._1 |
---|
| 424 | + | let outPrAmt = res._2 |
---|
| 425 | + | let poolStatus = parseIntValue(res._9) |
---|
| 426 | + | let state = res._10 |
---|
| 427 | + | if (if (isGlobalShutdown()) |
---|
| 428 | + | then true |
---|
| 429 | + | else (poolStatus == PoolShutdown)) |
---|
| 430 | + | then throw(("Get operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 431 | + | else $Tuple5(outAmAmt, outPrAmt, pmtAmt, pmtAssetId, state) |
---|
| 432 | + | } |
---|
| 433 | + | |
---|
| 434 | + | |
---|
| 435 | + | func commonPut (i,slippageTolerance,emitLp) = if ((size(i.payments) != 2)) |
---|
| 436 | + | then throw("exactly 2 payments are expected") |
---|
| 437 | + | else { |
---|
| 438 | + | let amAssetPmt = value(i.payments[0]) |
---|
| 439 | + | let prAssetPmt = value(i.payments[1]) |
---|
| 440 | + | let estPut = estimatePutOperation(toBase58String(i.transactionId), slippageTolerance, amAssetPmt.amount, amAssetPmt.assetId, prAssetPmt.amount, prAssetPmt.assetId, toString(i.caller), false, emitLp) |
---|
| 441 | + | let poolStatus = parseIntValue(estPut._8) |
---|
| 442 | + | if (if (if (isGlobalShutdown()) |
---|
| 443 | + | then true |
---|
| 444 | + | else (poolStatus == PoolPutDisabled)) |
---|
| 445 | + | then true |
---|
| 446 | + | else (poolStatus == PoolShutdown)) |
---|
| 447 | + | then throw(("Put operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 448 | + | else estPut |
---|
| 449 | + | } |
---|
| 450 | + | |
---|
| 451 | + | |
---|
| 452 | + | func emit (amount) = { |
---|
| 453 | + | let emitInv = invoke(factoryContract, "emit", [amount], nil) |
---|
| 454 | + | if ((emitInv == emitInv)) |
---|
| 455 | + | then { |
---|
| 456 | + | let emitInvLegacy = match emitInv { |
---|
| 457 | + | case legacyFactoryContract: Address => |
---|
| 458 | + | invoke(legacyFactoryContract, "emit", [amount], nil) |
---|
| 459 | + | case _ => |
---|
| 460 | + | unit |
---|
| 461 | + | } |
---|
| 462 | + | if ((emitInvLegacy == emitInvLegacy)) |
---|
| 463 | + | then amount |
---|
| 464 | + | else throw("Strict value is not equal to itself.") |
---|
| 465 | + | } |
---|
| 466 | + | else throw("Strict value is not equal to itself.") |
---|
| 467 | + | } |
---|
| 468 | + | |
---|
| 469 | + | |
---|
| 470 | + | func takeFee (amount,fee) = { |
---|
| 471 | + | let feeAmount = if ((fee == 0)) |
---|
| 472 | + | then 0 |
---|
| 473 | + | else fraction(amount, fee, scale8) |
---|
| 474 | + | $Tuple2((amount - feeAmount), feeAmount) |
---|
| 475 | + | } |
---|
| 476 | + | |
---|
| 477 | + | |
---|
| 478 | + | func calcPutOneToken (paymentAmountRaw,paymentAssetId,userAddress,txId) = { |
---|
| 479 | + | let isEval = (txId == unit) |
---|
| 480 | + | let amountBalanceRaw = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 481 | + | let priceBalanceRaw = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 482 | + | let paymentInAmountAsset = if ((paymentAssetId == cfgAmountAssetId)) |
---|
| 483 | + | then true |
---|
| 484 | + | else if ((paymentAssetId == cfgPriceAssetId)) |
---|
| 485 | + | then false |
---|
| 486 | + | else throwErr("invalid asset") |
---|
| 487 | + | let $t02266422957 = if (isEval) |
---|
| 488 | + | then $Tuple2(amountBalanceRaw, priceBalanceRaw) |
---|
| 489 | + | else if (paymentInAmountAsset) |
---|
| 490 | + | then $Tuple2((amountBalanceRaw - paymentAmountRaw), priceBalanceRaw) |
---|
| 491 | + | else $Tuple2(amountBalanceRaw, (priceBalanceRaw - paymentAmountRaw)) |
---|
| 492 | + | let amountBalanceOld = $t02266422957._1 |
---|
| 493 | + | let priceBalanceOld = $t02266422957._2 |
---|
| 494 | + | let $t02296123110 = if (paymentInAmountAsset) |
---|
| 495 | + | then $Tuple2(paymentAmountRaw, 0) |
---|
| 496 | + | else $Tuple2(0, paymentAmountRaw) |
---|
| 497 | + | let amountAssetAmountRaw = $t02296123110._1 |
---|
| 498 | + | let priceAssetAmountRaw = $t02296123110._2 |
---|
| 499 | + | let amountAssetAmount = takeFee(amountAssetAmountRaw, inFee)._1 |
---|
| 500 | + | let priceAssetAmount = takeFee(priceAssetAmountRaw, inFee)._1 |
---|
| 501 | + | let $t02324223306 = takeFee(paymentAmountRaw, inFee) |
---|
| 502 | + | let paymentAmount = $t02324223306._1 |
---|
| 503 | + | let feeAmount = $t02324223306._2 |
---|
| 504 | + | let amountBalanceNew = (amountBalanceOld + amountAssetAmount) |
---|
| 505 | + | let priceBalanceNew = (priceBalanceOld + priceAssetAmount) |
---|
| 506 | + | let priceNewX18 = calcPriceBigInt(toX18(priceBalanceNew, cfgPriceAssetDecimals), toX18(amountBalanceNew, cfgAmountAssetDecimals)) |
---|
| 507 | + | let priceNew = fromX18(priceNewX18, scale8) |
---|
| 508 | + | let paymentBalance = if (paymentInAmountAsset) |
---|
| 509 | + | then amountBalanceOld |
---|
| 510 | + | else priceBalanceOld |
---|
| 511 | + | let paymentBalanceBigInt = toBigInt(paymentBalance) |
---|
| 512 | + | let supplyBigInt = toBigInt(valueOrErrorMessage(assetInfo(cfgLpAssetId), (("asset " + toBase58String(cfgLpAssetId)) + " doesn't exist")).quantity) |
---|
| 513 | + | let chechSupply = if ((supplyBigInt > big0)) |
---|
| 514 | + | then true |
---|
| 515 | + | else throwErr("initial deposit requires all coins") |
---|
| 516 | + | if ((chechSupply == chechSupply)) |
---|
| 517 | + | then { |
---|
| 518 | + | let depositBigInt = toBigInt(paymentAmount) |
---|
| 519 | + | let issueAmount = max([0, toInt(((supplyBigInt * (sqrtBigInt((scale18 + ((depositBigInt * scale18) / paymentBalanceBigInt)), 18, 18, DOWN) - scale18)) / scale18))]) |
---|
| 520 | + | let commonState = if (isEval) |
---|
| 521 | + | then nil |
---|
| 522 | + | else [IntegerEntry(pl(), priceNew), IntegerEntry(ph(height, lastBlock.timestamp), priceNew), StringEntry(pau(toString(value(userAddress)), toBase58String(value(txId))), dataPutActionInfo(amountAssetAmountRaw, priceAssetAmountRaw, issueAmount, priceNew, 0, 0, height, lastBlock.timestamp, 0, 0))] |
---|
| 523 | + | let priceOldX18 = calcPriceBigInt(toX18(priceBalanceOld, cfgPriceAssetDecimals), toX18(amountBalanceOld, cfgAmountAssetDecimals)) |
---|
| 524 | + | let priceOld = fromX18(priceOldX18, scale8) |
---|
| 525 | + | let loss = { |
---|
| 526 | + | let $t02478324950 = if (paymentInAmountAsset) |
---|
| 527 | + | then $Tuple2(amountAssetAmountRaw, amountBalanceOld) |
---|
| 528 | + | else $Tuple2(priceAssetAmountRaw, priceBalanceOld) |
---|
| 529 | + | let amount = $t02478324950._1 |
---|
| 530 | + | let balance = $t02478324950._2 |
---|
| 531 | + | let issueAmountBoth = toInt(fraction(supplyBigInt, toBigInt((amount / 2)), toBigInt(balance))) |
---|
| 532 | + | fraction((issueAmount - issueAmountBoth), scale8, issueAmountBoth) |
---|
| 533 | + | } |
---|
| 534 | + | $Tuple4(issueAmount, commonState, feeAmount, loss) |
---|
| 535 | + | } |
---|
| 536 | + | else throw("Strict value is not equal to itself.") |
---|
| 537 | + | } |
---|
| 538 | + | |
---|
| 539 | + | |
---|
| 540 | + | func calcGetOneToken (outAssetId,paymentAmount,paymentAssetId,userAddress,txId) = { |
---|
| 541 | + | let isEval = (txId == unit) |
---|
| 542 | + | let checks = [if ((paymentAssetId == cfgLpAssetId)) |
---|
| 543 | + | then true |
---|
| 544 | + | else throwErr("invalid lp asset")] |
---|
| 545 | + | if ((checks == checks)) |
---|
| 546 | + | then { |
---|
| 547 | + | let outInAmountAsset = if ((outAssetId == cfgAmountAssetId)) |
---|
| 548 | + | then true |
---|
| 549 | + | else if ((outAssetId == cfgPriceAssetId)) |
---|
| 550 | + | then false |
---|
| 551 | + | else throwErr("invalid asset") |
---|
| 552 | + | let balanceBigInt = if (outInAmountAsset) |
---|
| 553 | + | then toBigInt(getAccBalance(assetIdToString(cfgAmountAssetId))) |
---|
| 554 | + | else toBigInt(getAccBalance(assetIdToString(cfgPriceAssetId))) |
---|
| 555 | + | let amBalanceOld = getAccBalance(assetIdToString(cfgAmountAssetId)) |
---|
| 556 | + | let prBalanceOld = getAccBalance(assetIdToString(cfgPriceAssetId)) |
---|
| 557 | + | let outBalance = if (outInAmountAsset) |
---|
| 558 | + | then amBalanceOld |
---|
| 559 | + | else prBalanceOld |
---|
| 560 | + | let outBalanceBigInt = toBigInt(outBalance) |
---|
| 561 | + | let supplyBigInt = toBigInt(valueOrErrorMessage(assetInfo(cfgLpAssetId), (("asset " + toBase58String(cfgLpAssetId)) + " doesn't exist")).quantity) |
---|
| 562 | + | let redeemedBigInt = toBigInt(paymentAmount) |
---|
| 563 | + | let amountRaw = max([0, toInt(((balanceBigInt * (scale18 - pow((scale18 - ((redeemedBigInt * scale18) / supplyBigInt)), 18, big2, 0, 18, DOWN))) / scale18))]) |
---|
| 564 | + | let $t02653026586 = takeFee(amountRaw, outFee) |
---|
| 565 | + | let totalAmount = $t02653026586._1 |
---|
| 566 | + | let feeAmount = $t02653026586._2 |
---|
| 567 | + | let $t02659026816 = if (outInAmountAsset) |
---|
| 568 | + | then $Tuple4(totalAmount, 0, (amBalanceOld - amountRaw), prBalanceOld) |
---|
| 569 | + | else $Tuple4(0, totalAmount, amBalanceOld, (prBalanceOld - amountRaw)) |
---|
| 570 | + | let outAmAmount = $t02659026816._1 |
---|
| 571 | + | let outPrAmount = $t02659026816._2 |
---|
| 572 | + | let amBalanceNew = $t02659026816._3 |
---|
| 573 | + | let prBalanceNew = $t02659026816._4 |
---|
| 574 | + | let priceNewX18 = calcPriceBigInt(toX18(prBalanceNew, cfgPriceAssetDecimals), toX18(amBalanceNew, cfgAmountAssetDecimals)) |
---|
| 575 | + | let priceNew = fromX18(priceNewX18, scale8) |
---|
| 576 | + | let commonState = if (isEval) |
---|
| 577 | + | then nil |
---|
| 578 | + | else [StringEntry(gau(toString(value(userAddress)), toBase58String(value(txId))), dataGetActionInfo(outAmAmount, outPrAmount, paymentAmount, priceNew, height, lastBlock.timestamp)), IntegerEntry(pl(), priceNew), IntegerEntry(ph(height, lastBlock.timestamp), priceNew)] |
---|
| 579 | + | let priceOldX18 = calcPriceBigInt(toX18(prBalanceOld, cfgPriceAssetDecimals), toX18(amBalanceOld, cfgAmountAssetDecimals)) |
---|
| 580 | + | let priceOld = fromX18(priceOldX18, scale8) |
---|
| 581 | + | let loss = { |
---|
| 582 | + | let amountBothInPaymentAsset = (toInt(fraction(balanceBigInt, redeemedBigInt, supplyBigInt)) * 2) |
---|
| 583 | + | fraction((totalAmount - amountBothInPaymentAsset), scale8, amountBothInPaymentAsset) |
---|
| 584 | + | } |
---|
| 585 | + | $Tuple4(totalAmount, commonState, feeAmount, loss) |
---|
| 586 | + | } |
---|
| 587 | + | else throw("Strict value is not equal to itself.") |
---|
| 588 | + | } |
---|
| 589 | + | |
---|
| 590 | + | |
---|
| 591 | + | func managerPublicKeyOrUnit () = match getString(mpk()) { |
---|
| 592 | + | case s: String => |
---|
| 593 | + | fromBase58String(s) |
---|
| 594 | + | case _: Unit => |
---|
| 595 | + | unit |
---|
| 596 | + | case _ => |
---|
| 597 | + | throw("Match error") |
---|
| 598 | + | } |
---|
| 599 | + | |
---|
| 600 | + | |
---|
| 601 | + | func pendingManagerPublicKeyOrUnit () = match getString(pmpk()) { |
---|
| 602 | + | case s: String => |
---|
| 603 | + | fromBase58String(s) |
---|
| 604 | + | case _: Unit => |
---|
| 605 | + | unit |
---|
| 606 | + | case _ => |
---|
| 607 | + | throw("Match error") |
---|
| 608 | + | } |
---|
| 609 | + | |
---|
| 610 | + | |
---|
| 611 | + | func isManager (i) = match managerPublicKeyOrUnit() { |
---|
| 612 | + | case pk: ByteVector => |
---|
| 613 | + | (i.callerPublicKey == pk) |
---|
| 614 | + | case _: Unit => |
---|
| 615 | + | (i.caller == this) |
---|
| 616 | + | case _ => |
---|
| 617 | + | throw("Match error") |
---|
| 618 | + | } |
---|
| 619 | + | |
---|
| 620 | + | |
---|
| 621 | + | func mustManager (i) = { |
---|
| 622 | + | let pd = throw("Permission denied") |
---|
| 623 | + | match managerPublicKeyOrUnit() { |
---|
| 624 | + | case pk: ByteVector => |
---|
| 625 | + | if ((i.callerPublicKey == pk)) |
---|
| 626 | + | then true |
---|
| 627 | + | else pd |
---|
| 628 | + | case _: Unit => |
---|
| 629 | + | if ((i.caller == this)) |
---|
| 630 | + | then true |
---|
| 631 | + | else pd |
---|
| 632 | + | case _ => |
---|
| 633 | + | throw("Match error") |
---|
| 634 | + | } |
---|
| 635 | + | } |
---|
| 636 | + | |
---|
| 637 | + | |
---|
| 638 | + | @Callable(i) |
---|
| 639 | + | func setManager (pendingManagerPublicKey) = { |
---|
| 640 | + | let checkCaller = mustManager(i) |
---|
| 641 | + | if ((checkCaller == checkCaller)) |
---|
| 642 | + | then { |
---|
| 643 | + | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
| 644 | + | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
| 645 | + | then [StringEntry(pmpk(), pendingManagerPublicKey)] |
---|
| 646 | + | else throw("Strict value is not equal to itself.") |
---|
| 647 | + | } |
---|
| 648 | + | else throw("Strict value is not equal to itself.") |
---|
| 649 | + | } |
---|
| 650 | + | |
---|
| 651 | + | |
---|
| 652 | + | |
---|
| 653 | + | @Callable(i) |
---|
| 654 | + | func confirmManager () = { |
---|
| 655 | + | let pm = pendingManagerPublicKeyOrUnit() |
---|
| 656 | + | let hasPM = if (isDefined(pm)) |
---|
| 657 | + | then true |
---|
| 658 | + | else throw("No pending manager") |
---|
| 659 | + | if ((hasPM == hasPM)) |
---|
| 660 | + | then { |
---|
| 661 | + | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
| 662 | + | then true |
---|
| 663 | + | else throw("You are not pending manager") |
---|
| 664 | + | if ((checkPM == checkPM)) |
---|
| 665 | + | then [StringEntry(mpk(), toBase58String(value(pm))), DeleteEntry(pmpk())] |
---|
| 666 | + | else throw("Strict value is not equal to itself.") |
---|
| 667 | + | } |
---|
| 668 | + | else throw("Strict value is not equal to itself.") |
---|
| 669 | + | } |
---|
| 670 | + | |
---|
| 671 | + | |
---|
| 672 | + | |
---|
| 673 | + | @Callable(i) |
---|
| 674 | + | func put (slippageTolerance,shouldAutoStake) = if ((0 > slippageTolerance)) |
---|
| 675 | + | then throw("Invalid slippageTolerance passed") |
---|
| 676 | + | else { |
---|
| 677 | + | let estPut = commonPut(i, slippageTolerance, true) |
---|
| 678 | + | let emitLpAmt = estPut._2 |
---|
| 679 | + | let lpAssetId = estPut._7 |
---|
| 680 | + | let state = estPut._9 |
---|
| 681 | + | let amDiff = estPut._10 |
---|
| 682 | + | let prDiff = estPut._11 |
---|
| 683 | + | let amId = estPut._12 |
---|
| 684 | + | let prId = estPut._13 |
---|
| 685 | + | let emitInv = invoke(factoryContract, "emit", [emitLpAmt], nil) |
---|
| 686 | + | if ((emitInv == emitInv)) |
---|
| 687 | + | then { |
---|
| 688 | + | let emitInvLegacy = match emitInv { |
---|
| 689 | + | case legacyFactoryContract: Address => |
---|
| 690 | + | invoke(legacyFactoryContract, "emit", [emitLpAmt], nil) |
---|
| 691 | + | case _ => |
---|
| 692 | + | unit |
---|
| 693 | + | } |
---|
| 694 | + | if ((emitInvLegacy == emitInvLegacy)) |
---|
| 695 | + | then { |
---|
| 696 | + | let slippageAInv = if ((amDiff > 0)) |
---|
| 697 | + | then invoke(slippageContract, "put", nil, [AttachedPayment(amId, amDiff)]) |
---|
| 698 | + | else nil |
---|
| 699 | + | if ((slippageAInv == slippageAInv)) |
---|
| 700 | + | then { |
---|
| 701 | + | let slippagePInv = if ((prDiff > 0)) |
---|
| 702 | + | then invoke(slippageContract, "put", nil, [AttachedPayment(prId, prDiff)]) |
---|
| 703 | + | else nil |
---|
| 704 | + | if ((slippagePInv == slippagePInv)) |
---|
| 705 | + | then { |
---|
| 706 | + | let lpTransfer = if (shouldAutoStake) |
---|
| 707 | + | then { |
---|
| 708 | + | let slpStakeInv = invoke(stakingContract, "stake", nil, [AttachedPayment(lpAssetId, emitLpAmt)]) |
---|
| 709 | + | if ((slpStakeInv == slpStakeInv)) |
---|
| 710 | + | then nil |
---|
| 711 | + | else throw("Strict value is not equal to itself.") |
---|
| 712 | + | } |
---|
| 713 | + | else [ScriptTransfer(i.caller, emitLpAmt, lpAssetId)] |
---|
| 714 | + | (state ++ lpTransfer) |
---|
| 715 | + | } |
---|
| 716 | + | else throw("Strict value is not equal to itself.") |
---|
| 717 | + | } |
---|
| 718 | + | else throw("Strict value is not equal to itself.") |
---|
| 719 | + | } |
---|
| 720 | + | else throw("Strict value is not equal to itself.") |
---|
| 721 | + | } |
---|
| 722 | + | else throw("Strict value is not equal to itself.") |
---|
| 723 | + | } |
---|
| 724 | + | |
---|
| 725 | + | |
---|
| 726 | + | |
---|
| 727 | + | @Callable(i) |
---|
| 728 | + | func putForFree (maxSlippage) = if ((0 > maxSlippage)) |
---|
| 729 | + | then throw("Invalid value passed") |
---|
| 730 | + | else { |
---|
| 731 | + | let estPut = commonPut(i, maxSlippage, false) |
---|
| 732 | + | estPut._9 |
---|
| 733 | + | } |
---|
| 734 | + | |
---|
| 735 | + | |
---|
| 736 | + | |
---|
| 737 | + | @Callable(i) |
---|
| 738 | + | func putOneTkn (minOutAmount,autoStake) = { |
---|
| 739 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 740 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 741 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 742 | + | then @ |
---|
| 743 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 744 | + | } |
---|
| 745 | + | let isPutDisabled = if (if (if (isGlobalShutdown()) |
---|
| 746 | + | then true |
---|
| 747 | + | else (cfgPoolStatus == PoolPutDisabled)) |
---|
| 748 | + | then true |
---|
| 749 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 750 | + | then true |
---|
| 751 | + | else isPoolOneTokenOperationsDisabled |
---|
| 752 | + | let checks = [if (if (!(isPutDisabled)) |
---|
| 753 | + | then true |
---|
| 754 | + | else isManager(i)) |
---|
| 755 | + | then true |
---|
| 756 | + | else throwErr("put operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 757 | + | then true |
---|
| 758 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 759 | + | if ((checks == checks)) |
---|
| 760 | + | then { |
---|
| 761 | + | let payment = i.payments[0] |
---|
| 762 | + | let paymentAssetId = payment.assetId |
---|
| 763 | + | let paymentAmountRaw = payment.amount |
---|
| 764 | + | let userAddress = i.caller |
---|
| 765 | + | let txId = i.transactionId |
---|
| 766 | + | let $t03172331853 = calcPutOneToken(paymentAmountRaw, paymentAssetId, userAddress, txId) |
---|
| 767 | + | if (($t03172331853 == $t03172331853)) |
---|
| 768 | + | then { |
---|
| 769 | + | let bonus = $t03172331853._4 |
---|
| 770 | + | let feeAmount = $t03172331853._3 |
---|
| 771 | + | let commonState = $t03172331853._2 |
---|
| 772 | + | let emitAmountEstimated = $t03172331853._1 |
---|
| 773 | + | let emitAmount = if (if ((minOutAmount > 0)) |
---|
| 774 | + | then (minOutAmount > emitAmountEstimated) |
---|
| 775 | + | else false) |
---|
| 776 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 777 | + | else emitAmountEstimated |
---|
| 778 | + | let emitInv = emit(emitAmount) |
---|
| 779 | + | if ((emitInv == emitInv)) |
---|
| 780 | + | then { |
---|
| 781 | + | let lpTransfer = if (autoStake) |
---|
| 782 | + | then { |
---|
| 783 | + | let stakeInv = invoke(stakingContract, "stake", nil, [AttachedPayment(cfgLpAssetId, emitAmount)]) |
---|
| 784 | + | if ((stakeInv == stakeInv)) |
---|
| 785 | + | then nil |
---|
| 786 | + | else throw("Strict value is not equal to itself.") |
---|
| 787 | + | } |
---|
| 788 | + | else [ScriptTransfer(i.caller, emitAmount, cfgLpAssetId)] |
---|
| 789 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 790 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, paymentAssetId)] |
---|
| 791 | + | else nil |
---|
| 792 | + | $Tuple2(((commonState ++ lpTransfer) ++ sendFee), emitAmount) |
---|
| 793 | + | } |
---|
| 794 | + | else throw("Strict value is not equal to itself.") |
---|
| 795 | + | } |
---|
| 796 | + | else throw("Strict value is not equal to itself.") |
---|
| 797 | + | } |
---|
| 798 | + | else throw("Strict value is not equal to itself.") |
---|
| 799 | + | } |
---|
| 800 | + | |
---|
| 801 | + | |
---|
| 802 | + | |
---|
| 803 | + | @Callable(i) |
---|
| 804 | + | func putOneTknREADONLY (paymentAssetId,paymentAmountRaw) = { |
---|
| 805 | + | let $t03258232717 = calcPutOneToken(paymentAmountRaw, parseAssetId(paymentAssetId), unit, unit) |
---|
| 806 | + | let emitAmountEstimated = $t03258232717._1 |
---|
| 807 | + | let commonState = $t03258232717._2 |
---|
| 808 | + | let feeAmount = $t03258232717._3 |
---|
| 809 | + | let bonus = $t03258232717._4 |
---|
| 810 | + | $Tuple2(nil, $Tuple3(emitAmountEstimated, feeAmount, bonus)) |
---|
| 811 | + | } |
---|
| 812 | + | |
---|
| 813 | + | |
---|
| 814 | + | |
---|
| 815 | + | @Callable(i) |
---|
| 816 | + | func getOneTkn (outAssetIdStr,minOutAmount) = { |
---|
| 817 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 818 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 819 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 820 | + | then @ |
---|
| 821 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 822 | + | } |
---|
| 823 | + | let isGetDisabled = if (if (isGlobalShutdown()) |
---|
| 824 | + | then true |
---|
| 825 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 826 | + | then true |
---|
| 827 | + | else isPoolOneTokenOperationsDisabled |
---|
| 828 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 829 | + | then true |
---|
| 830 | + | else isManager(i)) |
---|
| 831 | + | then true |
---|
| 832 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 1)) |
---|
| 833 | + | then true |
---|
| 834 | + | else throwErr("exactly 1 payment are expected")] |
---|
| 835 | + | if ((checks == checks)) |
---|
| 836 | + | then { |
---|
| 837 | + | let outAssetId = parseAssetId(outAssetIdStr) |
---|
| 838 | + | let payment = i.payments[0] |
---|
| 839 | + | let paymentAssetId = payment.assetId |
---|
| 840 | + | let paymentAmount = payment.amount |
---|
| 841 | + | let userAddress = i.caller |
---|
| 842 | + | let txId = i.transactionId |
---|
| 843 | + | let $t03352233657 = calcGetOneToken(outAssetId, paymentAmount, paymentAssetId, userAddress, txId) |
---|
| 844 | + | if (($t03352233657 == $t03352233657)) |
---|
| 845 | + | then { |
---|
| 846 | + | let bonus = $t03352233657._4 |
---|
| 847 | + | let feeAmount = $t03352233657._3 |
---|
| 848 | + | let commonState = $t03352233657._2 |
---|
| 849 | + | let amountEstimated = $t03352233657._1 |
---|
| 850 | + | let amount = if (if ((minOutAmount > 0)) |
---|
| 851 | + | then (minOutAmount > amountEstimated) |
---|
| 852 | + | else false) |
---|
| 853 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 854 | + | else amountEstimated |
---|
| 855 | + | let burnInv = invoke(factoryContract, "burn", [paymentAmount], [AttachedPayment(paymentAssetId, paymentAmount)]) |
---|
| 856 | + | if ((burnInv == burnInv)) |
---|
| 857 | + | then { |
---|
| 858 | + | let assetTransfer = [ScriptTransfer(userAddress, amount, outAssetId)] |
---|
| 859 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 860 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, outAssetId)] |
---|
| 861 | + | else nil |
---|
| 862 | + | $Tuple2(((commonState ++ assetTransfer) ++ sendFee), amount) |
---|
| 863 | + | } |
---|
| 864 | + | else throw("Strict value is not equal to itself.") |
---|
| 865 | + | } |
---|
| 866 | + | else throw("Strict value is not equal to itself.") |
---|
| 867 | + | } |
---|
| 868 | + | else throw("Strict value is not equal to itself.") |
---|
| 869 | + | } |
---|
| 870 | + | |
---|
| 871 | + | |
---|
| 872 | + | |
---|
| 873 | + | @Callable(i) |
---|
| 874 | + | func getOneTknREADONLY (outAssetId,paymentAmount) = { |
---|
| 875 | + | let $t03429234430 = calcGetOneToken(parseAssetId(outAssetId), paymentAmount, cfgLpAssetId, unit, unit) |
---|
| 876 | + | let amountEstimated = $t03429234430._1 |
---|
| 877 | + | let commonState = $t03429234430._2 |
---|
| 878 | + | let feeAmount = $t03429234430._3 |
---|
| 879 | + | let bonus = $t03429234430._4 |
---|
| 880 | + | $Tuple2(nil, $Tuple3(amountEstimated, feeAmount, bonus)) |
---|
| 881 | + | } |
---|
| 882 | + | |
---|
| 883 | + | |
---|
| 884 | + | |
---|
| 885 | + | @Callable(i) |
---|
| 886 | + | func unstakeAndGetOneTkn (unstakeAmount,outAssetIdStr,minOutAmount) = { |
---|
| 887 | + | let isPoolOneTokenOperationsDisabled = { |
---|
| 888 | + | let @ = invoke(factoryContract, "isPoolOneTokenOperationsDisabledREADONLY", [toString(this)], nil) |
---|
| 889 | + | if ($isInstanceOf(@, "Boolean")) |
---|
| 890 | + | then @ |
---|
| 891 | + | else throw(($getType(@) + " couldn't be cast to Boolean")) |
---|
| 892 | + | } |
---|
| 893 | + | let isGetDisabled = if (if (isGlobalShutdown()) |
---|
| 894 | + | then true |
---|
| 895 | + | else (cfgPoolStatus == PoolShutdown)) |
---|
| 896 | + | then true |
---|
| 897 | + | else isPoolOneTokenOperationsDisabled |
---|
| 898 | + | let checks = [if (if (!(isGetDisabled)) |
---|
| 899 | + | then true |
---|
| 900 | + | else isManager(i)) |
---|
| 901 | + | then true |
---|
| 902 | + | else throwErr("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 903 | + | then true |
---|
| 904 | + | else throwErr("no payments are expected")] |
---|
| 905 | + | if ((checks == checks)) |
---|
| 906 | + | then { |
---|
| 907 | + | let outAssetId = parseAssetId(outAssetIdStr) |
---|
| 908 | + | let userAddress = i.caller |
---|
| 909 | + | let txId = i.transactionId |
---|
| 910 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(cfgLpAssetId), unstakeAmount], nil) |
---|
| 911 | + | if ((unstakeInv == unstakeInv)) |
---|
| 912 | + | then { |
---|
| 913 | + | let $t03525535388 = calcGetOneToken(outAssetId, unstakeAmount, cfgLpAssetId, userAddress, txId) |
---|
| 914 | + | if (($t03525535388 == $t03525535388)) |
---|
| 915 | + | then { |
---|
| 916 | + | let bonus = $t03525535388._4 |
---|
| 917 | + | let feeAmount = $t03525535388._3 |
---|
| 918 | + | let commonState = $t03525535388._2 |
---|
| 919 | + | let amountEstimated = $t03525535388._1 |
---|
| 920 | + | let amount = if (if ((minOutAmount > 0)) |
---|
| 921 | + | then (minOutAmount > amountEstimated) |
---|
| 922 | + | else false) |
---|
| 923 | + | then throwErr(makeString(["amount to receive is less than ", toString(minOutAmount)], "")) |
---|
| 924 | + | else amountEstimated |
---|
| 925 | + | let burnInv = invoke(factoryContract, "burn", [unstakeAmount], [AttachedPayment(cfgLpAssetId, unstakeAmount)]) |
---|
| 926 | + | if ((burnInv == burnInv)) |
---|
| 927 | + | then { |
---|
| 928 | + | let assetTransfer = [ScriptTransfer(i.caller, amount, outAssetId)] |
---|
| 929 | + | let sendFee = if ((feeAmount > 0)) |
---|
| 930 | + | then [ScriptTransfer(feeCollectorAddress, feeAmount, outAssetId)] |
---|
| 931 | + | else nil |
---|
| 932 | + | $Tuple2(((commonState ++ assetTransfer) ++ sendFee), amount) |
---|
| 933 | + | } |
---|
| 934 | + | else throw("Strict value is not equal to itself.") |
---|
| 935 | + | } |
---|
| 936 | + | else throw("Strict value is not equal to itself.") |
---|
| 937 | + | } |
---|
| 938 | + | else throw("Strict value is not equal to itself.") |
---|
| 939 | + | } |
---|
| 940 | + | else throw("Strict value is not equal to itself.") |
---|
| 941 | + | } |
---|
| 942 | + | |
---|
| 943 | + | |
---|
| 944 | + | |
---|
| 945 | + | @Callable(i) |
---|
| 946 | + | func get () = { |
---|
| 947 | + | let res = commonGet(i) |
---|
| 948 | + | let outAmtAmt = res._1 |
---|
| 949 | + | let outPrAmt = res._2 |
---|
| 950 | + | let pmtAmt = res._3 |
---|
| 951 | + | let pmtAssetId = res._4 |
---|
| 952 | + | let state = res._5 |
---|
| 953 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 954 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 955 | + | then state |
---|
| 956 | + | else throw("Strict value is not equal to itself.") |
---|
| 957 | + | } |
---|
| 958 | + | |
---|
| 959 | + | |
---|
| 960 | + | |
---|
| 961 | + | @Callable(i) |
---|
| 962 | + | func getNoLess (noLessThenAmtAsset,noLessThenPriceAsset) = { |
---|
| 963 | + | let res = commonGet(i) |
---|
| 964 | + | let outAmAmt = res._1 |
---|
| 965 | + | let outPrAmt = res._2 |
---|
| 966 | + | let pmtAmt = res._3 |
---|
| 967 | + | let pmtAssetId = res._4 |
---|
| 968 | + | let state = res._5 |
---|
| 969 | + | if ((noLessThenAmtAsset > outAmAmt)) |
---|
| 970 | + | then throw(((("noLessThenAmtAsset failed: " + toString(outAmAmt)) + " < ") + toString(noLessThenAmtAsset))) |
---|
| 971 | + | else if ((noLessThenPriceAsset > outPrAmt)) |
---|
| 972 | + | then throw(((("noLessThenPriceAsset failed: " + toString(outPrAmt)) + " < ") + toString(noLessThenPriceAsset))) |
---|
| 973 | + | else { |
---|
| 974 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [pmtAmt], [AttachedPayment(pmtAssetId, pmtAmt)]) |
---|
| 975 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 976 | + | then state |
---|
| 977 | + | else throw("Strict value is not equal to itself.") |
---|
| 978 | + | } |
---|
| 979 | + | } |
---|
| 980 | + | |
---|
| 981 | + | |
---|
| 982 | + | |
---|
| 983 | + | @Callable(i) |
---|
| 984 | + | func unstakeAndGet (amount) = { |
---|
| 985 | + | let checkPayments = if ((size(i.payments) != 0)) |
---|
| 986 | + | then throw("No payments are expected") |
---|
| 987 | + | else true |
---|
| 988 | + | if ((checkPayments == checkPayments)) |
---|
| 989 | + | then { |
---|
| 990 | + | let cfg = getPoolConfig() |
---|
| 991 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 992 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(lpAssetId), amount], nil) |
---|
| 993 | + | if ((unstakeInv == unstakeInv)) |
---|
| 994 | + | then { |
---|
| 995 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(lpAssetId), amount, i.caller) |
---|
| 996 | + | let poolStatus = parseIntValue(res._9) |
---|
| 997 | + | let state = res._10 |
---|
| 998 | + | let checkPoolStatus = if (if (isGlobalShutdown()) |
---|
| 999 | + | then true |
---|
| 1000 | + | else (poolStatus == PoolShutdown)) |
---|
| 1001 | + | then throw(("Get operation is blocked by admin. Status = " + toString(poolStatus))) |
---|
| 1002 | + | else true |
---|
| 1003 | + | if ((checkPoolStatus == checkPoolStatus)) |
---|
| 1004 | + | then { |
---|
| 1005 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [amount], [AttachedPayment(lpAssetId, amount)]) |
---|
| 1006 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1007 | + | then state |
---|
| 1008 | + | else throw("Strict value is not equal to itself.") |
---|
| 1009 | + | } |
---|
| 1010 | + | else throw("Strict value is not equal to itself.") |
---|
| 1011 | + | } |
---|
| 1012 | + | else throw("Strict value is not equal to itself.") |
---|
| 1013 | + | } |
---|
| 1014 | + | else throw("Strict value is not equal to itself.") |
---|
| 1015 | + | } |
---|
| 1016 | + | |
---|
| 1017 | + | |
---|
| 1018 | + | |
---|
| 1019 | + | @Callable(i) |
---|
| 1020 | + | func unstakeAndGetNoLess (unstakeAmount,noLessThenAmountAsset,noLessThenPriceAsset) = { |
---|
| 1021 | + | let isGetDisabled = if (isGlobalShutdown()) |
---|
| 1022 | + | then true |
---|
| 1023 | + | else (cfgPoolStatus == PoolShutdown) |
---|
| 1024 | + | let checks = [if (!(isGetDisabled)) |
---|
| 1025 | + | then true |
---|
| 1026 | + | else throw("get operation is blocked by admin"), if ((size(i.payments) == 0)) |
---|
| 1027 | + | then true |
---|
| 1028 | + | else throw("no payments are expected")] |
---|
| 1029 | + | if ((checks == checks)) |
---|
| 1030 | + | then { |
---|
| 1031 | + | let unstakeInv = invoke(stakingContract, "unstake", [toBase58String(cfgLpAssetId), unstakeAmount], nil) |
---|
| 1032 | + | if ((unstakeInv == unstakeInv)) |
---|
| 1033 | + | then { |
---|
| 1034 | + | let res = estimateGetOperation(toBase58String(i.transactionId), toBase58String(cfgLpAssetId), unstakeAmount, i.caller) |
---|
| 1035 | + | let outAmAmt = res._1 |
---|
| 1036 | + | let outPrAmt = res._2 |
---|
| 1037 | + | let state = res._10 |
---|
| 1038 | + | let checkAmounts = [if ((outAmAmt >= noLessThenAmountAsset)) |
---|
| 1039 | + | then true |
---|
| 1040 | + | else throw(makeString(["amount asset amount to receive is less than ", toString(noLessThenAmountAsset)], "")), if ((outPrAmt >= noLessThenPriceAsset)) |
---|
| 1041 | + | then true |
---|
| 1042 | + | else throw(makeString(["price asset amount to receive is less than ", toString(noLessThenPriceAsset)], ""))] |
---|
| 1043 | + | if ((checkAmounts == checkAmounts)) |
---|
| 1044 | + | then { |
---|
| 1045 | + | let burnLPAssetOnFactory = invoke(factoryContract, "burn", [unstakeAmount], [AttachedPayment(cfgLpAssetId, unstakeAmount)]) |
---|
| 1046 | + | if ((burnLPAssetOnFactory == burnLPAssetOnFactory)) |
---|
| 1047 | + | then state |
---|
| 1048 | + | else throw("Strict value is not equal to itself.") |
---|
| 1049 | + | } |
---|
| 1050 | + | else throw("Strict value is not equal to itself.") |
---|
| 1051 | + | } |
---|
| 1052 | + | else throw("Strict value is not equal to itself.") |
---|
| 1053 | + | } |
---|
| 1054 | + | else throw("Strict value is not equal to itself.") |
---|
| 1055 | + | } |
---|
| 1056 | + | |
---|
| 1057 | + | |
---|
| 1058 | + | |
---|
| 1059 | + | @Callable(i) |
---|
| 1060 | + | func activate (amtAssetStr,priceAssetStr) = if ((toString(i.caller) != toString(factoryContract))) |
---|
| 1061 | + | then throw("permissions denied") |
---|
| 1062 | + | else $Tuple2([StringEntry(aa(), amtAssetStr), StringEntry(pa(), priceAssetStr)], "success") |
---|
| 1063 | + | |
---|
| 1064 | + | |
---|
| 1065 | + | |
---|
| 1066 | + | @Callable(i) |
---|
| 1067 | + | func getPoolConfigWrapperREADONLY () = $Tuple2(nil, getPoolConfig()) |
---|
| 1068 | + | |
---|
| 1069 | + | |
---|
| 1070 | + | |
---|
| 1071 | + | @Callable(i) |
---|
| 1072 | + | func getAccBalanceWrapperREADONLY (assetId) = $Tuple2(nil, getAccBalance(assetId)) |
---|
| 1073 | + | |
---|
| 1074 | + | |
---|
| 1075 | + | |
---|
| 1076 | + | @Callable(i) |
---|
| 1077 | + | func calcPricesWrapperREADONLY (amAmt,prAmt,lpAmt) = { |
---|
| 1078 | + | let prices = calcPrices(amAmt, prAmt, lpAmt) |
---|
| 1079 | + | $Tuple2(nil, [toString(prices[0]), toString(prices[1]), toString(prices[2])]) |
---|
| 1080 | + | } |
---|
| 1081 | + | |
---|
| 1082 | + | |
---|
| 1083 | + | |
---|
| 1084 | + | @Callable(i) |
---|
| 1085 | + | func toX18WrapperREADONLY (origVal,origScaleMult) = $Tuple2(nil, toString(toX18(origVal, origScaleMult))) |
---|
| 1086 | + | |
---|
| 1087 | + | |
---|
| 1088 | + | |
---|
| 1089 | + | @Callable(i) |
---|
| 1090 | + | func fromX18WrapperREADONLY (val,resultScaleMult) = $Tuple2(nil, fromX18(parseBigIntValue(val), resultScaleMult)) |
---|
| 1091 | + | |
---|
| 1092 | + | |
---|
| 1093 | + | |
---|
| 1094 | + | @Callable(i) |
---|
| 1095 | + | func calcPriceBigIntWrapperREADONLY (prAmtX18,amAmtX18) = $Tuple2(nil, toString(calcPriceBigInt(parseBigIntValue(prAmtX18), parseBigIntValue(amAmtX18)))) |
---|
| 1096 | + | |
---|
| 1097 | + | |
---|
| 1098 | + | |
---|
| 1099 | + | @Callable(i) |
---|
| 1100 | + | func estimatePutOperationWrapperREADONLY (txId58,slippageTolerance,inAmAssetAmt,inAmAssetId,inPrAssetAmt,inPrAssetId,userAddress,isEvaluate,emitLp) = $Tuple2(nil, estimatePutOperation(txId58, slippageTolerance, inAmAssetAmt, inAmAssetId, inPrAssetAmt, inPrAssetId, userAddress, isEvaluate, emitLp)) |
---|
| 1101 | + | |
---|
| 1102 | + | |
---|
| 1103 | + | |
---|
| 1104 | + | @Callable(i) |
---|
| 1105 | + | func estimateGetOperationWrapperREADONLY (txId58,pmtAssetId,pmtLpAmt,userAddress) = { |
---|
| 1106 | + | let res = estimateGetOperation(txId58, pmtAssetId, pmtLpAmt, addressFromStringValue(userAddress)) |
---|
| 1107 | + | $Tuple2(nil, $Tuple10(res._1, res._2, res._3, res._4, res._5, res._6, res._7, toString(res._8), res._9, res._10)) |
---|
| 1108 | + | } |
---|
| 1109 | + | |
---|
| 1110 | + | |
---|
| 1111 | + | |
---|
| 1112 | + | @Callable(i) |
---|
| 1113 | + | func statsREADONLY () = { |
---|
| 1114 | + | let cfg = getPoolConfig() |
---|
| 1115 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1116 | + | let amtAssetId = cfg[idxAmtAssetId] |
---|
| 1117 | + | let priceAssetId = cfg[idxPriceAssetId] |
---|
| 1118 | + | let iAmtAssetId = cfg[idxIAmtAssetId] |
---|
| 1119 | + | let iPriceAssetId = cfg[idxIPriceAssetId] |
---|
| 1120 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1121 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1122 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1123 | + | let accAmtAssetBalance = getAccBalance(amtAssetId) |
---|
| 1124 | + | let accPriceAssetBalance = getAccBalance(priceAssetId) |
---|
| 1125 | + | let pricesList = if ((poolLPBalance == 0)) |
---|
| 1126 | + | then [zeroBigInt, zeroBigInt, zeroBigInt] |
---|
| 1127 | + | else calcPrices(accAmtAssetBalance, accPriceAssetBalance, poolLPBalance) |
---|
| 1128 | + | let curPrice = 0 |
---|
| 1129 | + | let lpAmtAssetShare = fromX18(pricesList[1], scale8) |
---|
| 1130 | + | let lpPriceAssetShare = fromX18(pricesList[2], scale8) |
---|
| 1131 | + | let poolWeight = value(getInteger(factoryContract, keyPoolWeight(toString(this)))) |
---|
| 1132 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d", toString(accAmtAssetBalance), toString(accPriceAssetBalance), toString(poolLPBalance), toString(curPrice), toString(lpAmtAssetShare), toString(lpPriceAssetShare), toString(poolWeight)], SEP)) |
---|
| 1133 | + | } |
---|
| 1134 | + | |
---|
| 1135 | + | |
---|
| 1136 | + | |
---|
| 1137 | + | @Callable(i) |
---|
| 1138 | + | func evaluatePutByAmountAssetREADONLY (inAmAssetAmt) = { |
---|
| 1139 | + | let cfg = getPoolConfig() |
---|
| 1140 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1141 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 1142 | + | let amAssetId = fromBase58String(amAssetIdStr) |
---|
| 1143 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 1144 | + | let prAssetId = fromBase58String(prAssetIdStr) |
---|
| 1145 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1146 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1147 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 1148 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1149 | + | let accAmtAssetBalance = getAccBalance(amAssetIdStr) |
---|
| 1150 | + | let accPriceAssetBalance = getAccBalance(prAssetIdStr) |
---|
| 1151 | + | let amtAssetAmtX18 = toX18(accAmtAssetBalance, amtAssetDcm) |
---|
| 1152 | + | let priceAssetAmtX18 = toX18(accPriceAssetBalance, priceAssetDcm) |
---|
| 1153 | + | let curPriceX18 = if ((poolLPBalance == 0)) |
---|
| 1154 | + | then zeroBigInt |
---|
| 1155 | + | else calcPriceBigInt(priceAssetAmtX18, amtAssetAmtX18) |
---|
| 1156 | + | let inAmAssetAmtX18 = toX18(inAmAssetAmt, amtAssetDcm) |
---|
| 1157 | + | let inPrAssetAmtX18 = fraction(inAmAssetAmtX18, curPriceX18, scale18) |
---|
| 1158 | + | let inPrAssetAmt = fromX18(inPrAssetAmtX18, priceAssetDcm) |
---|
| 1159 | + | let estPut = estimatePutOperation("", 500000, inAmAssetAmt, amAssetId, inPrAssetAmt, prAssetId, "", true, false) |
---|
| 1160 | + | let calcLpAmt = estPut._1 |
---|
| 1161 | + | let curPriceCalc = estPut._3 |
---|
| 1162 | + | let amBalance = estPut._4 |
---|
| 1163 | + | let prBalance = estPut._5 |
---|
| 1164 | + | let lpEmission = estPut._6 |
---|
| 1165 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d%d", toString(calcLpAmt), toString(fromX18(curPriceX18, scale8)), toString(amBalance), toString(prBalance), toString(lpEmission), poolStatus, toString(inAmAssetAmt), toString(inPrAssetAmt)], SEP)) |
---|
| 1166 | + | } |
---|
| 1167 | + | |
---|
| 1168 | + | |
---|
| 1169 | + | |
---|
| 1170 | + | @Callable(i) |
---|
| 1171 | + | func evaluatePutByPriceAssetREADONLY (inPrAssetAmt) = { |
---|
| 1172 | + | let cfg = getPoolConfig() |
---|
| 1173 | + | let lpAssetId = fromBase58String(cfg[idxPoolLPAssetId]) |
---|
| 1174 | + | let amAssetIdStr = cfg[idxAmtAssetId] |
---|
| 1175 | + | let amAssetId = fromBase58String(amAssetIdStr) |
---|
| 1176 | + | let prAssetIdStr = cfg[idxPriceAssetId] |
---|
| 1177 | + | let prAssetId = fromBase58String(prAssetIdStr) |
---|
| 1178 | + | let amtAssetDcm = parseIntValue(cfg[idxAmtAssetDcm]) |
---|
| 1179 | + | let priceAssetDcm = parseIntValue(cfg[idxPriceAssetDcm]) |
---|
| 1180 | + | let poolStatus = cfg[idxPoolStatus] |
---|
| 1181 | + | let poolLPBalance = valueOrErrorMessage(assetInfo(lpAssetId), (("Asset " + toBase58String(lpAssetId)) + " doesn't exist")).quantity |
---|
| 1182 | + | let amBalanceRaw = getAccBalance(amAssetIdStr) |
---|
| 1183 | + | let prBalanceRaw = getAccBalance(prAssetIdStr) |
---|
| 1184 | + | let amBalanceRawX18 = toX18(amBalanceRaw, amtAssetDcm) |
---|
| 1185 | + | let prBalanceRawX18 = toX18(prBalanceRaw, priceAssetDcm) |
---|
| 1186 | + | let curPriceX18 = if ((poolLPBalance == 0)) |
---|
| 1187 | + | then zeroBigInt |
---|
| 1188 | + | else calcPriceBigInt(prBalanceRawX18, amBalanceRawX18) |
---|
| 1189 | + | let inPrAssetAmtX18 = toX18(inPrAssetAmt, priceAssetDcm) |
---|
| 1190 | + | let inAmAssetAmtX18 = fraction(inPrAssetAmtX18, scale18, curPriceX18) |
---|
| 1191 | + | let inAmAssetAmt = fromX18(inAmAssetAmtX18, amtAssetDcm) |
---|
| 1192 | + | let estPut = estimatePutOperation("", 500000, inAmAssetAmt, amAssetId, inPrAssetAmt, prAssetId, "", true, false) |
---|
| 1193 | + | let calcLpAmt = estPut._1 |
---|
| 1194 | + | let curPriceCalc = estPut._3 |
---|
| 1195 | + | let amBalance = estPut._4 |
---|
| 1196 | + | let prBalance = estPut._5 |
---|
| 1197 | + | let lpEmission = estPut._6 |
---|
| 1198 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d%d", toString(calcLpAmt), toString(fromX18(curPriceX18, scale8)), toString(amBalance), toString(prBalance), toString(lpEmission), poolStatus, toString(inAmAssetAmt), toString(inPrAssetAmt)], SEP)) |
---|
| 1199 | + | } |
---|
| 1200 | + | |
---|
| 1201 | + | |
---|
| 1202 | + | |
---|
| 1203 | + | @Callable(i) |
---|
| 1204 | + | func evaluateGetREADONLY (paymentLpAssetId,paymentLpAmt) = { |
---|
| 1205 | + | let res = estimateGetOperation("", paymentLpAssetId, paymentLpAmt, this) |
---|
| 1206 | + | let outAmAmt = res._1 |
---|
| 1207 | + | let outPrAmt = res._2 |
---|
| 1208 | + | let amBalance = res._5 |
---|
| 1209 | + | let prBalance = res._6 |
---|
| 1210 | + | let lpEmission = res._7 |
---|
| 1211 | + | let curPrice = res._8 |
---|
| 1212 | + | let poolStatus = parseIntValue(res._9) |
---|
| 1213 | + | $Tuple2(nil, makeString(["%d%d%d%d%d%d%d", toString(outAmAmt), toString(outPrAmt), toString(amBalance), toString(prBalance), toString(lpEmission), toString(curPrice), toString(poolStatus)], SEP)) |
---|
| 1214 | + | } |
---|
| 1215 | + | |
---|
| 1216 | + | |
---|
| 1217 | + | @Verifier(tx) |
---|
| 1218 | + | func verify () = { |
---|
| 1219 | + | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
| 1220 | + | case pk: ByteVector => |
---|
| 1221 | + | pk |
---|
| 1222 | + | case _: Unit => |
---|
| 1223 | + | tx.senderPublicKey |
---|
| 1224 | + | case _ => |
---|
| 1225 | + | throw("Match error") |
---|
| 1226 | + | } |
---|
| 1227 | + | match tx { |
---|
| 1228 | + | case order: Order => |
---|
| 1229 | + | let matcherPub = getMatcherPubOrFail() |
---|
| 1230 | + | let orderValid = validateMatcherOrderAllowed(order) |
---|
| 1231 | + | let senderValid = sigVerify(order.bodyBytes, order.proofs[0], order.senderPublicKey) |
---|
| 1232 | + | let matcherValid = sigVerify(order.bodyBytes, order.proofs[1], matcherPub) |
---|
| 1233 | + | if (if (if (orderValid) |
---|
| 1234 | + | then senderValid |
---|
| 1235 | + | else false) |
---|
| 1236 | + | then matcherValid |
---|
| 1237 | + | else false) |
---|
| 1238 | + | then true |
---|
| 1239 | + | else throwOrderError(orderValid, senderValid, matcherValid) |
---|
| 1240 | + | case s: SetScriptTransaction => |
---|
| 1241 | + | let newHash = blake2b256(value(s.script)) |
---|
| 1242 | + | let allowedHash = fromBase64String(value(getString(factoryContract, keyAllowedLpScriptHash()))) |
---|
| 1243 | + | let currentHash = scriptHash(this) |
---|
| 1244 | + | if (if ((allowedHash == newHash)) |
---|
| 1245 | + | then (currentHash != newHash) |
---|
| 1246 | + | else false) |
---|
| 1247 | + | then true |
---|
| 1248 | + | else sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 1249 | + | case _ => |
---|
| 1250 | + | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 1251 | + | } |
---|
| 1252 | + | } |
---|
| 1253 | + | |
---|