Skip to content

Commit

Permalink
server+wallet: add get balance rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 7, 2024
1 parent 055a681 commit a4b7289
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
37 changes: 29 additions & 8 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ use crate::{
CreateBmmCriticalDataTransactionResponse, CreateDepositTransactionRequest,
CreateDepositTransactionResponse, CreateNewAddressRequest, CreateNewAddressResponse,
CreateSidechainProposalRequest, CreateSidechainProposalResponse, GenerateBlocksRequest,
GenerateBlocksResponse, GetBlockHeaderInfoRequest, GetBlockHeaderInfoResponse,
GetBlockInfoRequest, GetBlockInfoResponse, GetBmmHStarCommitmentRequest,
GetBmmHStarCommitmentResponse, GetChainInfoRequest, GetChainInfoResponse,
GetChainTipRequest, GetChainTipResponse, GetCoinbasePsbtRequest,
GetCoinbasePsbtResponse, GetCtipRequest, GetCtipResponse, GetSidechainProposalsRequest,
GetSidechainProposalsResponse, GetSidechainsRequest, GetSidechainsResponse,
GetTwoWayPegDataRequest, GetTwoWayPegDataResponse, Network, SubscribeEventsRequest,
SubscribeEventsResponse,
GenerateBlocksResponse, GetBalanceRequest, GetBalanceResponse,
GetBlockHeaderInfoRequest, GetBlockHeaderInfoResponse, GetBlockInfoRequest,
GetBlockInfoResponse, GetBmmHStarCommitmentRequest, GetBmmHStarCommitmentResponse,
GetChainInfoRequest, GetChainInfoResponse, GetChainTipRequest, GetChainTipResponse,
GetCoinbasePsbtRequest, GetCoinbasePsbtResponse, GetCtipRequest, GetCtipResponse,
GetSidechainProposalsRequest, GetSidechainProposalsResponse, GetSidechainsRequest,
GetSidechainsResponse, GetTwoWayPegDataRequest, GetTwoWayPegDataResponse, Network,
SubscribeEventsRequest, SubscribeEventsResponse,
},
},
types::{Event, SidechainNumber},
Expand Down Expand Up @@ -904,6 +904,27 @@ impl WalletService for Arc<crate::wallet::Wallet> {
let response = CreateDepositTransactionResponse { txid: Some(txid) };
Ok(tonic::Response::new(response))
}

async fn get_balance(
&self,
request: tonic::Request<GetBalanceRequest>,
) -> Result<tonic::Response<GetBalanceResponse>, tonic::Status> {
let GetBalanceRequest {} = request.into_inner();

let balance = self
.get_wallet_balance()
.await
.map_err(|err| err.into_status())?;

let response = GetBalanceResponse {
confirmed_sats: balance.confirmed.to_sat(),
pending_sats: balance.immature.to_sat()
+ balance.trusted_pending.to_sat()
+ balance.untrusted_pending.to_sat(),
};

Ok(tonic::Response::new(response))
}
}

#[derive(Debug, Error)]
Expand Down
8 changes: 2 additions & 6 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,18 +970,14 @@ impl Wallet {
Ok(convert::bdk_txid_to_bitcoin_txid(txid))
}

pub fn get_balance(&self) -> Result<()> {
pub async fn get_wallet_balance(&self) -> Result<bdk_wallet::Balance> {
if self.last_sync.read().is_none() {
return Err(miette!("get balance: wallet not synced"));
}

let balance = self.bitcoin_wallet.lock().balance();

tracing::trace!("Confirmed: {}", balance.confirmed);
tracing::trace!("Immature: {}", balance.immature);
tracing::trace!("Untrusted pending: {}", balance.untrusted_pending);
tracing::trace!("Trusted pending: {}", balance.trusted_pending);
Ok(())
Ok(balance)
}

pub fn sync(&self) -> Result<()> {
Expand Down

0 comments on commit a4b7289

Please sign in to comment.