From e88017d6f44e66e97ccfa26fd5841a6628dd2ab7 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Wed, 20 Sep 2023 05:57:23 +0400 Subject: [PATCH] Fixed safe division --- build.gradle | 2 +- .../presentation/compose/util/PolkaswapFormulas.kt | 7 ++++--- .../common_wallet/presentation/PolkaswapFormulasTest.kt | 4 ++-- .../screens/liquidityremove/LiquidityRemoveViewModel.kt | 4 +++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 6a8b81cb5..e5edbcbe9 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ buildscript { composeMaterial : '1.4.3', composeCompiler : '1.4.6', composeConstraintLayout: '1.1.0-alpha05', - uiCore : '0.1.3', + uiCore : '0.1.4', soraCard : '0.1.41', lazySodium : '5.0.2', jna : '5.8.0', diff --git a/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt b/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt index ca20d0997..33388672f 100644 --- a/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt +++ b/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt @@ -82,8 +82,8 @@ object PolkaswapFormulas { ): BigDecimal { return pooled .plus(amount) - .safeDivide(amount.plus(reserves)) .multiply(Big100) + .safeDivide(amount.plus(reserves)) } fun estimateRemovingShareOfPool( @@ -92,8 +92,8 @@ object PolkaswapFormulas { reserves: BigDecimal ): BigDecimal = pooled .minus(amount) - .safeDivide(reserves.minus(amount)) .multiply(Big100) + .safeDivide(reserves.minus(amount)) fun calculateMinAmount( amount: BigDecimal, @@ -133,5 +133,6 @@ object PolkaswapFormulas { amount: BigDecimal, amountPooled: BigDecimal, otherPooled: BigDecimal, - ): BigDecimal = amount.multiply(otherPooled).safeDivide(amountPooled) + precision: Int + ): BigDecimal = amount.multiply(otherPooled).safeDivide(amountPooled).safeDivide(Big100, precision) } diff --git a/common_wallet/src/test/java/jp/co/soramitsu/common_wallet/presentation/PolkaswapFormulasTest.kt b/common_wallet/src/test/java/jp/co/soramitsu/common_wallet/presentation/PolkaswapFormulasTest.kt index 37f6e8801..6cab19446 100644 --- a/common_wallet/src/test/java/jp/co/soramitsu/common_wallet/presentation/PolkaswapFormulasTest.kt +++ b/common_wallet/src/test/java/jp/co/soramitsu/common_wallet/presentation/PolkaswapFormulasTest.kt @@ -128,8 +128,8 @@ class PolkaswapFormulasTest { fun `test estimateAddingShareOfPool`() { val shareOfPool = SECOND_POOLED .plus(TARGET_AMOUNT) - .safeDivide(TARGET_AMOUNT.plus(RESERVES_SECOND)) .multiply(Big100) + .safeDivide(TARGET_AMOUNT.plus(RESERVES_SECOND)) assertEquals( shareOfPool, @@ -145,8 +145,8 @@ class PolkaswapFormulasTest { fun `test estimateRemovingShareOfPool`() { val shareOfPool = SECOND_POOLED .minus(TARGET_AMOUNT) - .safeDivide(RESERVES_SECOND.minus(TARGET_AMOUNT)) .multiply(Big100) + .safeDivide(RESERVES_SECOND.minus(TARGET_AMOUNT)) assertEquals( shareOfPool, diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt index 53b6ae5c4..c1275dbf7 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt @@ -320,6 +320,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( amount1, it.user.basePooled, it.user.targetPooled, + removeState.assetState2?.token?.precision ?: 18 ) percent = PolkaswapFormulas.calculateShareOfPoolFromAmount( amount1, @@ -344,7 +345,8 @@ class LiquidityRemoveViewModel @AssistedInject constructor( PolkaswapFormulas.calculateOneAmountFromAnother( amount2, it.user.targetPooled, - it.user.basePooled + it.user.basePooled, + removeState.assetState1?.token?.precision ?: 18 ) percent = PolkaswapFormulas.calculateShareOfPoolFromAmount( amount1,