Skip to content

Commit

Permalink
feat: change default blueprint selection to fixed (#831)
Browse files Browse the repository at this point in the history
* add new function to balances precompile

* fix consesus data provider

* delegator stake on blueprint

* clean slate

* update flow

* fix: fix delegation error

* tests passing

* cleanup merge

* test passing

* cleanup

* clippy fix

* setup trait

* impl trait

* tests passing

* cleanup clippy

* setup recipient

* feat: change default blueprint selection to fixed

---------

Co-authored-by: drewstone <[email protected]>
  • Loading branch information
1xstj and drewstone authored Nov 22, 2024
1 parent 967bbae commit 8f0935c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 25 deletions.
3 changes: 1 addition & 2 deletions pallets/multi-asset-delegation/src/functions/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<T: Config> Pallet<T> {
operator: T::AccountId,
asset_id: T::AssetId,
amount: BalanceOf<T>,
blueprint_selection: Option<DelegatorBlueprintSelection<T::MaxDelegatorBlueprints>>,
blueprint_selection: DelegatorBlueprintSelection<T::MaxDelegatorBlueprints>,
) -> DispatchResult {
Delegators::<T>::try_mutate(&who, |maybe_metadata| {
let metadata = maybe_metadata.as_mut().ok_or(Error::<T>::NotDelegator)?;
Expand All @@ -68,7 +68,6 @@ impl<T: Config> Pallet<T> {
{
delegation.amount += amount;
} else {
let blueprint_selection = blueprint_selection.unwrap_or_default();
// Create the new delegation
let new_delegation = BondInfoDelegator {
operator: operator.clone(),
Expand Down
2 changes: 1 addition & 1 deletion pallets/multi-asset-delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ pub mod pallet {
operator: T::AccountId,
asset_id: T::AssetId,
amount: BalanceOf<T>,
blueprint_selection: Option<DelegatorBlueprintSelection<T::MaxDelegatorBlueprints>>,
blueprint_selection: DelegatorBlueprintSelection<T::MaxDelegatorBlueprints>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::process_delegate(
Expand Down
22 changes: 11 additions & 11 deletions pallets/multi-asset-delegation/src/tests/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn delegate_should_work() {
operator,
asset_id,
amount,
None
Default::default()
));

// Assert
Expand Down Expand Up @@ -84,7 +84,7 @@ fn schedule_delegator_unstake_should_work() {
operator,
asset_id,
amount,
None
Default::default()
));

assert_ok!(MultiAssetDelegation::schedule_delegator_unstake(
Expand Down Expand Up @@ -129,7 +129,7 @@ fn execute_delegator_unstake_should_work() {
operator,
asset_id,
amount,
None
Default::default()
));
assert_ok!(MultiAssetDelegation::schedule_delegator_unstake(
RuntimeOrigin::signed(who),
Expand Down Expand Up @@ -171,7 +171,7 @@ fn cancel_delegator_unstake_should_work() {
operator,
asset_id,
amount,
None
Default::default()
));

assert_ok!(MultiAssetDelegation::schedule_delegator_unstake(
Expand Down Expand Up @@ -237,7 +237,7 @@ fn cancel_delegator_unstake_should_update_already_existing() {
operator,
asset_id,
amount,
None
Default::default()
));

assert_ok!(MultiAssetDelegation::schedule_delegator_unstake(
Expand Down Expand Up @@ -312,7 +312,7 @@ fn delegate_should_fail_if_not_enough_balance() {
operator,
asset_id,
amount,
None
Default::default()
),
Error::<Test>::InsufficientBalance
);
Expand Down Expand Up @@ -367,7 +367,7 @@ fn execute_delegator_unstake_should_fail_if_not_ready() {
operator,
asset_id,
amount,
None
Default::default()
));

assert_noop!(
Expand Down Expand Up @@ -421,7 +421,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() {
operator,
asset_id,
amount,
None
Default::default()
));

// Assert first delegation
Expand All @@ -448,7 +448,7 @@ fn delegate_should_not_create_multiple_on_repeat_delegation() {
operator,
asset_id,
additional_amount,
None
Default::default()
));

// Assert updated delegation
Expand Down Expand Up @@ -645,7 +645,7 @@ fn delegator_can_add_blueprints() {
operator,
asset_id,
amount,
Some(DelegatorBlueprintSelection::Fixed(vec![200].try_into().unwrap())),
DelegatorBlueprintSelection::Fixed(vec![200].try_into().unwrap()),
));

// Add a blueprint
Expand Down Expand Up @@ -695,7 +695,7 @@ fn delegator_can_remove_blueprints() {
operator,
asset_id,
amount,
Some(DelegatorBlueprintSelection::Fixed(vec![blueprint_id].try_into().unwrap())),
DelegatorBlueprintSelection::Fixed(vec![blueprint_id].try_into().unwrap()),
));

// Verify it was added
Expand Down
6 changes: 3 additions & 3 deletions pallets/multi-asset-delegation/src/tests/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn slash_operator_success() {
1,
asset_id,
delegator_stake,
None
Fixed(vec![blueprint_id].try_into().unwrap()),
));

// Setup delegator with all blueprints
Expand All @@ -553,7 +553,7 @@ fn slash_operator_success() {
1,
asset_id,
delegator_stake,
None
Fixed(vec![blueprint_id].try_into().unwrap()),
));

// Slash 50% of stakes
Expand Down Expand Up @@ -624,7 +624,7 @@ fn slash_delegator_fixed_blueprint_not_selected() {
1,
1,
5_000,
Some(Fixed(vec![2].try_into().unwrap())),
Fixed(vec![2].try_into().unwrap()),
));

// Try to slash with unselected blueprint
Expand Down
6 changes: 3 additions & 3 deletions pallets/multi-asset-delegation/src/tests/session_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn handle_round_change_should_work() {
operator,
asset_id,
amount,
None
Default::default()
));

assert_ok!(Pallet::<Test>::handle_round_change());
Expand Down Expand Up @@ -89,7 +89,7 @@ fn handle_round_change_with_unstake_should_work() {
operator1,
asset_id,
amount1,
None
Default::default()
));

assert_ok!(MultiAssetDelegation::deposit(
Expand All @@ -102,7 +102,7 @@ fn handle_round_change_with_unstake_should_work() {
operator2,
asset_id,
amount2,
None
Default::default()
));

// Delegator1 schedules unstake
Expand Down
9 changes: 7 additions & 2 deletions pallets/multi-asset-delegation/src/types/delegator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ use frame_support::{pallet_prelude::Get, BoundedVec};
use tangle_primitives::BlueprintId;

/// Represents how a delegator selects which blueprints to work with.
#[derive(Clone, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default, Eq)]
#[derive(Clone, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Eq)]
pub enum DelegatorBlueprintSelection<MaxBlueprints: Get<u32>> {
/// The delegator works with a fixed set of blueprints.
Fixed(BoundedVec<BlueprintId, MaxBlueprints>),
/// The delegator works with all available blueprints.
#[default]
All,
}

impl<MaxBlueprints: Get<u32>> Default for DelegatorBlueprintSelection<MaxBlueprints> {
fn default() -> Self {
DelegatorBlueprintSelection::Fixed(Default::default())
}
}

/// Represents the status of a delegator.
#[derive(Clone, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, Default)]
pub enum DelegatorStatus {
Expand Down
3 changes: 2 additions & 1 deletion precompiles/multi-asset-delegation/MultiAssetDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ interface MultiAssetDelegation {
/// @param operator The address of the operator.
/// @param assetId The ID of the asset.
/// @param amount The amount to delegate.
function delegate(bytes32 operator, uint256 assetId, uint256 amount) external returns (uint8);
/// @param blueprintSelection The blueprint selection.
function delegate(bytes32 operator, uint256 assetId, uint256 amount, uint64[] memory blueprintSelection) external returns (uint8);

/// @dev Schedule an unstake of an amount of an asset as a delegator.
/// @param operator The address of the operator.
Expand Down
11 changes: 9 additions & 2 deletions precompiles/multi-asset-delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use frame_support::{
traits::Currency,
};
use pallet_evm::AddressMapping;
use pallet_multi_asset_delegation::types::DelegatorBlueprintSelection;
use precompile_utils::prelude::*;
use sp_core::{H160, H256, U256};
use sp_runtime::traits::Dispatchable;
Expand Down Expand Up @@ -298,24 +299,30 @@ where
Ok(())
}

#[precompile::public("delegate(bytes32,uint256,uint256)")]
#[precompile::public("delegate(bytes32,uint256,uint256,uint64[])")]
fn delegate(
handle: &mut impl PrecompileHandle,
operator: H256,
asset_id: U256,
amount: U256,
blueprint_selection: Vec<u64>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let operator = Self::convert_to_account_id(operator)?;
let asset_id: <Runtime as pallet_multi_asset_delegation::Config>::AssetId =
asset_id.try_into().map_err(|_| revert("Invalid asset id"))?;
let amount: BalanceOf<Runtime> = amount.try_into().map_err(|_| revert("Invalid amount"))?;
let blueprint_selection = DelegatorBlueprintSelection::Fixed(
blueprint_selection
.try_into()
.map_err(|_| revert("Invalid blueprint selection"))?,
);
let call = pallet_multi_asset_delegation::Call::<Runtime>::delegate {
operator,
asset_id,
amount,
blueprint_selection: None,
blueprint_selection,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;
Expand Down
7 changes: 7 additions & 0 deletions precompiles/multi-asset-delegation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn test_delegate_assets_invalid_operator() {
operator: sp_core::sr25519::Public::from(TestAccount::Eve).into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_reverts(|output| output == b"Dispatched call failed with error: Module(ModuleError { index: 5, error: [2, 0, 0, 0], message: Some(\"NotAnOperator\") })");
Expand Down Expand Up @@ -123,6 +124,7 @@ fn test_delegate_assets() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_returns(());
Expand Down Expand Up @@ -157,6 +159,7 @@ fn test_delegate_assets_insufficient_balance() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(300),
blueprint_selection: Default::default(),
},
)
.execute_reverts(|output| output == b"Dispatched call failed with error: Module(ModuleError { index: 5, error: [15, 0, 0, 0], message: Some(\"InsufficientBalance\") })");
Expand Down Expand Up @@ -199,6 +202,7 @@ fn test_schedule_withdraw() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_returns(());
Expand Down Expand Up @@ -252,6 +256,7 @@ fn test_execute_withdraw() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_returns(());
Expand Down Expand Up @@ -316,6 +321,7 @@ fn test_execute_withdraw_before_due() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_returns(());
Expand Down Expand Up @@ -375,6 +381,7 @@ fn test_cancel_withdraw() {
operator: operator_account.into(),
asset_id: U256::from(1),
amount: U256::from(100),
blueprint_selection: Default::default(),
},
)
.execute_returns(());
Expand Down

0 comments on commit 8f0935c

Please sign in to comment.