quicknode-sdk 0.1.0

Core library for quicknode sdk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#[cfg(feature = "rust")]
use bon::Builder;
#[cfg(feature = "node")]
use napi_derive::napi;
#[cfg(feature = "python")]
use pyo3::{pyclass, pymethods};
#[cfg(feature = "python")]
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use serde::{Deserialize, Deserializer, Serialize};

fn deserialize_as_optional_json_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
{
    let value = Option::<serde_json::Value>::deserialize(deserializer)?;
    match value {
        None => Ok(None),
        Some(v) => serde_json::to_string(&v)
            .map(Some)
            .map_err(serde::de::Error::custom),
    }
}

// ── Enums ──────────────────────────────────────────────────────────────────

/// Identifier of a predefined webhook filter template.
#[cfg_attr(feature = "node", napi(string_enum))]
#[cfg_attr(not(feature = "node"), derive(Clone))]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum WebhookTemplateId {
    EvmWalletFilter,
    EvmContractEvents,
    EvmAbiFilter,
    SolanaWalletFilter,
    BitcoinWalletFilter,
    XrplWalletFilter,
    HyperliquidWalletEventsFilter,
    StellarWalletTransactionsSourceAccountFilter,
}

impl WebhookTemplateId {
    pub fn as_str(&self) -> &'static str {
        match self {
            WebhookTemplateId::EvmWalletFilter => "evmWalletFilter",
            WebhookTemplateId::EvmContractEvents => "evmContractEvents",
            WebhookTemplateId::EvmAbiFilter => "evmAbiFilter",
            WebhookTemplateId::SolanaWalletFilter => "solanaWalletFilter",
            WebhookTemplateId::BitcoinWalletFilter => "bitcoinWalletFilter",
            WebhookTemplateId::XrplWalletFilter => "xrplWalletFilter",
            WebhookTemplateId::HyperliquidWalletEventsFilter => "hyperliquidWalletEventsFilter",
            WebhookTemplateId::StellarWalletTransactionsSourceAccountFilter => {
                "stellarWalletTransactionsSourceAccountFilter"
            }
        }
    }
}

/// Position a webhook begins (or resumes) delivering from when activated.
#[cfg_attr(feature = "node", napi(string_enum))]
#[cfg_attr(not(feature = "node"), derive(Clone))]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WebhookStartFrom {
    /// Resume from the last-delivered block.
    Last,
    /// Start from the newest available block.
    Latest,
}

// ── Template Arg Structs ───────────────────────────────────────────────────

/// Template arguments for an EVM wallet filter: matches activity for a list of
/// wallet addresses.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvmWalletFilterTemplate {
    /// Wallet addresses to match against.
    pub wallets: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl EvmWalletFilterTemplate {
    #[new]
    pub fn new(wallets: Vec<String>) -> Self {
        Self { wallets }
    }
}

/// Template arguments for filtering EVM contract events, optionally scoped to
/// a specific set of event topic hashes.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EvmContractEventsTemplate {
    /// Contract addresses to watch for events.
    pub contracts: Vec<String>,
    /// Optional list of event topic hashes to restrict the filter to specific events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_hashes: Option<Vec<String>>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl EvmContractEventsTemplate {
    #[new]
    #[pyo3(signature = (contracts, event_hashes=None))]
    pub fn new(contracts: Vec<String>, event_hashes: Option<Vec<String>>) -> Self {
        Self {
            contracts,
            event_hashes,
        }
    }
}

/// Template arguments for an EVM ABI filter: decodes and filters events for a
/// set of contracts using a provided ABI.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvmAbiFilterTemplate {
    /// JSON-encoded contract ABI used to decode event data.
    pub abi: String,
    /// Contract addresses to watch for events.
    pub contracts: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl EvmAbiFilterTemplate {
    #[new]
    pub fn new(abi: String, contracts: Vec<String>) -> Self {
        Self { abi, contracts }
    }
}

/// Template arguments for a Solana wallet filter: matches activity for a list
/// of Solana account addresses.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolanaWalletFilterTemplate {
    /// Solana account addresses to match against.
    pub accounts: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl SolanaWalletFilterTemplate {
    #[new]
    pub fn new(accounts: Vec<String>) -> Self {
        Self { accounts }
    }
}

/// Template arguments for a Bitcoin wallet filter.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BitcoinWalletFilterTemplate {
    /// Bitcoin wallet addresses to match against.
    pub wallets: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl BitcoinWalletFilterTemplate {
    #[new]
    pub fn new(wallets: Vec<String>) -> Self {
        Self { wallets }
    }
}

/// Template arguments for an XRPL wallet filter.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XrplWalletFilterTemplate {
    /// XRPL wallet addresses to match against.
    pub wallets: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl XrplWalletFilterTemplate {
    #[new]
    pub fn new(wallets: Vec<String>) -> Self {
        Self { wallets }
    }
}

/// Template arguments for a Hyperliquid wallet-events filter.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HyperliquidWalletEventsFilterTemplate {
    /// Hyperliquid wallet addresses to match against.
    pub wallets: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl HyperliquidWalletEventsFilterTemplate {
    #[new]
    pub fn new(wallets: Vec<String>) -> Self {
        Self { wallets }
    }
}

/// Template arguments for a Stellar wallet-transactions filter, matching
/// transactions where the given wallets are the source account.
#[cfg_attr(feature = "rust", derive(Builder))]
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StellarWalletTransactionsFilterTemplate {
    /// Stellar wallet addresses to match against.
    pub wallets: Vec<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl StellarWalletTransactionsFilterTemplate {
    #[new]
    pub fn new(wallets: Vec<String>) -> Self {
        Self { wallets }
    }
}

// ── Template Args ──────────────────────────────────────────────────────────

/// Template identifier paired with its arguments. Exactly one variant selects
/// which filter is applied. Consumed by `create_webhook_from_template` and
/// `update_webhook_template`.
// Pure-Rust discriminated union; no #[pyclass] / #[napi(object)] because PyO3
// and napi-rs cannot represent enum-with-data. Each language binding crate
// wraps this type for its own FFI surface.
// The serde tag/content pair matches the API wire format when flattened into
// a request struct.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "templateId", content = "templateArgs", rename_all = "camelCase")]
pub enum TemplateArgs {
    /// EVM wallet filter: matches activity for a list of wallet addresses.
    EvmWalletFilter(EvmWalletFilterTemplate),
    /// EVM contract events filter, optionally scoped to specific event topic hashes.
    EvmContractEvents(EvmContractEventsTemplate),
    /// EVM ABI filter: decodes and filters events using a provided ABI.
    EvmAbiFilter(EvmAbiFilterTemplate),
    /// Solana wallet filter.
    SolanaWalletFilter(SolanaWalletFilterTemplate),
    /// Bitcoin wallet filter.
    BitcoinWalletFilter(BitcoinWalletFilterTemplate),
    /// XRPL wallet filter.
    XrplWalletFilter(XrplWalletFilterTemplate),
    /// Hyperliquid wallet-events filter.
    HyperliquidWalletEventsFilter(HyperliquidWalletEventsFilterTemplate),
    /// Stellar wallet-transactions filter (source-account match).
    StellarWalletTransactionsSourceAccountFilter(StellarWalletTransactionsFilterTemplate),
}

impl TemplateArgs {
    pub fn tag(&self) -> WebhookTemplateId {
        match self {
            Self::EvmWalletFilter(_) => WebhookTemplateId::EvmWalletFilter,
            Self::EvmContractEvents(_) => WebhookTemplateId::EvmContractEvents,
            Self::EvmAbiFilter(_) => WebhookTemplateId::EvmAbiFilter,
            Self::SolanaWalletFilter(_) => WebhookTemplateId::SolanaWalletFilter,
            Self::BitcoinWalletFilter(_) => WebhookTemplateId::BitcoinWalletFilter,
            Self::XrplWalletFilter(_) => WebhookTemplateId::XrplWalletFilter,
            Self::HyperliquidWalletEventsFilter(_) => {
                WebhookTemplateId::HyperliquidWalletEventsFilter
            }
            Self::StellarWalletTransactionsSourceAccountFilter(_) => {
                WebhookTemplateId::StellarWalletTransactionsSourceAccountFilter
            }
        }
    }
}

// ── Webhook Destination Attributes ─────────────────────────────────────────

/// Destination configuration for a webhook.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookDestinationAttributes {
    /// Target URL that receives webhook payloads.
    pub url: String,
    /// Optional token sent with each payload so the receiver can verify authenticity; generated automatically when omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub security_token: Option<String>,
    /// Optional payload compression (`gzip` or `none`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compression: Option<String>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl WebhookDestinationAttributes {
    #[new]
    #[pyo3(signature = (url, security_token=None, compression=None))]
    pub fn new(url: String, security_token: Option<String>, compression: Option<String>) -> Self {
        Self {
            url,
            security_token,
            compression,
        }
    }
}

// ── Request Types ──────────────────────────────────────────────────────────

/// Parameters for `list_webhooks`.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[cfg_attr(not(feature = "node"), derive(Clone))]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct GetWebhooksParams {
    /// Maximum number of webhooks returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Starting index into the result set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<i64>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl GetWebhooksParams {
    #[new]
    #[pyo3(signature = (limit=None, offset=None))]
    pub fn new(limit: Option<i64>, offset: Option<i64>) -> Self {
        Self { limit, offset }
    }
}

/// Parameters for `update_webhook`. All fields are optional; only set fields
/// are modified.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[cfg_attr(not(feature = "node"), derive(Clone))]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct UpdateWebhookParams {
    /// New human-readable name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// New notification email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_email: Option<String>,
    /// New destination configuration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_attributes: Option<WebhookDestinationAttributes>,
}

#[cfg(feature = "python")]
#[gen_stub_pymethods]
#[pymethods]
impl UpdateWebhookParams {
    #[new]
    #[pyo3(signature = (name=None, notification_email=None, destination_attributes=None))]
    pub fn new(
        name: Option<String>,
        notification_email: Option<String>,
        destination_attributes: Option<WebhookDestinationAttributes>,
    ) -> Self {
        Self {
            name,
            notification_email,
            destination_attributes,
        }
    }
}

/// Parameters for `activate_webhook`.
#[cfg_attr(feature = "node", napi(object))]
#[cfg_attr(not(feature = "node"), derive(Clone))]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActivateWebhookParams {
    /// Position to begin (or resume) delivery from.
    pub start_from: WebhookStartFrom,
}

/// Parameters for `create_webhook_from_template`.
#[cfg_attr(feature = "rust", derive(Builder))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWebhookFromTemplateParams {
    /// Human-readable label for the webhook.
    pub name: String,
    /// Blockchain network to watch (e.g. `ethereum-mainnet`).
    pub network: String,
    /// Optional email that receives alerts if the webhook terminates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_email: Option<String>,
    /// Destination configuration for delivered payloads.
    pub destination_attributes: WebhookDestinationAttributes,
    /// Filter template identifier and its arguments.
    // Flattening the enum's tag/content produces { templateId, templateArgs }.
    #[serde(flatten)]
    pub template_args: TemplateArgs,
}

/// Parameters for `update_webhook_template`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateWebhookTemplateParams {
    /// New human-readable name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// New notification email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_email: Option<String>,
    /// New destination configuration.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination_attributes: Option<WebhookDestinationAttributes>,
    /// New template identifier and arguments.
    // Flattening the enum's tag/content produces { templateId, templateArgs }.
    #[serde(flatten)]
    pub template_args: TemplateArgs,
}

// ── Response Types ─────────────────────────────────────────────────────────

/// A webhook's full configuration and current state.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Webhook {
    /// Unique webhook identifier.
    pub id: String,
    /// Human-readable webhook name.
    pub name: String,
    /// Current operational state (e.g. `active`, `paused`).
    pub status: String,
    /// Blockchain network the webhook is watching.
    pub network: String,
    /// Timestamp when the webhook was created.
    pub created_at: String,
    /// Timestamp of the most recent modification.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
    /// Template identifier used to create the webhook, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template_id: Option<String>,
    /// Email address notified of webhook terminations or failures.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_email: Option<String>,
    /// Destination-specific configuration as a JSON string.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_as_optional_json_string"
    )]
    pub destination_attributes: Option<String>,
}

/// Pagination metadata returned alongside a paginated webhooks list.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookPageInfo {
    /// Page size used for this response.
    pub limit: i64,
    /// Starting index of this page within the full result set.
    pub offset: i64,
    /// Total number of webhooks matching the query across all pages.
    pub total: i64,
}

/// Response from `list_webhooks`.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListWebhooksResponse {
    /// Webhooks on the current page.
    pub data: Vec<Webhook>,
    /// Pagination metadata for the response.
    #[serde(rename = "pageInfo")]
    pub page_info: WebhookPageInfo,
}

/// Response from `get_enabled_count` for webhooks.
#[cfg_attr(feature = "python", gen_stub_pyclass)]
#[cfg_attr(feature = "python", pyclass(get_all, set_all))]
#[cfg_attr(feature = "node", napi(object))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookEnabledCountResponse {
    /// Total count of enabled webhooks on the account.
    pub total: i64,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod template_args_tests {
    use super::*;

    #[test]
    fn evm_wallet_filter_roundtrip() {
        let args = TemplateArgs::EvmWalletFilter(EvmWalletFilterTemplate {
            wallets: vec!["0xabc".to_string()],
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"evmWalletFilter""#));
        assert!(json.contains(r#""wallets":["0xabc"]"#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::EvmWalletFilter(_)));
        assert!(matches!(parsed.tag(), WebhookTemplateId::EvmWalletFilter));
    }

    #[test]
    fn evm_contract_events_roundtrip() {
        let args = TemplateArgs::EvmContractEvents(EvmContractEventsTemplate {
            contracts: vec!["0xdef".to_string()],
            event_hashes: Some(vec!["0x1234".to_string()]),
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"evmContractEvents""#));
        // API expects camelCase `eventHashes` — snake_case is silently rejected with a 500.
        assert!(json.contains(r#""eventHashes":["0x1234"]"#));
        assert!(!json.contains("event_hashes"));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::EvmContractEvents(_)));
    }

    #[test]
    fn evm_abi_filter_roundtrip() {
        let args = TemplateArgs::EvmAbiFilter(EvmAbiFilterTemplate {
            abi: "[]".to_string(),
            contracts: vec!["0xdef".to_string()],
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"evmAbiFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::EvmAbiFilter(_)));
    }

    #[test]
    fn solana_wallet_filter_roundtrip() {
        let args = TemplateArgs::SolanaWalletFilter(SolanaWalletFilterTemplate {
            accounts: vec!["acc".to_string()],
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"solanaWalletFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::SolanaWalletFilter(_)));
    }

    #[test]
    fn bitcoin_wallet_filter_roundtrip() {
        let args = TemplateArgs::BitcoinWalletFilter(BitcoinWalletFilterTemplate {
            wallets: vec!["bc1".to_string()],
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"bitcoinWalletFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::BitcoinWalletFilter(_)));
    }

    #[test]
    fn xrpl_wallet_filter_roundtrip() {
        let args = TemplateArgs::XrplWalletFilter(XrplWalletFilterTemplate {
            wallets: vec!["r1".to_string()],
        });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"xrplWalletFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, TemplateArgs::XrplWalletFilter(_)));
    }

    #[test]
    fn hyperliquid_wallet_events_filter_roundtrip() {
        let args =
            TemplateArgs::HyperliquidWalletEventsFilter(HyperliquidWalletEventsFilterTemplate {
                wallets: vec!["0xhl".to_string()],
            });
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"hyperliquidWalletEventsFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(
            parsed,
            TemplateArgs::HyperliquidWalletEventsFilter(_)
        ));
    }

    #[test]
    fn stellar_wallet_transactions_filter_roundtrip() {
        let args = TemplateArgs::StellarWalletTransactionsSourceAccountFilter(
            StellarWalletTransactionsFilterTemplate {
                wallets: vec!["G...".to_string()],
            },
        );
        let json = serde_json::to_string(&args).unwrap();
        assert!(json.contains(r#""templateId":"stellarWalletTransactionsSourceAccountFilter""#));
        let parsed: TemplateArgs = serde_json::from_str(&json).unwrap();
        assert!(matches!(
            parsed,
            TemplateArgs::StellarWalletTransactionsSourceAccountFilter(_)
        ));
    }

    #[test]
    fn create_params_flattens_template_args() {
        let params = CreateWebhookFromTemplateParams {
            name: "n".to_string(),
            network: "ethereum-mainnet".to_string(),
            notification_email: None,
            destination_attributes: WebhookDestinationAttributes {
                url: "https://x".to_string(),
                security_token: None,
                compression: None,
            },
            template_args: TemplateArgs::EvmWalletFilter(EvmWalletFilterTemplate {
                wallets: vec!["0xabc".to_string()],
            }),
        };
        let json = serde_json::to_value(&params).unwrap();
        let obj = json.as_object().unwrap();
        assert_eq!(
            obj.get("templateId").and_then(|v| v.as_str()),
            Some("evmWalletFilter")
        );
        assert!(obj.get("templateArgs").unwrap().is_object());
        assert_eq!(obj["templateArgs"]["wallets"][0].as_str(), Some("0xabc"));
    }
}