use rand::{CryptoRng, Rng};
use crate::error::Result;
use crate::facade::registration::{
build_deregistration_recipe, build_registration_recipe, RegistrationParams,
SignedRegistrationRecipe,
};
use crate::facade::state::FacadeState;
use crate::facade::swap::{build_init_swap_recipe, SwapOptions, SwapSigningKeys};
use crate::facade::transfer::{
build_transfer_recipe, estimate_unproven_fee, CombinedTokenTransfer, TransferOptions,
TransferSigningKeys, UnprovenTransaction, UnprovenTransactionRecipe,
};
use crate::unshielded::UtxoWithMeta;
use moonproof_ledger::ledger::dust::DustPublicKey;
#[derive(Debug, Clone)]
pub struct PreviewCost {
pub estimated_dust_fee: u128,
}
#[derive(Debug, Clone)]
pub struct TransferPreview {
pub cost: PreviewCost,
pub recipe: UnprovenTransactionRecipe,
pub shielded_outputs: Vec<crate::facade::transfer::ShieldedTokenTransfer>,
pub unshielded_outputs: Vec<crate::facade::transfer::UnshieldedTokenTransfer>,
}
#[derive(Debug, Clone)]
pub struct SwapPreview {
pub cost: PreviewCost,
pub recipe: UnprovenTransactionRecipe,
}
#[derive(Debug, Clone)]
pub struct RegistrationPreview {
pub cost: PreviewCost,
pub recipe: SignedRegistrationRecipe,
pub night_utxo_count: usize,
}
pub fn preview_transfer<R>(
rng: &mut R,
state: &FacadeState,
outputs: &[CombinedTokenTransfer],
keys: &TransferSigningKeys<'_>,
options: &TransferOptions,
) -> Result<TransferPreview>
where
R: Rng + CryptoRng,
{
let (recipe, _new_state) = build_transfer_recipe(rng, state.clone(), outputs, keys, options)?;
let estimated_dust_fee = estimate_unproven_fee(&recipe.transaction, state.ledger_parameters());
let (shielded_outputs, unshielded_outputs) = split_outputs(outputs);
Ok(TransferPreview {
cost: PreviewCost { estimated_dust_fee },
recipe,
shielded_outputs,
unshielded_outputs,
})
}
pub fn preview_swap<R>(
rng: &mut R,
state: &FacadeState,
shielded_leg: Option<UnprovenTransaction>,
unshielded_leg: Option<UnprovenTransaction>,
keys: &SwapSigningKeys<'_>,
options: &SwapOptions,
) -> Result<SwapPreview>
where
R: Rng + CryptoRng,
{
let (recipe, _new_state) = build_init_swap_recipe(
rng,
state.clone(),
shielded_leg,
unshielded_leg,
keys,
options,
)?;
let estimated_dust_fee = estimate_unproven_fee(&recipe.transaction, state.ledger_parameters());
Ok(SwapPreview {
cost: PreviewCost { estimated_dust_fee },
recipe,
})
}
pub fn preview_registration<R>(
rng: &mut R,
state: &FacadeState,
night_utxos: &[UtxoWithMeta],
dust_receiver: DustPublicKey,
params: &RegistrationParams<'_>,
) -> Result<RegistrationPreview>
where
R: Rng + CryptoRng,
{
let (recipe, _new_state) =
build_registration_recipe(rng, state.clone(), night_utxos, dust_receiver, params)?;
let estimated_dust_fee =
estimate_unproven_fee(&recipe.recipe.transaction, state.ledger_parameters());
Ok(RegistrationPreview {
cost: PreviewCost { estimated_dust_fee },
recipe,
night_utxo_count: night_utxos.len(),
})
}
pub fn preview_deregistration<R>(
rng: &mut R,
state: &FacadeState,
night_utxos: &[UtxoWithMeta],
params: &RegistrationParams<'_>,
) -> Result<RegistrationPreview>
where
R: Rng + CryptoRng,
{
let (recipe, _new_state) =
build_deregistration_recipe(rng, state.clone(), night_utxos, params)?;
let estimated_dust_fee =
estimate_unproven_fee(&recipe.recipe.transaction, state.ledger_parameters());
Ok(RegistrationPreview {
cost: PreviewCost { estimated_dust_fee },
recipe,
night_utxo_count: night_utxos.len(),
})
}
fn split_outputs(
outputs: &[CombinedTokenTransfer],
) -> (
Vec<crate::facade::transfer::ShieldedTokenTransfer>,
Vec<crate::facade::transfer::UnshieldedTokenTransfer>,
) {
let mut shielded = Vec::new();
let mut unshielded = Vec::new();
for o in outputs {
match o {
CombinedTokenTransfer::Shielded(v) => shielded.extend(v.iter().cloned()),
CombinedTokenTransfer::Unshielded(v) => unshielded.extend(v.iter().cloned()),
}
}
(shielded, unshielded)
}