Skip to content

Commit 9f415d1

Browse files
committed
Cli: send encoded funding info
1 parent b2ea063 commit 9f415d1

3 files changed

Lines changed: 63 additions & 37 deletions

File tree

src/bus/ctl.rs

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

227-
#[derive(Clone, Debug, Display, NetworkDecode, NetworkEncode)]
227+
#[cfg_attr(feature = "serde", serde_as)]
228+
#[derive(Clone, Debug, Display, Eq, PartialEq, NetworkDecode, NetworkEncode)]
229+
#[cfg_attr(
230+
feature = "serde",
231+
derive(Serialize, Deserialize),
232+
serde(crate = "serde_crate")
233+
)]
228234
pub enum FundingInfo {
229-
#[display("bitcoin(..)")]
235+
#[display("{0}")]
230236
Bitcoin(BitcoinFundingInfo),
231-
#[display("monero(..)")]
237+
#[display("{0}")]
232238
Monero(MoneroFundingInfo),
233239
}
234240

235-
#[derive(Clone, Debug, NetworkDecode, NetworkEncode)]
241+
#[cfg_attr(feature = "serde", serde_as)]
242+
#[derive(Clone, Debug, Eq, PartialEq, NetworkDecode, NetworkEncode)]
243+
#[cfg_attr(
244+
feature = "serde",
245+
derive(Serialize, Deserialize),
246+
serde(crate = "serde_crate")
247+
)]
236248
pub struct BitcoinFundingInfo {
237249
pub swap_id: SwapId,
238250
pub address: bitcoin::Address,
251+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
239252
pub amount: bitcoin::Amount,
240253
}
241254

@@ -262,9 +275,16 @@ impl fmt::Display for BitcoinFundingInfo {
262275
}
263276
}
264277

265-
#[derive(Clone, Debug, NetworkEncode, NetworkDecode)]
278+
#[cfg_attr(feature = "serde", serde_as)]
279+
#[derive(Clone, Debug, Eq, PartialEq, NetworkEncode, NetworkDecode)]
280+
#[cfg_attr(
281+
feature = "serde",
282+
derive(Serialize, Deserialize),
283+
serde(crate = "serde_crate")
284+
)]
266285
pub struct MoneroFundingInfo {
267286
pub swap_id: SwapId,
287+
#[serde(with = "monero::util::amount::serde::as_xmr")]
268288
pub amount: monero::Amount,
269289
pub address: monero::Address,
270290
}

src/bus/rpc.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::str::FromStr;
23
use std::time::Duration;
34

@@ -14,6 +15,8 @@ use crate::bus::{
1415
};
1516
use crate::cli::OfferSelector;
1617

18+
use super::ctl::FundingInfo;
19+
1720
#[derive(Clone, Debug, Display, From, NetworkEncode, NetworkDecode)]
1821
#[non_exhaustive]
1922
pub enum Rpc {
@@ -160,6 +163,8 @@ pub enum Rpc {
160163
#[display("checkpoint_entry({0})")]
161164
CheckpointEntry(CheckpointEntry),
162165
// - End GetCheckpointEntry section
166+
#[display("{0}")]
167+
FundingInfos(FundingInfos),
163168
}
164169

165170
#[cfg_attr(feature = "serde", serde_as)]
@@ -295,6 +300,31 @@ pub struct SwapProgress {
295300
pub progress: Vec<ProgressEvent>,
296301
}
297302

303+
#[cfg_attr(feature = "serde", serde_as)]
304+
#[derive(Clone, PartialEq, Eq, Debug, Default, NetworkEncode, NetworkDecode)]
305+
#[cfg_attr(
306+
feature = "serde",
307+
derive(Serialize, Deserialize),
308+
serde(crate = "serde_crate")
309+
)]
310+
pub struct FundingInfos {
311+
pub funding_infos: Vec<FundingInfo>,
312+
}
313+
314+
impl fmt::Display for FundingInfos {
315+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
316+
let len = self.funding_infos.len();
317+
for (i, info) in self.funding_infos.iter().enumerate() {
318+
if i < len - 1 {
319+
write!(f, "{}\n", info,)?;
320+
} else {
321+
write!(f, "{}", info,)?;
322+
}
323+
}
324+
Ok(())
325+
}
326+
}
327+
298328
#[cfg_attr(feature = "serde", serde_as)]
299329
#[derive(Clone, PartialEq, Eq, Debug, Display, NetworkEncode, NetworkDecode)]
300330
#[cfg_attr(

src/farcasterd/runtime.rs

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

16-
use crate::bus::ctl::{BitcoinFundingInfo, Ctl, GetKeys, MoneroFundingInfo};
16+
use crate::bus::ctl::{Ctl, FundingInfo, GetKeys};
1717
use crate::bus::msg::{self, Msg};
18-
use crate::bus::rpc::NodeInfo;
18+
use crate::bus::rpc::{FundingInfos, NodeInfo};
1919
use crate::bus::sync::SyncMsg;
2020
use crate::bus::{BusMsg, List, ServiceBus};
2121
use crate::event::{Event, StateMachine};
@@ -683,54 +683,30 @@ impl Runtime {
683683
}
684684

685685
Rpc::NeedsFunding(Blockchain::Monero) => {
686-
let funding_infos: Vec<MoneroFundingInfo> = self
686+
let funding_infos: Vec<FundingInfo> = self
687687
.trade_state_machines
688688
.iter()
689-
.filter_map(|tsm| tsm.needs_funding_monero())
690-
.collect();
691-
let len = funding_infos.len();
692-
let res = funding_infos
693-
.iter()
694-
.enumerate()
695-
.map(|(i, funding_info)| {
696-
let mut res = format!("{}", funding_info);
697-
if i < len - 1 {
698-
res.push('\n');
699-
}
700-
res
701-
})
689+
.filter_map(|tsm| tsm.needs_funding_monero().map(|f| FundingInfo::Monero(f)))
702690
.collect();
703691
endpoints.send_to(
704692
ServiceBus::Rpc,
705693
self.identity(),
706694
source,
707-
BusMsg::Rpc(Rpc::String(res)),
695+
BusMsg::Rpc(Rpc::FundingInfos(FundingInfos { funding_infos })),
708696
)?;
709697
}
710698

711699
Rpc::NeedsFunding(Blockchain::Bitcoin) => {
712-
let funding_infos: Vec<BitcoinFundingInfo> = self
700+
let funding_infos: Vec<FundingInfo> = self
713701
.trade_state_machines
714702
.iter()
715-
.filter_map(|tsm| tsm.needs_funding_bitcoin())
716-
.collect();
717-
let len = funding_infos.len();
718-
let res = funding_infos
719-
.iter()
720-
.enumerate()
721-
.map(|(i, funding_info)| {
722-
let mut res = format!("{}", funding_info);
723-
if i < len - 1 {
724-
res.push('\n');
725-
}
726-
res
727-
})
703+
.filter_map(|tsm| tsm.needs_funding_bitcoin().map(|f| FundingInfo::Bitcoin(f)))
728704
.collect();
729705
endpoints.send_to(
730706
ServiceBus::Rpc,
731707
self.identity(),
732708
source,
733-
BusMsg::Rpc(Rpc::String(res)),
709+
BusMsg::Rpc(Rpc::FundingInfos(FundingInfos { funding_infos })),
734710
)?;
735711
}
736712

0 commit comments

Comments
 (0)