Skip to content

Commit 9d35565

Browse files
committed
server: Add endpoint "POST /transactions"
This is a part for #26, it allows to push a hex-encoded transaction to the network.
1 parent 5313e76 commit 9d35565

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.xsn.explorer.models.request
2+
3+
import play.api.libs.json.{Json, Reads}
4+
5+
case class SendRawTransactionRequest(hex: String)
6+
7+
object SendRawTransactionRequest {
8+
9+
implicit val reads: Reads[SendRawTransactionRequest] = Json.reads[SendRawTransactionRequest]
10+
}

server/app/com/xsn/explorer/services/TransactionService.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import com.alexitc.playsonify.core.{FutureApplicationResult, FuturePaginatedResu
77
import com.alexitc.playsonify.models.PaginatedQuery
88
import com.alexitc.playsonify.validators.PaginatedQueryValidator
99
import com.xsn.explorer.data.async.TransactionFutureDataHandler
10-
import com.xsn.explorer.errors.{AddressFormatError, TransactionFormatError, TransactionNotFoundError}
10+
import com.xsn.explorer.errors.{AddressFormatError, InvalidRawTransactionError, TransactionFormatError, TransactionNotFoundError}
1111
import com.xsn.explorer.models._
1212
import com.xsn.explorer.models.rpc.TransactionVIN
1313
import org.scalactic.{Bad, Good, One, Or}
14-
import play.api.libs.json.JsValue
14+
import play.api.libs.json.{JsObject, JsString, JsValue}
1515

1616
import scala.concurrent.{ExecutionContext, Future}
1717

@@ -89,6 +89,15 @@ class TransactionService @Inject() (
8989
result.toFuture
9090
}
9191

92+
def sendRawTransaction(hexString: String): FutureApplicationResult[JsValue] = {
93+
val result = for {
94+
hex <- Or.from(HexString.from(hexString), One(InvalidRawTransactionError)).toFutureOr
95+
_ <- xsnService.sendRawTransaction(hex).toFutureOr
96+
} yield JsObject.empty + ("hex" -> JsString(hex.string))
97+
98+
result.toFuture
99+
}
100+
92101
private def getTransactionValue(vin: TransactionVIN): FutureApplicationResult[TransactionValue] = {
93102
val valueMaybe = for {
94103
value <- vin.value

server/app/controllers/TransactionsController.scala

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package controllers
22

33
import javax.inject.Inject
44

5+
import com.alexitc.playsonify.models.PublicContextWithModel
6+
import com.xsn.explorer.models.request.SendRawTransactionRequest
57
import com.xsn.explorer.services.TransactionService
68
import controllers.common.{MyJsonController, MyJsonControllerComponents}
79

@@ -17,4 +19,8 @@ class TransactionsController @Inject() (
1719
def getRawTransaction(txid: String) = publicNoInput { _ =>
1820
transactionService.getRawTransaction(txid)
1921
}
22+
23+
def sendRawTransaction() = publicWithInput { ctx: PublicContextWithModel[SendRawTransactionRequest] =>
24+
transactionService.sendRawTransaction(ctx.model.hex)
25+
}
2026
}

server/conf/routes

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ GET /health controllers.HealthController.check()
77

88
GET /transactions/:txid controllers.TransactionsController.getTransaction(txid: String)
99
GET /transactions/:txid/raw controllers.TransactionsController.getRawTransaction(txid: String)
10+
POST /transactions controllers.TransactionsController.sendRawTransaction()
1011

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

0 commit comments

Comments
 (0)