Skip to content

Commit a154632

Browse files
committed
Cli: send encoded funding info
1 parent 4e171c6 commit a154632

6 files changed

Lines changed: 96 additions & 63 deletions

File tree

src/bus/ctl.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,31 @@ fn format_keys(keys: &Keys) -> String {
236236
format!("sk: {}, pk: {}", keys.0.display_secret(), keys.1,)
237237
}
238238

239-
#[derive(Clone, Debug, Display, NetworkDecode, NetworkEncode)]
239+
#[cfg_attr(feature = "serde", serde_as)]
240+
#[derive(Clone, Debug, Display, Eq, PartialEq, NetworkDecode, NetworkEncode)]
241+
#[cfg_attr(
242+
feature = "serde",
243+
derive(Serialize, Deserialize),
244+
serde(crate = "serde_crate")
245+
)]
240246
pub enum FundingInfo {
241-
#[display("bitcoin(..)")]
247+
#[display("{0}")]
242248
Bitcoin(BitcoinFundingInfo),
243-
#[display("monero(..)")]
249+
#[display("{0}")]
244250
Monero(MoneroFundingInfo),
245251
}
246252

247-
#[derive(Clone, Debug, NetworkDecode, NetworkEncode)]
253+
#[cfg_attr(feature = "serde", serde_as)]
254+
#[derive(Clone, Debug, Eq, PartialEq, NetworkDecode, NetworkEncode)]
255+
#[cfg_attr(
256+
feature = "serde",
257+
derive(Serialize, Deserialize),
258+
serde(crate = "serde_crate")
259+
)]
248260
pub struct BitcoinFundingInfo {
249261
pub swap_id: SwapId,
250262
pub address: bitcoin::Address,
263+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
251264
pub amount: bitcoin::Amount,
252265
}
253266

@@ -274,9 +287,16 @@ impl fmt::Display for BitcoinFundingInfo {
274287
}
275288
}
276289

277-
#[derive(Clone, Debug, NetworkEncode, NetworkDecode)]
290+
#[cfg_attr(feature = "serde", serde_as)]
291+
#[derive(Clone, Debug, Eq, PartialEq, NetworkEncode, NetworkDecode)]
292+
#[cfg_attr(
293+
feature = "serde",
294+
derive(Serialize, Deserialize),
295+
serde(crate = "serde_crate")
296+
)]
278297
pub struct MoneroFundingInfo {
279298
pub swap_id: SwapId,
299+
#[serde(with = "monero::util::amount::serde::as_xmr")]
280300
pub amount: monero::Amount,
281301
pub address: monero::Address,
282302
}

src/bus/info.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::cli::OfferSelector;
1616
use crate::farcasterd::stats::Stats;
1717
use crate::syncerd::runtime::SyncerdTask;
1818

19+
use super::ctl::FundingInfo;
20+
1921
#[derive(Clone, Debug, Display, From, NetworkEncode, NetworkDecode)]
2022
#[non_exhaustive]
2123
pub enum InfoMsg {
@@ -162,6 +164,8 @@ pub enum InfoMsg {
162164
#[display("checkpoint_entry({0})")]
163165
CheckpointEntry(CheckpointEntry),
164166
// - End GetCheckpointEntry section
167+
#[display("{0}")]
168+
FundingInfos(FundingInfos),
165169
}
166170

167171
#[cfg_attr(feature = "serde", serde_as)]
@@ -299,6 +303,18 @@ pub struct SwapProgress {
299303
pub progress: Vec<ProgressEvent>,
300304
}
301305

306+
#[cfg_attr(feature = "serde", serde_as)]
307+
#[derive(Clone, PartialEq, Eq, Debug, Display, Default, NetworkEncode, NetworkDecode)]
308+
#[cfg_attr(
309+
feature = "serde",
310+
derive(Serialize, Deserialize),
311+
serde(crate = "serde_crate")
312+
)]
313+
#[display(FundingInfos::to_yaml_string)]
314+
pub struct FundingInfos {
315+
pub swaps_need_funding: Vec<FundingInfo>,
316+
}
317+
302318
#[cfg_attr(feature = "serde", serde_as)]
303319
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
304320
#[cfg_attr(
@@ -394,3 +410,5 @@ impl ToYamlString for SwapInfo {}
394410
impl ToYamlString for SyncerInfo {}
395411
#[cfg(feature = "serde")]
396412
impl ToYamlString for ProgressEvent {}
413+
#[cfg(feature = "serde")]
414+
impl ToYamlString for FundingInfos {}

src/farcasterd/runtime.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// along with this software.
1414
// If not, see <https://opensource.org/licenses/MIT>.
1515

16-
use crate::bus::ctl::{BitcoinFundingInfo, CtlMsg, GetKeys, MoneroFundingInfo};
17-
use crate::bus::p2p::{PeerMsg, TakerCommit};
16+
use crate::bus::ctl::FundingInfo;
17+
use crate::bus::ctl::{CtlMsg, GetKeys};
18+
use crate::bus::info::FundingInfos;
19+
use crate::bus::p2p::PeerMsg;
20+
use crate::bus::p2p::TakerCommit;
1821
use crate::bus::sync::SyncMsg;
1922
use crate::bus::{BusMsg, List, ServiceBus};
2023
use crate::event::StateMachineExecutor;
@@ -594,45 +597,29 @@ impl Runtime {
594597
}
595598

596599
InfoMsg::NeedsFunding(Blockchain::Monero) => {
597-
let funding_infos: Vec<MoneroFundingInfo> = self
600+
let swaps_need_funding: Vec<FundingInfo> = self
598601
.trade_state_machines
599602
.iter()
600-
.filter_map(|tsm| tsm.needs_funding_monero())
603+
.filter_map(|tsm| tsm.needs_funding_monero().map(|f| FundingInfo::Monero(f)))
601604
.collect();
602-
let len = funding_infos.len();
603-
let res = funding_infos
604-
.iter()
605-
.enumerate()
606-
.map(|(i, funding_info)| {
607-
let mut res = format!("{}", funding_info);
608-
if i < len - 1 {
609-
res.push('\n');
610-
}
611-
res
612-
})
613-
.collect();
614-
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
605+
self.send_client_info(
606+
endpoints,
607+
source,
608+
InfoMsg::FundingInfos(FundingInfos { swaps_need_funding }),
609+
)?;
615610
}
616611

617612
InfoMsg::NeedsFunding(Blockchain::Bitcoin) => {
618-
let funding_infos: Vec<BitcoinFundingInfo> = self
613+
let swaps_need_funding: Vec<FundingInfo> = self
619614
.trade_state_machines
620615
.iter()
621-
.filter_map(|tsm| tsm.needs_funding_bitcoin())
616+
.filter_map(|tsm| tsm.needs_funding_bitcoin().map(|f| FundingInfo::Bitcoin(f)))
622617
.collect();
623-
let len = funding_infos.len();
624-
let res = funding_infos
625-
.iter()
626-
.enumerate()
627-
.map(|(i, funding_info)| {
628-
let mut res = format!("{}", funding_info);
629-
if i < len - 1 {
630-
res.push('\n');
631-
}
632-
res
633-
})
634-
.collect();
635-
self.send_client_info(endpoints, source, InfoMsg::String(res))?;
618+
self.send_client_info(
619+
endpoints,
620+
source,
621+
InfoMsg::FundingInfos(FundingInfos { swaps_need_funding }),
622+
)?;
636623
}
637624

638625
req => {

src/grpcd/proto/farcaster.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ message NeedsFundingRequest {
173173

174174
message NeedsFundingResponse {
175175
uint32 id = 1;
176-
string funding_infos = 2;
176+
repeated FundingInfo funding_infos = 2;
177+
}
178+
179+
message FundingInfo {
180+
string swap_id = 1;
181+
string address = 2;
182+
uint64 amount = 3;
177183
}
178184

179185
message SweepAddressRequest {

src/grpcd/runtime.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::bus::bridge::BridgeMsg;
2+
use crate::bus::ctl::FundingInfo;
23
use crate::bus::ctl::ProtoPublicOffer;
34
use crate::bus::ctl::PubOffer;
45
use crate::bus::info::Address;
@@ -664,10 +665,25 @@ impl Farcaster for FarcasterService {
664665
.await?;
665666

666667
match oneshot_rx.await {
667-
Ok(BusMsg::Info(InfoMsg::String(infos))) => {
668+
Ok(BusMsg::Info(InfoMsg::FundingInfos(infos))) => {
668669
let reply = NeedsFundingResponse {
669670
id,
670-
funding_infos: infos,
671+
funding_infos: infos
672+
.swaps_need_funding
673+
.iter()
674+
.map(|info| match info {
675+
FundingInfo::Bitcoin(b_info) => farcaster::FundingInfo {
676+
swap_id: b_info.swap_id.to_string(),
677+
address: b_info.address.to_string(),
678+
amount: b_info.amount.as_sat(),
679+
},
680+
FundingInfo::Monero(m_info) => farcaster::FundingInfo {
681+
swap_id: m_info.swap_id.to_string(),
682+
address: m_info.address.to_string(),
683+
amount: m_info.amount.as_pico(),
684+
},
685+
})
686+
.collect(),
671687
};
672688
Ok(GrpcResponse::new(reply))
673689
}

tests/grpc.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,11 @@ async fn grpc_server_functional_test() {
150150
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
151151
assert_eq!(id, 11);
152152

153-
let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
153+
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
154+
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);
155+
154156
bitcoin_rpc
155-
.send_to_address(
156-
&funding_info.address,
157-
funding_info.amount,
158-
None,
159-
None,
160-
None,
161-
None,
162-
None,
163-
None,
164-
)
157+
.send_to_address(&address, amount, None, None, None, None, None, None)
165158
.unwrap();
166159

167160
// Test abort swap
@@ -184,7 +177,7 @@ async fn grpc_server_functional_test() {
184177
let mut farcaster_client_1 = FarcasterClient::new(channel_1);
185178
let request = tonic::Request::new(SweepAddressRequest {
186179
id: 13,
187-
source_address: funding_info.address.to_string(),
180+
source_address: address.to_string(),
188181
destination_address: btc_address.to_string(),
189182
});
190183
let response = farcaster_client_1.sweep_address(request).await;
@@ -220,18 +213,11 @@ async fn grpc_server_functional_test() {
220213
let response = farcaster_client_1.needs_funding(request).await;
221214
let NeedsFundingResponse { id, funding_infos } = response.unwrap().into_inner();
222215
assert_eq!(id, 11);
223-
let funding_info = BitcoinFundingInfo::from_str(&funding_infos).unwrap();
216+
let address = bitcoin::Address::from_str(&funding_infos[0].address).unwrap();
217+
let amount = bitcoin::Amount::from_sat(funding_infos[0].amount);
218+
224219
bitcoin_rpc
225-
.send_to_address(
226-
&funding_info.address,
227-
funding_info.amount,
228-
None,
229-
None,
230-
None,
231-
None,
232-
None,
233-
None,
234-
)
220+
.send_to_address(&address, amount, None, None, None, None, None, None)
235221
.unwrap();
236222

237223
// test swap info

0 commit comments

Comments
 (0)