Browse Source

server: Add estimateSmartFee method to the XSNService

bitcoin
Alexis Hernandez 6 years ago
parent
commit
092f3b7b94
  1. 23
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 1
      server/test/com/xsn/explorer/helpers/DummyXSNService.scala
  3. 14
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

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

@ -50,6 +50,8 @@ trait XSNService {
def isTPoSContract(txid: TransactionId): FutureApplicationResult[Boolean]
def estimateSmartFee(confirmationsTarget: Int): FutureApplicationResult[JsValue]
def cleanGenesisBlock(block: rpc.Block): rpc.Block = {
Option(block)
.filter(_.hash == genesisBlockhash)
@ -448,6 +450,27 @@ class XSNServiceRPCImpl @Inject() (
}
}
override def estimateSmartFee(confirmationsTarget: Int): FutureApplicationResult[JsValue] = {
val body = s"""
|{
| "jsonrpc": "1.0",
| "method": "estimatesmartfee",
| "params": [$confirmationsTarget]
|}
|""".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")

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

@ -26,4 +26,5 @@ class DummyXSNService extends XSNService {
override def getUnspentOutputs(address: Address): FutureApplicationResult[JsValue] = ???
override def sendRawTransaction(hex: HexString): FutureApplicationResult[String] = ???
override def isTPoSContract(txid: TransactionId): FutureApplicationResult[Boolean] = ???
override def estimateSmartFee(confirmationsTarget: Int): FutureApplicationResult[JsValue] = ???
}

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

@ -629,6 +629,20 @@ class XSNServiceRPCImplSpec extends WordSpec {
}
}
"estimateSmartFee" should {
"return the result" in {
val content = """{"feerate":0.00001,"blocks":2}"""
val responseBody = createRPCSuccessfulResponse(Json.parse(content))
val json = Json.parse(responseBody)
mockRequest(request, response)(200, json)
whenReady(service.estimateSmartFee(1)) { result =>
result mustEqual Good(Json.parse(content))
}
}
}
private def mockRequest(request: WSRequest, response: WSResponse)(status: Int, body: JsValue) = {
when(response.status).thenReturn(status)
when(response.json).thenReturn(body)

Loading…
Cancel
Save