Skip to content

Commit

Permalink
Remove getters in slp (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
MJLNSN authored Aug 12, 2024
1 parent 9301b33 commit cf89c76
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 86 deletions.
6 changes: 3 additions & 3 deletions pallets/slp/src/agents/filecoin_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
primitives::{FilecoinLedger, Ledger},
traits::StakingAgent,
AccountIdOf, BalanceOf, BoundedVec, Config, DelegatorLatestTuneRecord, DelegatorLedgers,
LedgerUpdateEntry, MinimumsAndMaximums, Pallet, TimeUnit, Validators, ValidatorsByDelegator,
ValidatorsByDelegatorUpdateEntry,
HostingFees, LedgerUpdateEntry, MinimumsAndMaximums, Pallet, TimeUnit, Validators,
ValidatorsByDelegator, ValidatorsByDelegatorUpdateEntry,
};
use bifrost_primitives::{CurrencyId, VtokenMintingOperator};
use core::marker::PhantomData;
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<T: Config>
// issue the increased interest amount to the entrance account
// Get charged fee value
let (fee_permill, _beneficiary) =
Pallet::<T>::get_hosting_fee(currency_id).ok_or(Error::<T>::InvalidHostingFee)?;
HostingFees::<T>::get(currency_id).ok_or(Error::<T>::InvalidHostingFee)?;
let fee_to_charge = fee_permill.mul_floor(token_amount);
let amount_to_increase =
token_amount.checked_sub(&fee_to_charge).ok_or(Error::<T>::UnderFlow)?;
Expand Down
13 changes: 7 additions & 6 deletions pallets/slp/src/agents/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{
blake2_256, pallet::Error, AccountIdOf, Config, Decode, LedgerUpdateEntry, MinimumsAndMaximums,
Pallet, TrailingZeroInput, Validators, ValidatorsByDelegatorUpdateEntry, ASTR, DOT, GLMR, H160,
blake2_256, pallet::Error, AccountIdOf, Config, Decode, DelegatorLedgerXcmUpdateQueue,
LedgerUpdateEntry, MinimumsAndMaximums, Pallet, TrailingZeroInput, Validators,
ValidatorsByDelegatorUpdateEntry, ValidatorsByDelegatorXcmUpdateQueue, ASTR, DOT, GLMR, H160,
KSM, MANTA, MOVR, PHA,
};
use bifrost_primitives::CurrencyId;
Expand Down Expand Up @@ -211,7 +212,7 @@ impl<T: Config> Pallet<T> {
// See if the query exists. If it exists, call corresponding chain storage update
// function.
let (entry, timeout) =
Self::get_delegator_ledger_update_entry(query_id).ok_or(Error::<T>::QueryNotExist)?;
DelegatorLedgerXcmUpdateQueue::<T>::get(query_id).ok_or(Error::<T>::QueryNotExist)?;

let now = frame_system::Pallet::<T>::block_number();
let mut updated = true;
Expand Down Expand Up @@ -244,7 +245,7 @@ impl<T: Config> Pallet<T> {
) -> Result<bool, Error<T>> {
// See if the query exists. If it exists, call corresponding chain storage update
// function.
let (entry, timeout) = Self::get_validators_by_delegator_update_entry(query_id)
let (entry, timeout) = ValidatorsByDelegatorXcmUpdateQueue::<T>::get(query_id)
.ok_or(Error::<T>::QueryNotExist)?;

let now = frame_system::Pallet::<T>::block_number();
Expand Down Expand Up @@ -274,7 +275,7 @@ impl<T: Config> Pallet<T> {
// See if the query exists. If it exists, call corresponding chain storage update
// function.
let (entry, _) =
Self::get_delegator_ledger_update_entry(query_id).ok_or(Error::<T>::QueryNotExist)?;
DelegatorLedgerXcmUpdateQueue::<T>::get(query_id).ok_or(Error::<T>::QueryNotExist)?;
let currency_id = match entry {
LedgerUpdateEntry::Substrate(substrate_entry) => Some(substrate_entry.currency_id),
LedgerUpdateEntry::ParachainStaking(moonbeam_entry) => Some(moonbeam_entry.currency_id),
Expand All @@ -293,7 +294,7 @@ impl<T: Config> Pallet<T> {
) -> Result<(), Error<T>> {
// See if the query exists. If it exists, call corresponding chain storage update
// function.
let (entry, _) = Self::get_validators_by_delegator_update_entry(query_id)
let (entry, _) = ValidatorsByDelegatorXcmUpdateQueue::<T>::get(query_id)
.ok_or(Error::<T>::QueryNotExist)?;
let currency_id = match entry {
ValidatorsByDelegatorUpdateEntry::Substrate(substrate_entry) =>
Expand Down
40 changes: 9 additions & 31 deletions pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,25 +537,21 @@ pub mod pallet {
/// One operate origin(can be a multisig account) for a currency. An operating origins are
/// normal account in Bifrost chain.
#[pallet::storage]
#[pallet::getter(fn get_operate_origin)]
pub type OperateOrigins<T> = StorageMap<_, Blake2_128Concat, CurrencyId, AccountIdOf<T>>;

/// Origins and Amounts for the staking operating account fee supplement. An operating account
/// is identified in MultiLocation format.
#[pallet::storage]
#[pallet::getter(fn get_fee_source)]
pub type FeeSources<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, (MultiLocation, BalanceOf<T>)>;

/// Hosting fee percentage and beneficiary account for different chains
#[pallet::storage]
#[pallet::getter(fn get_hosting_fee)]
pub type HostingFees<T> = StorageMap<_, Blake2_128Concat, CurrencyId, (Permill, MultiLocation)>;

/// Delegators in service. A delegator is identified in MultiLocation format.
/// Currency Id + Sub-account index => MultiLocation
#[pallet::storage]
#[pallet::getter(fn get_delegator_multilocation_by_index)]
pub type DelegatorsIndex2Multilocation<T> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -568,7 +564,6 @@ pub mod pallet {

/// Delegators in service. Currency Id + MultiLocation => Sub-account index
#[pallet::storage]
#[pallet::getter(fn get_delegator_index_by_multilocation)]
pub type DelegatorsMultilocation2Index<T> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -581,18 +576,15 @@ pub mod pallet {

/// Next index of different currency delegators.
#[pallet::storage]
#[pallet::getter(fn get_delegator_next_index)]
pub type DelegatorNextIndex<T> = StorageMap<_, Blake2_128Concat, CurrencyId, u16, ValueQuery>;

/// (VWL) Validator in service. A validator is identified in MultiLocation format.
#[pallet::storage]
#[pallet::getter(fn get_validators)]
pub type Validators<T: Config> =
StorageMap<_, Blake2_128Concat, CurrencyId, BoundedVec<MultiLocation, T::MaxLengthLimit>>;

/// (VBL) Validator Boost List -> (validator multilocation, due block number)
#[pallet::storage]
#[pallet::getter(fn get_validator_boost_list)]
pub type ValidatorBoostList<T: Config> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -602,7 +594,6 @@ pub mod pallet {

/// Validators for each delegator. CurrencyId + Delegator => Vec<Validator>
#[pallet::storage]
#[pallet::getter(fn get_validators_by_delegator)]
pub type ValidatorsByDelegator<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -614,7 +605,6 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn get_validators_by_delegator_update_entry)]
pub type ValidatorsByDelegatorXcmUpdateQueue<T> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -624,7 +614,6 @@ pub mod pallet {

/// Delegator ledgers. A delegator is identified in MultiLocation format.
#[pallet::storage]
#[pallet::getter(fn get_delegator_ledger)]
pub type DelegatorLedgers<T> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -636,7 +625,6 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn get_delegator_ledger_update_entry)]
pub type DelegatorLedgerXcmUpdateQueue<T> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -646,19 +634,16 @@ pub mod pallet {

/// Minimum and Maximum constraints for different chains.
#[pallet::storage]
#[pallet::getter(fn get_minimums_maximums)]
pub type MinimumsAndMaximums<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, MinimumsMaximums<BalanceOf<T>>>;

/// TimeUnit delay params for different chains.
#[pallet::storage]
#[pallet::getter(fn get_currency_delays)]
pub type CurrencyDelays<T> = StorageMap<_, Blake2_128Concat, CurrencyId, Delays>;

/// A delegator's tuning record of exchange rate for the current time unit.
/// Currency Id + Delegator Id => latest tuned TimeUnit
#[pallet::storage]
#[pallet::getter(fn get_delegator_latest_tune_record)]
pub type DelegatorLatestTuneRecord<T> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -672,36 +657,30 @@ pub mod pallet {
/// Currency's tuning record of exchange rate for the current time unit.
/// Currency Id => (latest tuned TimeUnit, number of tuning times)
#[pallet::storage]
#[pallet::getter(fn get_currency_latest_tune_record)]
pub type CurrencyLatestTuneRecord<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, (TimeUnit, u32), OptionQuery>;

/// For each currencyId: how many times that a Currency's all delegators can tune the exchange
/// rate for a single time unit, and how much at most each time can tune the
/// exchange rate
#[pallet::storage]
#[pallet::getter(fn get_currency_tune_exchange_rate_limit)]
pub type CurrencyTuneExchangeRateLimit<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, (u32, Permill)>;

/// reflect if all delegations are on a decrease/revoke status. If yes, then new user redeeming
/// is unaccepted.
#[pallet::storage]
#[pallet::getter(fn get_all_delegations_occupied_status)]
pub type DelegationsOccupied<T> = StorageMap<_, Blake2_128Concat, CurrencyId, bool>;

#[pallet::storage]
#[pallet::getter(fn get_last_time_updated_ongoing_time_unit)]
pub type LastTimeUpdatedOngoingTimeUnit<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, BlockNumberFor<T>>;

#[pallet::storage]
#[pallet::getter(fn get_ongoing_time_unit_update_interval)]
pub type OngoingTimeUnitUpdateInterval<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, BlockNumberFor<T>>;

#[pallet::storage]
#[pallet::getter(fn get_supplement_fee_account_wihtelist)]
pub type SupplementFeeAccountWhitelist<T> =
StorageMap<_, Blake2_128Concat, CurrencyId, Vec<(MultiLocation, Hash<T>)>>;

Expand Down Expand Up @@ -1271,7 +1250,7 @@ pub mod pallet {
ensure!(value > Zero::zero(), Error::<T>::AmountZero);

// Ensure the value is valid.
let (limit_num, max_permill) = Self::get_currency_tune_exchange_rate_limit(currency_id)
let (limit_num, max_permill) = CurrencyTuneExchangeRateLimit::<T>::get(currency_id)
.ok_or(Error::<T>::TuneExchangeRateLimitNotSet)?;
// Get pool token value
let pool_token = T::VtokenMinting::get_token_pool(currency_id);
Expand All @@ -1290,9 +1269,8 @@ pub mod pallet {
}

// Get CurrencyLatestTuneRecord for the currencyId.
let (latest_time_unit, tune_num) =
Self::get_currency_latest_tune_record(currency_id)
.ok_or(Error::<T>::CurrencyLatestTuneRecordNotExist)?;
let (latest_time_unit, tune_num) = CurrencyLatestTuneRecord::<T>::get(currency_id)
.ok_or(Error::<T>::CurrencyLatestTuneRecordNotExist)?;

// See if exceeds tuning limit.
// If it has been tuned in the current time unit, ensure this tuning is within limit.
Expand All @@ -1306,7 +1284,7 @@ pub mod pallet {

// Get charged fee value
let (fee_permill, beneficiary) =
Self::get_hosting_fee(currency_id).ok_or(Error::<T>::InvalidHostingFee)?;
HostingFees::<T>::get(currency_id).ok_or(Error::<T>::InvalidHostingFee)?;
let fee_to_charge = fee_permill.mul_floor(value);

// Should first charge fee, and then tune exchange rate. Otherwise, the rate will be
Expand Down Expand Up @@ -1940,7 +1918,7 @@ pub mod pallet {

// Add the boost list to the validator set
let mut validator_vec;
if let Some(validator_set) = Self::get_validators(currency_id) {
if let Some(validator_set) = Validators::<T>::get(currency_id) {
validator_vec = validator_set.to_vec();
} else {
validator_vec = vec![];
Expand Down Expand Up @@ -2036,7 +2014,7 @@ pub mod pallet {
due_block_number,
});

let validator_set_op = Self::get_validators(currency_id);
let validator_set_op = Validators::<T>::get(currency_id);

let mut validator_vec;
// Add the newly added validator to the validator set
Expand Down Expand Up @@ -2282,22 +2260,22 @@ impl<T: Config, F: Contains<CurrencyIdOf<T>>>
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> bool {
Pallet::<T>::get_delegator_multilocation_by_index(token, derivative_index).is_some()
DelegatorsIndex2Multilocation::<T>::get(token, derivative_index).is_some()
}

fn get_multilocation(
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> Option<MultiLocation> {
Pallet::<T>::get_delegator_multilocation_by_index(token, derivative_index)
DelegatorsIndex2Multilocation::<T>::get(token, derivative_index)
}

fn get_stake_info(
token: CurrencyIdOf<T>,
derivative_index: DerivativeIndex,
) -> Option<(BalanceOf<T>, BalanceOf<T>)> {
Self::get_multilocation(token, derivative_index).and_then(|location| {
Pallet::<T>::get_delegator_ledger(token, location).and_then(|ledger| match ledger {
DelegatorLedgers::<T>::get(token, location).and_then(|ledger| match ledger {
Ledger::Substrate(l) if F::contains(&token) => Some((l.total, l.active)),
_ => None,
})
Expand Down
3 changes: 2 additions & 1 deletion pallets/slp/src/tests/kusama_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ fn set_hosting_fees_works() {
Some((pct, treasury_location))
));

let (fee, location) = Slp::get_hosting_fee(KSM).unwrap();
// let (fee, location) = Slp::get_hosting_fee(KSM).unwrap();
let (fee, location) = HostingFees::<Runtime>::get(KSM).unwrap();
assert_eq!(fee, pct);
assert_eq!(location, treasury_location);
});
Expand Down
Loading

0 comments on commit cf89c76

Please sign in to comment.