Browse Source

server: Update AddressBalance to use BigDecimal values instead of BigInt

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
f2beb40fa2
  1. 17
      server/app/com/xsn/explorer/models/AddressBalance.scala
  2. 4
      server/app/com/xsn/explorer/models/AddressDetails.scala
  3. 12
      server/app/com/xsn/explorer/util/Extensions.scala
  4. 6
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala
  5. 6
      server/test/controllers/AddressesTransactionSpec.scala

17
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]
}

4
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)

12
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
}
}
}

6
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")
}
}

6
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
}

Loading…
Cancel
Save