Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize slp v2 #1423

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions pallets/slp-v2/src/astar_dapp_staking/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::{
common::types::{
Delegator, DelegatorIndex, Ledger, PendingStatus, StakingProtocol, Validator, XcmTask,
},
Call, Config, ConfigurationByStakingProtocol, DelegatorByStakingProtocolAndDelegatorIndex,
DelegatorIndexByStakingProtocolAndDelegator, Error, Event, LedgerByStakingProtocolAndDelegator,
Pallet, PendingStatusByQueryId, ValidatorsByStakingProtocolAndDelegator,
Call, Config, ConfigurationByStakingProtocol, Error, Event,
LedgerByStakingProtocolAndDelegator, Pallet, PendingStatusByQueryId,
ValidatorsByStakingProtocolAndDelegator,
};
use bifrost_primitives::VtokenMintingOperator;
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure};
Expand Down Expand Up @@ -54,18 +54,7 @@ impl<T: Config> Pallet<T> {
delegator: Delegator<T::AccountId>,
task: DappStaking<T::AccountId>,
) -> DispatchResultWithPostInfo {
let delegator_index = DelegatorIndexByStakingProtocolAndDelegator::<T>::get(
ASTAR_DAPP_STAKING,
delegator.clone(),
)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
ensure!(
DelegatorByStakingProtocolAndDelegatorIndex::<T>::contains_key(
ASTAR_DAPP_STAKING,
delegator_index
),
Error::<T>::DelegatorNotFound
);
let delegator_index = Self::ensure_delegator_exist(&ASTAR_DAPP_STAKING, &delegator)?;
let (call, pending_status) = match task.clone() {
DappStaking::Lock(amount) => (
AstarCall::<T>::DappStaking(DappStaking::<T::AccountId>::Lock(amount)).encode(),
Expand Down
2 changes: 2 additions & 0 deletions pallets/slp-v2/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ mod benchmarks {
.unwrap()
.into(),
);
assert_ok!(SlpV2::<T>::add_delegator(RawOrigin::Root.into(), STAKING_PROTOCOL, None));
let validator = Validator::AstarDappStaking(AstarValidator::Evm(H160::zero()));
#[extrinsic_call]
_(RawOrigin::Root, STAKING_PROTOCOL, delegator, validator);
Expand All @@ -97,6 +98,7 @@ mod benchmarks {
.unwrap()
.into(),
);
assert_ok!(SlpV2::<T>::add_delegator(RawOrigin::Root.into(), STAKING_PROTOCOL, None));
let validator = Validator::AstarDappStaking(AstarValidator::Evm(H160::zero()));
assert_ok!(SlpV2::<T>::add_validator(
RawOrigin::Root.into(),
Expand Down
82 changes: 50 additions & 32 deletions pallets/slp-v2/src/common/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
},
Config, ConfigurationByStakingProtocol, DelegatorByStakingProtocolAndDelegatorIndex,
DelegatorIndexByStakingProtocolAndDelegator, Error, Event, LedgerByStakingProtocolAndDelegator,
NextDelegatorIndexByStakingProtocol, Pallet,
NextDelegatorIndexByStakingProtocol, Pallet, ValidatorsByStakingProtocolAndDelegator,
};
use bifrost_primitives::{Balance, CurrencyId, VtokenMintingOperator};
use frame_support::{
Expand Down Expand Up @@ -103,15 +103,15 @@ impl<T: Config> Pallet<T> {
staking_protocol: StakingProtocol,
delegator: Delegator<T::AccountId>,
) -> DispatchResultWithPostInfo {
let delegator_index = DelegatorIndexByStakingProtocolAndDelegator::<T>::take(
staking_protocol,
delegator.clone(),
)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
DelegatorByStakingProtocolAndDelegatorIndex::<T>::take(staking_protocol, delegator_index)
.ok_or(Error::<T>::DelegatorNotFound)?;
LedgerByStakingProtocolAndDelegator::<T>::take(staking_protocol, delegator.clone())
.ok_or(Error::<T>::LedgerNotFound)?;
let delegator_index =
DelegatorIndexByStakingProtocolAndDelegator::<T>::take(&staking_protocol, &delegator)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
DelegatorByStakingProtocolAndDelegatorIndex::<T>::remove(
&staking_protocol,
delegator_index,
);
ValidatorsByStakingProtocolAndDelegator::<T>::remove(&staking_protocol, &delegator);
LedgerByStakingProtocolAndDelegator::<T>::remove(&staking_protocol, &delegator);
Self::deposit_event(Event::RemoveDelegator {
staking_protocol,
delegator_index,
Expand All @@ -124,21 +124,28 @@ impl<T: Config> Pallet<T> {
staking_protocol: StakingProtocol,
delegator: Delegator<T::AccountId>,
) -> DispatchResultWithPostInfo {
Self::ensure_delegator_exist(&staking_protocol, &delegator)?;
let currency_id = staking_protocol.info().currency_id;
let dest_beneficiary_location = staking_protocol
.get_dest_beneficiary_location::<T>(delegator)
.get_dest_beneficiary_location::<T>(delegator.clone())
.ok_or(Error::<T>::UnsupportedStakingProtocol)?;
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();
let entrance_account_fee_balance =
let entrance_account_free_balance =
T::MultiCurrency::free_balance(currency_id, &entrance_account);
T::XcmTransfer::transfer(
entrance_account,
entrance_account.clone(),
currency_id,
entrance_account_fee_balance,
entrance_account_free_balance,
dest_beneficiary_location,
WeightLimit::Unlimited,
)
.map_err(|_| Error::<T>::DerivativeAccountIdFailed)?;
Self::deposit_event(Event::TransferTo {
staking_protocol,
from: entrance_account,
to: delegator,
amount: entrance_account_free_balance,
});
Ok(().into())
}

Expand All @@ -147,23 +154,13 @@ impl<T: Config> Pallet<T> {
delegator: Delegator<T::AccountId>,
amount: Balance,
) -> DispatchResultWithPostInfo {
let delegator_index = DelegatorIndexByStakingProtocolAndDelegator::<T>::get(
staking_protocol,
delegator.clone(),
)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
ensure!(
DelegatorByStakingProtocolAndDelegatorIndex::<T>::contains_key(
staking_protocol,
delegator_index
),
Error::<T>::DelegatorNotFound
);

let delegator_index = Self::ensure_delegator_exist(&staking_protocol, &delegator)?;
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();
let transfer_back_call_data =
Self::wrap_polkadot_xcm_limited_reserve_transfer_assets_call_data(
&staking_protocol,
amount,
entrance_account.clone(),
)?;
let utility_as_derivative_call_data = Self::wrap_utility_as_derivative_call_data(
&staking_protocol,
Expand All @@ -173,6 +170,12 @@ impl<T: Config> Pallet<T> {
let xcm_message =
Self::wrap_xcm_message(&staking_protocol, utility_as_derivative_call_data)?;
Self::send_xcm_message(staking_protocol, xcm_message)?;
Self::deposit_event(Event::TransferBack {
staking_protocol,
from: delegator,
to: entrance_account,
amount,
});
Ok(().into())
}

Expand Down Expand Up @@ -207,14 +210,12 @@ impl<T: Config> Pallet<T> {
pub fn wrap_polkadot_xcm_limited_reserve_transfer_assets_call_data(
staking_protocol: &StakingProtocol,
amount: Balance,
to: T::AccountId,
) -> Result<Vec<u8>, Error<T>> {
let xcm_pallet_index = staking_protocol.info().xcm_pallet_index;
let bifrost_dest_location = staking_protocol.info().bifrost_dest_location;
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();
let account_id = entrance_account
.encode()
.try_into()
.map_err(|_| Error::<T>::DerivativeAccountIdFailed)?;
let account_id =
to.encode().try_into().map_err(|_| Error::<T>::DerivativeAccountIdFailed)?;
let beneficiary = Location::new(0, AccountId32 { network: None, id: account_id });
let fee_asset_item = 0u32;
let weight_limit = WeightLimit::Unlimited;
Expand Down Expand Up @@ -351,4 +352,21 @@ impl<T: Config> Pallet<T> {
},
}
}

pub fn ensure_delegator_exist(
staking_protocol: &StakingProtocol,
delegator: &Delegator<T::AccountId>,
) -> Result<DelegatorIndex, Error<T>> {
let delegator_index =
DelegatorIndexByStakingProtocolAndDelegator::<T>::get(staking_protocol, delegator)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
ensure!(
DelegatorByStakingProtocolAndDelegatorIndex::<T>::contains_key(
staking_protocol,
delegator_index
),
Error::<T>::DelegatorNotFound
);
Ok(delegator_index)
}
}
41 changes: 28 additions & 13 deletions pallets/slp-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,28 @@ pub mod pallet {
/// Amount of exchange rates updated
amount: Balance,
},
/// Transfer the staking token to remote chain.
TransferTo {
/// Slp supports staking protocols.
staking_protocol: StakingProtocol,
/// Bifrost Account
from: T::AccountId,
/// Delegator account.
to: Delegator<T::AccountId>,
/// Amount
amount: Balance,
},
/// Transfer the staking token back from remote chain.
TransferBack {
/// Slp supports staking protocols.
staking_protocol: StakingProtocol,
/// Delegator account.
from: Delegator<T::AccountId>,
/// Bifrost Account.
to: T::AccountId,
/// Amount
amount: Balance,
},
}

#[pallet::error]
Expand Down Expand Up @@ -430,6 +452,7 @@ pub mod pallet {
validator: Validator<T::AccountId>,
) -> DispatchResultWithPostInfo {
T::ControlOrigin::ensure_origin(origin)?;
Self::ensure_delegator_exist(&staking_protocol, &delegator)?;
ValidatorsByStakingProtocolAndDelegator::<T>::mutate(
staking_protocol,
delegator.clone(),
Expand Down Expand Up @@ -465,6 +488,7 @@ pub mod pallet {
validator: Validator<T::AccountId>,
) -> DispatchResultWithPostInfo {
T::ControlOrigin::ensure_origin(origin)?;
Self::ensure_delegator_exist(&staking_protocol, &delegator)?;
ValidatorsByStakingProtocolAndDelegator::<T>::mutate(
staking_protocol,
delegator.clone(),
Expand Down Expand Up @@ -498,18 +522,7 @@ pub mod pallet {
ledger: Ledger,
) -> DispatchResultWithPostInfo {
T::ControlOrigin::ensure_origin(origin)?;
let delegator_index = DelegatorIndexByStakingProtocolAndDelegator::<T>::get(
staking_protocol,
delegator.clone(),
)
.ok_or(Error::<T>::DelegatorIndexNotFound)?;
ensure!(
DelegatorByStakingProtocolAndDelegatorIndex::<T>::contains_key(
staking_protocol,
delegator_index
),
Error::<T>::DelegatorNotFound
);
Self::ensure_delegator_exist(&staking_protocol, &delegator)?;
LedgerByStakingProtocolAndDelegator::<T>::mutate(
staking_protocol,
delegator.clone(),
Expand Down Expand Up @@ -743,7 +756,9 @@ pub mod pallet {
.ok_or(Error::<T>::PendingStatusNotFound)?;
if Response::DispatchResult(MaybeErrorCode::Success) == response {
Self::do_notify_astar_dapp_staking(responder, pending_status)?;
};
} else {
PendingStatusByQueryId::<T>::remove(query_id);
}
Ok(().into())
}
}
Expand Down
37 changes: 18 additions & 19 deletions pallets/slp-v2/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,18 @@ fn remove_delegator_should_work() {
None
);
assert_eq!(
DelegatorIndexByStakingProtocolAndDelegator::<Test>::get(STAKING_PROTOCOL, delegator),
DelegatorIndexByStakingProtocolAndDelegator::<Test>::get(
STAKING_PROTOCOL,
delegator.clone()
),
None
);
assert_eq!(NextDelegatorIndexByStakingProtocol::<Test>::get(STAKING_PROTOCOL), 1);
assert_eq!(
ValidatorsByStakingProtocolAndDelegator::<Test>::get(STAKING_PROTOCOL, delegator)
.to_vec(),
vec![]
);
});
}

Expand All @@ -281,24 +289,6 @@ fn remove_delegator_delegator_index_not_found() {
});
}

#[test]
fn remove_delegator_delegator_not_found() {
new_test_ext().execute_with(|| {
let delegator = Delegator::Substrate(
AccountId::from_ss58check("YLF9AnL6V1vQRfuiB832NXNGZYCPAWkKLLkh7cf3KwXhB9o").unwrap(),
);
DelegatorIndexByStakingProtocolAndDelegator::<Test>::insert(
STAKING_PROTOCOL,
delegator.clone(),
0,
);
assert_noop!(
SlpV2::remove_delegator(RuntimeOrigin::root(), STAKING_PROTOCOL, delegator.clone()),
SlpV2Error::<Test>::DelegatorNotFound
);
});
}

#[test]
fn add_validator_should_work() {
new_test_ext().execute_with(|| {
Expand All @@ -307,6 +297,8 @@ fn add_validator_should_work() {
);
let validator = Validator::AstarDappStaking(AstarValidator::Evm(H160::default()));

assert_ok!(SlpV2::add_delegator(RuntimeOrigin::root(), STAKING_PROTOCOL, None));

assert_ok!(SlpV2::add_validator(
RuntimeOrigin::root(),
STAKING_PROTOCOL,
Expand Down Expand Up @@ -337,6 +329,8 @@ fn repeat_add_validator_should_work() {
AccountId::from_ss58check("YeKP2BdVpFrXbbqkoVhDFZP9u3nUuop7fpMppQczQXBLhD1").unwrap(),
));

assert_ok!(SlpV2::add_delegator(RuntimeOrigin::root(), STAKING_PROTOCOL, None));

assert_ok!(SlpV2::add_validator(
RuntimeOrigin::root(),
STAKING_PROTOCOL,
Expand Down Expand Up @@ -374,6 +368,8 @@ fn remove_validator_should_work() {
);
let validator = Validator::AstarDappStaking(AstarValidator::Evm(H160::default()));

assert_ok!(SlpV2::add_delegator(RuntimeOrigin::root(), STAKING_PROTOCOL, None));

assert_ok!(SlpV2::add_validator(
RuntimeOrigin::root(),
STAKING_PROTOCOL,
Expand Down Expand Up @@ -644,9 +640,11 @@ fn staking_protocol_get_dest_beneficiary_location() {
#[test]
fn astar_polkadot_xcm_call() {
new_test_ext().execute_with(|| {
let (to, _) = VtokenMinting::get_entrance_and_exit_accounts();
let calldata = SlpV2::wrap_polkadot_xcm_limited_reserve_transfer_assets_call_data(
&StakingProtocol::AstarDappStaking,
100,
to.clone()
)
.unwrap();

Expand All @@ -655,6 +653,7 @@ fn astar_polkadot_xcm_call() {
let call_data = SlpV2::wrap_polkadot_xcm_limited_reserve_transfer_assets_call_data(
&StakingProtocol::PolkadotStaking,
100,
to
)
.unwrap();
assert_eq!(to_hex(&call_data, false), "0x630804000100b91f04000101006d6f646c62662f76746b696e0000000000000000000000000000000000000000040400000091010000000000");
Expand Down
Loading