Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigDecimal parsing #339

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.7.0"
version = "1.7.1"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal class WithdrawalCapacityProcessor(parser: ParserProtocol) : BaseProcess
if (limiterCapacityList.size != 2) {
return existing
}
var dailyLimit = parser.asDecimal(parser.asMap(limiterCapacityList[0])?.get("capacity"))
var weeklyLimit = parser.asDecimal(parser.asMap(limiterCapacityList[1])?.get("capacity"))
val dailyLimit = parser.asDecimal(parser.asMap(limiterCapacityList[0])?.get("capacity"))
val weeklyLimit = parser.asDecimal(parser.asMap(limiterCapacityList[1])?.get("capacity"))
if (dailyLimit != null && weeklyLimit != null) {
var maxWithdrawalCapacity: BigDecimal?
if (dailyLimit < weeklyLimit) {
Expand Down
8 changes: 7 additions & 1 deletion src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ class Parser : ParserProtocol {
val jsonLiteral = data as? JsonPrimitive
if (jsonLiteral != null) {
return if (jsonLiteral.isString) {
jsonLiteral.content.toBigDecimal(null, null)
try {
jsonLiteral.content.toBigDecimal(null, null)
} catch (e: Exception) {
Logger.e { "Failed to parse jsonLiteral: $jsonLiteral.content" }
Logger.e { "Exception: $e" }
null
}
} else {
jsonLiteral.doubleOrNull?.toBigDecimal(null, null)
}
Expand Down
37 changes: 37 additions & 0 deletions src/commonTest/kotlin/exchange.dydx.abacus/utils/ParserTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package exchange.dydx.abacus.utils

import com.ionspin.kotlin.bignum.decimal.BigDecimal
import kotlinx.serialization.json.JsonPrimitive
import kotlin.test.Test
import kotlin.test.assertEquals

class ParserTests {
@Test
fun testAsDecimal() {
val parser = Parser()

var x = parser.asDecimal(1000000000000000000L)
assertEquals(BigDecimal.fromLong(1000000000000000000), x)

x = parser.asDecimal(100)
assertEquals(BigDecimal.fromLong(100), x)

x = parser.asDecimal("1000000000000000000")
assertEquals(BigDecimal.fromLong(1000000000000000000), x)

x = parser.asDecimal(0.00000000000000000000002034002340)
assertEquals(BigDecimal.parseString("0.0000000000000000000000203400234"), x)

x = parser.asDecimal("0.00000000000000000000002034002340")
assertEquals(BigDecimal.parseString("0.0000000000000000000000203400234"), x)

x = parser.asDecimal("invalid")
assertEquals(null, x)

x = parser.asDecimal(JsonPrimitive("1000000000000000000"))
assertEquals(BigDecimal.fromLong(1000000000000000000), x)

x = parser.asDecimal(JsonPrimitive("invalid"))
assertEquals(null, x)
}
}
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.7.0'
spec.version = '1.7.1'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
Loading