Skip to content

Commit

Permalink
mwr-641 input field
Browse files Browse the repository at this point in the history
  • Loading branch information
arvifox committed Sep 20, 2023
1 parent 563ce69 commit 45c4c25
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"))
)
}

Expand All @@ -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,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -110,7 +112,7 @@ object PoolLocalMapper {
user = UserPoolData(
basePooled = basePooled,
targetPooled = secondPooled,
share.toDouble(),
share,
poolLocal.userPoolLocal.poolProvidersBalance,
poolLocal.userPoolLocal.favorite,
poolLocal.userPoolLocal.sortOrder,
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.298112417815485325766582281222788"), details.shareOfPool)
assertEquals(BigDecimal("66.298112417815485326"), details.shareOfPool)
}
}

0 comments on commit 45c4c25

Please sign in to comment.