Merge pull request #1174 from benthecarman/sats-the-standard

Allow getbalance to return in sats
This commit is contained in:
Ben Carman 2020-02-28 09:51:10 -06:00 committed by GitHub
commit 5dbff6a841
5 changed files with 62 additions and 15 deletions

View File

@ -91,8 +91,18 @@ object ConsoleCli {
),
cmd("getbalance")
.hidden()
.action((_, conf) => conf.copy(command = GetBalance))
.text("Get the wallet balance"),
.action((_, conf) => conf.copy(command = GetBalance(false)))
.text("Get the wallet balance")
.children(
opt[Unit]("sats")
.required()
.action((_, conf) =>
conf.copy(command = conf.command match {
case getBalance: GetBalance =>
getBalance.copy(isSats = true)
case other => other
}))
),
cmd("getnewaddress")
.hidden()
.action((_, conf) => conf.copy(command = GetNewAddress))
@ -230,8 +240,8 @@ object ConsoleCli {
}
val requestParam: RequestParam = config.command match {
case GetBalance =>
RequestParam("getbalance")
case GetBalance(isSats) =>
RequestParam("getbalance", Seq(up.writeJs(isSats)))
case GetNewAddress =>
RequestParam("getnewaddress")
case Rescan(addressBatchSize, startBlock, endBlock, force) =>
@ -358,7 +368,7 @@ object CliCommand {
case class SendToAddress(destination: BitcoinAddress, amount: Bitcoins)
extends CliCommand
case object GetNewAddress extends CliCommand
case object GetBalance extends CliCommand
case class GetBalance(isSats: Boolean) extends CliCommand
// Node
case object GetPeers extends CliCommand

View File

@ -1,7 +1,7 @@
package org.bitcoins
import org.bitcoins.core.protocol.{BitcoinAddress, BlockStamp}
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.currency.{Bitcoins, Satoshis}
import org.bitcoins.core.protocol.transaction.Transaction
import org.bitcoins.core.psbt.PSBT
import upickle.default._

View File

@ -198,11 +198,26 @@ class RoutesSpec
.returning(Future.successful(Bitcoins(50)))
val route =
walletRoutes.handleCommand(ServerCommand("getbalance", Arr()))
walletRoutes.handleCommand(
ServerCommand("getbalance", Arr(Bool(false))))
Get() ~> route ~> check {
contentType shouldEqual `application/json`
responseAs[String] shouldEqual """{"result":50,"error":null}"""
responseAs[String] shouldEqual """{"result":"50.00000000 BTC","error":null}"""
}
}
"return the wallet's balance in sats" in {
(mockWalletApi.getBalance: () => Future[CurrencyUnit])
.expects()
.returning(Future.successful(Bitcoins(50)))
val route =
walletRoutes.handleCommand(ServerCommand("getbalance", Arr(Bool(true))))
Get() ~> route ~> check {
contentType shouldEqual `application/json`
responseAs[String] shouldEqual """{"result":"5000000000 sats","error":null}"""
}
}

View File

@ -17,6 +17,18 @@ object ServerCommand {
implicit val rw: ReadWriter[ServerCommand] = macroRW
}
case class GetBalance(isSats: Boolean)
object GetBalance extends ServerJsonModels {
def fromJsArr(jsArr: ujson.Arr): Try[GetBalance] = {
require(jsArr.arr.size == 1,
s"Bad number of arguments: ${jsArr.arr.size}. Expected: 1")
Try(GetBalance(jsArr.arr.head.bool))
}
}
case class CombinePSBTs(psbts: Seq[PSBT])
object CombinePSBTs extends ServerJsonModels {

View File

@ -20,14 +20,24 @@ case class WalletRoutes(wallet: UnlockedWalletApi, node: Node)(
implicit val materializer = ActorMaterializer()
def handleCommand: PartialFunction[ServerCommand, StandardRoute] = {
case ServerCommand("getbalance", _) =>
complete {
wallet.getBalance().map { balance =>
Server.httpSuccess(
Bitcoins(balance.satoshis)
)
}
case ServerCommand("getbalance", arr) =>
GetBalance.fromJsArr(arr) match {
case Failure(exception) =>
reject(ValidationRejection("failure", Some(exception)))
case Success(GetBalance(isSats)) =>
complete {
wallet.getBalance().map { balance =>
Server.httpSuccess(
if (isSats) {
balance.satoshis.toString
} else {
Bitcoins(balance.satoshis).toString
}
)
}
}
}
case ServerCommand("getnewaddress", _) =>
complete {
wallet.getNewAddress().map { address =>