-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
correctly calculate usd price per unit with extension, add tests for …
…extension
- Loading branch information
1 parent
3e4fc39
commit 248db86
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
service/src/main/kotlin/io/provenance/explorer/domain/extensions/NavEventExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.provenance.explorer.domain.extensions | ||
|
||
import io.provenance.explorer.model.base.USD_LOWER | ||
import io.provlabs.flow.api.NavEvent | ||
import java.math.BigDecimal | ||
import java.math.RoundingMode | ||
|
||
/** | ||
* Calculates the USD price per unit for a NAV event. | ||
* | ||
* The `priceAmount` is in dollar millis (e.g., 1234 = $1.234) and is divided by the volume to get the price per unit. | ||
* If the `priceDenom` is not "usd" or the volume is 0, it returns `BigDecimal.ZERO`. | ||
* | ||
* @return The USD price per unit or `BigDecimal.ZERO` if not a USD event or volume is 0. | ||
*/ | ||
fun NavEvent.calculateUsdPricePerUnit(): BigDecimal { | ||
if (this.priceDenom != USD_LOWER) { | ||
return BigDecimal.ZERO | ||
} | ||
|
||
if (this.volume == 0L) { | ||
return BigDecimal.ZERO | ||
} | ||
|
||
return BigDecimal(this.priceAmount) | ||
.setScale(3, RoundingMode.DOWN) | ||
.divide(BigDecimal(1000), RoundingMode.DOWN) | ||
.divide(BigDecimal(this.volume), 3, RoundingMode.DOWN) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
service/src/test/kotlin/io/provenance/explorer/domain/extensions/NavEventExtensionsKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.provenance.explorer.domain.extensions | ||
|
||
import io.provlabs.flow.api.NavEvent | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import java.math.BigDecimal | ||
|
||
class NavEventExtensionsKtTest { | ||
|
||
@Test | ||
fun `test calculateUsdPricePerUnit for flow nav events`() { | ||
val usdDenom = "usd" | ||
val nonUsdDenom = "non-usd" | ||
|
||
val navEventUsdVolume1Price1 = NavEvent.newBuilder() | ||
.setPriceAmount(1) | ||
.setPriceDenom(usdDenom) | ||
.setVolume(1L) | ||
.build() | ||
|
||
val navEventUsdVolume1Price12 = NavEvent.newBuilder() | ||
.setPriceAmount(12) | ||
.setPriceDenom(usdDenom) | ||
.setVolume(1L) | ||
.build() | ||
|
||
val navEventUsdVolume1Price123 = NavEvent.newBuilder() | ||
.setPriceAmount(123) | ||
.setPriceDenom(usdDenom) | ||
.setVolume(1L) | ||
.build() | ||
|
||
val navEventUsdVolume1Price1234 = NavEvent.newBuilder() | ||
.setPriceAmount(1234) | ||
.setPriceDenom(usdDenom) | ||
.setVolume(1L) | ||
.build() | ||
|
||
val navEventNonUsd = NavEvent.newBuilder() | ||
.setPriceAmount(1234) | ||
.setPriceDenom(nonUsdDenom) | ||
.setVolume(1L) | ||
.build() | ||
|
||
val navEventZeroVolume = NavEvent.newBuilder() | ||
.setPriceAmount(1234) | ||
.setPriceDenom(usdDenom) | ||
.setVolume(0L) | ||
.build() | ||
|
||
assertEquals(BigDecimal("0.001"), navEventUsdVolume1Price1.calculateUsdPricePerUnit(), "Price amount 1 should be converted to 0.001") | ||
assertEquals(BigDecimal("0.012"), navEventUsdVolume1Price12.calculateUsdPricePerUnit(), "Price amount 12 should be converted to 0.012") | ||
assertEquals(BigDecimal("0.123"), navEventUsdVolume1Price123.calculateUsdPricePerUnit(), "Price amount 123 should be converted to 0.123") | ||
assertEquals(BigDecimal("1.234"), navEventUsdVolume1Price1234.calculateUsdPricePerUnit(), "Price amount 1234 should be converted to 1.234") | ||
assertEquals(BigDecimal.ZERO, navEventNonUsd.calculateUsdPricePerUnit(), "Non-USD denomination should return 0") | ||
assertEquals(BigDecimal.ZERO, navEventZeroVolume.calculateUsdPricePerUnit(), "Zero volume should return 0") | ||
} | ||
} |