36
36
#-----------------
37
37
# GLOBAL VARIABLES
38
38
#-----------------
39
+ let defaultDecimals = 1_000_000
40
+
39
41
let scale8 = 100_000_000
40
42
let scale8BigInt = 100_000_000.toBigInt()
41
43
let scale18 = 1_000_000_000_000_000_000.toBigInt()
@@ -248,21 +250,18 @@ func estimateGetOperation(txId58: String, paymentAssetId: String, paymentLpAmoun
248
250
let lpEmission = assetInfo(lpAssetId.fromBase58String()).valueOrErrorMessage("Wrong LP id").quantity
249
251
250
252
strict validationBlock = lpAssetId == paymentAssetId || throwErr("Wrong payment asset")
253
+ let amountBalance = getAccBalance(amountId)
254
+ let priceBalance = getAccBalance(priceId)
251
255
252
- strict decimals = if amountDecimals >= priceDecimals then {
253
- amountDecimals
254
- } else {
255
- priceDecimals
256
- }
256
+ let amountBalanceDefaultDecimals = fraction(amountBalance, defaultDecimals, amountDecimals)
257
+ let priceBalanceDefaultDecimals = fraction(priceBalance, defaultDecimals, priceDecimals)
257
258
258
- let amountBalance = getAccBalance(amountId)
259
- let amountBalanceX18 = amountBalance.toX18(decimals)
260
- let priceBalance = getAccBalance(priceId)
261
- let priceBalanceX18 = priceBalance.toX18(decimals)
259
+ let amountBalanceX18 = amountBalanceDefaultDecimals.toX18(defaultDecimals)
260
+ let priceBalanceX18 = priceBalanceDefaultDecimals.toX18(defaultDecimals)
262
261
263
262
let currentPriceX18 = calcPriceBigInt(priceBalanceX18, amountBalanceX18)
264
263
let curPrice = currentPriceX18.fromX18(scale8)
265
-
264
+
266
265
let paymentLpAmountX18 = paymentLpAmount.toX18(scale8)
267
266
let lpEmissionX18 = lpEmission.toX18(scale8)
268
267
@@ -271,8 +270,11 @@ func estimateGetOperation(txId58: String, paymentAssetId: String, paymentLpAmoun
271
270
let outPriceAmountX18 = fraction(priceBalanceX18, paymentLpAmountX18, lpEmissionX18)
272
271
273
272
# cast amounts to asset decimals
274
- let outAmountAmount = outAmountAmountX18.fromX18(decimals)
275
- let outPriceAmount = outPriceAmountX18.fromX18(decimals)
273
+ let outAmountAmountDefaultDecimals = outAmountAmountX18.fromX18(defaultDecimals)
274
+ let outPriceAmountDefaultDecimals = outPriceAmountX18.fromX18(defaultDecimals)
275
+
276
+ let outAmountAmount = fraction(outAmountAmountDefaultDecimals, amountDecimals, defaultDecimals)
277
+ let outPriceAmount = fraction(outPriceAmountDefaultDecimals, priceDecimals, defaultDecimals)
276
278
277
279
let state = if (txId58 == "") then [] else
278
280
[
@@ -332,26 +334,25 @@ func estimatePutOperation(
332
334
if(isOneAsset && paymentId == amountIdStr) then getAccBalance(amountIdStr) - paymentAmount else
333
335
if(isOneAsset) then getAccBalance(amountIdStr) else getAccBalance(amountIdStr) - inAmountAssetAmount
334
336
335
- # calc pool price
336
337
let priceBalance = if(isEvaluate) then getAccBalance(priceIdStr) else
337
338
if(isOneAsset && paymentId == priceIdStr) then getAccBalance(priceIdStr) - paymentAmount else
338
339
if(isOneAsset) then getAccBalance(priceIdStr) else getAccBalance(priceIdStr) - inPriceAssetAmount
339
340
340
- strict decimals = if amountDecimals >= priceDecimals then {
341
- amountDecimals
342
- } else {
343
- priceDecimals
344
- }
341
+ let amountBalanceDefaultDecimals = fraction(amountBalance, defaultDecimals, amountDecimals)
342
+ let priceBalanceDefaultDecimals = fraction(priceBalance, defaultDecimals, priceDecimals)
343
+
344
+ let inAmountAssetAmountDefaultDecimals = fraction(inAmountAssetAmount, defaultDecimals, amountDecimals)
345
+ let inPriceAssetAmountDefaultDecimals = fraction(inPriceAssetAmount, defaultDecimals, priceDecimals)
345
346
346
347
# cast amounts to the lp decimals
347
- let inAmountAssetAmountX18 = inAmountAssetAmount .toX18(decimals )
348
- let inPriceAssetAmountX18 = inPriceAssetAmount .toX18(decimals )
348
+ let inAmountAssetAmountX18 = inAmountAssetAmountDefaultDecimals .toX18(defaultDecimals )
349
+ let inPriceAssetAmountX18 = inPriceAssetAmountDefaultDecimals .toX18(defaultDecimals )
349
350
350
351
# calc user expected price
351
352
let userPriceX18 = calcPriceBigInt(inPriceAssetAmountX18, inAmountAssetAmountX18)
352
353
353
- let amountBalanceX18 = amountBalance .toX18(decimals )
354
- let priceBalanceX18 = priceBalance .toX18(decimals )
354
+ let amountBalanceX18 = amountBalanceDefaultDecimals .toX18(defaultDecimals )
355
+ let priceBalanceX18 = priceBalanceDefaultDecimals .toX18(defaultDecimals )
355
356
356
357
# case of the initial or first deposit
357
358
# result is a tuple containing the following:
@@ -366,8 +367,8 @@ func estimatePutOperation(
366
367
let lpAmountX18 = pow(inAmountAssetAmountX18 * inPriceAssetAmountX18, 0, 5.toBigInt(), 1, 0, DOWN)
367
368
(
368
369
lpAmountX18.fromX18(scale8),
369
- inAmountAssetAmountX18.fromX18(decimals ),
370
- inPriceAssetAmountX18.fromX18(decimals ),
370
+ inAmountAssetAmountX18.fromX18(defaultDecimals ),
371
+ inPriceAssetAmountX18.fromX18(defaultDecimals ),
371
372
calcPriceBigInt(priceBalanceX18 + inPriceAssetAmountX18, amountBalanceX18 + inAmountAssetAmountX18),
372
373
slippageX18
373
374
)
@@ -395,16 +396,16 @@ func estimatePutOperation(
395
396
let lpAmountX18 = fraction(lpEmissionX18, expectedPriceAssetAmountX18, priceBalanceX18)
396
397
(
397
398
lpAmountX18.fromX18(scale8),
398
- expectedAmountAssetAmountX18.fromX18(decimals ),
399
- expectedPriceAssetAmountX18.fromX18(decimals ),
399
+ expectedAmountAssetAmountX18.fromX18(defaultDecimals ),
400
+ expectedPriceAssetAmountX18.fromX18(defaultDecimals ),
400
401
currentPriceX18,
401
402
slippageX18
402
403
)
403
404
}
404
405
405
406
let calculateLpAmount = r._1
406
- let calculateAmountAssetPayment = r._2
407
- let calculatePriceAssetPayment = r._3
407
+ let calculateAmountAssetPayment = fraction( r._2, amountDecimals, defaultDecimals)
408
+ let calculatePriceAssetPayment = fraction( r._3, priceDecimals, defaultDecimals)
408
409
let currentPrice = r._4.fromX18(scale8)
409
410
let slippageCalculate = r._5.fromX18(scale8)
410
411
0 commit comments