diff --git a/build.gradle b/build.gradle index e5edbcbe9..e00ea0a8f 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ buildscript { composeCompiler : '1.4.6', composeConstraintLayout: '1.1.0-alpha05', uiCore : '0.1.4', - soraCard : '0.1.41', + soraCard : '0.1.42', lazySodium : '5.0.2', jna : '5.8.0', accompanist : '0.30.1', diff --git a/common/src/main/java/jp/co/soramitsu/common/util/ext/BigDecimalExt.kt b/common/src/main/java/jp/co/soramitsu/common/util/ext/BigDecimalExt.kt index fb73ab768..cb50e4f64 100644 --- a/common/src/main/java/jp/co/soramitsu/common/util/ext/BigDecimalExt.kt +++ b/common/src/main/java/jp/co/soramitsu/common/util/ext/BigDecimalExt.kt @@ -62,10 +62,10 @@ fun BigDecimal.nullZero(): BigDecimal? = if (this.isZero()) null else this fun BigDecimal.divideBy( divisor: BigDecimal, - scale: Int? = null + scale: Int? = null, ): BigDecimal { return if (scale == null) { - val maxScale = max(this.scale(), divisor.scale()) + val maxScale = max(this.scale(), divisor.scale()).coerceAtMost(OptionsProvider.defaultScale) if (maxScale != 0) { this.divide(divisor, maxScale, RoundingMode.HALF_EVEN) @@ -79,7 +79,7 @@ fun BigDecimal.divideBy( fun BigDecimal.safeDivide( divisor: BigDecimal, - scale: Int? = null + scale: Int? = null, ): BigDecimal { return if (divisor.isZero()) { BigDecimal.ZERO 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 c961cc980..af38a1233 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 @@ -45,11 +45,12 @@ object PolkaswapFormulas { fun calculatePooledValue( reserves: BigDecimal, poolProvidersBalance: BigDecimal, - totalIssuance: BigDecimal + totalIssuance: BigDecimal, + precision: Int? = OptionsProvider.defaultScale, ): BigDecimal = - reserves.multiply(poolProvidersBalance).divideBy(totalIssuance) + reserves.multiply(poolProvidersBalance).divideBy(totalIssuance, precision) - fun calculateShareOfPool( + private fun calculateShareOfPool( poolProvidersBalance: BigDecimal, totalIssuance: BigDecimal ): BigDecimal = 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 6cab19446..524231893 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 @@ -35,10 +35,12 @@ package jp.co.soramitsu.common_wallet.presentation import jp.co.soramitsu.common.util.ext.Big100 import java.math.BigDecimal import jp.co.soramitsu.common.util.ext.divideBy +import jp.co.soramitsu.common.util.ext.equalTo import jp.co.soramitsu.common.util.ext.safeDivide import jp.co.soramitsu.common_wallet.domain.model.WithDesired import jp.co.soramitsu.common_wallet.presentation.compose.util.PolkaswapFormulas import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue import org.junit.Test class PolkaswapFormulasTest { @@ -64,13 +66,12 @@ class PolkaswapFormulasTest { val pooledValue = RESERVES_FIRST.multiply(POOL_PROVIDERS_BALANCE).divideBy(TOTAL_ISSUANCE) - assertEquals( - pooledValue, + assertTrue( PolkaswapFormulas.calculatePooledValue( RESERVES_FIRST, POOL_PROVIDERS_BALANCE, TOTAL_ISSUANCE - ) + ).equalTo(BigDecimal("132.337834284157373294")) ) } @@ -79,11 +80,12 @@ class PolkaswapFormulasTest { val shareOfPool = POOL_PROVIDERS_BALANCE.divideBy(TOTAL_ISSUANCE).multiply(Big100) assertEquals( - shareOfPool, - PolkaswapFormulas.calculateShareOfPool( + shareOfPool.toDouble(), + PolkaswapFormulas.calculateShareOfPoolFromAmount( POOL_PROVIDERS_BALANCE, TOTAL_ISSUANCE - ) + ), + 0.001, ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/data/mappers/PoolLocalMapper.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/data/mappers/PoolLocalMapper.kt index e0a6a1a29..3be2a9578 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/data/mappers/PoolLocalMapper.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/data/mappers/PoolLocalMapper.kt @@ -87,13 +87,15 @@ object PoolLocalMapper { poolLocal.basicPoolLocal.reserveBase, poolLocal.userPoolLocal.poolProvidersBalance, poolLocal.basicPoolLocal.totalIssuance, + baseToken.precision, ) val secondPooled = PolkaswapFormulas.calculatePooledValue( poolLocal.basicPoolLocal.reserveTarget, poolLocal.userPoolLocal.poolProvidersBalance, poolLocal.basicPoolLocal.totalIssuance, + token.precision, ) - val share = PolkaswapFormulas.calculateShareOfPool( + val share = PolkaswapFormulas.calculateShareOfPoolFromAmount( poolLocal.userPoolLocal.poolProvidersBalance, poolLocal.basicPoolLocal.totalIssuance, ) @@ -110,7 +112,7 @@ object PoolLocalMapper { user = UserPoolData( basePooled = basePooled, targetPooled = secondPooled, - share.toDouble(), + share, poolLocal.userPoolLocal.poolProvidersBalance, poolLocal.userPoolLocal.favorite, poolLocal.userPoolLocal.sortOrder, diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/domain/PoolsInteractorTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/domain/PoolsInteractorTest.kt index d953eb513..dba9ab6f2 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/domain/PoolsInteractorTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/domain/PoolsInteractorTest.kt @@ -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.298112417815485325766582281222788"), details.shareOfPool) + assertEquals(BigDecimal("66.298112417815485326"), details.shareOfPool) } }