Browse Source

server: Add getUnspentOutputs method to XSNService

This is a part for #23.
prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
41abe0e741
  1. 25
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 1
      server/test/com/xsn/explorer/helpers/DummyXSNService.scala
  3. 38
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

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

@ -41,6 +41,8 @@ trait XSNService {
def getMasternodes(): FutureApplicationResult[List[rpc.Masternode]]
def getMasternode(ipAddress: IPAddress): FutureApplicationResult[rpc.Masternode]
def getUnspentOutputs(address: Address): FutureApplicationResult[JsValue]
}
class XSNServiceRPCImpl @Inject() (
@ -322,6 +324,29 @@ class XSNServiceRPCImpl @Inject() (
}
}
override def getUnspentOutputs(address: Address): FutureApplicationResult[JsValue] = {
val body = s"""
|{
| "jsonrpc": "1.0",
| "method": "getaddressutxos",
| "params": [
| { "addresses": ["${address.string}"] }
| ]
|}
|""".stripMargin
server
.post(body)
.map { response =>
val maybe = getResult[JsValue](response)
maybe.getOrElse {
logger.warn(s"Unexpected response from XSN Server, status = ${response.status}, response = ${response.body}")
Bad(XSNUnexpectedResponseError).accumulating
}
}
}
private def mapError(json: JsValue, errorCodeMapper: Map[Int, ApplicationError]): Option[ApplicationError] = {
val jsonErrorMaybe = (json \ "error")
.asOpt[JsValue]

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

@ -20,4 +20,5 @@ class DummyXSNService extends XSNService {
override def getMasternodeCount(): FutureApplicationResult[Int] = ???
override def getMasternodes(): FutureApplicationResult[List[rpc.Masternode]] = ???
override def getMasternode(ipAddress: IPAddress): FutureApplicationResult[Masternode] = ???
override def getUnspentOutputs(address: Address): FutureApplicationResult[JsValue] = ???
}

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

@ -544,4 +544,42 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
}
}
}
"getUnspentOutputs" should {
"get the results" in {
val content =
"""
|[
| {
| "address": "XeNEPsgeWqNbrEGEN5vqv4wYcC3qQrqNyp",
| "height": 22451,
| "outputIndex": 0,
| "satoshis": 1500000000000,
| "script": "76a914285b6f1ccacea0059ff5393cb4eb2f0569e2b3e988ac",
| "txid": "ea837f2011974b6a1a2fa077dc33684932c514a4ec6febc10e1a19ebe1336539"
| },
| {
| "address": "XeNEPsgeWqNbrEGEN5vqv4wYcC3qQrqNyp",
| "height": 25093,
| "outputIndex": 3,
| "satoshis": 2250000000,
| "script": "76a914285b6f1ccacea0059ff5393cb4eb2f0569e2b3e988ac",
| "txid": "96a06b802d1c15818a42aa9b46dd2e236cde746000d35f74d3eb940ab9d5694d"
| }
|]
""".stripMargin
val responseBody = createRPCSuccessfulResponse(Json.parse(content))
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))
val address = DataHelper.createAddress("XeNEPsgeWqNbrEGEN5vqv4wYcC3qQrqNyp")
whenReady(service.getUnspentOutputs(address)) { result =>
result mustEqual Good(Json.parse(content))
}
}
}
}

Loading…
Cancel
Save