Skip to content

Commit

Permalink
server: Add endpoint "POST /transactions"
Browse files Browse the repository at this point in the history
This is a part for #26, it allows to push a hex-encoded
transaction to the network.
  • Loading branch information
AlexITC committed Jun 18, 2018
1 parent 5313e76 commit 9d35565
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xsn.explorer.models.request

import play.api.libs.json.{Json, Reads}

case class SendRawTransactionRequest(hex: String)

object SendRawTransactionRequest {

implicit val reads: Reads[SendRawTransactionRequest] = Json.reads[SendRawTransactionRequest]
}
13 changes: 11 additions & 2 deletions server/app/com/xsn/explorer/services/TransactionService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import com.alexitc.playsonify.core.{FutureApplicationResult, FuturePaginatedResu
import com.alexitc.playsonify.models.PaginatedQuery
import com.alexitc.playsonify.validators.PaginatedQueryValidator
import com.xsn.explorer.data.async.TransactionFutureDataHandler
import com.xsn.explorer.errors.{AddressFormatError, TransactionFormatError, TransactionNotFoundError}
import com.xsn.explorer.errors.{AddressFormatError, InvalidRawTransactionError, TransactionFormatError, TransactionNotFoundError}
import com.xsn.explorer.models._
import com.xsn.explorer.models.rpc.TransactionVIN
import org.scalactic.{Bad, Good, One, Or}
import play.api.libs.json.JsValue
import play.api.libs.json.{JsObject, JsString, JsValue}

import scala.concurrent.{ExecutionContext, Future}

Expand Down Expand Up @@ -89,6 +89,15 @@ class TransactionService @Inject() (
result.toFuture
}

def sendRawTransaction(hexString: String): FutureApplicationResult[JsValue] = {
val result = for {
hex <- Or.from(HexString.from(hexString), One(InvalidRawTransactionError)).toFutureOr
_ <- xsnService.sendRawTransaction(hex).toFutureOr
} yield JsObject.empty + ("hex" -> JsString(hex.string))

result.toFuture
}

private def getTransactionValue(vin: TransactionVIN): FutureApplicationResult[TransactionValue] = {
val valueMaybe = for {
value <- vin.value
Expand Down
6 changes: 6 additions & 0 deletions server/app/controllers/TransactionsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package controllers

import javax.inject.Inject

import com.alexitc.playsonify.models.PublicContextWithModel
import com.xsn.explorer.models.request.SendRawTransactionRequest
import com.xsn.explorer.services.TransactionService
import controllers.common.{MyJsonController, MyJsonControllerComponents}

Expand All @@ -17,4 +19,8 @@ class TransactionsController @Inject() (
def getRawTransaction(txid: String) = publicNoInput { _ =>
transactionService.getRawTransaction(txid)
}

def sendRawTransaction() = publicWithInput { ctx: PublicContextWithModel[SendRawTransactionRequest] =>
transactionService.sendRawTransaction(ctx.model.hex)
}
}
1 change: 1 addition & 0 deletions server/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GET /health controllers.HealthController.check()

GET /transactions/:txid controllers.TransactionsController.getTransaction(txid: String)
GET /transactions/:txid/raw controllers.TransactionsController.getRawTransaction(txid: String)
POST /transactions controllers.TransactionsController.sendRawTransaction()

GET /addresses/:address controllers.AddressesController.getDetails(address: String)
GET /addresses/:address/transactions controllers.AddressesController.getTransactions(address: String, offset: Int ?= 0, limit: Int ?= 10)
Expand Down

0 comments on commit 9d35565

Please sign in to comment.