Browse Source

server: Add endpoint "POST /transactions"

This is a part for #26, it allows to push a hex-encoded
transaction to the network.
prometheus-integration v2018.06.17
Alexis Hernandez 7 years ago
parent
commit
9d355650cf
  1. 10
      server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala
  2. 13
      server/app/com/xsn/explorer/services/TransactionService.scala
  3. 6
      server/app/controllers/TransactionsController.scala
  4. 1
      server/conf/routes

10
server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala

@ -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
server/app/com/xsn/explorer/services/TransactionService.scala

@ -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}
@ -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

6
server/app/controllers/TransactionsController.scala

@ -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}
@ -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
server/conf/routes

@ -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)

Loading…
Cancel
Save