Skip to content

Commit

Permalink
sdk: refactor Client::zap method
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 20, 2024
1 parent 13e34a8 commit 096f072
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions crates/nostr-sdk/src/client/zapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,31 @@ impl Client {

// Get entity metadata
let to: ZapEntity = to.into();
let (public_key, lud): (XOnlyPublicKey, Lud06OrLud16) = match to {
let (public_key, metadata): (XOnlyPublicKey, Metadata) = match to {
ZapEntity::Event(event_id) => {
// Get event
let filter: Filter = Filter::new().id(event_id);
let events: Vec<Event> = self.get_events_of(vec![filter], None).await?;
let event: &Event = events.first().ok_or(Error::EventNotFound(event_id))?;
let public_key: XOnlyPublicKey = event.author();
let metadata: Metadata = self.metadata(public_key).await?;

if let Some(lud16) = &metadata.lud16 {
(public_key, LightningAddress::parse(lud16)?.into())
} else if let Some(lud06) = &metadata.lud06 {
(public_key, LnUrl::from_str(lud06)?.into())
} else {
return Err(Error::ImpossibleToZap(String::from("LUD06/LUD16 not set")));
}
(public_key, metadata)
}
ZapEntity::PublicKey(public_key) => {
let metadata: Metadata = self.metadata(public_key).await?;

if let Some(lud16) = &metadata.lud16 {
(public_key, LightningAddress::parse(lud16)?.into())
} else if let Some(lud06) = &metadata.lud06 {
(public_key, LnUrl::from_str(lud06)?.into())
} else {
return Err(Error::ImpossibleToZap(String::from("LUD06/LUD16 not set")));
}
(public_key, metadata)
}
};

// Parse lud
let lud: Lud06OrLud16 = if let Some(lud16) = &metadata.lud16 {
LightningAddress::parse(lud16)?.into()
} else if let Some(lud06) = &metadata.lud06 {
LnUrl::from_str(lud06)?.into()
} else {
return Err(Error::ImpossibleToZap(String::from("LUD06/LUD16 not set")));
};

// Compose zap request
let zap_request: Option<String> = match details {
Some(details) => {
Expand Down

0 comments on commit 096f072

Please sign in to comment.