Skip to content

Commit

Permalink
- I simplified the tax credit purchase calculation by breaking it dow…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
didiermis committed Mar 22, 2024
1 parent 0936cea commit 240f3f8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pallets/afloat/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,18 @@ impl<T: Config> Pallet<T> {
Error::<T>::NotEnoughTaxCreditsAvailable
);

let price_per_credit: T::Balance = offer.price_per_credit.into();
let total_price: T::Balance = Self::safe_multiply_offer(price_per_credit, tax_credit_amount)?;
//ensure user has enough afloat balance
ensure!(
Self::do_get_afloat_balance(who.clone())? >=
Self::safe_multiply_offer(offer.price_per_credit, tax_credit_amount.into())?,
Self::do_get_afloat_balance(who.clone())? >= total_price,
Error::<T>::NotEnoughAfloatBalanceAvailable
);
let zero_balance: T::Balance = Zero::zero();
//ensure tax credit amount is greater than zero
ensure!(tax_credit_amount > zero_balance, Error::<T>::Underflow);

let creation_date: u64 = T::Timestamp::now().into();
let price_per_credit: T::Balance = offer.price_per_credit.into();
let total_price: T::Balance = Self::safe_multiply_offer(price_per_credit, tax_credit_amount)?;
let fee: Option<T::Balance> = None;
let tax_credit_id: <T as pallet_uniques::Config>::ItemId = offer.tax_credit_id;
let seller_id: T::AccountId = offer.creator_id;
Expand Down
10 changes: 5 additions & 5 deletions pallets/fund-admin/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,11 @@ impl<T: Config> Pallet<T> {
let revenue_transaction_id =
(revenue_id, revenue_amount, job_eligible_id, project_id, timestamp)
.using_encoded(blake2_256);
// Ensure revenue transaction id doesn't exist
ensure!(
!RevenueTransactionsInfo::<T>::contains_key(revenue_transaction_id),
Error::<T>::RevenueTransactionIdAlreadyExists
);

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

// Insert revenue transaction data into RevenueTransactionsInfo
// Ensure revenue transaction id doesn't exist
ensure!(
!RevenueTransactionsInfo::<T>::contains_key(revenue_transaction_id),
Error::<T>::RevenueTransactionIdAlreadyExists
);
<RevenueTransactionsInfo<T>>::insert(revenue_transaction_id, revenue_transaction_data);

// Insert revenue transaction id into TransactionsByRevenue
Expand Down

0 comments on commit 240f3f8

Please sign in to comment.