diff --git a/server/app/com/xsn/explorer/models/AddressBalance.scala b/server/app/com/xsn/explorer/models/AddressBalance.scala index 496d1f3..5e38750 100644 --- a/server/app/com/xsn/explorer/models/AddressBalance.scala +++ b/server/app/com/xsn/explorer/models/AddressBalance.scala @@ -1,24 +1,23 @@ package com.xsn.explorer.models +import com.xsn.explorer.util.Extensions.BigDecimalExt import play.api.libs.functional.syntax._ import play.api.libs.json._ -case class AddressBalance(balance: BigInt, received: BigInt) +case class AddressBalance(balance: BigDecimal, received: BigDecimal) object AddressBalance { + /** + * The RPC server is giving us these values in satoshis, we transform + * them to BigDecimal to match the format used by the application. + */ implicit val reads: Reads[AddressBalance] = { val builder = (__ \ 'balance).read[BigDecimal] and (__ \ 'received).read[BigDecimal] builder.apply { (balance, received) => - AddressBalance(balance.toBigInt(), received.toBigInt()) + AddressBalance(balance.fromSatoshis, received.fromSatoshis) } } - implicit val writes: Writes[AddressBalance] = Writes { obj => - val values = Map( - "balance" -> JsNumber(BigDecimal(obj.balance)), - "received" -> JsNumber(BigDecimal(obj.received))) - - JsObject.apply(values) - } + implicit val writes: Writes[AddressBalance] = Json.writes[AddressBalance] } diff --git a/server/app/com/xsn/explorer/models/AddressDetails.scala b/server/app/com/xsn/explorer/models/AddressDetails.scala index 4e56f79..b034cce 100644 --- a/server/app/com/xsn/explorer/models/AddressDetails.scala +++ b/server/app/com/xsn/explorer/models/AddressDetails.scala @@ -8,8 +8,8 @@ object AddressDetails { implicit val writes: Writes[AddressDetails] = Writes { obj => val values = Map( - "balance" -> JsNumber(BigDecimal(obj.balance.balance)), - "received" -> JsNumber(BigDecimal(obj.balance.received)), + "balance" -> JsNumber(obj.balance.balance), + "received" -> JsNumber(obj.balance.received), "transactionCount" -> JsNumber(obj.transactionCount)) JsObject.apply(values) diff --git a/server/app/com/xsn/explorer/util/Extensions.scala b/server/app/com/xsn/explorer/util/Extensions.scala new file mode 100644 index 0000000..c7f7ed1 --- /dev/null +++ b/server/app/com/xsn/explorer/util/Extensions.scala @@ -0,0 +1,12 @@ +package com.xsn.explorer.util + +object Extensions { + + private val SatoshiScale = 100000000L + + implicit class BigDecimalExt(val inner: BigDecimal) extends AnyVal { + def fromSatoshis: BigDecimal = { + inner / SatoshiScale + } + } +} diff --git a/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala b/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala index 76e35b9..0b975f4 100644 --- a/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala +++ b/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala @@ -237,7 +237,7 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures """ |{ | "result": { - | "balance": 24650100000000, + | "balance": 2465010000000000, | "received": 1060950100000000 | }, | "error": null, @@ -257,8 +257,8 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures result.isGood mustEqual true val balance = result.get - balance.balance mustEqual BigInt("24650100000000") - balance.received mustEqual BigInt("1060950100000000") + balance.balance mustEqual BigDecimal("24650100.00000000") + balance.received mustEqual BigDecimal("10609501.00000000") } } diff --git a/server/test/controllers/AddressesTransactionSpec.scala b/server/test/controllers/AddressesTransactionSpec.scala index 81557d5..3a1246a 100644 --- a/server/test/controllers/AddressesTransactionSpec.scala +++ b/server/test/controllers/AddressesTransactionSpec.scala @@ -18,7 +18,7 @@ import scala.concurrent.Future class AddressesTransactionSpec extends MyAPISpec { def addressDetails(balance: Int, received: Int, txCount: Int) = { - AddressDetails(AddressBalance(BigInt(balance), BigInt(received)), txCount) + AddressDetails(AddressBalance(BigDecimal(balance), BigDecimal(received)), txCount) } val addressEmpty = addressDetails(0, 0, 0) @@ -56,8 +56,8 @@ class AddressesTransactionSpec extends MyAPISpec { status(response) mustEqual OK val json = contentAsJson(response) - (json \ "balance").as[Int] mustEqual address.balance.balance.intValue() - (json \ "received").as[Int] mustEqual address.balance.received.intValue() + (json \ "balance").as[BigDecimal] mustEqual address.balance.balance + (json \ "received").as[BigDecimal] mustEqual address.balance.received (json \ "transactionCount").as[Int] mustEqual address.transactionCount }