|
| 1 | +package io.iohk.ethereum.jsonrpc |
| 2 | + |
| 3 | +import io.iohk.ethereum.jsonrpc.EthBlocksService._ |
| 4 | +import io.iohk.ethereum.jsonrpc.JsonRpcError.InvalidParams |
| 5 | +import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodDecoder} |
| 6 | +import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder.NoParamsMethodDecoder |
| 7 | +import org.json4s.Extraction |
| 8 | +import org.json4s.JsonAST.{JArray, JBool, JField, JString, JValue} |
| 9 | + |
| 10 | +object EthBlocksJsonMethodsImplicits extends JsonMethodsImplicits { |
| 11 | + implicit val eth_blockNumber = new NoParamsMethodDecoder(BestBlockNumberRequest()) |
| 12 | + with JsonEncoder[BestBlockNumberResponse] { |
| 13 | + override def encodeJson(t: BestBlockNumberResponse): JValue = Extraction.decompose(t.bestBlockNumber) |
| 14 | + } |
| 15 | + |
| 16 | + implicit val eth_getBlockTransactionCountByHash = new JsonMethodDecoder[TxCountByBlockHashRequest] |
| 17 | + with JsonEncoder[TxCountByBlockHashResponse] { |
| 18 | + override def decodeJson(params: Option[JArray]): Either[JsonRpcError, TxCountByBlockHashRequest] = |
| 19 | + params match { |
| 20 | + case Some(JArray(JString(input) :: Nil)) => |
| 21 | + extractHash(input).map(TxCountByBlockHashRequest) |
| 22 | + case _ => Left(InvalidParams()) |
| 23 | + } |
| 24 | + |
| 25 | + override def encodeJson(t: TxCountByBlockHashResponse): JValue = |
| 26 | + Extraction.decompose(t.txsQuantity.map(BigInt(_))) |
| 27 | + } |
| 28 | + |
| 29 | + implicit val eth_getBlockByHash = new JsonMethodDecoder[BlockByBlockHashRequest] |
| 30 | + with JsonEncoder[BlockByBlockHashResponse] { |
| 31 | + override def decodeJson(params: Option[JArray]): Either[JsonRpcError, BlockByBlockHashRequest] = { |
| 32 | + params match { |
| 33 | + case Some(JArray(JString(blockHash) :: JBool(fullTxs) :: Nil)) => |
| 34 | + extractHash(blockHash).map(BlockByBlockHashRequest(_, fullTxs)) |
| 35 | + case _ => Left(InvalidParams()) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override def encodeJson(t: BlockByBlockHashResponse): JValue = |
| 40 | + Extraction.decompose(t.blockResponse) |
| 41 | + } |
| 42 | + |
| 43 | + implicit val eth_getBlockByNumber = new JsonMethodDecoder[BlockByNumberRequest] |
| 44 | + with JsonEncoder[BlockByNumberResponse] { |
| 45 | + override def decodeJson(params: Option[JArray]): Either[JsonRpcError, BlockByNumberRequest] = { |
| 46 | + params match { |
| 47 | + case Some(JArray(blockStr :: JBool(fullTxs) :: Nil)) => |
| 48 | + extractBlockParam(blockStr).map(BlockByNumberRequest(_, fullTxs)) |
| 49 | + case _ => Left(InvalidParams()) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + override def encodeJson(t: BlockByNumberResponse): JValue = |
| 54 | + Extraction.decompose(t.blockResponse) |
| 55 | + } |
| 56 | + |
| 57 | + implicit val eth_getUncleByBlockHashAndIndex = new JsonMethodDecoder[UncleByBlockHashAndIndexRequest] |
| 58 | + with JsonEncoder[UncleByBlockHashAndIndexResponse] { |
| 59 | + override def decodeJson(params: Option[JArray]): Either[JsonRpcError, UncleByBlockHashAndIndexRequest] = |
| 60 | + params match { |
| 61 | + case Some(JArray(JString(blockHash) :: uncleIndex :: Nil)) => |
| 62 | + for { |
| 63 | + hash <- extractHash(blockHash) |
| 64 | + uncleBlockIndex <- extractQuantity(uncleIndex) |
| 65 | + } yield UncleByBlockHashAndIndexRequest(hash, uncleBlockIndex) |
| 66 | + case _ => Left(InvalidParams()) |
| 67 | + } |
| 68 | + |
| 69 | + override def encodeJson(t: UncleByBlockHashAndIndexResponse): JValue = { |
| 70 | + val uncleBlockResponse = Extraction.decompose(t.uncleBlockResponse) |
| 71 | + uncleBlockResponse.removeField { |
| 72 | + case JField("transactions", _) => true |
| 73 | + case _ => false |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + implicit val eth_getUncleByBlockNumberAndIndex = new JsonMethodDecoder[UncleByBlockNumberAndIndexRequest] |
| 79 | + with JsonEncoder[UncleByBlockNumberAndIndexResponse] { |
| 80 | + override def decodeJson(params: Option[JArray]): Either[JsonRpcError, UncleByBlockNumberAndIndexRequest] = |
| 81 | + params match { |
| 82 | + case Some(JArray(blockStr :: uncleIndex :: Nil)) => |
| 83 | + for { |
| 84 | + block <- extractBlockParam(blockStr) |
| 85 | + uncleBlockIndex <- extractQuantity(uncleIndex) |
| 86 | + } yield UncleByBlockNumberAndIndexRequest(block, uncleBlockIndex) |
| 87 | + case _ => Left(InvalidParams()) |
| 88 | + } |
| 89 | + |
| 90 | + override def encodeJson(t: UncleByBlockNumberAndIndexResponse): JValue = { |
| 91 | + val uncleBlockResponse = Extraction.decompose(t.uncleBlockResponse) |
| 92 | + uncleBlockResponse.removeField { |
| 93 | + case JField("transactions", _) => true |
| 94 | + case _ => false |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + implicit val eth_getUncleCountByBlockNumber = new JsonMethodDecoder[GetUncleCountByBlockNumberRequest] |
| 100 | + with JsonEncoder[GetUncleCountByBlockNumberResponse] { |
| 101 | + def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetUncleCountByBlockNumberRequest] = |
| 102 | + params match { |
| 103 | + case Some(JArray((blockValue: JValue) :: Nil)) => |
| 104 | + for { |
| 105 | + block <- extractBlockParam(blockValue) |
| 106 | + } yield GetUncleCountByBlockNumberRequest(block) |
| 107 | + case _ => Left(InvalidParams()) |
| 108 | + } |
| 109 | + |
| 110 | + def encodeJson(t: GetUncleCountByBlockNumberResponse): JValue = encodeAsHex(t.result) |
| 111 | + } |
| 112 | + |
| 113 | + implicit val eth_getUncleCountByBlockHash = new JsonMethodDecoder[GetUncleCountByBlockHashRequest] |
| 114 | + with JsonEncoder[GetUncleCountByBlockHashResponse] { |
| 115 | + def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetUncleCountByBlockHashRequest] = |
| 116 | + params match { |
| 117 | + case Some(JArray(JString(hash) :: Nil)) => |
| 118 | + for { |
| 119 | + blockHash <- extractHash(hash) |
| 120 | + } yield GetUncleCountByBlockHashRequest(blockHash) |
| 121 | + case _ => Left(InvalidParams()) |
| 122 | + } |
| 123 | + |
| 124 | + def encodeJson(t: GetUncleCountByBlockHashResponse): JValue = encodeAsHex(t.result) |
| 125 | + } |
| 126 | + |
| 127 | + implicit val eth_getBlockTransactionCountByNumber = new JsonMethodDecoder[GetBlockTransactionCountByNumberRequest] |
| 128 | + with JsonEncoder[GetBlockTransactionCountByNumberResponse] { |
| 129 | + def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetBlockTransactionCountByNumberRequest] = |
| 130 | + params match { |
| 131 | + case Some(JArray((blockValue: JValue) :: Nil)) => |
| 132 | + for { |
| 133 | + block <- extractBlockParam(blockValue) |
| 134 | + } yield GetBlockTransactionCountByNumberRequest(block) |
| 135 | + case _ => Left(InvalidParams()) |
| 136 | + } |
| 137 | + |
| 138 | + def encodeJson(t: GetBlockTransactionCountByNumberResponse): JValue = encodeAsHex(t.result) |
| 139 | + } |
| 140 | +} |
0 commit comments