Skip to content

Commit

Permalink
move transaction-related calls from EthService to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaap van der Plas committed Jan 15, 2021
1 parent e569bd1 commit b0c7f4e
Show file tree
Hide file tree
Showing 16 changed files with 976 additions and 778 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.iohk.ethereum.jsonrpc.AkkaTaskOps.TaskActorOps
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
import io.iohk.ethereum.ommers.OmmersPool
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
import io.iohk.ethereum.transactions.TransactionPicker
import monix.eval.Task
import scala.concurrent.duration.FiniteDuration

Expand Down
103 changes: 0 additions & 103 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthJsonMethodsImplicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ object EthJsonMethodsImplicits extends JsonMethodsImplicits {
override def encodeJson(t: SubmitHashRateResponse): JValue = JBool(t.success)
}

implicit val eth_gasPrice = new NoParamsMethodDecoder(GetGasPriceRequest()) with JsonEncoder[GetGasPriceResponse] {
override def encodeJson(t: GetGasPriceResponse): JValue = encodeAsHex(t.price)
}

implicit val eth_mining = new NoParamsMethodDecoder(GetMiningRequest()) with JsonEncoder[GetMiningResponse] {
override def encodeJson(t: GetMiningResponse): JValue = JBool(t.isMining)
}
Expand Down Expand Up @@ -131,91 +127,6 @@ object EthJsonMethodsImplicits extends JsonMethodsImplicits {
Extraction.decompose(t.blockResponse)
}

implicit val eth_pendingTransactions = new NoParamsMethodDecoder(EthPendingTransactionsRequest())
with JsonEncoder[EthPendingTransactionsResponse] {

override def encodeJson(t: EthPendingTransactionsResponse): JValue =
JArray(t.pendingTransactions.toList.map { pendingTx =>
encodeAsHex(pendingTx.stx.tx.hash)
})
}

implicit val eth_getTransactionByHash =
new JsonMethodDecoder[GetTransactionByHashRequest] with JsonEncoder[GetTransactionByHashResponse] {
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetTransactionByHashRequest] =
params match {
case Some(JArray(JString(txHash) :: Nil)) =>
for {
parsedTxHash <- extractHash(txHash)
} yield GetTransactionByHashRequest(parsedTxHash)
case _ => Left(InvalidParams())
}

override def encodeJson(t: GetTransactionByHashResponse): JValue =
JsonEncoder.encode(t.txResponse)
}

implicit val eth_getTransactionReceipt =
new JsonMethodDecoder[GetTransactionReceiptRequest] with JsonEncoder[GetTransactionReceiptResponse] {
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetTransactionReceiptRequest] =
params match {
case Some(JArray(JString(txHash) :: Nil)) =>
for {
parsedTxHash <- extractHash(txHash)
} yield GetTransactionReceiptRequest(parsedTxHash)
case _ => Left(InvalidParams())
}

override def encodeJson(t: GetTransactionReceiptResponse): JValue =
Extraction.decompose(t.txResponse)
}

implicit val GetTransactionByBlockHashAndIndexResponseEncoder =
new JsonEncoder[GetTransactionByBlockHashAndIndexResponse] {
override def encodeJson(t: GetTransactionByBlockHashAndIndexResponse): JValue =
JsonEncoder.encode(t.transactionResponse)
}

implicit val GetTransactionByBlockHashAndIndexRequestDecoder =
new JsonMethodDecoder[GetTransactionByBlockHashAndIndexRequest] {
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetTransactionByBlockHashAndIndexRequest] =
params match {
case Some(JArray(JString(blockHash) :: transactionIndex :: Nil)) =>
for {
parsedBlockHash <- extractHash(blockHash)
parsedTransactionIndex <- extractQuantity(transactionIndex)
} yield GetTransactionByBlockHashAndIndexRequest(parsedBlockHash, parsedTransactionIndex)
case _ => Left(InvalidParams())
}
}

implicit val GetTransactionByBlockNumberAndIndexResponseEncoder =
new JsonEncoder[GetTransactionByBlockNumberAndIndexResponse] {
override def encodeJson(t: GetTransactionByBlockNumberAndIndexResponse): JValue =
JsonEncoder.encode(t.transactionResponse)
}

implicit val GetTransactionByBlockNumberAndIndexRequestDecoder =
new JsonMethodDecoder[GetTransactionByBlockNumberAndIndexRequest] {
override def decodeJson(
params: Option[JArray]
): Either[JsonRpcError, GetTransactionByBlockNumberAndIndexRequest] =
params match {
case Some(JArray(blockParam :: transactionIndex :: Nil)) =>
for {
blockParam <- extractBlockParam(blockParam)
parsedTransactionIndex <- extractQuantity(transactionIndex)
} yield GetTransactionByBlockNumberAndIndexRequest(blockParam, parsedTransactionIndex)
case _ => Left(InvalidParams())
}
}

implicit val RawTransactionResponseJsonEncoder: JsonEncoder[RawTransactionResponse] =
new JsonEncoder[RawTransactionResponse] {
override def encodeJson(t: RawTransactionResponse): JValue =
t.transactionResponse.map(RawTransactionCodec.asRawTransaction _ andThen encodeAsHex)
}

implicit val eth_getUncleByBlockHashAndIndex = new JsonMethodDecoder[UncleByBlockHashAndIndexRequest]
with JsonEncoder[UncleByBlockHashAndIndexResponse] {
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, UncleByBlockHashAndIndexRequest] =
Expand Down Expand Up @@ -265,20 +176,6 @@ object EthJsonMethodsImplicits extends JsonMethodsImplicits {
}
}

implicit val eth_sendRawTransaction = new JsonMethodDecoder[SendRawTransactionRequest]
with JsonEncoder[SendRawTransactionResponse] {
def decodeJson(params: Option[JArray]): Either[JsonRpcError, SendRawTransactionRequest] =
params match {
case Some(JArray(JString(dataStr) :: Nil)) =>
for {
data <- extractBytes(dataStr)
} yield SendRawTransactionRequest(data)
case _ => Left(InvalidParams())
}

def encodeJson(t: SendRawTransactionResponse): JValue = encodeAsHex(t.transactionHash)
}

implicit val eth_sendTransaction = new JsonMethodCodec[SendTransactionRequest, SendTransactionResponse] {
def decodeJson(params: Option[JArray]): Either[JsonRpcError, SendTransactionRequest] =
params match {
Expand Down
Loading

0 comments on commit b0c7f4e

Please sign in to comment.