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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use super::bitcoin::{TxIn, TxOut};
use crate::events::*;
use schemars::JsonSchema;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt::Display;
use std::hash::{Hash, Hasher};
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct BlockIdentifier {
pub index: u64,
pub hash: String,
}
impl Display for BlockIdentifier {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Block #{} ({}...{})",
self.index,
&self.hash.as_str()[0..6],
&self.hash.as_str()[62..]
)
}
}
impl Hash for BlockIdentifier {
fn hash<H: Hasher>(&self, state: &mut H) {
self.hash.hash(state);
}
}
impl Ord for BlockIdentifier {
fn cmp(&self, other: &Self) -> Ordering {
(other.index, &other.hash).cmp(&(self.index, &self.hash))
}
}
impl PartialOrd for BlockIdentifier {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(other.cmp(self))
}
}
impl PartialEq for BlockIdentifier {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}
impl Eq for BlockIdentifier {}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksBlockData {
pub block_identifier: BlockIdentifier,
pub parent_block_identifier: BlockIdentifier,
pub timestamp: i64,
pub transactions: Vec<StacksTransactionData>,
pub metadata: StacksBlockMetadata,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksMicroblockData {
pub block_identifier: BlockIdentifier,
pub parent_block_identifier: BlockIdentifier,
pub timestamp: i64,
pub transactions: Vec<StacksTransactionData>,
pub metadata: StacksMicroblockMetadata,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksMicroblockMetadata {
pub anchor_block_identifier: BlockIdentifier,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksMicroblocksTrail {
pub microblocks: Vec<StacksMicroblockData>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksBlockMetadata {
pub bitcoin_anchor_block_identifier: BlockIdentifier,
pub pox_cycle_index: u32,
pub pox_cycle_position: u32,
pub pox_cycle_length: u32,
pub confirm_microblock_identifier: Option<BlockIdentifier>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BitcoinBlockData {
pub block_identifier: BlockIdentifier,
pub parent_block_identifier: BlockIdentifier,
pub timestamp: u32,
pub transactions: Vec<BitcoinTransactionData>,
pub metadata: BitcoinBlockMetadata,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BitcoinBlockMetadata {}
#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Timestamp(i64);
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksTransactionData {
pub transaction_identifier: TransactionIdentifier,
pub operations: Vec<Operation>,
pub metadata: StacksTransactionMetadata,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum StacksTransactionKind {
ContractCall(StacksContractCallData),
ContractDeployment(StacksContractDeploymentData),
NativeTokenTransfer,
Coinbase,
Other,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksContractCallData {
pub contract_identifier: String,
pub method: String,
pub args: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksContractDeploymentData {
pub contract_identifier: String,
pub code: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksTransactionMetadata {
pub success: bool,
pub raw_tx: String,
pub result: String,
pub sender: String,
pub nonce: u64,
pub fee: u64,
pub kind: StacksTransactionKind,
pub receipt: StacksTransactionReceipt,
pub description: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub sponsor: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub execution_cost: Option<StacksTransactionExecutionCost>,
pub position: StacksTransactionPosition,
pub proof: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum StacksTransactionPosition {
AnchorBlock(AnchorBlockPosition),
MicroBlock(MicroBlockPosition),
}
impl StacksTransactionPosition {
pub fn anchor_block(index: usize) -> StacksTransactionPosition {
StacksTransactionPosition::AnchorBlock(AnchorBlockPosition { index })
}
pub fn micro_block(
micro_block_identifier: BlockIdentifier,
index: usize,
) -> StacksTransactionPosition {
StacksTransactionPosition::MicroBlock(MicroBlockPosition {
micro_block_identifier,
index,
})
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct AnchorBlockPosition {
index: usize,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct MicroBlockPosition {
micro_block_identifier: BlockIdentifier,
index: usize,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StacksTransactionExecutionCost {
pub write_length: u64,
pub write_count: u64,
pub read_length: u64,
pub read_count: u64,
pub runtime: u64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
pub struct StacksTransactionReceipt {
pub mutated_contracts_radius: HashSet<String>,
pub mutated_assets_radius: HashSet<String>,
pub contract_calls_stack: HashSet<String>,
pub events: Vec<StacksTransactionEvent>,
}
impl StacksTransactionReceipt {
pub fn new(
mutated_contracts_radius: HashSet<String>,
mutated_assets_radius: HashSet<String>,
events: Vec<StacksTransactionEvent>,
) -> StacksTransactionReceipt {
StacksTransactionReceipt {
mutated_contracts_radius,
mutated_assets_radius,
contract_calls_stack: HashSet::new(),
events,
}
}
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BitcoinTransactionData {
pub transaction_identifier: TransactionIdentifier,
pub operations: Vec<Operation>,
pub metadata: BitcoinTransactionMetadata,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BitcoinTransactionMetadata {
pub inputs: Vec<TxIn>,
pub outputs: Vec<TxOut>,
pub stacks_operations: Vec<StacksBaseChainOperation>,
pub ordinal_operations: Vec<OrdinalOperation>,
pub proof: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum OrdinalOperation {
InscriptionCommitted(OrdinalInscriptionCommitData),
InscriptionRevealed(OrdinalInscriptionRevealData),
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct OrdinalInscriptionCommitData {}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct OrdinalInscriptionRevealData {
pub content_type: String,
pub content: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum StacksBaseChainOperation {
PoxBlockCommitment(PoxBlockCommitmentData),
PobBlockCommitment(PobBlockCommitmentData),
KeyRegistration(KeyRegistrationData),
TransferSTX(TransferSTXData),
LockSTX(LockSTXData),
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct PoxBlockCommitmentData {
pub signers: Vec<String>,
pub stacks_block_hash: String,
pub rewards: Vec<PoxReward>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct PoxReward {
pub recipient: String,
pub amount: u64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct KeyRegistrationData;
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct PobBlockCommitmentData {
pub signers: Vec<String>,
pub stacks_block_hash: String,
pub amount: u64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct BlockCommitmentData {
pub stacks_block_hash: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct TransferSTXData {
pub sender: String,
pub recipient: String,
pub amount: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct LockSTXData {
pub sender: String,
pub amount: String,
pub duration: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash)]
pub struct TransactionIdentifier {
pub hash: String,
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::EnumIter, strum::IntoStaticStr,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OperationType {
Credit,
Debit,
Lock,
}
#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
pub struct OperationMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub public_key: Option<PublicKey>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub method_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub args: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PublicKey {
pub hex_bytes: Option<String>,
pub curve_type: CurveType,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CurveType {
Edwards25519,
Secp256k1,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Operation {
pub operation_identifier: OperationIdentifier,
#[serde(skip_serializing_if = "Option::is_none")]
pub related_operations: Option<Vec<OperationIdentifier>>,
#[serde(rename = "type")]
pub type_: OperationType,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<OperationStatusKind>,
pub account: AccountIdentifier,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<Amount>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<OperationMetadata>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperationIdentifier {
pub index: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub network_index: Option<i64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, strum::EnumIter)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OperationStatusKind {
Success,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct AccountIdentifier {
pub address: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub sub_account: Option<SubAccountIdentifier>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct SubAccountIdentifier {
pub address: SubAccount,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SubAccount {
LiquidBalanceForStorage,
Locked,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Amount {
pub value: u128,
pub currency: Currency,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Currency {
pub symbol: String,
pub decimals: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<CurrencyMetadata>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CurrencyStandard {
Sip09,
Sip10,
None,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CurrencyMetadata {
pub asset_class_identifier: String,
pub asset_identifier: Option<String>,
pub standard: CurrencyStandard,
}
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum BitcoinChainEvent {
ChainUpdatedWithBlocks(BitcoinChainUpdatedWithBlocksData),
ChainUpdatedWithReorg(BitcoinChainUpdatedWithReorgData),
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct BitcoinChainUpdatedWithBlocksData {
pub new_blocks: Vec<BitcoinBlockData>,
pub confirmed_blocks: Vec<BitcoinBlockData>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct BitcoinChainUpdatedWithReorgData {
pub blocks_to_rollback: Vec<BitcoinBlockData>,
pub blocks_to_apply: Vec<BitcoinBlockData>,
pub confirmed_blocks: Vec<BitcoinBlockData>,
}
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum StacksChainEvent {
ChainUpdatedWithBlocks(StacksChainUpdatedWithBlocksData),
ChainUpdatedWithReorg(StacksChainUpdatedWithReorgData),
ChainUpdatedWithMicroblocks(StacksChainUpdatedWithMicroblocksData),
ChainUpdatedWithMicroblocksReorg(StacksChainUpdatedWithMicroblocksReorgData),
}
impl StacksChainEvent {
pub fn get_confirmed_blocks(self) -> Vec<StacksBlockData> {
match self {
StacksChainEvent::ChainUpdatedWithBlocks(event) => event.confirmed_blocks,
StacksChainEvent::ChainUpdatedWithReorg(event) => event.confirmed_blocks,
_ => vec![],
}
}
pub fn new_block(&self) -> Option<&StacksBlockData> {
match self {
StacksChainEvent::ChainUpdatedWithBlocks(event) => {
event.new_blocks.first().and_then(|b| Some(&b.block))
}
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StacksBlockUpdate {
pub block: StacksBlockData,
pub parent_microblocks_to_rollback: Vec<StacksMicroblockData>,
pub parent_microblocks_to_apply: Vec<StacksMicroblockData>,
}
impl StacksBlockUpdate {
pub fn new(block: StacksBlockData) -> StacksBlockUpdate {
StacksBlockUpdate {
block,
parent_microblocks_to_rollback: vec![],
parent_microblocks_to_apply: vec![],
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StacksChainUpdatedWithBlocksData {
pub new_blocks: Vec<StacksBlockUpdate>,
pub confirmed_blocks: Vec<StacksBlockData>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StacksChainUpdatedWithReorgData {
pub blocks_to_rollback: Vec<StacksBlockUpdate>,
pub blocks_to_apply: Vec<StacksBlockUpdate>,
pub confirmed_blocks: Vec<StacksBlockData>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StacksChainUpdatedWithMicroblocksData {
pub new_microblocks: Vec<StacksMicroblockData>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StacksChainUpdatedWithMicroblocksReorgData {
pub microblocks_to_rollback: Vec<StacksMicroblockData>,
pub microblocks_to_apply: Vec<StacksMicroblockData>,
}
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum StacksNetwork {
Simnet,
Devnet,
Testnet,
Mainnet,
}
impl StacksNetwork {
pub fn is_simnet(&self) -> bool {
match self {
StacksNetwork::Simnet => true,
_ => false,
}
}
pub fn is_testnet(&self) -> bool {
match self {
StacksNetwork::Testnet => true,
_ => false,
}
}
pub fn either_devnet_or_testnet(&self) -> bool {
match self {
StacksNetwork::Devnet | StacksNetwork::Testnet => true,
_ => false,
}
}
pub fn either_testnet_or_mainnet(&self) -> bool {
match self {
StacksNetwork::Mainnet | StacksNetwork::Testnet => true,
_ => false,
}
}
pub fn is_devnet(&self) -> bool {
match self {
StacksNetwork::Devnet => true,
_ => false,
}
}
pub fn is_mainnet(&self) -> bool {
match self {
StacksNetwork::Mainnet => true,
_ => false,
}
}
pub fn get_networks(&self) -> (BitcoinNetwork, StacksNetwork) {
match &self {
StacksNetwork::Simnet => (BitcoinNetwork::Regtest, StacksNetwork::Simnet),
StacksNetwork::Devnet => (BitcoinNetwork::Testnet, StacksNetwork::Devnet),
StacksNetwork::Testnet => (BitcoinNetwork::Testnet, StacksNetwork::Testnet),
StacksNetwork::Mainnet => (BitcoinNetwork::Mainnet, StacksNetwork::Mainnet),
}
}
}
#[allow(dead_code)]
#[derive(
Debug, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum BitcoinNetwork {
Regtest,
Testnet,
Mainnet,
}