drive/util/batch/drive_op_batch/
address_funds.rs

1use crate::drive::Drive;
2use crate::error::Error;
3use crate::fees::op::LowLevelDriveOperation;
4use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter;
5use dpp::address_funds::PlatformAddress;
6use dpp::block::block_info::BlockInfo;
7use dpp::fee::Credits;
8use dpp::prelude::AddressNonce;
9use grovedb::batch::KeyInfoPath;
10use grovedb::{EstimatedLayerInformation, TransactionArg};
11use platform_version::version::PlatformVersion;
12use std::collections::HashMap;
13
14/// Operations on Address Funds
15#[derive(Clone, Debug)]
16pub enum AddressFundsOperationType {
17    /// Sets a balance for a given address in the AddressBalances tree.
18    /// This operation directly sets (or overwrites) the balance for the address with the given nonce.
19    SetBalanceToAddress {
20        /// The platform address
21        address: PlatformAddress,
22        /// The nonce for the address
23        nonce: AddressNonce,
24        /// The balance value to set
25        balance: Credits,
26    },
27    /// Adds a balance for a given address in the AddressBalances tree.
28    /// This operation adds the balance for the address with the given nonce, that nonce is not changed.
29    AddBalanceToAddress {
30        /// The platform address
31        address: PlatformAddress,
32        /// The balance value to add
33        balance_to_add: Credits,
34    },
35}
36
37impl DriveLowLevelOperationConverter for AddressFundsOperationType {
38    fn into_low_level_drive_operations(
39        self,
40        drive: &Drive,
41        estimated_costs_only_with_layer_info: &mut Option<
42            HashMap<KeyInfoPath, EstimatedLayerInformation>,
43        >,
44        _block_info: &BlockInfo,
45        transaction: TransactionArg,
46        platform_version: &PlatformVersion,
47    ) -> Result<Vec<LowLevelDriveOperation>, Error> {
48        match self {
49            AddressFundsOperationType::SetBalanceToAddress {
50                address,
51                nonce,
52                balance,
53            } => drive.set_balance_to_address_operations(
54                address,
55                nonce,
56                balance,
57                estimated_costs_only_with_layer_info,
58                platform_version,
59            ),
60            AddressFundsOperationType::AddBalanceToAddress {
61                address,
62                balance_to_add,
63            } => drive.add_balance_to_address_operations(
64                address,
65                balance_to_add,
66                estimated_costs_only_with_layer_info,
67                transaction,
68                platform_version,
69            ),
70        }
71    }
72}