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