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