Browse Source

server: Allow total and circulating supply to be missing

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
444b7d37b3
  1. 2
      server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala
  2. 4
      server/app/com/xsn/explorer/models/Statistics.scala
  3. 22
      server/app/com/xsn/explorer/models/StatisticsDetails.scala

2
server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala

@ -11,7 +11,7 @@ object StatisticsParsers {
val parseTotalSupply = get[BigDecimal]("total_supply")
val parseCirculatingSupply = get[BigDecimal]("circulating_supply")
val parseStatistics = (parseBlocks ~ parseTransactions ~ parseTotalSupply ~ parseCirculatingSupply).map {
val parseStatistics = (parseBlocks ~ parseTransactions ~ parseTotalSupply.? ~ parseCirculatingSupply.?).map {
case blocks ~ transactions ~ totalSupply ~ circulatingSupply =>
Statistics(blocks, transactions, totalSupply, circulatingSupply)
}

4
server/app/com/xsn/explorer/models/Statistics.scala

@ -5,8 +5,8 @@ import play.api.libs.json.{Json, Writes}
case class Statistics(
blocks: Int,
transactions: Int,
totalSupply: BigDecimal,
circulatingSupply: BigDecimal)
totalSupply: Option[BigDecimal],
circulatingSupply: Option[BigDecimal])
object Statistics {

22
server/app/com/xsn/explorer/models/StatisticsDetails.scala

@ -9,17 +9,19 @@ object StatisticsDetails {
implicit val writes: Writes[StatisticsDetails] = Writes { obj =>
val values = Map(
"blocks" -> JsNumber(obj.statistics.blocks),
"transactions" -> JsNumber(obj.statistics.transactions),
"totalSupply" -> JsNumber(obj.statistics.totalSupply),
"circulatingSupply" -> JsNumber(obj.statistics.circulatingSupply))
"transactions" -> JsNumber(obj.statistics.transactions))
val result = obj.masternodes
.map { count =>
values + ("masternodes" -> JsNumber(count))
}
.getOrElse {
values
}
val extras = List(
"totalSupply" -> obj.statistics.totalSupply.map(JsNumber.apply),
"circulatingSupply" -> obj.statistics.circulatingSupply.map(JsNumber.apply),
"masternodes" -> obj.masternodes.map(c => JsNumber.apply(c))
).flatMap { case (key, maybe) =>
maybe.map(key -> _)
}
val result = extras.foldLeft(values) { case (acc, value) =>
acc + value
}
JsObject.apply(result)
}

Loading…
Cancel
Save