use fuel_tx::{
field,
input::{
coin::{
CoinPredicate,
CoinSigned,
},
message::{
MessageCoinPredicate,
MessageCoinSigned,
MessageDataPredicate,
MessageDataSigned,
},
},
Chargeable,
FeeParameters,
GasCosts,
Input,
Output,
TransactionFee,
ValidityError,
};
use fuel_types::{
AssetId,
Word,
};
use alloc::collections::BTreeMap;
pub(crate) fn initial_free_balances<T>(
transaction: &T,
gas_costs: &GasCosts,
params: &FeeParameters,
base_asset_id: &AssetId,
) -> Result<AvailableBalances, ValidityError>
where
T: Chargeable + field::Inputs + field::Outputs,
{
let mut non_retryable_balances = BTreeMap::<AssetId, Word>::new();
let mut retryable_balance: Word = 0;
for input in transaction.inputs().iter() {
match input {
Input::CoinPredicate(CoinPredicate {
asset_id, amount, ..
})
| Input::CoinSigned(CoinSigned {
asset_id, amount, ..
}) => {
*non_retryable_balances.entry(*asset_id).or_default() += amount;
}
Input::MessageCoinSigned(MessageCoinSigned { amount, .. })
| Input::MessageCoinPredicate(MessageCoinPredicate { amount, .. }) => {
*non_retryable_balances.entry(*base_asset_id).or_default() += amount;
}
Input::MessageDataSigned(MessageDataSigned { amount, .. })
| Input::MessageDataPredicate(MessageDataPredicate { amount, .. }) => {
retryable_balance += *amount;
}
Input::Contract(_) => {}
_ => {}
}
}
let fee = TransactionFee::checked_from_tx(gas_costs, params, transaction)
.ok_or(ValidityError::BalanceOverflow)?;
let base_asset_balance = non_retryable_balances.entry(*base_asset_id).or_default();
*base_asset_balance = fee.checked_deduct_total(*base_asset_balance).ok_or(
ValidityError::InsufficientFeeAmount {
expected: fee.max_fee(),
provided: *base_asset_balance,
},
)?;
for (asset_id, amount) in
transaction
.outputs()
.iter()
.filter_map(|output| match output {
Output::Coin {
asset_id, amount, ..
} => Some((asset_id, amount)),
_ => None,
})
{
let balance = non_retryable_balances.get_mut(asset_id).ok_or(
ValidityError::TransactionOutputCoinAssetIdNotFound(*asset_id),
)?;
*balance = balance.checked_sub(*amount).ok_or(
ValidityError::InsufficientInputAmount {
asset: *asset_id,
expected: *amount,
provided: *balance,
},
)?;
}
Ok(AvailableBalances {
non_retryable_balances,
retryable_balance,
fee,
})
}
pub(crate) struct AvailableBalances {
pub(crate) non_retryable_balances: BTreeMap<AssetId, Word>,
pub(crate) retryable_balance: Word,
pub(crate) fee: TransactionFee,
}