Skip to content

Commit 219db41

Browse files
committed
Filter expired options and swaps via client-side filtering
1 parent d5e3ea9 commit 219db41

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

crates/options-relay/src/client/read_only.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::config::NostrRelayConfig;
22
use crate::error::{ParseError, RelayError};
3+
use crate::events::kinds::TAG_EXPIRY;
34
use crate::events::{ActionCompletedEvent, OptionCreatedEvent, SwapCreatedEvent, filters};
45

56
use nostr::prelude::*;
@@ -8,6 +9,18 @@ use nostr_sdk::prelude::Events;
89
use simplicityhl::elements::AddressParams;
910
use tracing::instrument;
1011

12+
/// Check if an event is still active (not expired) based on its expiry tag.
13+
/// Returns `false` if the expiry tag is missing or if the contract has expired.
14+
fn is_active(event: &Event) -> bool {
15+
let now = Timestamp::now().as_secs();
16+
event
17+
.tags
18+
.iter()
19+
.find(|t| matches!(t.kind(), TagKind::Custom(s) if s.as_ref() == TAG_EXPIRY))
20+
.and_then(|t| t.content()?.parse::<u64>().ok())
21+
.is_some_and(|expiry| expiry > now)
22+
}
23+
1124
#[derive(Debug, Clone)]
1225
pub struct ReadOnlyClient {
1326
client: Client,
@@ -50,6 +63,7 @@ impl ReadOnlyClient {
5063
let events = self.fetch_events(filters::option_created()).await?;
5164
Ok(events
5265
.iter()
66+
.filter(|e| is_active(e))
5367
.map(|e| OptionCreatedEvent::from_event(e, params))
5468
.collect())
5569
}
@@ -59,7 +73,11 @@ impl ReadOnlyClient {
5973
params: &'static AddressParams,
6074
) -> Result<Vec<Result<SwapCreatedEvent, ParseError>>, RelayError> {
6175
let events = self.fetch_events(filters::swap_created()).await?;
62-
Ok(events.iter().map(|e| SwapCreatedEvent::from_event(e, params)).collect())
76+
Ok(events
77+
.iter()
78+
.filter(|e| is_active(e))
79+
.map(|e| SwapCreatedEvent::from_event(e, params))
80+
.collect())
6381
}
6482

6583
pub async fn fetch_actions_for_event(

crates/options-relay/src/events/kinds.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub const TAG_SWAP_UTXO: &str = "swap_utxo";
1111
pub const TAG_TAPROOT_GEN: &str = "t";
1212
pub const TAG_ACTION: &str = "action";
1313
pub const TAG_OUTPOINT: &str = "outpoint";
14+
pub const TAG_EXPIRY: &str = "expiry";
1415

1516
pub const ACTION_SWAP_EXERCISED: &str = "swap_exercised";
1617
pub const ACTION_SWAP_CANCELLED: &str = "swap_cancelled";

crates/options-relay/src/events/option_created.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::{ParseError, RelayError};
2-
use crate::events::kinds::{OPTION_CREATED, TAG_OPTIONS_ARGS, TAG_OPTIONS_UTXO, TAG_TAPROOT_GEN};
2+
use crate::events::kinds::{OPTION_CREATED, TAG_EXPIRY, TAG_OPTIONS_ARGS, TAG_OPTIONS_UTXO, TAG_TAPROOT_GEN};
33

44
use contracts::options::{OptionsArguments, get_options_address};
55
use contracts::sdk::taproot_pubkey_gen::TaprootPubkeyGen;
@@ -40,6 +40,10 @@ impl OptionCreatedEvent {
4040
.tag(Tag::custom(
4141
TagKind::custom(TAG_TAPROOT_GEN),
4242
[self.taproot_pubkey_gen.to_string()],
43+
))
44+
.tag(Tag::custom(
45+
TagKind::custom(TAG_EXPIRY),
46+
[self.options_args.expiry_time().to_string()],
4347
)))
4448
}
4549

crates/options-relay/src/events/swap_created.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::{ParseError, RelayError};
2-
use crate::events::kinds::{SWAP_CREATED, TAG_SWAP_ARGS, TAG_SWAP_UTXO, TAG_TAPROOT_GEN};
2+
use crate::events::kinds::{SWAP_CREATED, TAG_EXPIRY, TAG_SWAP_ARGS, TAG_SWAP_UTXO, TAG_TAPROOT_GEN};
33

44
use contracts::sdk::taproot_pubkey_gen::TaprootPubkeyGen;
55
use contracts::swap_with_change::{SwapWithChangeArguments, get_swap_with_change_address};
@@ -40,6 +40,10 @@ impl SwapCreatedEvent {
4040
.tag(Tag::custom(
4141
TagKind::custom(TAG_TAPROOT_GEN),
4242
[self.taproot_pubkey_gen.to_string()],
43+
))
44+
.tag(Tag::custom(
45+
TagKind::custom(TAG_EXPIRY),
46+
[self.swap_args.expiry_time().to_string()],
4347
)))
4448
}
4549

0 commit comments

Comments
 (0)