Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 8a6ba10

Browse files
Merge pull request #791 from MutinyWallet/get-contact-activity
Get contact activity
2 parents e926b9e + 72c273e commit 8a6ba10

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

mutiny-core/src/nodemanager.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,46 @@ impl<S: MutinyStorage> NodeManager<S> {
12741274
Ok(activity)
12751275
}
12761276

1277+
/// Returns all the on-chain and lightning activity for a given label
1278+
pub async fn get_label_activity(
1279+
&self,
1280+
label: &String,
1281+
) -> Result<Vec<ActivityItem>, MutinyError> {
1282+
let Some(label_item) = self.get_label(label)? else {
1283+
return Ok(Vec::new());
1284+
};
1285+
1286+
let mut activity = vec![];
1287+
for inv in label_item.invoices.iter() {
1288+
let ln = self.get_invoice(inv).await?;
1289+
// Only show paid and in-flight invoices
1290+
match ln.status {
1291+
HTLCStatus::Succeeded | HTLCStatus::InFlight => {
1292+
activity.push(ActivityItem::Lightning(Box::new(ln)));
1293+
}
1294+
HTLCStatus::Pending | HTLCStatus::Failed => {}
1295+
}
1296+
}
1297+
let onchain = self
1298+
.list_onchain()
1299+
.map_err(|e| {
1300+
log_warn!(self.logger, "Failed to get bdk history: {e}");
1301+
e
1302+
})
1303+
.unwrap_or_default();
1304+
1305+
for on in onchain {
1306+
if on.labels.contains(label) {
1307+
activity.push(ActivityItem::OnChain(on));
1308+
}
1309+
}
1310+
1311+
// Newest first
1312+
activity.sort_by(|a, b| b.cmp(a));
1313+
1314+
Ok(activity)
1315+
}
1316+
12771317
/// Adds labels to the TransactionDetails based on the address labels.
12781318
/// This will panic if the TransactionDetails does not have a transaction.
12791319
/// Make sure you flag `include_raw` when calling `list_transactions` to

mutiny-wasm/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,35 @@ impl MutinyWallet {
980980
Ok(JsValue::from_serde(&activity)?)
981981
}
982982

983+
/// Returns all the on-chain and lightning activity for a given label
984+
#[wasm_bindgen]
985+
pub async fn get_label_activity(
986+
&self,
987+
label: String,
988+
) -> Result<JsValue /* Vec<ActivityItem> */, MutinyJsError> {
989+
// get activity from the node manager
990+
let activity = self.inner.node_manager.get_label_activity(&label).await?;
991+
let mut activity: Vec<ActivityItem> = activity.into_iter().map(|a| a.into()).collect();
992+
993+
// add contact to the activity item if it is one
994+
let Some(contact) = self.inner.node_manager.get_contact(&label)? else {
995+
return Ok(JsValue::from_serde(&activity)?);
996+
};
997+
998+
for a in activity.iter_mut() {
999+
// find labels that have a contact and add them to the item
1000+
for a_label in a.labels.iter() {
1001+
if label == *a_label {
1002+
a.contacts.push(Contact::from(contact.clone()));
1003+
}
1004+
}
1005+
// remove labels that have the contact to prevent duplicates
1006+
a.labels.retain(|l| l != &label);
1007+
}
1008+
1009+
Ok(JsValue::from_serde(&activity)?)
1010+
}
1011+
9831012
/// Initiates a redshift
9841013
#[wasm_bindgen]
9851014
pub async fn init_redshift(

0 commit comments

Comments
 (0)