quantus-cli 1.5.0

Command line interface and library for interacting with the Quantus Network
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
//! Types for Subsquid API responses.

use serde::{Deserialize, Serialize};

/// A transfer as returned by the Subsquid indexer.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transfer {
	/// Unique identifier
	pub id: String,

	/// Block ID
	pub block_id: String,

	/// Block height
	pub block_height: i64,

	/// Timestamp of the transfer
	pub timestamp: String,

	/// Extrinsic hash (if available)
	pub extrinsic_hash: Option<String>,

	/// Sender address (SS58 format)
	pub from_id: String,

	/// Recipient address (SS58 format)
	pub to_id: String,

	/// Transfer amount (as string to handle large numbers)
	pub amount: String,

	/// Transaction fee
	pub fee: String,

	/// Blake3 hash of the sender's raw address
	pub from_hash: String,

	/// Blake3 hash of the recipient's raw address
	pub to_hash: String,

	/// Index in the ZK trie for Merkle proof generation
	pub leaf_index: String,

	/// Transfer count from Wormhole pallet - required for nullifier computation
	#[serde(default)]
	pub transfer_count: String,
}

/// Deserialize a Hasura `numeric` scalar into a `String`.
///
/// Hasura serializes Postgres `numeric` columns as JSON numbers by default
/// (or strings when `HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES` is set), so
/// accept both representations.
fn numeric_string<'de, D>(deserializer: D) -> std::result::Result<String, D::Error>
where
	D: serde::Deserializer<'de>,
{
	struct NumericVisitor;

	impl serde::de::Visitor<'_> for NumericVisitor {
		type Value = String;

		fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
			formatter.write_str("a number or a numeric string")
		}

		fn visit_str<E: serde::de::Error>(self, v: &str) -> std::result::Result<String, E> {
			Ok(v.to_string())
		}

		fn visit_u64<E: serde::de::Error>(self, v: u64) -> std::result::Result<String, E> {
			Ok(v.to_string())
		}

		fn visit_i64<E: serde::de::Error>(self, v: i64) -> std::result::Result<String, E> {
			Ok(v.to_string())
		}

		fn visit_f64<E: serde::de::Error>(self, v: f64) -> std::result::Result<String, E> {
			if v.fract() == 0.0 && v.is_finite() {
				Ok(format!("{:.0}", v))
			} else {
				Ok(v.to_string())
			}
		}
	}

	deserializer.deserialize_any(NumericVisitor)
}

/// Nested `block { height }` relationship in Hasura responses.
#[derive(Debug, Clone, Deserialize)]
pub struct HasuraBlockRef {
	pub height: i64,
}

/// A transfer row as returned by the Hasura GraphQL server.
///
/// Uses snake_case column names and nested relationships; converted into the
/// flat [`Transfer`] struct that the rest of the codebase consumes.
#[derive(Debug, Clone, Deserialize)]
pub struct HasuraTransferRow {
	pub id: String,
	pub block_id: Option<String>,
	pub block: Option<HasuraBlockRef>,
	pub timestamp: String,
	pub extrinsic_id: Option<String>,
	pub from_id: Option<String>,
	pub to_id: Option<String>,
	#[serde(deserialize_with = "numeric_string")]
	pub amount: String,
	#[serde(deserialize_with = "numeric_string")]
	pub fee: String,
	pub from_hash: String,
	pub to_hash: String,
	#[serde(deserialize_with = "numeric_string")]
	pub leaf_index: String,
	#[serde(deserialize_with = "numeric_string")]
	pub transfer_count: String,
}

impl From<HasuraTransferRow> for Transfer {
	fn from(row: HasuraTransferRow) -> Self {
		Transfer {
			id: row.id,
			block_id: row.block_id.unwrap_or_default(),
			block_height: row.block.map(|b| b.height).unwrap_or_default(),
			timestamp: row.timestamp,
			extrinsic_hash: row.extrinsic_id,
			from_id: row.from_id.unwrap_or_default(),
			to_id: row.to_id.unwrap_or_default(),
			amount: row.amount,
			fee: row.fee,
			from_hash: row.from_hash,
			to_hash: row.to_hash,
			leaf_index: row.leaf_index,
			transfer_count: row.transfer_count,
		}
	}
}

/// A wormhole nullifier row as returned by the Hasura GraphQL server.
#[derive(Debug, Clone, Deserialize)]
pub struct HasuraNullifierRow {
	pub nullifier: String,
	pub nullifier_hash: String,
	pub block: Option<HasuraBlockRef>,
	pub timestamp: String,
	#[serde(rename = "wormholeExtrinsic")]
	pub wormhole_extrinsic: Option<HasuraWormholeExtrinsicRef>,
}

/// Nested `wormholeExtrinsic { extrinsic_id }` relationship.
#[derive(Debug, Clone, Deserialize)]
pub struct HasuraWormholeExtrinsicRef {
	pub extrinsic_id: Option<String>,
}

impl From<HasuraNullifierRow> for NullifierResult {
	fn from(row: HasuraNullifierRow) -> Self {
		NullifierResult {
			nullifier: row.nullifier,
			nullifier_hash: row.nullifier_hash,
			extrinsic_hash: row.wormhole_extrinsic.and_then(|e| e.extrinsic_id).unwrap_or_default(),
			block_height: row.block.map(|b| b.height).unwrap_or_default(),
			timestamp: row.timestamp,
		}
	}
}

/// GraphQL response wrapper.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLResponse<T> {
	pub data: Option<T>,
	pub errors: Option<Vec<GraphQLError>>,
}

/// GraphQL error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLError {
	pub message: String,
	pub locations: Option<Vec<GraphQLErrorLocation>>,
	pub path: Option<Vec<serde_json::Value>>,
}

/// GraphQL error location.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLErrorLocation {
	pub line: i64,
	pub column: i64,
}

/// A nullifier as returned by the Subsquid indexer.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NullifierResult {
	/// The nullifier bytes as hex
	pub nullifier: String,

	/// Blake3 hash of the nullifier for prefix queries
	pub nullifier_hash: String,

	/// Extrinsic hash that consumed this nullifier
	pub extrinsic_hash: String,

	/// Block height where the nullifier was consumed
	pub block_height: i64,

	/// Timestamp when the nullifier was consumed
	pub timestamp: String,
}

/// Query parameters for nullifier prefix queries.
#[derive(Debug, Clone, Default)]
pub struct NullifierQueryParams {
	/// Minimum block number (inclusive)
	pub after_block: Option<u32>,
}

impl NullifierQueryParams {
	pub fn new() -> Self {
		Self::default()
	}

	#[allow(dead_code)]
	pub fn with_after_block(mut self, block: u32) -> Self {
		self.after_block = Some(block);
		self
	}
}

/// Query parameters for transfer prefix queries.
#[derive(Debug, Clone, Default)]
pub struct TransferQueryParams {
	/// Minimum block number (inclusive)
	pub after_block: Option<u32>,

	/// Maximum block number (inclusive)
	pub before_block: Option<u32>,

	/// Minimum transfer amount
	pub min_amount: Option<u128>,

	/// Maximum transfer amount
	pub max_amount: Option<u128>,

	/// Maximum number of results
	pub limit: u32,

	/// Offset for pagination
	pub offset: u32,
}

impl TransferQueryParams {
	pub fn new() -> Self {
		Self { limit: 100, offset: 0, ..Default::default() }
	}

	pub fn with_limit(mut self, limit: u32) -> Self {
		self.limit = limit;
		self
	}

	#[allow(dead_code)]
	pub fn with_offset(mut self, offset: u32) -> Self {
		self.offset = offset;
		self
	}

	pub fn with_after_block(mut self, block: u32) -> Self {
		self.after_block = Some(block);
		self
	}

	pub fn with_before_block(mut self, block: u32) -> Self {
		self.before_block = Some(block);
		self
	}

	pub fn with_min_amount(mut self, amount: u128) -> Self {
		self.min_amount = Some(amount);
		self
	}

	#[allow(dead_code)]
	pub fn with_max_amount(mut self, amount: u128) -> Self {
		self.max_amount = Some(amount);
		self
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_transfer_query_params_default() {
		let params = TransferQueryParams::default();
		assert_eq!(params.limit, 0);
		assert_eq!(params.offset, 0);
		assert!(params.after_block.is_none());
		assert!(params.before_block.is_none());
		assert!(params.min_amount.is_none());
		assert!(params.max_amount.is_none());
	}

	#[test]
	fn test_transfer_query_params_new() {
		let params = TransferQueryParams::new();
		assert_eq!(params.limit, 100);
		assert_eq!(params.offset, 0);
	}

	#[test]
	fn test_transfer_query_params_builder() {
		let params = TransferQueryParams::new()
			.with_limit(50)
			.with_offset(10)
			.with_after_block(1000)
			.with_before_block(2000)
			.with_min_amount(1_000_000)
			.with_max_amount(10_000_000);

		assert_eq!(params.limit, 50);
		assert_eq!(params.offset, 10);
		assert_eq!(params.after_block, Some(1000));
		assert_eq!(params.before_block, Some(2000));
		assert_eq!(params.min_amount, Some(1_000_000));
		assert_eq!(params.max_amount, Some(10_000_000));
	}

	#[test]
	fn test_transfer_deserialization() {
		let json = r#"{
            "id": "transfer-123",
            "blockId": "block-456",
            "blockHeight": 12345,
            "timestamp": "2024-01-15T12:30:00Z",
            "extrinsicHash": "0xabcd1234",
            "fromId": "qzAlice123",
            "toId": "qzBob456",
            "amount": "1000000000000",
            "fee": "1000000",
            "fromHash": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234",
            "toHash": "5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh",
            "leafIndex": "42",
            "transferCount": "100"
        }"#;

		let transfer: Transfer = serde_json::from_str(json).expect("should deserialize");

		assert_eq!(transfer.id, "transfer-123");
		assert_eq!(transfer.block_id, "block-456");
		assert_eq!(transfer.block_height, 12345);
		assert_eq!(transfer.timestamp, "2024-01-15T12:30:00Z");
		assert_eq!(transfer.extrinsic_hash, Some("0xabcd1234".to_string()));
		assert_eq!(transfer.from_id, "qzAlice123");
		assert_eq!(transfer.to_id, "qzBob456");
		assert_eq!(transfer.amount, "1000000000000");
		assert_eq!(transfer.fee, "1000000");
		assert_eq!(transfer.leaf_index, "42");
		assert_eq!(transfer.transfer_count, "100");
	}

	#[test]
	fn test_transfer_deserialization_null_extrinsic_hash() {
		let json = r#"{
            "id": "transfer-123",
            "blockId": "block-456",
            "blockHeight": 12345,
            "timestamp": "2024-01-15T12:30:00Z",
            "extrinsicHash": null,
            "fromId": "qzAlice123",
            "toId": "qzBob456",
            "amount": "1000000000000",
            "fee": "1000000",
            "fromHash": "abcd1234",
            "toHash": "5678efgh",
            "leafIndex": "0",
            "transferCount": "1"
        }"#;

		let transfer: Transfer = serde_json::from_str(json).expect("should deserialize");
		assert!(transfer.extrinsic_hash.is_none());
	}

	#[test]
	fn test_hasura_transfer_row_deserialization() {
		// Hasura returns numeric columns as JSON numbers by default.
		let json = r#"{
            "id": "transfer-123",
            "block_id": "block-456",
            "block": { "height": 12345 },
            "timestamp": "2024-01-15T12:30:00+00:00",
            "extrinsic_id": "0xabcd1234",
            "from_id": "qzAlice123",
            "to_id": "qzBob456",
            "amount": 1000000000000,
            "fee": 1000000,
            "from_hash": "abcd1234",
            "to_hash": "5678efgh",
            "leaf_index": 42,
            "transfer_count": 100
        }"#;

		let row: HasuraTransferRow = serde_json::from_str(json).expect("should deserialize");
		let transfer: Transfer = row.into();

		assert_eq!(transfer.id, "transfer-123");
		assert_eq!(transfer.block_id, "block-456");
		assert_eq!(transfer.block_height, 12345);
		assert_eq!(transfer.extrinsic_hash, Some("0xabcd1234".to_string()));
		assert_eq!(transfer.from_id, "qzAlice123");
		assert_eq!(transfer.to_id, "qzBob456");
		assert_eq!(transfer.amount, "1000000000000");
		assert_eq!(transfer.fee, "1000000");
		assert_eq!(transfer.leaf_index, "42");
		assert_eq!(transfer.transfer_count, "100");
	}

	#[test]
	fn test_hasura_transfer_row_stringified_numerics_and_nulls() {
		// With HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES enabled, numerics come as strings.
		let json = r#"{
            "id": "transfer-123",
            "block_id": null,
            "block": null,
            "timestamp": "2024-01-15T12:30:00+00:00",
            "extrinsic_id": null,
            "from_id": null,
            "to_id": null,
            "amount": "1000000000000",
            "fee": "0",
            "from_hash": "abcd1234",
            "to_hash": "5678efgh",
            "leaf_index": "0",
            "transfer_count": "1"
        }"#;

		let row: HasuraTransferRow = serde_json::from_str(json).expect("should deserialize");
		let transfer: Transfer = row.into();

		assert_eq!(transfer.block_id, "");
		assert_eq!(transfer.block_height, 0);
		assert!(transfer.extrinsic_hash.is_none());
		assert_eq!(transfer.amount, "1000000000000");
		assert_eq!(transfer.fee, "0");
	}

	#[test]
	fn test_hasura_nullifier_row_deserialization() {
		let json = r#"{
            "nullifier": "0xdeadbeef",
            "nullifier_hash": "aabbccdd",
            "block": { "height": 777 },
            "timestamp": "2024-02-01T00:00:00+00:00",
            "wormholeExtrinsic": { "extrinsic_id": "0xfeed" }
        }"#;

		let row: HasuraNullifierRow = serde_json::from_str(json).expect("should deserialize");
		let result: NullifierResult = row.into();

		assert_eq!(result.nullifier, "0xdeadbeef");
		assert_eq!(result.nullifier_hash, "aabbccdd");
		assert_eq!(result.extrinsic_hash, "0xfeed");
		assert_eq!(result.block_height, 777);
		assert_eq!(result.timestamp, "2024-02-01T00:00:00+00:00");
	}

	#[test]
	fn test_graphql_response_with_error() {
		let json = r#"{
            "data": null,
            "errors": [
                {
                    "message": "Query returned too many results",
                    "locations": [{"line": 1, "column": 1}],
                    "path": ["transfer"]
                }
            ]
        }"#;

		let response: GraphQLResponse<serde_json::Value> =
			serde_json::from_str(json).expect("should deserialize");

		assert!(response.data.is_none());
		assert!(response.errors.is_some());

		let errors = response.errors.unwrap();
		assert_eq!(errors.len(), 1);
		assert_eq!(errors[0].message, "Query returned too many results");
	}
}