Alexis Hernandez
7 years ago
4 changed files with 99 additions and 0 deletions
@ -0,0 +1,36 @@ |
|||||
|
package com.xsn.explorer.models |
||||
|
|
||||
|
import play.api.libs.functional.syntax._ |
||||
|
import play.api.libs.json._ |
||||
|
|
||||
|
case class Transaction( |
||||
|
id: TransactionId, |
||||
|
size: Int, |
||||
|
blockhash: Blockhash, |
||||
|
time: Long, |
||||
|
blocktime: Long, |
||||
|
confirmations: Int, |
||||
|
vin: Option[TransactionVIN], |
||||
|
vout: List[TransactionVOUT], |
||||
|
) |
||||
|
|
||||
|
object Transaction { |
||||
|
|
||||
|
implicit val reads: Reads[Transaction] = { |
||||
|
val builder = (__ \ 'txid).read[TransactionId] and |
||||
|
(__ \ 'size).read[Int] and |
||||
|
(__ \ 'blockhash).read[Blockhash] and |
||||
|
(__ \ 'time).read[Long] and |
||||
|
(__ \ 'blocktime).read[Long] and |
||||
|
(__ \ 'confirmations).read[Int] and |
||||
|
(__ \ 'vout).read[List[TransactionVOUT]] and |
||||
|
(__ \ 'vin).readNullable[List[JsValue]] |
||||
|
.map(_ getOrElse List.empty) |
||||
|
.map { list => list.flatMap(_.asOpt[TransactionVIN]) } |
||||
|
.map(_.headOption) |
||||
|
|
||||
|
builder.apply { (id, size, blockHash, time, blockTime, confirmations, vout, vin) => |
||||
|
Transaction(id, size, blockHash, time, blockTime, confirmations, vin, vout) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package com.xsn.explorer.models |
||||
|
|
||||
|
import play.api.libs.functional.syntax._ |
||||
|
import play.api.libs.json.{Reads, __} |
||||
|
|
||||
|
case class TransactionVIN(txid: TransactionId, voutIndex: Int) |
||||
|
|
||||
|
object TransactionVIN { |
||||
|
|
||||
|
implicit val reads: Reads[TransactionVIN] = { |
||||
|
val builder = (__ \ 'txid).read[TransactionId] and (__ \ 'vout).read[Int] |
||||
|
|
||||
|
builder.apply { (txid, index) => |
||||
|
TransactionVIN(txid, index) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package com.xsn.explorer.models |
||||
|
|
||||
|
import play.api.libs.functional.syntax._ |
||||
|
import play.api.libs.json.{JsObject, Reads, __} |
||||
|
|
||||
|
case class TransactionVOUT( |
||||
|
value: BigDecimal, |
||||
|
n: Int, |
||||
|
scriptPubKeyType: String, |
||||
|
address: Option[Address]) |
||||
|
|
||||
|
object TransactionVOUT { |
||||
|
|
||||
|
implicit val reads: Reads[TransactionVOUT] = { |
||||
|
val builder = (__ \ 'value).read[BigDecimal] and |
||||
|
(__ \ 'n).read[Int] and |
||||
|
(__ \ 'scriptPubKey).read[JsObject].map { json => |
||||
|
val t = (json \ "type").as[String] |
||||
|
val a = (json \ "addresses") |
||||
|
.asOpt[List[String]] |
||||
|
.flatMap(_.headOption) |
||||
|
.flatMap(Address.from) |
||||
|
|
||||
|
(t, a) |
||||
|
} |
||||
|
|
||||
|
builder.apply { (value, n, tuple) => |
||||
|
val (scriptType, address) = tuple |
||||
|
TransactionVOUT(value, n, scriptType, address) |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue