Skip to main content

drive/util/batch/drive_op_batch/
contract_fee_pot.rs

1use crate::drive::Drive;
2use crate::error::Error;
3use crate::fees::op::LowLevelDriveOperation;
4use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter;
5use dpp::balances::credits::Credits;
6use dpp::block::block_info::BlockInfo;
7use dpp::data_contract::document_type::action_fees::{ContractFeePot, ContractFeePotLastClaim};
8use dpp::identifier::Identifier;
9use grovedb::batch::KeyInfoPath;
10use grovedb::{EstimatedLayerInformation, TransactionArg};
11use platform_version::version::PlatformVersion;
12use std::collections::HashMap;
13
14/// Operations on the fee pots a contract's document action fees accumulate in.
15///
16/// Neither `AddToPot` nor `DeductFromPot` creates or destroys credits: the batch that carries
17/// one also moves the same credits out of, or into, identity balances.
18#[derive(Clone, Debug)]
19pub enum ContractFeePotOperationType {
20    /// Adds credits to a pot. The new total is computed from the committed one, so
21    /// `apply_drive_operations` merges every write of one pot in a batch into one.
22    AddToPot {
23        /// The contract the pot belongs to.
24        contract_id: Identifier,
25        /// The pot.
26        pot: ContractFeePot,
27        /// The credits to add.
28        amount: Credits,
29    },
30    /// Takes credits out of a pot.
31    DeductFromPot {
32        /// The contract the pot belongs to.
33        contract_id: Identifier,
34        /// The pot.
35        pot: ContractFeePot,
36        /// The credits to take out.
37        amount: Credits,
38    },
39    /// Records the claim that paid a pot out.
40    SetLastClaim {
41        /// The contract the pot belongs to.
42        contract_id: Identifier,
43        /// The pot.
44        pot: ContractFeePot,
45        /// The claim: its epoch, its block time and who claimed.
46        last_claim: ContractFeePotLastClaim,
47    },
48}
49
50impl DriveLowLevelOperationConverter for ContractFeePotOperationType {
51    fn into_low_level_drive_operations(
52        self,
53        drive: &Drive,
54        estimated_costs_only_with_layer_info: &mut Option<
55            HashMap<KeyInfoPath, EstimatedLayerInformation>,
56        >,
57        _block_info: &BlockInfo,
58        transaction: TransactionArg,
59        platform_version: &PlatformVersion,
60    ) -> Result<Vec<LowLevelDriveOperation>, Error> {
61        match self {
62            ContractFeePotOperationType::AddToPot {
63                contract_id,
64                pot,
65                amount,
66            } => drive.add_to_contract_fee_pot_operations(
67                contract_id,
68                pot,
69                amount,
70                estimated_costs_only_with_layer_info,
71                transaction,
72                platform_version,
73            ),
74            ContractFeePotOperationType::DeductFromPot {
75                contract_id,
76                pot,
77                amount,
78            } => drive.deduct_from_contract_fee_pot_operations(
79                contract_id,
80                pot,
81                amount,
82                estimated_costs_only_with_layer_info,
83                transaction,
84                platform_version,
85            ),
86            ContractFeePotOperationType::SetLastClaim {
87                contract_id,
88                pot,
89                last_claim,
90            } => drive.set_contract_last_fee_claim_operations(
91                contract_id,
92                pot,
93                &last_claim,
94                estimated_costs_only_with_layer_info,
95                platform_version,
96            ),
97        }
98    }
99}