Skip to content

Commit 240f3f8

Browse files
committed
- I simplified the tax credit purchase calculation by breaking it down into step-by-step processes in the Pallet for Afloat. I changed the order of operations by calculating the 'price_per_credit' and 'total_price' before checking user's afloat balance. This streamlines the computation and the ensures the growth of variables price_per_credit and total_price.
- I added the check for existing Revenue Transaction ID earlier in the code to prevent duplicate entries in the Fund Admin. We ensure the transaction ID is unique before creating the revenue transaction data now. This helps in reducing the instances of `RevenueTransactionIdAlreadyExists` error. Removal of duplicate checks after the data creation provides smoother execution and cleaner code.
1 parent 0936cea commit 240f3f8

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

pallets/afloat/src/functions.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,18 @@ impl<T: Config> Pallet<T> {
474474
Error::<T>::NotEnoughTaxCreditsAvailable
475475
);
476476

477+
let price_per_credit: T::Balance = offer.price_per_credit.into();
478+
let total_price: T::Balance = Self::safe_multiply_offer(price_per_credit, tax_credit_amount)?;
477479
//ensure user has enough afloat balance
478480
ensure!(
479-
Self::do_get_afloat_balance(who.clone())? >=
480-
Self::safe_multiply_offer(offer.price_per_credit, tax_credit_amount.into())?,
481+
Self::do_get_afloat_balance(who.clone())? >= total_price,
481482
Error::<T>::NotEnoughAfloatBalanceAvailable
482483
);
483484
let zero_balance: T::Balance = Zero::zero();
484485
//ensure tax credit amount is greater than zero
485486
ensure!(tax_credit_amount > zero_balance, Error::<T>::Underflow);
486487

487488
let creation_date: u64 = T::Timestamp::now().into();
488-
let price_per_credit: T::Balance = offer.price_per_credit.into();
489-
let total_price: T::Balance = Self::safe_multiply_offer(price_per_credit, tax_credit_amount)?;
490489
let fee: Option<T::Balance> = None;
491490
let tax_credit_id: <T as pallet_uniques::Config>::ItemId = offer.tax_credit_id;
492491
let seller_id: T::AccountId = offer.creator_id;

pallets/fund-admin/src/functions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,11 @@ impl<T: Config> Pallet<T> {
20102010
let revenue_transaction_id =
20112011
(revenue_id, revenue_amount, job_eligible_id, project_id, timestamp)
20122012
.using_encoded(blake2_256);
2013+
// Ensure revenue transaction id doesn't exist
2014+
ensure!(
2015+
!RevenueTransactionsInfo::<T>::contains_key(revenue_transaction_id),
2016+
Error::<T>::RevenueTransactionIdAlreadyExists
2017+
);
20132018

20142019
// Create revenue transaction data
20152020
let revenue_transaction_data = RevenueTransactionData {
@@ -2026,11 +2031,6 @@ impl<T: Config> Pallet<T> {
20262031
};
20272032

20282033
// Insert revenue transaction data into RevenueTransactionsInfo
2029-
// Ensure revenue transaction id doesn't exist
2030-
ensure!(
2031-
!RevenueTransactionsInfo::<T>::contains_key(revenue_transaction_id),
2032-
Error::<T>::RevenueTransactionIdAlreadyExists
2033-
);
20342034
<RevenueTransactionsInfo<T>>::insert(revenue_transaction_id, revenue_transaction_data);
20352035

20362036
// Insert revenue transaction id into TransactionsByRevenue

0 commit comments

Comments
 (0)