hivecomb 0.1.2

A Rust library for the Hive blockchain: keys, Graphene serialization, transaction signing and RPC.
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
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Virtual operations.
//!
//! Virtual operations are emitted **by the chain**, never submitted. They record things
//! that happen as a consequence of consensus rather than of a signed request: a payout
//! landing, a conversion settling, a power-down instalment, a witness missing a block.
//! They appear in `account_history_api` and in block output, and they can never be
//! signed or broadcast — which is why they are a separate type here rather than
//! variants of [`Operation`](crate::operations::Operation).
//!
//! # beem models none of these
//!
//! beem has no class for any virtual operation. It hands back the raw dictionary from
//! the API and leaves every caller to reach into it by key, so a renamed or newly-added
//! field surfaces as a `KeyError` at the point of use rather than at the point of
//! decode. Roughly a third of the operations here — everything marked *Added in HF25*
//! or later — postdates beem's last release entirely.
//!
//! Its operation-id table is also wrong for all of them: because two non-virtual
//! operations are missing from it, **every virtual id beem reports is two lower than
//! the chain's** (see `SECURITY_FINDINGS.md` finding 2).

use crate::asset::Amount;
use crate::error::{Error, Result};
use crate::operations::OperationId;

/// `fill_convert_request_operation` (id 50).
///
/// An HBD to HIVE conversion completing after 3.5 days.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillConvertRequest {
    pub owner: String,
    pub requestid: u32,
    pub amount_in: Amount,
    pub amount_out: Amount,
}
/// `author_reward_operation` (id 51).
///
/// An author's share of a post payout.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AuthorReward {
    pub author: String,
    pub permlink: String,
    pub hbd_payout: Amount,
    pub hive_payout: Amount,
    pub vesting_payout: Amount,
    pub curators_vesting_payout: Amount,
    pub payout_must_be_claimed: bool,
}
/// `curation_reward_operation` (id 52).
///
/// A curator's share of a post payout.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CurationReward {
    pub curator: String,
    pub reward: Amount,
    pub author: String,
    pub permlink: String,
    pub payout_must_be_claimed: bool,
}
/// `comment_reward_operation` (id 53).
///
/// The totals for a post's payout.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CommentReward {
    pub author: String,
    pub permlink: String,
    pub payout: Amount,
    pub author_rewards: i64,
    pub total_payout_value: Amount,
    pub curator_payout_value: Amount,
    pub beneficiary_payout_value: Amount,
}
/// `liquidity_reward_operation` (id 54).
///
/// The market-making reward. Discontinued.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LiquidityReward {
    pub owner: String,
    pub payout: Amount,
}
/// `interest_operation` (id 55).
///
/// HBD savings interest.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Interest {
    pub owner: String,
    pub interest: Amount,
    pub is_saved_into_hbd_balance: bool,
}
/// `fill_vesting_withdraw_operation` (id 56).
///
/// One weekly instalment of a power-down.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillVestingWithdraw {
    pub from_account: String,
    pub to_account: String,
    pub withdrawn: Amount,
    pub deposited: Amount,
}
/// `fill_order_operation` (id 57).
///
/// A market order matching.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillOrder {
    pub current_owner: String,
    pub current_orderid: u32,
    pub current_pays: Amount,
    pub open_owner: String,
    pub open_orderid: u32,
    pub open_pays: Amount,
}
/// `shutdown_witness_operation` (id 58).
///
/// A witness disabled for missing too many blocks.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ShutdownWitness {
    pub owner: String,
}
/// `fill_transfer_from_savings_operation` (id 59).
///
/// A savings withdrawal completing after three days.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillTransferFromSavings {
    pub from: String,
    pub to: String,
    pub amount: Amount,
    pub request_id: u32,
    pub memo: String,
}
/// `hardfork_operation` (id 60).
///
/// A hardfork activating.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Hardfork {
    pub hardfork_id: u32,
}
/// `comment_payout_update_operation` (id 61).
///
/// A post's payout being recalculated.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CommentPayoutUpdate {
    pub author: String,
    pub permlink: String,
}
/// `return_vesting_delegation_operation` (id 62).
///
/// Delegated VESTS returning after the cooldown.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ReturnVestingDelegation {
    pub account: String,
    pub vesting_shares: Amount,
}
/// `comment_benefactor_reward_operation` (id 63).
///
/// A beneficiary's share of a post payout.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CommentBenefactorReward {
    pub benefactor: String,
    pub author: String,
    pub permlink: String,
    pub hbd_payout: Amount,
    pub hive_payout: Amount,
    pub vesting_payout: Amount,
    pub payout_must_be_claimed: bool,
}
/// `producer_reward_operation` (id 64).
///
/// A witness's reward for producing a block.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProducerReward {
    pub producer: String,
    pub vesting_shares: Amount,
}
/// `clear_null_account_balance_operation` (id 65).
///
/// Funds sent to @null being burned.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ClearNullAccountBalance {
    pub total_cleared: Vec<Amount>,
}
/// `proposal_pay_operation` (id 66).
///
/// An hourly DHF proposal payment.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProposalPay {
    pub proposal_id: u32,
    pub receiver: String,
    pub payer: String,
    pub payment: Amount,
}
/// `dhf_funding_operation` (id 67).
///
/// The DHF receiving its share of inflation.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DhfFunding {
    pub treasury: String,
    pub additional_funds: Amount,
}
/// `hardfork_hive_operation` (id 68).
///
/// An account's balance moved to the treasury at the Hive fork.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct HardforkHive {
    pub account: String,
    pub treasury: String,
    pub other_affected_accounts: Vec<String>,
    pub hbd_transferred: Amount,
    pub hive_transferred: Amount,
    pub vests_converted: Amount,
    pub total_hive_from_vests: Amount,
}
/// `hardfork_hive_restore_operation` (id 69).
///
/// A balance restored after the Hive fork.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct HardforkHiveRestore {
    pub account: String,
    pub treasury: String,
    pub hbd_transferred: Amount,
    pub hive_transferred: Amount,
}
/// `delayed_voting_operation` (id 70).
///
/// Newly powered-up VESTS becoming eligible to vote for witnesses.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DelayedVoting {
    pub voter: String,
    pub votes: u64,
}
/// `consolidate_treasury_balance_operation` (id 71).
///
/// Balances moved into the treasury account.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ConsolidateTreasuryBalance {
    pub total_moved: Vec<Amount>,
}
/// `effective_comment_vote_operation` (id 72).
///
/// A vote's effect on a post, after all the curve maths.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EffectiveCommentVote {
    pub voter: String,
    pub author: String,
    pub permlink: String,
    pub weight: u64,
    pub rshares: i64,
    pub total_vote_weight: u64,
    pub pending_payout: Amount,
}
/// `ineffective_delete_comment_operation` (id 73).
///
/// A delete that could not take effect.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IneffectiveDeleteComment {
    pub author: String,
    pub permlink: String,
}
/// `dhf_conversion_operation` (id 74).
///
/// The DHF converting HIVE to HBD.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DhfConversion {
    pub treasury: String,
    pub hive_amount_in: Amount,
    pub hbd_amount_out: Amount,
}
/// `expired_account_notification_operation` (id 75).
///
/// A governance vote expiring after a year of inactivity. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExpiredAccountNotification {
    pub account: String,
}
/// `changed_recovery_account_operation` (id 76).
///
/// A recovery-account change taking effect after 30 days.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ChangedRecoveryAccount {
    pub account: String,
    pub old_recovery_account: String,
    pub new_recovery_account: String,
}
/// `transfer_to_vesting_completed_operation` (id 77).
///
/// A power-up completing, naming the VESTS actually received.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TransferToVestingCompleted {
    pub from_account: String,
    pub to_account: String,
    pub hive_vested: Amount,
    pub vesting_shares_received: Amount,
}
/// `pow_reward_operation` (id 78).
///
/// A mining reward. Obsolete since HF17.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PowReward {
    pub worker: String,
    pub reward: Amount,
}
/// `vesting_shares_split_operation` (id 79).
///
/// The one-off VESTS redenomination.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct VestingSharesSplit {
    pub owner: String,
    pub vesting_shares_before_split: Amount,
    pub vesting_shares_after_split: Amount,
}
/// `account_created_operation` (id 80).
///
/// An account being created.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AccountCreated {
    pub new_account_name: String,
    pub creator: String,
    pub initial_vesting_shares: Amount,
    pub initial_delegation: Amount,
}
/// `fill_collateralized_convert_request_operation` (id 81).
///
/// A collateralized conversion settling. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillCollateralizedConvertRequest {
    pub owner: String,
    pub requestid: u32,
    pub amount_in: Amount,
    pub amount_out: Amount,
    pub excess_collateral: Amount,
}
/// `system_warning_operation` (id 82).
///
/// A diagnostic emitted by the node.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SystemWarning {
    pub message: String,
}
/// `fill_recurrent_transfer_operation` (id 83).
///
/// One instalment of a recurrent transfer. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FillRecurrentTransfer {
    pub from: String,
    pub to: String,
    pub amount: Amount,
    pub memo: String,
    pub remaining_executions: u16,
    /// HF28 `pair_id` extension, absent on older records.
    #[serde(default)]
    pub extensions: Vec<crate::operations::RecurrentTransferExtension>,
}
/// `failed_recurrent_transfer_operation` (id 84).
///
/// A recurrent transfer instalment that could not be paid. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FailedRecurrentTransfer {
    pub from: String,
    pub to: String,
    pub amount: Amount,
    pub memo: String,
    pub consecutive_failures: u8,
    pub remaining_executions: u16,
    pub deleted: bool,
    /// HF28 `pair_id` extension, absent on older records.
    #[serde(default)]
    pub extensions: Vec<crate::operations::RecurrentTransferExtension>,
}
/// `limit_order_cancelled_operation` (id 85).
///
/// An order cancelled or expired, naming what came back. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LimitOrderCancelled {
    pub seller: String,
    pub orderid: u32,
    pub amount_back: Amount,
}
/// `producer_missed_operation` (id 86).
///
/// A witness missing its block. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProducerMissed {
    pub producer: String,
}
/// `proposal_fee_operation` (id 87).
///
/// The fee charged for creating a DHF proposal. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProposalFee {
    pub creator: String,
    pub treasury: String,
    pub proposal_id: u32,
    pub fee: Amount,
}
/// `collateralized_convert_immediate_conversion_operation` (id 88).
///
/// The HBD paid out immediately by a collateralized conversion. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CollateralizedConvertImmediateConversion {
    pub owner: String,
    pub requestid: u32,
    pub hbd_out: Amount,
}
/// `escrow_approved_operation` (id 89).
///
/// An escrow both parties approved. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EscrowApproved {
    pub from: String,
    pub to: String,
    pub agent: String,
    pub escrow_id: u32,
    pub fee: Amount,
}
/// `escrow_rejected_operation` (id 90).
///
/// An escrow that was not ratified in time. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EscrowRejected {
    pub from: String,
    pub to: String,
    pub agent: String,
    pub escrow_id: u32,
    pub hbd_amount: Amount,
    pub hive_amount: Amount,
    pub fee: Amount,
}
/// `proxy_cleared_operation` (id 91).
///
/// A witness proxy being cleared. Added in HF25.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ProxyCleared {
    pub account: String,
    pub proxy: String,
}
/// `declined_voting_rights_operation` (id 92).
///
/// A decline_voting_rights request taking effect after 30 days. Added in HF26.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DeclinedVotingRights {
    pub account: String,
}

/// An operation emitted by the chain.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum VirtualOperation {
    FillConvertRequest(FillConvertRequest),
    AuthorReward(AuthorReward),
    CurationReward(CurationReward),
    CommentReward(CommentReward),
    LiquidityReward(LiquidityReward),
    Interest(Interest),
    FillVestingWithdraw(FillVestingWithdraw),
    FillOrder(FillOrder),
    ShutdownWitness(ShutdownWitness),
    FillTransferFromSavings(FillTransferFromSavings),
    Hardfork(Hardfork),
    CommentPayoutUpdate(CommentPayoutUpdate),
    ReturnVestingDelegation(ReturnVestingDelegation),
    CommentBenefactorReward(CommentBenefactorReward),
    ProducerReward(ProducerReward),
    ClearNullAccountBalance(ClearNullAccountBalance),
    ProposalPay(ProposalPay),
    DhfFunding(DhfFunding),
    HardforkHive(HardforkHive),
    HardforkHiveRestore(HardforkHiveRestore),
    DelayedVoting(DelayedVoting),
    ConsolidateTreasuryBalance(ConsolidateTreasuryBalance),
    EffectiveCommentVote(EffectiveCommentVote),
    IneffectiveDeleteComment(IneffectiveDeleteComment),
    DhfConversion(DhfConversion),
    ExpiredAccountNotification(ExpiredAccountNotification),
    ChangedRecoveryAccount(ChangedRecoveryAccount),
    TransferToVestingCompleted(TransferToVestingCompleted),
    PowReward(PowReward),
    VestingSharesSplit(VestingSharesSplit),
    AccountCreated(AccountCreated),
    FillCollateralizedConvertRequest(FillCollateralizedConvertRequest),
    SystemWarning(SystemWarning),
    FillRecurrentTransfer(FillRecurrentTransfer),
    FailedRecurrentTransfer(FailedRecurrentTransfer),
    LimitOrderCancelled(LimitOrderCancelled),
    ProducerMissed(ProducerMissed),
    ProposalFee(ProposalFee),
    CollateralizedConvertImmediateConversion(CollateralizedConvertImmediateConversion),
    EscrowApproved(EscrowApproved),
    EscrowRejected(EscrowRejected),
    ProxyCleared(ProxyCleared),
    DeclinedVotingRights(DeclinedVotingRights),
}

impl VirtualOperation {
    /// The operation's id in hived's static variant.
    pub fn id(&self) -> OperationId {
        match self {
            VirtualOperation::FillConvertRequest(_) => OperationId::FillConvertRequest,
            VirtualOperation::AuthorReward(_) => OperationId::AuthorReward,
            VirtualOperation::CurationReward(_) => OperationId::CurationReward,
            VirtualOperation::CommentReward(_) => OperationId::CommentReward,
            VirtualOperation::LiquidityReward(_) => OperationId::LiquidityReward,
            VirtualOperation::Interest(_) => OperationId::Interest,
            VirtualOperation::FillVestingWithdraw(_) => OperationId::FillVestingWithdraw,
            VirtualOperation::FillOrder(_) => OperationId::FillOrder,
            VirtualOperation::ShutdownWitness(_) => OperationId::ShutdownWitness,
            VirtualOperation::FillTransferFromSavings(_) => OperationId::FillTransferFromSavings,
            VirtualOperation::Hardfork(_) => OperationId::Hardfork,
            VirtualOperation::CommentPayoutUpdate(_) => OperationId::CommentPayoutUpdate,
            VirtualOperation::ReturnVestingDelegation(_) => OperationId::ReturnVestingDelegation,
            VirtualOperation::CommentBenefactorReward(_) => OperationId::CommentBenefactorReward,
            VirtualOperation::ProducerReward(_) => OperationId::ProducerReward,
            VirtualOperation::ClearNullAccountBalance(_) => OperationId::ClearNullAccountBalance,
            VirtualOperation::ProposalPay(_) => OperationId::ProposalPay,
            VirtualOperation::DhfFunding(_) => OperationId::DhfFunding,
            VirtualOperation::HardforkHive(_) => OperationId::HardforkHive,
            VirtualOperation::HardforkHiveRestore(_) => OperationId::HardforkHiveRestore,
            VirtualOperation::DelayedVoting(_) => OperationId::DelayedVoting,
            VirtualOperation::ConsolidateTreasuryBalance(_) => {
                OperationId::ConsolidateTreasuryBalance
            }
            VirtualOperation::EffectiveCommentVote(_) => OperationId::EffectiveCommentVote,
            VirtualOperation::IneffectiveDeleteComment(_) => OperationId::IneffectiveDeleteComment,
            VirtualOperation::DhfConversion(_) => OperationId::DhfConversion,
            VirtualOperation::ExpiredAccountNotification(_) => {
                OperationId::ExpiredAccountNotification
            }
            VirtualOperation::ChangedRecoveryAccount(_) => OperationId::ChangedRecoveryAccount,
            VirtualOperation::TransferToVestingCompleted(_) => {
                OperationId::TransferToVestingCompleted
            }
            VirtualOperation::PowReward(_) => OperationId::PowReward,
            VirtualOperation::VestingSharesSplit(_) => OperationId::VestingSharesSplit,
            VirtualOperation::AccountCreated(_) => OperationId::AccountCreated,
            VirtualOperation::FillCollateralizedConvertRequest(_) => {
                OperationId::FillCollateralizedConvertRequest
            }
            VirtualOperation::SystemWarning(_) => OperationId::SystemWarning,
            VirtualOperation::FillRecurrentTransfer(_) => OperationId::FillRecurrentTransfer,
            VirtualOperation::FailedRecurrentTransfer(_) => OperationId::FailedRecurrentTransfer,
            VirtualOperation::LimitOrderCancelled(_) => OperationId::LimitOrderCancelled,
            VirtualOperation::ProducerMissed(_) => OperationId::ProducerMissed,
            VirtualOperation::ProposalFee(_) => OperationId::ProposalFee,
            VirtualOperation::CollateralizedConvertImmediateConversion(_) => {
                OperationId::CollateralizedConvertImmediateConversion
            }
            VirtualOperation::EscrowApproved(_) => OperationId::EscrowApproved,
            VirtualOperation::EscrowRejected(_) => OperationId::EscrowRejected,
            VirtualOperation::ProxyCleared(_) => OperationId::ProxyCleared,
            VirtualOperation::DeclinedVotingRights(_) => OperationId::DeclinedVotingRights,
        }
    }

    /// The hived name, without the `_operation` suffix.
    pub fn name(&self) -> &'static str {
        self.id().name()
    }

    /// Parse from either JSON shape the API uses.
    ///
    /// `condenser_api` sends `["producer_reward", {...}]`; appbase APIs send
    /// `{"type": "producer_reward_operation", "value": {...}}`. Both are accepted,
    /// and an unrecognised name is an error rather than a silently-dropped record.
    pub fn from_json(value: &serde_json::Value) -> Result<Self> {
        let (name, payload) = split_operation_json(value)?;
        Self::from_parts(&name, payload)
    }

    fn from_parts(name: &str, value: serde_json::Value) -> Result<Self> {
        let name = name.strip_suffix("_operation").unwrap_or(name);
        let de_err = |e: serde_json::Error| Error::ser(format!("could not decode {name}: {e}"));
        Ok(match name {
            "fill_convert_request" => {
                VirtualOperation::FillConvertRequest(serde_json::from_value(value).map_err(de_err)?)
            }
            "author_reward" => {
                VirtualOperation::AuthorReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "curation_reward" => {
                VirtualOperation::CurationReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "comment_reward" => {
                VirtualOperation::CommentReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "liquidity_reward" => {
                VirtualOperation::LiquidityReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "interest" => {
                VirtualOperation::Interest(serde_json::from_value(value).map_err(de_err)?)
            }
            "fill_vesting_withdraw" => VirtualOperation::FillVestingWithdraw(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "fill_order" => {
                VirtualOperation::FillOrder(serde_json::from_value(value).map_err(de_err)?)
            }
            "shutdown_witness" => {
                VirtualOperation::ShutdownWitness(serde_json::from_value(value).map_err(de_err)?)
            }
            "fill_transfer_from_savings" => VirtualOperation::FillTransferFromSavings(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "hardfork" => {
                VirtualOperation::Hardfork(serde_json::from_value(value).map_err(de_err)?)
            }
            "comment_payout_update" => VirtualOperation::CommentPayoutUpdate(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "return_vesting_delegation" => VirtualOperation::ReturnVestingDelegation(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "comment_benefactor_reward" => VirtualOperation::CommentBenefactorReward(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "producer_reward" => {
                VirtualOperation::ProducerReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "clear_null_account_balance" => VirtualOperation::ClearNullAccountBalance(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "proposal_pay" => {
                VirtualOperation::ProposalPay(serde_json::from_value(value).map_err(de_err)?)
            }
            "dhf_funding" => {
                VirtualOperation::DhfFunding(serde_json::from_value(value).map_err(de_err)?)
            }
            "hardfork_hive" => {
                VirtualOperation::HardforkHive(serde_json::from_value(value).map_err(de_err)?)
            }
            "hardfork_hive_restore" => VirtualOperation::HardforkHiveRestore(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "delayed_voting" => {
                VirtualOperation::DelayedVoting(serde_json::from_value(value).map_err(de_err)?)
            }
            "consolidate_treasury_balance" => VirtualOperation::ConsolidateTreasuryBalance(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "effective_comment_vote" => VirtualOperation::EffectiveCommentVote(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "ineffective_delete_comment" => VirtualOperation::IneffectiveDeleteComment(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "dhf_conversion" => {
                VirtualOperation::DhfConversion(serde_json::from_value(value).map_err(de_err)?)
            }
            "expired_account_notification" => VirtualOperation::ExpiredAccountNotification(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "changed_recovery_account" => VirtualOperation::ChangedRecoveryAccount(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "transfer_to_vesting_completed" => VirtualOperation::TransferToVestingCompleted(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "pow_reward" => {
                VirtualOperation::PowReward(serde_json::from_value(value).map_err(de_err)?)
            }
            "vesting_shares_split" => {
                VirtualOperation::VestingSharesSplit(serde_json::from_value(value).map_err(de_err)?)
            }
            "account_created" => {
                VirtualOperation::AccountCreated(serde_json::from_value(value).map_err(de_err)?)
            }
            "fill_collateralized_convert_request" => {
                VirtualOperation::FillCollateralizedConvertRequest(
                    serde_json::from_value(value).map_err(de_err)?,
                )
            }
            "system_warning" => {
                VirtualOperation::SystemWarning(serde_json::from_value(value).map_err(de_err)?)
            }
            "fill_recurrent_transfer" => VirtualOperation::FillRecurrentTransfer(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "failed_recurrent_transfer" => VirtualOperation::FailedRecurrentTransfer(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "limit_order_cancelled" => VirtualOperation::LimitOrderCancelled(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            "producer_missed" => {
                VirtualOperation::ProducerMissed(serde_json::from_value(value).map_err(de_err)?)
            }
            "proposal_fee" => {
                VirtualOperation::ProposalFee(serde_json::from_value(value).map_err(de_err)?)
            }
            "collateralized_convert_immediate_conversion" => {
                VirtualOperation::CollateralizedConvertImmediateConversion(
                    serde_json::from_value(value).map_err(de_err)?,
                )
            }
            "escrow_approved" => {
                VirtualOperation::EscrowApproved(serde_json::from_value(value).map_err(de_err)?)
            }
            "escrow_rejected" => {
                VirtualOperation::EscrowRejected(serde_json::from_value(value).map_err(de_err)?)
            }
            "proxy_cleared" => {
                VirtualOperation::ProxyCleared(serde_json::from_value(value).map_err(de_err)?)
            }
            "declined_voting_rights" => VirtualOperation::DeclinedVotingRights(
                serde_json::from_value(value).map_err(de_err)?,
            ),
            other => {
                return Err(Error::Unknown {
                    kind: "virtual operation",
                    name: other.to_string(),
                })
            }
        })
    }
}

/// Split either JSON operation shape into a name and a payload.
///
/// Shared with [`crate::operations`], which parses the non-virtual half the same way.
/// Split an operation into its name and payload, **taking ownership**.
///
/// The borrowing version has to clone the payload, and for a caller that already owns
/// the `Value` — anything that just parsed it out of a request body — that clone is a
/// full deep copy of every field for nothing. Measured through the Node addon it was
/// the largest single cost in converting a 50-operation transaction.
///
/// Same two shapes as [`split_operation_json`]: appbase `{"type", "value"}` and
/// condenser `["name", {...}]`.
pub(crate) fn split_operation_json_owned(
    value: serde_json::Value,
) -> Result<(String, serde_json::Value)> {
    match value {
        serde_json::Value::Object(mut map) => {
            // Only treat it as appbase if both keys are there; otherwise fall through to
            // the error, rather than half-consuming the map and guessing.
            if map.contains_key("type") && map.contains_key("value") {
                let payload = map.remove("value").expect("checked above");
                match map.remove("type").expect("checked above") {
                    serde_json::Value::String(name) => return Ok((name, payload)),
                    _ => return Err(Error::ser("operation \"type\" is not a string")),
                }
            }
            Err(Error::ser(
                "operation must be [name, value] or {type, value}".to_string(),
            ))
        }
        serde_json::Value::Array(mut arr) if arr.len() == 2 => {
            // Pop from the back so neither removal shifts the other.
            let payload = arr.pop().expect("length checked");
            match arr.pop().expect("length checked") {
                serde_json::Value::String(name) => Ok((name, payload)),
                _ => Err(Error::ser("operation name is not a string")),
            }
        }
        _ => Err(Error::ser(
            "operation must be [name, value] or {type, value}".to_string(),
        )),
    }
}

pub(crate) fn split_operation_json(
    value: &serde_json::Value,
) -> Result<(String, serde_json::Value)> {
    // Appbase: {"type": "...", "value": {...}}
    if let Some(obj) = value.as_object() {
        if let (Some(t), Some(v)) = (obj.get("type"), obj.get("value")) {
            let name = t
                .as_str()
                .ok_or_else(|| Error::ser("operation \"type\" is not a string"))?;
            return Ok((name.to_string(), v.clone()));
        }
    }
    // Condenser: ["name", {...}]
    if let Some(arr) = value.as_array() {
        if arr.len() == 2 {
            let name = arr[0]
                .as_str()
                .ok_or_else(|| Error::ser("operation name is not a string"))?;
            return Ok((name.to_string(), arr[1].clone()));
        }
    }
    Err(Error::ser(
        "operation must be [name, value] or {type, value}".to_string(),
    ))
}