Alexis Hernandez
7 years ago
5 changed files with 28 additions and 17 deletions
@ -1,24 +1,23 @@ |
|||||
package com.xsn.explorer.models |
package com.xsn.explorer.models |
||||
|
|
||||
|
import com.xsn.explorer.util.Extensions.BigDecimalExt |
||||
import play.api.libs.functional.syntax._ |
import play.api.libs.functional.syntax._ |
||||
import play.api.libs.json._ |
import play.api.libs.json._ |
||||
|
|
||||
case class AddressBalance(balance: BigInt, received: BigInt) |
case class AddressBalance(balance: BigDecimal, received: BigDecimal) |
||||
|
|
||||
object AddressBalance { |
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] = { |
implicit val reads: Reads[AddressBalance] = { |
||||
val builder = (__ \ 'balance).read[BigDecimal] and (__ \ 'received).read[BigDecimal] |
val builder = (__ \ 'balance).read[BigDecimal] and (__ \ 'received).read[BigDecimal] |
||||
|
|
||||
builder.apply { (balance, received) => |
builder.apply { (balance, received) => |
||||
AddressBalance(balance.toBigInt(), received.toBigInt()) |
AddressBalance(balance.fromSatoshis, received.fromSatoshis) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
implicit val writes: Writes[AddressBalance] = Writes { obj => |
implicit val writes: Writes[AddressBalance] = Json.writes[AddressBalance] |
||||
val values = Map( |
|
||||
"balance" -> JsNumber(BigDecimal(obj.balance)), |
|
||||
"received" -> JsNumber(BigDecimal(obj.received))) |
|
||||
|
|
||||
JsObject.apply(values) |
|
||||
} |
|
||||
} |
} |
||||
|
@ -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 |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue