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