use moonproof_ledger::base_crypto::time::Timestamp;
use moonproof_ledger::ledger::dust::DustSecretKey;
use moonproof_ledger::ledger::structure::LedgerParameters;
use moonproof_wallet::{DustState, FacadeState};
use crate::error::{ContractRuntimeError, Result};
#[derive(Debug, Clone)]
pub struct FeeBalancingConfig {
pub dust_state: DustState,
pub dust_secret_key: DustSecretKey,
pub current_time: Timestamp,
pub ttl: Timestamp,
pub ledger_parameters: LedgerParameters,
}
impl FeeBalancingConfig {
#[must_use]
pub fn new(
dust_state: DustState,
dust_secret_key: DustSecretKey,
current_time: Timestamp,
ttl: Timestamp,
ledger_parameters: LedgerParameters,
) -> Self {
Self {
dust_state,
dust_secret_key,
current_time,
ttl,
ledger_parameters,
}
}
#[must_use]
pub fn from_synced(
synced: &FacadeState,
dust_secret_key: DustSecretKey,
ttl: Timestamp,
) -> Self {
Self::new(
synced.dust.clone(),
dust_secret_key,
synced.chain_tip_ts(),
ttl,
synced.ledger_parameters().clone(),
)
}
}
pub fn apply_fee_balancing<R>(
rng: &mut R,
#[expect(
clippy::needless_pass_by_value,
reason = "exported signature; callers hand over the transaction they are replacing with the balanced one"
)]
tx: moonproof_wallet::UnprovenTransaction,
config: &FeeBalancingConfig,
network_id: &str,
) -> Result<moonproof_wallet::UnprovenTransaction>
where
R: rand::Rng + rand::CryptoRng,
{
let (_dust_state_after, merged) = moonproof_wallet::balance_dust_fee(
rng,
config.dust_state.clone(),
&config.dust_secret_key,
&tx,
&moonproof_wallet::FeeContext {
current_time: config.current_time,
ttl: config.ttl,
network_id,
ledger_parameters: &config.ledger_parameters,
},
)
.map_err(ContractRuntimeError::Wallet)?;
Ok(merged)
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_send_sync<T: Send + Sync>() {}
#[test]
fn fee_balancing_config_is_send_sync() {
assert_send_sync::<FeeBalancingConfig>();
}
}