Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle order refunded when genetic testing rejected #396

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pallets/genetic-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub mod pallet {
DataStakerNotFound,
DataHashNotFound,
UnpaidOrder,
RefundFailed,
}

pub type HashOf<T> = <T as frame_system::Config>::Hash;
Expand Down Expand Up @@ -412,6 +413,14 @@ impl<T: Config> GeneticTestingInterface<T> for Pallet<T> {
dna_sample.status = DnaSampleStatus::Rejected;
dna_sample.updated_at = now;
DnaSamples::<T>::insert(tracking_id, &dna_sample);

let succeed = T::Orders::update_status_failed(&dna_sample.order_id);

if !succeed {
return Err(Error::<T>::RefundFailed)
}

T::Orders::emit_event_order_failed(&dna_sample.order_id);
T::Orders::remove_order_id_from_pending_orders_by_seller(
&dna_sample.lab_id,
&dna_sample.order_id,
Expand Down
100 changes: 97 additions & 3 deletions pallets/orders/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::*;

use frame_support::{
pallet_prelude::*,
sp_runtime::{traits::Hash, SaturatedConversion},
sp_runtime::{
traits::{Hash, Zero},
SaturatedConversion,
},
traits::{fungibles, Currency, ExistenceRequirement},
};
use primitives_price_and_currency::CurrencyType;
Expand Down Expand Up @@ -135,6 +138,44 @@ impl<T: Config> Pallet<T> {
Ok(Some(asset_id))
}

pub fn do_balance_sufficient(
sender: &T::AccountId,
amount: &BalanceOf<T>,
asset_id: Option<u32>,
) -> Result<(), Error<T>> {
if let Some(asset_id) = asset_id {
let min_asset_balance =
<T::Assets as fungibles::Inspect<T::AccountId>>::minimum_balance(asset_id);
let current_asset_balance =
<T::Assets as fungibles::Inspect<T::AccountId>>::balance(asset_id, sender);
let transferable_asset_balance = if current_asset_balance >= min_asset_balance {
current_asset_balance - min_asset_balance
} else {
0u128
};

if (*amount).saturated_into::<u128>() > transferable_asset_balance {
return Err(Error::<T>::InsufficientBalance)
}

return Ok(())
}

let minimum_balance = CurrencyOf::<T>::minimum_balance();
let current_balance = CurrencyOf::<T>::free_balance(sender);
let transferable_balance = if current_balance >= minimum_balance {
current_balance - minimum_balance
} else {
Zero::zero()
};

if *amount > transferable_balance {
return Err(Error::<T>::InsufficientBalance)
}

Ok(())
}

pub fn do_transfer(
currency: &CurrencyType,
sender: &T::AccountId,
Expand All @@ -143,12 +184,15 @@ impl<T: Config> Pallet<T> {
asset_id: Option<u32>,
keep_alive: bool,
) -> Result<(), Error<T>> {
let _ = Self::do_balance_sufficient(sender, &amount, asset_id)?;

if currency == &CurrencyType::DBIO {
let existence = if keep_alive {
ExistenceRequirement::KeepAlive
} else {
ExistenceRequirement::AllowDeath
};

let result = CurrencyOf::<T>::transfer(sender, receiver, amount, existence);

if let Err(dispatch) = result {
Expand All @@ -174,6 +218,8 @@ impl<T: Config> Pallet<T> {
keep_alive,
);

// check balance

if let Err(dispatch) = result {
return match dispatch {
DispatchError::Other(_) => Err(Error::<T>::Other),
Expand Down Expand Up @@ -203,11 +249,59 @@ impl<T: Config> OrderEventEmitter<T> for Pallet<T> {
}

impl<T: Config> OrderStatusUpdater<T> for Pallet<T> {
fn update_status_failed(order_id: &HashOf<T>) {
fn update_status_failed(order_id: &HashOf<T>) -> bool {
match Self::order_by_id(order_id) {
None => Self::deposit_event(Event::OrderNotFound),
None => {
Self::deposit_event(Event::OrderNotFound);
false
},
Some(order) => {
if !order.currency.can_transfer() {
return false
}

let pallet_id = Self::pallet_id();

if pallet_id.is_none() {
return false
}

let pallet_id = pallet_id.unwrap();

let mut testing_price = Zero::zero();
let mut qc_price = Zero::zero();

for price in order.prices.iter() {
testing_price += price.value;
}

for price in order.additional_prices.iter() {
qc_price += price.value;
}

// TODO: check balance;

let _ = Self::do_transfer(
&order.currency,
&pallet_id,
order.get_seller_id(),
qc_price,
order.asset_id,
false,
);

let _ = Self::do_transfer(
&order.currency,
&pallet_id,
&order.customer_id,
testing_price,
order.asset_id,
false,
);

Self::update_order_status(&order.id, OrderStatus::Failed);

true
},
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/orders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub mod pallet {
NoProviders,
Token,
Arithmetic,
InsufficientBalance,
}

#[pallet::call]
Expand Down
2 changes: 1 addition & 1 deletion pallets/orders/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait OrderEventEmitter<T: Config> {
}

pub trait OrderStatusUpdater<T: Config> {
fn update_status_failed(order_id: &T::Hash);
fn update_status_failed(order_id: &T::Hash) -> bool;
fn remove_order_id_from_pending_orders_by_seller(
seller_id: &T::AccountId,
genetic_analysis_order_id: &T::Hash,
Expand Down