Skip to content

Commit

Permalink
onchain walet sync
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyhodl committed Dec 17, 2024
1 parent 1d3e21a commit efc100f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ddk-node/src/cli_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum WalletCommand {
/// Fee rate in sats/vbyte
fee_rate: u64,
},
#[command(about = "Sync the on-chain wallet.")]
Sync,
}

#[derive(Clone, Debug, Subcommand)]
Expand Down
16 changes: 8 additions & 8 deletions ddk-node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ddkrpc::{
AcceptOfferRequest, ConnectRequest, GetWalletTransactionsRequest, InfoRequest,
ListContractsRequest, ListOffersRequest, ListOraclesRequest, ListPeersRequest,
ListUtxosRequest, NewAddressRequest, OracleAnnouncementsRequest, SendOfferRequest, SendRequest,
WalletBalanceRequest,
WalletBalanceRequest, WalletSyncRequest,
};
use anyhow::anyhow;
use bitcoin::Transaction;
Expand Down Expand Up @@ -172,8 +172,6 @@ pub async fn cli_command(
})
.await?
.into_inner();
let offer_hex = hex::encode(&offer.offer_dlc);
println!("{}", offer_hex);
let offer_dlc: OfferDlc = serde_json::from_slice(&offer.offer_dlc)?;
let offer = serde_json::to_string_pretty(&offer_dlc)?;
print!("{}", offer);
Expand All @@ -195,8 +193,6 @@ pub async fn cli_command(
.await?
.into_inner();
let accept_dlc: AcceptDlc = serde_json::from_slice(&accept.accept_dlc)?;
let accept_hex = hex::encode(&accept.accept_dlc);
println!("{}", accept_hex);
let accept_dlc = serde_json::to_string_pretty(&accept_dlc)?;
print!("{}", accept_dlc)
}
Expand All @@ -222,15 +218,15 @@ pub async fn cli_command(
.await?
.into_inner();
let pretty_string = serde_json::to_string_pretty(&balance)?;
println!("{}", pretty_string);
print!("{}", pretty_string);
}
WalletCommand::NewAddress => {
let address = client
.new_address(NewAddressRequest::default())
.await?
.into_inner();
let pretty_string = serde_json::to_string_pretty(&address)?;
println!("{}", pretty_string);
print!("{}", pretty_string);
}
WalletCommand::Transactions => {
let transactions = client
Expand Down Expand Up @@ -273,6 +269,10 @@ pub async fn cli_command(
.into_inner();
print!("{}", serde_json::to_string_pretty(&txid)?)
}
WalletCommand::Sync => {
let _ = client.wallet_sync(WalletSyncRequest {}).await?.into_inner();
println!("Wallet synced.")
}
},
CliCommand::Oracle(command) => match command {
OracleCommand::Announcements => {
Expand Down Expand Up @@ -303,7 +303,7 @@ pub async fn cli_command(
host: parts[1].to_string(),
})
.await?;
println!("Connected to {}", parts[0])
print!("Connected to {}", parts[0])
}
}

Expand Down
81 changes: 81 additions & 0 deletions ddk-node/src/ddkrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ pub struct OracleAnnouncementsResponse {
#[prost(bytes = "vec", repeated, tag = "1")]
pub announcements: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec<u8>>,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct WalletSyncRequest {}
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct WalletSyncResponse {}
/// Generated client implementations.
pub mod ddk_rpc_client {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
Expand Down Expand Up @@ -410,6 +418,28 @@ pub mod ddk_rpc_client {
.insert(GrpcMethod::new("ddkrpc.DdkRpc", "WalletBalance"));
self.inner.unary(req, path, codec).await
}
pub async fn wallet_sync(
&mut self,
request: impl tonic::IntoRequest<super::WalletSyncRequest>,
) -> std::result::Result<
tonic::Response<super::WalletSyncResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static("/ddkrpc.DdkRpc/WalletSync");
let mut req = request.into_request();
req.extensions_mut().insert(GrpcMethod::new("ddkrpc.DdkRpc", "WalletSync"));
self.inner.unary(req, path, codec).await
}
pub async fn get_wallet_transactions(
&mut self,
request: impl tonic::IntoRequest<super::GetWalletTransactionsRequest>,
Expand Down Expand Up @@ -644,6 +674,13 @@ pub mod ddk_rpc_server {
tonic::Response<super::WalletBalanceResponse>,
tonic::Status,
>;
async fn wallet_sync(
&self,
request: tonic::Request<super::WalletSyncRequest>,
) -> std::result::Result<
tonic::Response<super::WalletSyncResponse>,
tonic::Status,
>;
async fn get_wallet_transactions(
&self,
request: tonic::Request<super::GetWalletTransactionsRequest>,
Expand Down Expand Up @@ -1042,6 +1079,50 @@ pub mod ddk_rpc_server {
};
Box::pin(fut)
}
"/ddkrpc.DdkRpc/WalletSync" => {
#[allow(non_camel_case_types)]
struct WalletSyncSvc<T: DdkRpc>(pub Arc<T>);
impl<T: DdkRpc> tonic::server::UnaryService<super::WalletSyncRequest>
for WalletSyncSvc<T> {
type Response = super::WalletSyncResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::WalletSyncRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as DdkRpc>::wallet_sync(&inner, request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = WalletSyncSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/ddkrpc.DdkRpc/GetWalletTransactions" => {
#[allow(non_camel_case_types)]
struct GetWalletTransactionsSvc<T: DdkRpc>(pub Arc<T>);
Expand Down
13 changes: 12 additions & 1 deletion ddk-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ddkrpc::{
ListOraclesResponse, ListPeersRequest, ListPeersResponse, ListUtxosRequest, ListUtxosResponse,
NewAddressRequest, NewAddressResponse, OracleAnnouncementsRequest, OracleAnnouncementsResponse,
Peer, SendOfferRequest, SendOfferResponse, SendRequest, SendResponse, WalletBalanceRequest,
WalletBalanceResponse,
WalletBalanceResponse, WalletSyncRequest, WalletSyncResponse,
};
use ddkrpc::{InfoRequest, InfoResponse};
use opts::NodeOpts;
Expand Down Expand Up @@ -359,4 +359,15 @@ impl DdkRpc for DdkNode {
.collect();
Ok(Response::new(OracleAnnouncementsResponse { announcements }))
}

async fn wallet_sync(
&self,
_request: Request<WalletSyncRequest>,
) -> Result<Response<WalletSyncResponse>, Status> {
self.node
.wallet
.sync()
.map_err(|_| Status::new(Code::Aborted, "Did not sync wallet."))?;
Ok(Response::new(WalletSyncResponse {}))
}
}
4 changes: 4 additions & 0 deletions ddk-node/src/proto/ddkrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service DdkRpc {
rpc ListOffers (ListOffersRequest) returns (ListOffersResponse);
rpc NewAddress (NewAddressRequest) returns (NewAddressResponse);
rpc WalletBalance (WalletBalanceRequest) returns (WalletBalanceResponse);
rpc WalletSync (WalletSyncRequest) returns (WalletSyncResponse);
rpc GetWalletTransactions (GetWalletTransactionsRequest) returns (GetWalletTransactionsResponse);
rpc ListUtxos (ListUtxosRequest) returns (ListUtxosResponse);
rpc ListPeers (ListPeersRequest) returns (ListPeersResponse);
Expand Down Expand Up @@ -122,3 +123,6 @@ message OracleAnnouncementsRequest {}
message OracleAnnouncementsResponse {
repeated bytes announcements = 1;
}

message WalletSyncRequest {}
message WalletSyncResponse {}

0 comments on commit efc100f

Please sign in to comment.