Skip to content

Commit

Permalink
Remove getters in salp. (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
MJLNSN authored Aug 12, 2024
1 parent f034bd6 commit 386ba16
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 71 deletions.
8 changes: 4 additions & 4 deletions pallets/salp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mod benchmarks {

assert_ok!(Salp::<T>::fund_fail(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::withdraw(RawOrigin::Root.into(), fund_index));
let fund = Salp::<T>::funds(fund_index).unwrap();
let fund = Funds::<T>::get(fund_index).unwrap();
let (_, status) = Salp::<T>::contribution(fund.trie_index, &caller);
assert_eq!(status, ContributionStatus::Idle);

Expand Down Expand Up @@ -187,12 +187,12 @@ mod benchmarks {
));
assert_ok!(Salp::<T>::fund_retire(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::withdraw(RawOrigin::Root.into(), fund_index));
assert_eq!(Salp::<T>::redeem_pool(), T::MinContribution::get());
assert_eq!(RedeemPool::<T>::get(), T::MinContribution::get());

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), fund_index, contribution);

assert_eq!(Salp::<T>::redeem_pool(), 0_u32.saturated_into());
assert_eq!(RedeemPool::<T>::get(), 0_u32.saturated_into());
}

#[benchmark]
Expand Down Expand Up @@ -547,7 +547,7 @@ mod benchmarks {
));
assert_ok!(Salp::<T>::fund_retire(RawOrigin::Root.into(), fund_index));
assert_ok!(Salp::<T>::withdraw(RawOrigin::Root.into(), fund_index));
assert_eq!(Salp::<T>::redeem_pool(), T::MinContribution::get());
assert_eq!(RedeemPool::<T>::get(), T::MinContribution::get());

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), fund_index);
Expand Down
81 changes: 37 additions & 44 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,29 +317,24 @@ pub mod pallet {

/// Multisig confirm account
#[pallet::storage]
#[pallet::getter(fn multisig_confirm_account)]
pub type MultisigConfirmAccount<T: Config> = StorageValue<_, AccountIdOf<T>, OptionQuery>;

/// Tracker for the next available fund index
#[pallet::storage]
#[pallet::getter(fn current_trie_index)]
pub(super) type CurrentTrieIndex<T: Config> = StorageValue<_, TrieIndex, ValueQuery>;

/// Tracker for the next nonce index
#[pallet::storage]
#[pallet::getter(fn current_nonce)]
pub(super) type CurrentNonce<T: Config> =
StorageMap<_, Blake2_128Concat, ParaId, Nonce, ValueQuery>;

/// Record contribution
#[pallet::storage]
#[pallet::getter(fn contributing_value)]
pub type QueryIdContributionInfo<T: Config> =
StorageMap<_, Blake2_128Concat, QueryId, (ParaId, AccountIdOf<T>, BalanceOf<T>)>;

/// Info on all of the funds.
#[pallet::storage]
#[pallet::getter(fn funds)]
pub(super) type Funds<T: Config> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -350,11 +345,9 @@ pub mod pallet {

/// The balance can be redeemed to users.
#[pallet::storage]
#[pallet::getter(fn redeem_pool)]
pub type RedeemPool<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn failed_funds_to_refund)]
pub(super) type FailedFundsToRefund<T: Config> = StorageNMap<
_,
(
Expand All @@ -367,7 +360,6 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn reserve_infos)]
pub type ReserveInfos<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
Expand Down Expand Up @@ -416,7 +408,7 @@ pub mod pallet {
) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Ongoing, Error::<T>::InvalidFundStatus);

let fund_new = FundInfo { status: FundStatus::Success, ..fund };
Expand All @@ -432,7 +424,7 @@ pub mod pallet {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

// crownload is failed, so enable the withdrawal function of vsToken/vsBond
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Ongoing, Error::<T>::InvalidFundStatus);

let fund_new = FundInfo { status: FundStatus::Failed, ..fund };
Expand All @@ -453,7 +445,7 @@ pub mod pallet {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

// crownload is failed, so enable the withdrawal function of vsToken/vsBond
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::RefundWithdrew, Error::<T>::InvalidFundStatus);
ensure!(
fund.first_slot != first_slot || fund.last_slot != last_slot,
Expand Down Expand Up @@ -512,7 +504,7 @@ pub mod pallet {
) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Success, Error::<T>::InvalidFundStatus);

let fund_new = FundInfo { status: FundStatus::Retired, ..fund };
Expand All @@ -527,7 +519,7 @@ pub mod pallet {
pub fn fund_end(origin: OriginFor<T>, #[pallet::compact] index: ParaId) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(
fund.status == FundStatus::RefundWithdrew ||
fund.status == FundStatus::RedeemWithdrew,
Expand Down Expand Up @@ -622,7 +614,7 @@ pub mod pallet {
) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

let status = match fund_status {
None => fund.status,
Expand Down Expand Up @@ -655,7 +647,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Ongoing, Error::<T>::InvalidFundStatus);

ensure!(value >= T::MinContribution::get(), Error::<T>::ContributionTooSmall);
Expand Down Expand Up @@ -708,7 +700,7 @@ pub mod pallet {
let (index, contributer, _amount) = QueryIdContributionInfo::<T>::get(query_id)
.ok_or(Error::<T>::NotFindContributionValue)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
let can_confirm = fund.status == FundStatus::Ongoing ||
fund.status == FundStatus::Failed ||
fund.status == FundStatus::Success;
Expand Down Expand Up @@ -781,7 +773,7 @@ pub mod pallet {
#[pallet::compact] index: ParaId,
) -> DispatchResult {
ensure_signed(origin)?;
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

let (contributed, _) = Self::contribution(fund.trie_index, &who);

Expand Down Expand Up @@ -835,7 +827,7 @@ pub mod pallet {
_ => return Err(Error::<T>::NotSupportTokenType.into()),
};

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

let (contributed, _) = Self::contribution(fund.trie_index, &who);

Expand Down Expand Up @@ -906,7 +898,7 @@ pub mod pallet {
) -> DispatchResult {
ensure_signed(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

let mut unlock_count = 0u32;
let contributions = Self::contribution_iterator(fund.trie_index);
Expand Down Expand Up @@ -951,13 +943,14 @@ pub mod pallet {
pub fn withdraw(origin: OriginFor<T>, #[pallet::compact] index: ParaId) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin.clone())?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
let can = fund.status == FundStatus::Failed || fund.status == FundStatus::Retired;
ensure!(can, Error::<T>::InvalidFundStatus);

let amount_withdrew = fund.raised;
let total =
Self::redeem_pool().checked_add(&amount_withdrew).ok_or(Error::<T>::Overflow)?;
let total = RedeemPool::<T>::get()
.checked_add(&amount_withdrew)
.ok_or(Error::<T>::Overflow)?;
RedeemPool::<T>::set(total);

if fund.status == FundStatus::Retired {
Expand Down Expand Up @@ -996,7 +989,7 @@ pub mod pallet {
Error::<T>::InvalidRefund
);
ensure!(fund.raised >= value, Error::<T>::NotEnoughBalanceInFund);
ensure!(Self::redeem_pool() >= value, Error::<T>::NotEnoughBalanceInRefundPool);
ensure!(RedeemPool::<T>::get() >= value, Error::<T>::NotEnoughBalanceInRefundPool);

let vs_token = T::CurrencyIdConversion::convert_to_vstoken(T::RelayChainToken::get())
.map_err(|_| Error::<T>::NotSupportTokenType)?;
Expand All @@ -1015,8 +1008,8 @@ pub mod pallet {
T::MultiCurrency::withdraw(vs_token, &who, value)?;
T::MultiCurrency::withdraw(vs_bond, &who, value)?;

RedeemPool::<T>::set(Self::redeem_pool().saturating_sub(value));
let mut fund_new = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
RedeemPool::<T>::set(RedeemPool::<T>::get().saturating_sub(value));
let mut fund_new = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
fund_new.raised = fund_new.raised.saturating_sub(value);
Funds::<T>::insert(index, Some(fund_new));
if fund.status == FundStatus::FailedToContinue {
Expand Down Expand Up @@ -1054,7 +1047,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

let mut fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let mut fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::RedeemWithdrew, Error::<T>::InvalidFundStatus);
ensure!(fund.raised >= value, Error::<T>::NotEnoughBalanceInRedeemPool);

Expand All @@ -1068,7 +1061,7 @@ pub mod pallet {
)
.map_err(|_| Error::<T>::NotSupportTokenType)?;

ensure!(Self::redeem_pool() >= value, Error::<T>::NotEnoughBalanceInRedeemPool);
ensure!(RedeemPool::<T>::get() >= value, Error::<T>::NotEnoughBalanceInRedeemPool);
let cur_block = <frame_system::Pallet<T>>::block_number();
let expired = Self::is_expired(cur_block, fund.last_slot)?;
ensure!(!expired, Error::<T>::VSBondExpired);
Expand All @@ -1079,7 +1072,7 @@ pub mod pallet {

T::MultiCurrency::withdraw(vs_token, &who, value)?;
T::MultiCurrency::withdraw(vs_bond, &who, value)?;
RedeemPool::<T>::set(Self::redeem_pool().saturating_sub(value));
RedeemPool::<T>::set(RedeemPool::<T>::get().saturating_sub(value));

fund.raised = fund.raised.saturating_sub(value);
Funds::<T>::insert(index, Some(fund.clone()));
Expand Down Expand Up @@ -1112,7 +1105,7 @@ pub mod pallet {
) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::failed_funds_to_refund((index, first_slot, last_slot))
let fund = FailedFundsToRefund::<T>::get((index, first_slot, last_slot))
.ok_or(Error::<T>::InvalidRefund)?;

ensure!(fund.status == FundStatus::FailedToContinue, Error::<T>::InvalidFundStatus);
Expand All @@ -1130,7 +1123,7 @@ pub mod pallet {
pub fn dissolve(origin: OriginFor<T>, #[pallet::compact] index: ParaId) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let mut fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let mut fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::End, Error::<T>::InvalidFundStatus);

let mut refund_count = 0u32;
Expand Down Expand Up @@ -1217,7 +1210,7 @@ pub mod pallet {
let (index, contributer, _amount) = QueryIdContributionInfo::<T>::get(query_id)
.ok_or(Error::<T>::NotFindContributionValue)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
let can_confirm = fund.status == FundStatus::Ongoing ||
fund.status == FundStatus::Failed ||
fund.status == FundStatus::Success;
Expand Down Expand Up @@ -1343,7 +1336,7 @@ pub mod pallet {
if_mint: bool,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

ensure!(
fund.status == FundStatus::Ongoing || fund.status == FundStatus::Success,
Expand Down Expand Up @@ -1379,7 +1372,7 @@ pub mod pallet {
pub fn batch_handle_reserve(origin: OriginFor<T>, index: ParaId) -> DispatchResult {
let _who = ensure_signed(origin.clone())?;

let mut fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let mut fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
let vs_token = T::CurrencyIdConversion::convert_to_vstoken(T::RelayChainToken::get())
.map_err(|_| Error::<T>::NotSupportTokenType)?;
let vs_bond = T::CurrencyIdConversion::convert_to_vsbond(
Expand Down Expand Up @@ -1472,7 +1465,7 @@ pub mod pallet {
pub fn cancel_reservation(origin: OriginFor<T>, index: ParaId) -> DispatchResult {
let who = ensure_signed(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;

let vs_token = T::CurrencyIdConversion::convert_to_vstoken(T::RelayChainToken::get())
.map_err(|_| Error::<T>::NotSupportTokenType)?;
Expand All @@ -1497,7 +1490,7 @@ pub mod pallet {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
// Release x% KSM/DOT from redeem-pool to bancor-pool per cycle
if n != Zero::zero() && (n % T::ReleaseCycle::get()) == Zero::zero() {
if let Ok(rp_balance) = TryInto::<u128>::try_into(Self::redeem_pool()) {
if let Ok(rp_balance) = TryInto::<u128>::try_into(RedeemPool::<T>::get()) {
// Calculate the release amount
let release_amount = T::ReleaseRatio::get() * rp_balance;

Expand All @@ -1508,7 +1501,7 @@ pub mod pallet {
T::BancorPool::add_token(T::RelayChainToken::get(), release_amount)
{
RedeemPool::<T>::set(
Self::redeem_pool().saturating_sub(release_amount),
RedeemPool::<T>::get().saturating_sub(release_amount),
);
}
} else {
Expand Down Expand Up @@ -1557,9 +1550,9 @@ pub mod pallet {
first_slot: LeasePeriod,
last_slot: LeasePeriod,
) -> Result<FundInfo<BalanceOf<T>, LeasePeriod>, Error<T>> {
return match Self::failed_funds_to_refund((index, first_slot, last_slot)) {
return match FailedFundsToRefund::<T>::get((index, first_slot, last_slot)) {
Some(fund) => Ok(fund),
_ => match Self::funds(index) {
_ => match Funds::<T>::get(index) {
Some(fund) => Ok(fund),
_ => Err(Error::<T>::InvalidFundNotExist),
},
Expand Down Expand Up @@ -1593,7 +1586,7 @@ pub mod pallet {
index: ParaId,
who: &AccountIdOf<T>,
) -> Result<(BalanceOf<T>, ContributionStatus<BalanceOf<T>>), Error<T>> {
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let fund = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
let (contributed, status) = Self::contribution(fund.trie_index, who);
Ok((contributed, status))
}
Expand Down Expand Up @@ -1647,7 +1640,7 @@ pub mod pallet {
) -> DispatchResult {
ensure!(fund.raised >= value, Error::<T>::NotEnoughBalanceInRedeemPool);

ensure!(Self::redeem_pool() >= value, Error::<T>::NotEnoughBalanceInRedeemPool);
ensure!(RedeemPool::<T>::get() >= value, Error::<T>::NotEnoughBalanceInRedeemPool);
let cur_block = <frame_system::Pallet<T>>::block_number();
let expired = Self::is_expired(cur_block, fund.last_slot)?;
ensure!(!expired, Error::<T>::VSBondExpired);
Expand All @@ -1658,7 +1651,7 @@ pub mod pallet {

T::MultiCurrency::withdraw(vs_token, &who, value)?;
T::MultiCurrency::withdraw(vs_bond, &who, value)?;
RedeemPool::<T>::set(Self::redeem_pool().saturating_sub(value));
RedeemPool::<T>::set(RedeemPool::<T>::get().saturating_sub(value));

fund.raised = fund.raised.saturating_sub(value);
Funds::<T>::insert(index, Some(fund.clone()));
Expand Down Expand Up @@ -1701,7 +1694,7 @@ pub mod pallet {
Error::<T>::InvalidRefund
);
ensure!(fund.raised >= value, Error::<T>::NotEnoughBalanceInFund);
ensure!(Self::redeem_pool() >= value, Error::<T>::NotEnoughBalanceInRefundPool);
ensure!(RedeemPool::<T>::get() >= value, Error::<T>::NotEnoughBalanceInRefundPool);

T::MultiCurrency::ensure_can_withdraw(vs_token, &who, value)
.map_err(|_e| Error::<T>::NotEnoughFreeAssetsToRedeem)?;
Expand All @@ -1711,8 +1704,8 @@ pub mod pallet {
T::MultiCurrency::withdraw(vs_token, &who, value)?;
T::MultiCurrency::withdraw(vs_bond, &who, value)?;

RedeemPool::<T>::set(Self::redeem_pool().saturating_sub(value));
let mut fund_new = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
RedeemPool::<T>::set(RedeemPool::<T>::get().saturating_sub(value));
let mut fund_new = Funds::<T>::get(index).ok_or(Error::<T>::InvalidParaId)?;
fund_new.raised = fund_new.raised.saturating_sub(value);
Funds::<T>::insert(index, Some(fund_new));
if fund.status == FundStatus::FailedToContinue {
Expand Down
Loading

0 comments on commit 386ba16

Please sign in to comment.