Skip to content

Commit

Permalink
chore: Rename Pool to Vault on multi-asset-delegation Pallet (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
AtelyPham authored Sep 25, 2024
1 parent 186c8f5 commit c60215c
Show file tree
Hide file tree
Showing 19 changed files with 1,619 additions and 8,366 deletions.
42 changes: 21 additions & 21 deletions pallets/multi-asset-delegation/src/functions/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ impl<T: Config> Pallet<T> {
if let Some(reward_config) = RewardConfigStorage::<T>::get() {
// Distribute rewards for each asset
for (asset_id, delegations) in delegation_info.iter() {
// We only reward asset in a reward pool
if let Some(pool_id) = AssetLookupRewardPools::<T>::get(asset_id) {
if let Some(config) = reward_config.configs.get(&pool_id) {
// We only reward asset in a reward vault
if let Some(vault_id) = AssetLookupRewardVaults::<T>::get(asset_id) {
if let Some(config) = reward_config.configs.get(&vault_id) {
// Calculate total amount and distribute rewards
let total_amount: BalanceOf<T> =
delegations.iter().fold(Zero::zero(), |acc, d| acc + d.amount);
Expand Down Expand Up @@ -95,46 +95,46 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn add_asset_to_pool(pool_id: &T::PoolId, asset_id: &T::AssetId) -> DispatchResult {
// Ensure the asset is not already associated with any pool
pub fn add_asset_to_vault(vault_id: &T::VaultId, asset_id: &T::AssetId) -> DispatchResult {
// Ensure the asset is not already associated with any vault
ensure!(
!AssetLookupRewardPools::<T>::contains_key(asset_id),
Error::<T>::AssetAlreadyInPool
!AssetLookupRewardVaults::<T>::contains_key(asset_id),
Error::<T>::AssetAlreadyInVault
);

// Update RewardPools storage
RewardPools::<T>::try_mutate(pool_id, |maybe_assets| -> DispatchResult {
// Update RewardVaults storage
RewardVaults::<T>::try_mutate(vault_id, |maybe_assets| -> DispatchResult {
let assets = maybe_assets.get_or_insert_with(Vec::new);

// Ensure the asset is not already in the pool
ensure!(!assets.contains(asset_id), Error::<T>::AssetAlreadyInPool);
// Ensure the asset is not already in the vault
ensure!(!assets.contains(asset_id), Error::<T>::AssetAlreadyInVault);

assets.push(*asset_id);

Ok(())
})?;

// Update AssetLookupRewardPools storage
AssetLookupRewardPools::<T>::insert(asset_id, pool_id);
// Update AssetLookupRewardVaults storage
AssetLookupRewardVaults::<T>::insert(asset_id, vault_id);

Ok(())
}

pub fn remove_asset_from_pool(pool_id: &T::PoolId, asset_id: &T::AssetId) -> DispatchResult {
// Update RewardPools storage
RewardPools::<T>::try_mutate(pool_id, |maybe_assets| -> DispatchResult {
let assets = maybe_assets.as_mut().ok_or(Error::<T>::PoolNotFound)?;
pub fn remove_asset_from_vault(vault_id: &T::VaultId, asset_id: &T::AssetId) -> DispatchResult {
// Update RewardVaults storage
RewardVaults::<T>::try_mutate(vault_id, |maybe_assets| -> DispatchResult {
let assets = maybe_assets.as_mut().ok_or(Error::<T>::VaultNotFound)?;

// Ensure the asset is in the pool
ensure!(assets.contains(asset_id), Error::<T>::AssetNotInPool);
// Ensure the asset is in the vault
ensure!(assets.contains(asset_id), Error::<T>::AssetNotInVault);

assets.retain(|id| id != asset_id);

Ok(())
})?;

// Update AssetLookupRewardPools storage
AssetLookupRewardPools::<T>::remove(asset_id);
// Update AssetLookupRewardVaults storage
AssetLookupRewardVaults::<T>::remove(asset_id);

Ok(())
}
Expand Down
62 changes: 31 additions & 31 deletions pallets/multi-asset-delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ pub mod pallet {
+ PartialOrd
+ MaxEncodedLen;

/// The pool ID type.
type PoolId: AtLeast32BitUnsigned
/// The vault ID type.
type VaultId: AtLeast32BitUnsigned
+ Parameter
+ Member
+ MaybeSerializeDeserialize
Expand Down Expand Up @@ -199,23 +199,23 @@ pub mod pallet {
StorageMap<_, Twox64Concat, T::AccountId, DelegatorMetadataOf<T>, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn reward_pools)]
/// Storage for the reward pools
pub type RewardPools<T: Config> =
StorageMap<_, Twox64Concat, T::PoolId, Vec<T::AssetId>, OptionQuery>;
#[pallet::getter(fn reward_vaults)]
/// Storage for the reward vaults
pub type RewardVaults<T: Config> =
StorageMap<_, Twox64Concat, T::VaultId, Vec<T::AssetId>, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn asset_reward_pool_lookup)]
/// Storage for the reward pools
pub type AssetLookupRewardPools<T: Config> =
StorageMap<_, Twox64Concat, T::AssetId, T::PoolId, OptionQuery>;
#[pallet::getter(fn asset_reward_vault_lookup)]
/// Storage for the reward vaults
pub type AssetLookupRewardVaults<T: Config> =
StorageMap<_, Twox64Concat, T::AssetId, T::VaultId, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn reward_config)]
/// Storage for the reward configuration, which includes APY, cap for assets, and whitelisted
/// blueprints.
pub type RewardConfigStorage<T: Config> =
StorageValue<_, RewardConfig<T::PoolId, BalanceOf<T>>, OptionQuery>;
StorageValue<_, RewardConfig<T::VaultId, BalanceOf<T>>, OptionQuery>;

/// Events emitted by the pallet.
#[pallet::event]
Expand Down Expand Up @@ -267,14 +267,14 @@ pub mod pallet {
ExecutedDelegatorBondLess { who: T::AccountId },
/// A delegator unstake request has been cancelled.
CancelledDelegatorBondLess { who: T::AccountId },
/// Event emitted when an incentive APY and cap are set for a reward pool
IncentiveAPYAndCapSet { pool_id: T::PoolId, apy: sp_runtime::Percent, cap: BalanceOf<T> },
/// Event emitted when an incentive APY and cap are set for a reward vault
IncentiveAPYAndCapSet { vault_id: T::VaultId, apy: sp_runtime::Percent, cap: BalanceOf<T> },
/// Event emitted when a blueprint is whitelisted for rewards
BlueprintWhitelisted { blueprint_id: u32 },
/// Asset has been updated to reward pool
AssetUpdatedInPool {
/// Asset has been updated to reward vault
AssetUpdatedInVault {
who: T::AccountId,
pool_id: T::PoolId,
vault_id: T::VaultId,
asset_id: T::AssetId,
action: AssetAction,
},
Expand Down Expand Up @@ -337,12 +337,12 @@ pub mod pallet {
NowithdrawRequests,
/// No matching withdraw reqests found
NoMatchingwithdrawRequest,
/// Asset already exists in a reward pool
AssetAlreadyInPool,
/// Asset not found in reward pool
AssetNotInPool,
/// The reward pool does not exist
PoolNotFound,
/// Asset already exists in a reward vault
AssetAlreadyInVault,
/// Asset not found in reward vault
AssetNotInVault,
/// The reward vault does not exist
VaultNotFound,
}

/// Hooks for the pallet.
Expand Down Expand Up @@ -580,7 +580,7 @@ pub mod pallet {
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
pub fn set_incentive_apy_and_cap(
origin: OriginFor<T>,
pool_id: T::PoolId,
vault_id: T::VaultId,
apy: sp_runtime::Percent,
cap: BalanceOf<T>,
) -> DispatchResult {
Expand All @@ -594,13 +594,13 @@ pub mod pallet {
whitelisted_blueprint_ids: Vec::new(),
});

config.configs.insert(pool_id, RewardConfigForAssetPool { apy, cap });
config.configs.insert(vault_id, RewardConfigForAssetVault { apy, cap });

*maybe_config = Some(config);
});

// Emit an event
Self::deposit_event(Event::IncentiveAPYAndCapSet { pool_id, apy, cap });
Self::deposit_event(Event::IncentiveAPYAndCapSet { vault_id, apy, cap });

Ok(())
}
Expand Down Expand Up @@ -635,23 +635,23 @@ pub mod pallet {
Ok(())
}

/// Manage asset id to pool rewards
/// Manage asset id to vault rewards
#[pallet::call_index(21)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
pub fn manage_asset_in_pool(
pub fn manage_asset_in_vault(
origin: OriginFor<T>,
pool_id: T::PoolId,
vault_id: T::VaultId,
asset_id: T::AssetId,
action: AssetAction,
) -> DispatchResult {
let who = ensure_signed(origin)?;

match action {
AssetAction::Add => Self::add_asset_to_pool(&pool_id, &asset_id)?,
AssetAction::Remove => Self::remove_asset_from_pool(&pool_id, &asset_id)?,
AssetAction::Add => Self::add_asset_to_vault(&vault_id, &asset_id)?,
AssetAction::Remove => Self::remove_asset_from_vault(&vault_id, &asset_id)?,
}

Self::deposit_event(Event::AssetUpdatedInPool { who, pool_id, asset_id, action });
Self::deposit_event(Event::AssetUpdatedInVault { who, vault_id, asset_id, action });

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/multi-asset-delegation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl pallet_multi_asset_delegation::Config for Test {
type MinDelegateAmount = ConstU64<100>;
type Fungibles = Assets;
type AssetId = AssetId;
type PoolId = AssetId;
type VaultId = AssetId;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type PalletId = PID;
type WeightInfo = ();
Expand Down
16 changes: 8 additions & 8 deletions pallets/multi-asset-delegation/src/tests/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,15 @@ fn distribute_rewards_should_work() {
let reward_config = RewardConfig {
configs: {
let mut map = BTreeMap::new();
map.insert(asset_id, RewardConfigForAssetPool { apy, cap });
map.insert(asset_id, RewardConfigForAssetVault { apy, cap });
map
},
whitelisted_blueprint_ids: vec![],
};
RewardConfigStorage::<Test>::put(reward_config);

// Set up asset pool lookup
AssetLookupRewardPools::<Test>::insert(asset_id, asset_id);
// Set up asset vault lookup
AssetLookupRewardVaults::<Test>::insert(asset_id, asset_id);

// Add delegation information
AtStake::<Test>::insert(
Expand Down Expand Up @@ -543,17 +543,17 @@ fn distribute_rewards_with_multiple_delegators_and_operators_should_work() {
let reward_config = RewardConfig {
configs: {
let mut map = BTreeMap::new();
map.insert(asset_id1, RewardConfigForAssetPool { apy: apy1, cap: cap1 });
map.insert(asset_id2, RewardConfigForAssetPool { apy: apy2, cap: cap2 });
map.insert(asset_id1, RewardConfigForAssetVault { apy: apy1, cap: cap1 });
map.insert(asset_id2, RewardConfigForAssetVault { apy: apy2, cap: cap2 });
map
},
whitelisted_blueprint_ids: vec![],
};
RewardConfigStorage::<Test>::put(reward_config);

// Set up asset pool lookup
AssetLookupRewardPools::<Test>::insert(asset_id1, asset_id1);
AssetLookupRewardPools::<Test>::insert(asset_id2, asset_id2);
// Set up asset vault lookup
AssetLookupRewardVaults::<Test>::insert(asset_id1, asset_id1);
AssetLookupRewardVaults::<Test>::insert(asset_id2, asset_id2);

// Add delegation information
AtStake::<Test>::insert(
Expand Down
8 changes: 4 additions & 4 deletions pallets/multi-asset-delegation/src/types/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sp_runtime::Percent;

/// Configuration for rewards associated with a specific asset.
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct RewardConfigForAssetPool<Balance> {
pub struct RewardConfigForAssetVault<Balance> {
// The annual percentage yield (APY) for the asset, represented as a Percent
pub apy: Percent,
// The minimum amount required before the asset can be rewarded.
Expand All @@ -28,14 +28,14 @@ pub struct RewardConfigForAssetPool<Balance> {

/// Configuration for rewards in the system.
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct RewardConfig<PoolId, Balance> {
pub struct RewardConfig<VaultId, Balance> {
// A map of asset IDs to their respective reward configurations.
pub configs: BTreeMap<PoolId, RewardConfigForAssetPool<Balance>>,
pub configs: BTreeMap<VaultId, RewardConfigForAssetVault<Balance>>,
// A list of blueprint IDs that are whitelisted for rewards.
pub whitelisted_blueprint_ids: Vec<u32>,
}

/// Asset action for pools
/// Asset action for vaults
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Eq)]
pub enum AssetAction {
Add,
Expand Down
2 changes: 1 addition & 1 deletion precompiles/multi-asset-delegation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl pallet_multi_asset_delegation::Config for Runtime {
type MinDelegateAmount = ConstU64<100>;
type Fungibles = Assets;
type AssetId = AssetId;
type PoolId = AssetId;
type VaultId = AssetId;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type PalletId = PID;
type WeightInfo = ();
Expand Down
2 changes: 1 addition & 1 deletion runtime/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ impl pallet_multi_asset_delegation::Config for Runtime {
type AssetId = AssetId;
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
type PalletId = PID;
type PoolId = AssetId;
type VaultId = AssetId;
type WeightInfo = ();
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ impl pallet_multi_asset_delegation::Config for Runtime {
type AssetId = AssetId;
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
type PalletId = PID;
type PoolId = AssetId;
type VaultId = AssetId;
type WeightInfo = ();
}

Expand Down
Loading

0 comments on commit c60215c

Please sign in to comment.