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 parseTotalSupply = get[BigDecimal]("total_supply")
val parseCirculatingSupply = get[BigDecimal]("circulating_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 => case blocks ~ transactions ~ totalSupply ~ circulatingSupply =>
Statistics(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( case class Statistics(
blocks: Int, blocks: Int,
transactions: Int, transactions: Int,
totalSupply: BigDecimal, totalSupply: Option[BigDecimal],
circulatingSupply: BigDecimal) circulatingSupply: Option[BigDecimal])
object Statistics { object Statistics {

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

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

Loading…
Cancel
Save