Skip to content

Commit

Permalink
Fixed safe division
Browse files Browse the repository at this point in the history
  • Loading branch information
AmadeyKuspakov committed Sep 20, 2023
1 parent fa06f70 commit e88017d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 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 @@ -82,8 +82,8 @@ object PolkaswapFormulas {
): BigDecimal {
return pooled
.plus(amount)
.safeDivide(amount.plus(reserves))
.multiply(Big100)
.safeDivide(amount.plus(reserves))
}

fun estimateRemovingShareOfPool(
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
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 ?: 18
)
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 ?: 18
)
percent = PolkaswapFormulas.calculateShareOfPoolFromAmount(
amount1,
Expand Down

0 comments on commit e88017d

Please sign in to comment.