Skip to content

Commit a5c2f01

Browse files
committed
refactor move min_bonded for fetching in common function and return both u128 and BalanceOf types
1 parent 0a38134 commit a5c2f01

File tree

1 file changed

+42
-31
lines changed
  • pallets/mining/rewards-allowance/src

1 file changed

+42
-31
lines changed

pallets/mining/rewards-allowance/src/lib.rs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ pub mod pallet {
520520
log::info!("Unable to get rm_current_ratio");
521521
}
522522

523-
// let mut min_bonded_dhx_daily_default: u128 = 10_000_000_000_000_000_000_u128; // 10 DHX
524523
let mut min_bonded_dhx_daily_default: BalanceOf<T> = 10u32.into(); // initialize
525524
if let Some(_min_bonded_dhx_daily_default) = <MinBondedDHXDailyDefault<T>>::get() {
526525
min_bonded_dhx_daily_default = _min_bonded_dhx_daily_default;
@@ -628,24 +627,17 @@ pub mod pallet {
628627
// multiply the next ratio by the current min bonded dhx daily to determine the
629628
// new min. bonded dhx daily for the next period
630629

631-
// let mut min_bonded_dhx_daily_u128 = 10_000_000_000_000_000_000_u128;
632-
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into(); // initialize
633-
if let Some(_min_bonded_dhx_daily) = <MinBondedDHXDaily<T>>::get() {
634-
min_bonded_dhx_daily = _min_bonded_dhx_daily;
635-
} else {
636-
log::error!("Unable to retrieve any min. bonded DHX daily");
637-
return 0;
638-
}
639-
640-
let min_bonded_dhx_daily_u128;
641-
let _min_bonded_dhx_daily_u128 = Self::convert_balance_to_u128(min_bonded_dhx_daily.clone());
642-
match _min_bonded_dhx_daily_u128.clone() {
630+
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into();
631+
let mut min_bonded_dhx_daily_u128;
632+
let _min_bonded_dhx_daily = Self::get_min_bonded_dhx_daily();
633+
match _min_bonded_dhx_daily {
643634
Err(_e) => {
644-
log::error!("Unable to convert balance to u128 for min_bonded_dhx_daily_u128");
635+
log::error!("Unable to retrieve any min. bonded DHX daily as BalanceOf and u128");
645636
return 0;
646637
},
647-
Ok(x) => {
648-
min_bonded_dhx_daily_u128 = x;
638+
Ok(ref x) => {
639+
min_bonded_dhx_daily = x.0;
640+
min_bonded_dhx_daily_u128 = x.1;
649641
}
650642
}
651643

@@ -848,24 +840,17 @@ pub mod pallet {
848840
}
849841
log::info!("set_bonded_dhx_of_account_for_date: {:?} {:?}", start_of_requested_date_millis.clone(), bonded_dhx_current_u128.clone());
850842

851-
// let mut min_bonded_dhx_daily_u128 = 10_000_000_000_000_000_000_u128;
852-
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into(); // initialize
853-
if let Some(_min_bonded_dhx_daily) = <MinBondedDHXDaily<T>>::get() {
854-
min_bonded_dhx_daily = _min_bonded_dhx_daily;
855-
} else {
856-
log::error!("Unable to retrieve any min. bonded DHX daily");
857-
return 0;
858-
}
859-
860-
let min_bonded_dhx_daily_u128;
861-
let _min_bonded_dhx_daily_u128 = Self::convert_balance_to_u128(min_bonded_dhx_daily.clone());
862-
match _min_bonded_dhx_daily_u128.clone() {
843+
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into();
844+
let mut min_bonded_dhx_daily_u128;
845+
let _min_bonded_dhx_daily = Self::get_min_bonded_dhx_daily();
846+
match _min_bonded_dhx_daily {
863847
Err(_e) => {
864-
log::error!("Unable to convert balance to u128 for min_bonded_dhx_daily_u128");
848+
log::error!("Unable to retrieve any min. bonded DHX daily as BalanceOf and u128");
865849
return 0;
866850
},
867-
Ok(x) => {
868-
min_bonded_dhx_daily_u128 = x;
851+
Ok(ref x) => {
852+
min_bonded_dhx_daily = x.0;
853+
min_bonded_dhx_daily_u128 = x.1;
869854
}
870855
}
871856

@@ -1900,5 +1885,31 @@ pub mod pallet {
19001885
// Return a successful DispatchResultWithPostInfo
19011886
Ok(bonded_dhx_current_u128.clone())
19021887
}
1888+
1889+
fn get_min_bonded_dhx_daily() -> Result<(BalanceOf<T>, u128), DispatchError> {
1890+
let mut min_bonded_dhx_daily: BalanceOf<T> = 10u32.into(); // initialize
1891+
if let Some(_min_bonded_dhx_daily) = <MinBondedDHXDaily<T>>::get() {
1892+
min_bonded_dhx_daily = _min_bonded_dhx_daily;
1893+
} else {
1894+
log::error!("Unable to retrieve any min. bonded DHX daily");
1895+
return Err(DispatchError::Other("Unable to retrieve any min. bonded DHX daily"));
1896+
}
1897+
1898+
let min_bonded_dhx_daily_u128;
1899+
let _min_bonded_dhx_daily_u128 = Self::convert_balance_to_u128(min_bonded_dhx_daily.clone());
1900+
match _min_bonded_dhx_daily_u128.clone() {
1901+
Err(_e) => {
1902+
log::error!("Unable to convert balance to u128 for min_bonded_dhx_daily_u128");
1903+
return Err(DispatchError::Other("Unable to convert balance to u128 for min_bonded_dhx_daily_u128"));
1904+
},
1905+
Ok(x) => {
1906+
min_bonded_dhx_daily_u128 = x;
1907+
}
1908+
}
1909+
// Return a successful DispatchResultWithPostInfo
1910+
Ok(
1911+
(min_bonded_dhx_daily.clone(), min_bonded_dhx_daily_u128.clone())
1912+
)
1913+
}
19031914
}
19041915
}

0 commit comments

Comments
 (0)