Skip to content

Commit

Permalink
Merge pull request #204 from sora-xor/MWR-641/fix-on-safe-division
Browse files Browse the repository at this point in the history
MWR-641/Fixed on basicNumberInput precision apperance
  • Loading branch information
arvifox authored Sep 20, 2023
2 parents fa06f70 + 3ecd39e commit 563ce69
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package jp.co.soramitsu.common_wallet.presentation.compose.util

import java.math.BigDecimal
import jp.co.soramitsu.common.domain.OptionsProvider
import jp.co.soramitsu.common.util.ext.Big100
import jp.co.soramitsu.common.util.ext.divideBy
import jp.co.soramitsu.common.util.ext.equalTo
Expand Down Expand Up @@ -82,8 +83,8 @@ object PolkaswapFormulas {
): BigDecimal {
return pooled
.plus(amount)
.safeDivide(amount.plus(reserves))
.multiply(Big100)
.safeDivide(amount.plus(reserves))
}

fun estimateRemovingShareOfPool(
Expand All @@ -92,8 +93,8 @@ object PolkaswapFormulas {
reserves: BigDecimal
): BigDecimal = pooled
.minus(amount)
.safeDivide(reserves.minus(amount))
.multiply(Big100)
.safeDivide(reserves.minus(amount))

fun calculateMinAmount(
amount: BigDecimal,
Expand Down Expand Up @@ -133,5 +134,6 @@ object PolkaswapFormulas {
amount: BigDecimal,
amountPooled: BigDecimal,
otherPooled: BigDecimal,
): BigDecimal = amount.multiply(otherPooled).safeDivide(amountPooled)
precision: Int? = OptionsProvider.defaultScale,
): BigDecimal = amount.multiply(otherPooled).safeDivide(amountPooled, precision)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor(
amount1,
it.user.basePooled,
it.user.targetPooled,
removeState.assetState2?.token?.precision
)
percent = PolkaswapFormulas.calculateShareOfPoolFromAmount(
amount1,
Expand All @@ -344,7 +345,8 @@ class LiquidityRemoveViewModel @AssistedInject constructor(
PolkaswapFormulas.calculateOneAmountFromAnother(
amount2,
it.user.targetPooled,
it.user.basePooled
it.user.basePooled,
removeState.assetState1?.token?.precision
)
percent = PolkaswapFormulas.calculateShareOfPoolFromAmount(
amount1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ class PoolsInteractorTest {
assertEquals(BigDecimal("5.051548910147228600"), details.targetAmount)
assertEquals(BigDecimal("1.979590849830759653"), details.perFirst)
assertEquals(BigDecimal("0.505154891014722860"), details.perSecond)
assertEquals(BigDecimal("66.298112417815485325766582281222800"), details.shareOfPool)
assertEquals(BigDecimal("66.298112417815485325766582281222788"), details.shareOfPool)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import jp.co.soramitsu.common.R
import jp.co.soramitsu.common.resourses.ResourceManager
import jp.co.soramitsu.common.util.NumbersFormatter
import jp.co.soramitsu.common.util.ext.equalTo
import jp.co.soramitsu.feature_assets_api.domain.AssetsInteractor
import jp.co.soramitsu.feature_assets_api.presentation.AssetsRouter
import jp.co.soramitsu.feature_polkaswap_api.domain.interfaces.PoolsInteractor
Expand Down Expand Up @@ -198,7 +199,7 @@ class RemoveLiquidityViewModelTest {
advanceUntilIdle()

assertEquals(BigDecimal(0.5), viewModel.removeState.assetState1?.amount)
assertEquals(BigDecimal(0.5), viewModel.removeState.assetState2?.amount)
assertTrue(viewModel.removeState.assetState2?.amount?.equalTo(BigDecimal(0.5)) == true)
assertEquals("Remove", viewModel.removeState.btnState.text)
assertTrue(viewModel.removeState.btnState.enabled)
}
Expand All @@ -212,7 +213,7 @@ class RemoveLiquidityViewModelTest {
viewModel.onAmount2Change(BigDecimal("0.35"))
advanceUntilIdle()

assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState1?.amount)
assertTrue(viewModel.removeState.assetState1?.amount?.equalTo(amount) == true)
assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState2?.amount)
assertTrue(viewModel.removeState.btnState.enabled)
}
Expand Down

0 comments on commit 563ce69

Please sign in to comment.