Skip to content

Commit

Permalink
ensure invest pools when vault withdraw from stake pools
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleydon committed Dec 13, 2024
1 parent cb723b1 commit 23e47ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions pallets/phala/src/compute/stake_pool_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,6 @@ pub mod pallet {
who.clone(),
pool_info.basepool.pid,
)?;
let nft_id = maybe_nft_id.ok_or(Error::<T>::NoNftToWithdraw)?;
// The nft instance must be wrote to Nft storage at the end of the function
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
// is called. Or the property of the nft will be overwrote incorrectly.
let mut nft_guard =
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
let nft = &mut nft_guard.attr;
let in_queue_shares = match pool_info
.basepool
.withdraw_queue
Expand All @@ -803,10 +796,33 @@ pub mod pallet {
}
None => Zero::zero(),
};
ensure!(maybe_nft_id.is_some() || in_queue_shares > Zero::zero(), Error::<T>::NoNftToWithdraw);
let nft_id = match maybe_nft_id {
Some(nft_id) => nft_id,
// An nft is necessary to initiate a smaller withdrawal
None => base_pool::Pallet::<T>::mint_nft(
pool_info.basepool.cid,
who.clone(),
Zero::zero(),
pool_info.basepool.pid,
)?,
};
// The nft instance must be wrote to Nft storage at the end of the function
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
// is called. Or the property of the nft will be overwrote incorrectly.
let mut nft_guard =
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
let nft = &mut nft_guard.attr;
ensure!(
base_pool::is_nondust_balance(shares) && (shares <= nft.shares + in_queue_shares),
Error::<T>::InvalidWithdrawalAmount
);
if let Some(vault_pid) = as_vault {
let mut vault_info = ensure_vault::<T>(vault_pid)?;
if !vault_info.invest_pools.contains(&pid) {
vault_info.invest_pools.push(pid);
}
}
base_pool::Pallet::<T>::try_withdraw(
&mut pool_info.basepool,
nft,
Expand Down
2 changes: 1 addition & 1 deletion pallets/phala/src/compute/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ pub mod pallet {
if base_pool::is_nondust_balance(property.shares) {
true
} else {
if (!base_pool::is_nondust_balance(total_shares)) {
if !base_pool::is_nondust_balance(total_shares) {
let _ = base_pool::Pallet::<T>::burn_nft(
&base_pool::pallet_id::<T::AccountId>(),
stake_pool.basepool.cid,
Expand Down

0 comments on commit 23e47ee

Please sign in to comment.