Skip to content

Commit d874292

Browse files
committedAug 24, 2022
feat: WXDEFI-58 different decimals in lp_stable

File tree

6 files changed

+53
-52
lines changed

6 files changed

+53
-52
lines changed
 

‎ride/lp_stable.ride

+29-28
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#-----------------
3737
# GLOBAL VARIABLES
3838
#-----------------
39+
let defaultDecimals = 1_000_000
40+
3941
let scale8 = 100_000_000
4042
let scale8BigInt = 100_000_000.toBigInt()
4143
let scale18 = 1_000_000_000_000_000_000.toBigInt()
@@ -248,21 +250,18 @@ func estimateGetOperation(txId58: String, paymentAssetId: String, paymentLpAmoun
248250
let lpEmission = assetInfo(lpAssetId.fromBase58String()).valueOrErrorMessage("Wrong LP id").quantity
249251

250252
strict validationBlock = lpAssetId == paymentAssetId || throwErr("Wrong payment asset")
253+
let amountBalance = getAccBalance(amountId)
254+
let priceBalance = getAccBalance(priceId)
251255

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)
257258

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)
262261

263262
let currentPriceX18 = calcPriceBigInt(priceBalanceX18, amountBalanceX18)
264263
let curPrice = currentPriceX18.fromX18(scale8)
265-
264+
266265
let paymentLpAmountX18 = paymentLpAmount.toX18(scale8)
267266
let lpEmissionX18 = lpEmission.toX18(scale8)
268267

@@ -271,8 +270,11 @@ func estimateGetOperation(txId58: String, paymentAssetId: String, paymentLpAmoun
271270
let outPriceAmountX18 = fraction(priceBalanceX18, paymentLpAmountX18, lpEmissionX18)
272271

273272
# 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)
276278

277279
let state = if (txId58 == "") then [] else
278280
[
@@ -332,26 +334,25 @@ func estimatePutOperation(
332334
if(isOneAsset && paymentId == amountIdStr) then getAccBalance(amountIdStr) - paymentAmount else
333335
if(isOneAsset) then getAccBalance(amountIdStr) else getAccBalance(amountIdStr) - inAmountAssetAmount
334336

335-
# calc pool price
336337
let priceBalance = if(isEvaluate) then getAccBalance(priceIdStr) else
337338
if(isOneAsset && paymentId == priceIdStr) then getAccBalance(priceIdStr) - paymentAmount else
338339
if(isOneAsset) then getAccBalance(priceIdStr) else getAccBalance(priceIdStr) - inPriceAssetAmount
339340

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)
345346

346347
# 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)
349350

350351
# calc user expected price
351352
let userPriceX18 = calcPriceBigInt(inPriceAssetAmountX18, inAmountAssetAmountX18)
352353

353-
let amountBalanceX18 = amountBalance.toX18(decimals)
354-
let priceBalanceX18 = priceBalance.toX18(decimals)
354+
let amountBalanceX18 = amountBalanceDefaultDecimals.toX18(defaultDecimals)
355+
let priceBalanceX18 = priceBalanceDefaultDecimals.toX18(defaultDecimals)
355356

356357
# case of the initial or first deposit
357358
# result is a tuple containing the following:
@@ -366,8 +367,8 @@ func estimatePutOperation(
366367
let lpAmountX18 = pow(inAmountAssetAmountX18 * inPriceAssetAmountX18, 0, 5.toBigInt(), 1, 0, DOWN)
367368
(
368369
lpAmountX18.fromX18(scale8),
369-
inAmountAssetAmountX18.fromX18(decimals),
370-
inPriceAssetAmountX18.fromX18(decimals),
370+
inAmountAssetAmountX18.fromX18(defaultDecimals),
371+
inPriceAssetAmountX18.fromX18(defaultDecimals),
371372
calcPriceBigInt(priceBalanceX18 + inPriceAssetAmountX18, amountBalanceX18 + inAmountAssetAmountX18),
372373
slippageX18
373374
)
@@ -395,16 +396,16 @@ func estimatePutOperation(
395396
let lpAmountX18 = fraction(lpEmissionX18, expectedPriceAssetAmountX18, priceBalanceX18)
396397
(
397398
lpAmountX18.fromX18(scale8),
398-
expectedAmountAssetAmountX18.fromX18(decimals),
399-
expectedPriceAssetAmountX18.fromX18(decimals),
399+
expectedAmountAssetAmountX18.fromX18(defaultDecimals),
400+
expectedPriceAssetAmountX18.fromX18(defaultDecimals),
400401
currentPriceX18,
401402
slippageX18
402403
)
403404
}
404405

405406
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)
408409
let currentPrice = r._4.fromX18(scale8)
409410
let slippageCalculate = r._5.fromX18(scale8)
410411

‎test/lp_stable/decimals_case/estimatePutOperationWrapperREADONLY.mjs

+13-13
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ describe(
2525
const isEval = true;
2626
const emitLp = false;
2727

28-
const expected1 = { type: 'Int', value: 100000000 };
29-
const expected2 = { type: 'Int', value: 0 };
30-
const expected3 = { type: 'Int', value: 100000000 };
31-
const expected4 = { type: 'Int', value: 0 };
32-
const expected5 = { type: 'Int', value: 0 };
33-
const expected6 = { type: 'Int', value: 0 };
34-
const expected7 = { type: 'ByteVector', value: this.lpStableAssetId };
35-
const expected8 = { type: 'String', value: '1' };
28+
const expected1 = { type: 'Int', value: 1000000000 }; // outLp
29+
const expected2 = { type: 'Int', value: 0 }; // emitLpAmount
30+
const expected3 = { type: 'Int', value: 10000000000 }; // currentPrice
31+
const expected4 = { type: 'Int', value: 0 }; // amountBalance
32+
const expected5 = { type: 'Int', value: 0 }; // priceBalance
33+
const expected6 = { type: 'Int', value: 0 }; // lpEmission
34+
const expected7 = { type: 'ByteVector', value: this.lpStableAssetId }; // lpAssetId
35+
const expected8 = { type: 'String', value: '1' }; // sts
3636
const expected9 = {
3737
type: 'IntegerEntry',
3838
value: {
@@ -42,15 +42,15 @@ describe(
4242
},
4343
value: {
4444
type: 'Int',
45-
value: 100000000,
45+
value: 10000000000,
4646
},
4747
},
4848
};
4949

50-
const expected10 = { type: 'Int', value: 0 };
51-
const expected11 = { type: 'Int', value: 0 };
52-
const expected12 = { type: 'String', value: this.usdtAssetId };
53-
const expected13 = { type: 'String', value: this.usdnAssetId };
50+
const expected10 = { type: 'Int', value: 0 }; // amountDiff
51+
const expected11 = { type: 'Int', value: 0 }; // priceDiff
52+
const expected12 = { type: 'String', value: this.usdtAssetId }; // inAmountAssetId
53+
const expected13 = { type: 'String', value: this.usdnAssetId }; // inPriceAssetId
5454

5555
const lpStable = address(this.accounts.lpStable, chainId);
5656

‎test/lp_stable/decimals_case/get.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ describe('lp_stable: get.mjs', /** @this {MochaSuiteModified} */() => {
1818
async function () {
1919
const usdnAmount = 1e16 / 10;
2020
const usdtAmount = 1e8 / 10;
21-
const lpStableAmount = 1e10;
21+
const lpStableAmount = 1e11;
2222
const shouldAutoStake = false;
23-
const priceLast = 1e16;
24-
const priceHistory = 1e16;
23+
const priceLast = 1e18;
24+
const priceHistory = 1e18;
2525

2626
const lpStable = address(this.accounts.lpStable, chainId);
2727

‎test/lp_stable/decimals_case/getOneTkn.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('lp_stable: getOneTkn.mjs', /** @this {MochaSuiteModified} */() => {
1616
it('should successfully getOneTkn.', async function () {
1717
const amAssetPart = 1e10;
1818
const prAssetPart = 1e10;
19-
const outLp = 1e10;
19+
const outLp = 1e11;
2020
const slippage = 1e3;
2121
const autoStake = false;
2222
const usdtAmount = 1e10;

‎test/lp_stable/decimals_case/put.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ describe('lp_stable: put.mjs', /** @this {MochaSuiteModified} */() => {
1616
it('should successfully put with shouldAutoStake false', async function () {
1717
const usdnAmount = 1e16 / 10;
1818
const usdtAmount = 1e8 / 10;
19-
const expectedLpAmount = 1e11;
19+
const expectedLpAmount = 1e12;
2020
const shouldAutoStake = false;
21-
const priceLast = 1e16;
22-
const priceHistory = 1e16;
21+
const priceLast = 1e18;
22+
const priceHistory = 1e18;
2323

2424
const lpStable = address(this.accounts.lpStable, chainId);
2525

‎test/lp_stable/decimals_case/putOneTkn.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ describe('lp_stable: putOneTkn.mjs', /** @this {MochaSuiteModified} */() => {
1616
it('should successfully putOneTkn with autoStake false', async function () {
1717
const amAssetPart = 1e10;
1818
const prAssetPart = 1e10;
19-
const outLp = 1e10;
19+
const outLp = 1e11;
2020
const slippage = 1e3;
2121
const autoStake = false;
2222
const usdtAmount = 1e10;
2323

24-
const expectedPriceLast = 1e8;
25-
const expectedPriceHistory = 1e8;
24+
const expectedPriceLast = 1e10;
25+
const expectedPriceHistory = 1e10;
2626
const expectedWriteAmAmt = 1e10;
2727
const expectedWritePrAmt = 0;
28-
const expectedEmitLpAmt = 1e10;
28+
const expectedEmitLpAmt = 1e11;
2929
const expectedslippageCalc = 0;
3030
const expectedAmDiff = 0;
3131
const expectedPrDiff = 0;

0 commit comments

Comments
 (0)
Please sign in to comment.