Browse Source

server: Add getRawTransaction method to XSNService

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
4e96219d77
  1. 18
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 2
      server/test/com/xsn/explorer/helpers/DummyXSNService.scala
  3. 18
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

18
server/app/com/xsn/explorer/services/XSNService.scala

@ -20,6 +20,8 @@ trait XSNService {
def getTransaction(txid: TransactionId): FutureApplicationResult[rpc.Transaction]
def getRawTransaction(txid: TransactionId): FutureApplicationResult[JsValue]
def getAddressBalance(address: Address): FutureApplicationResult[rpc.AddressBalance]
def getTransactions(address: Address): FutureApplicationResult[List[TransactionId]]
@ -68,6 +70,22 @@ class XSNServiceRPCImpl @Inject() (
}
}
override def getRawTransaction(txid: TransactionId): FutureApplicationResult[JsValue] = {
val errorCodeMapper = Map(-5 -> TransactionNotFoundError)
server
.post(s"""{ "jsonrpc": "1.0", "method": "getrawtransaction", "params": ["${txid.string}", 1] }""")
.map { response =>
val maybe = getResult[JsValue](response, errorCodeMapper)
maybe.getOrElse {
logger.warn(s"Unexpected response from XSN Server, txid = ${txid.string}, status = ${response.status}, response = ${response.body}")
Bad(XSNUnexpectedResponseError).accumulating
}
}
}
override def getAddressBalance(address: Address): FutureApplicationResult[rpc.AddressBalance] = {
val body = s"""
|{

2
server/test/com/xsn/explorer/helpers/DummyXSNService.scala

@ -4,10 +4,12 @@ import com.alexitc.playsonify.core.FutureApplicationResult
import com.xsn.explorer.models._
import com.xsn.explorer.models.rpc.Masternode
import com.xsn.explorer.services.XSNService
import play.api.libs.json.JsValue
class DummyXSNService extends XSNService {
override def getTransaction(txid: TransactionId): FutureApplicationResult[rpc.Transaction] = ???
override def getRawTransaction(txid: TransactionId): FutureApplicationResult[JsValue] = ???
override def getAddressBalance(address: Address): FutureApplicationResult[rpc.AddressBalance] = ???
override def getTransactions(address: Address): FutureApplicationResult[List[TransactionId]] = ???
override def getBlock(blockhash: Blockhash): FutureApplicationResult[rpc.Block] = ???

18
server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

@ -157,6 +157,24 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
}
}
"getRawTransaction" should {
"retrieve the raw transaction" in {
val txid = createTransactionId("024aba1d535cfe5dd3ea465d46a828a57b00e1df012d7a2d158e0f7484173f7c")
val expected = TransactionLoader.json(txid.string)
val responseBody = createRPCSuccessfulResponse(TransactionLoader.json(txid.string))
val json = Json.parse(responseBody)
when(response.status).thenReturn(200)
when(response.json).thenReturn(json)
when(request.post[String](anyString())(any())).thenReturn(Future.successful(response))
whenReady(service.getRawTransaction(txid)) { result =>
result mustEqual Good(expected)
}
}
}
"getAddressBalance" should {
"return the balance" in {
val responseBody =

Loading…
Cancel
Save