diff --git a/crates/issue/src/lib.rs b/crates/issue/src/lib.rs index fc67ea87d4..6f597baf59 100644 --- a/crates/issue/src/lib.rs +++ b/crates/issue/src/lib.rs @@ -305,46 +305,63 @@ impl Pallet { T::TreasuryPalletId::get().into_account_truncating() } + // Function to cancel an issue request and slash collateral pub fn cancel_issue_request_and_slash_collateral(issue_id: &H256) -> Result, DispatchError> { + // Get the issue request using the provided issue_id let issue = Self::get_issue_request_from_id(issue_id)?; + // Retrieve the amount of griefing collateral from the issue let griefing_collateral = issue.griefing_collateral(); + // Transfer griefing collateral from the requester's account to the vault's account ext::vault_registry::transfer_funds::( CurrencySource::UserGriefing(issue.requester.get_account().clone()), CurrencySource::FreeBalance(issue.vault.account_id), &griefing_collateral, )?; + // Set the status of the issue request to 'Cancelled' Self::set_issue_status(issue_id.clone(), IssueRequestStatus::Cancelled); + // Emit an event to indicate the cancellation of the issue request Self::deposit_event(Event::CancelIssue { issue_id: issue_id.clone(), requester: issue.requester.get_account().clone(), griefing_collateral: griefing_collateral.amount(), }); + // Return the amount of griefing collateral that was slashed Ok(griefing_collateral.amount()) } + // Function to retrieve the vault ID associated with a given issue ID pub fn get_vault_from_id_from_issue_id(issue_id: &H256) -> Result, DispatchError> { + // Retrieve the issue request using the provided issue_id let issue_request = Self::get_issue_request_from_id(issue_id)?; + + // Extract and return the associated vault ID from the issue request Ok(issue_request.vault) } + // Function to complete a vault issue process pub fn _complete_vault_issue(issue_id: H256) -> Result<(), DispatchError> { + // Retrieve the issue request using the provided issue_id let issue = Self::get_issue_request_from_id(&issue_id)?; + + // Calculate the total amount including both issue amount and fee let total = issue.amount().checked_add(&issue.fee())?; - // issue_tokens -> decrease to_be_issued & increase issued for new vault + + // Decrease the to-be-issued tokens and increase the issued tokens for the new vault ext::vault_registry::issue_tokens::(&issue.vault, &total)?; - // distribute rewards + // Distribute rewards based on the issue fee ext::fee::distribute_rewards::(&issue.fee())?; - // release griefing collateral + // Release the locked griefing collateral to the requester's account let griefing_collateral: Amount = issue.griefing_collateral(); griefing_collateral.unlock_on(&issue.requester.get_account())?; + // Set the issue status to 'Completed' Self::set_issue_status(issue_id, IssueRequestStatus::Completed); Ok(()) diff --git a/crates/redeem/src/lib.rs b/crates/redeem/src/lib.rs index c38239927a..93c24f9573 100644 --- a/crates/redeem/src/lib.rs +++ b/crates/redeem/src/lib.rs @@ -404,6 +404,20 @@ pub mod pallet { Ok(().into()) } + /// Request a replace operation for a vault + /// + /// ## Parameters + /// + /// - `origin`: The origin from which the request is made, typically the vault owner. + /// - `currency_pair`: The trading pair associated with the collateral and backing assets. + /// - `amount`: The amount of backed asset to be replaced. + /// - `new_vault_id`: The identifier of the new vault that will replace the existing vault. + /// - `griefing_currency`: The currency in which a griefing deposit is required for the replace. + /// + /// ## Returns + /// + /// If the replace operation is successful, this function returns `Ok`. If the operation fails, + /// an appropriate error indicating the reason for failure is returned. #[pallet::call_index(7)] #[pallet::weight(::WeightInfo::request_replace())] #[transactional] @@ -845,6 +859,8 @@ impl Pallet { Ok(()) } + // Cancels a redeem request, replaces it with a new issue request, adjusts vault balances, and + // updates the system state. fn _cancel_replace_request(redeem_id: H256, issue_id: H256) -> DispatchResult { let redeem = Self::get_open_redeem_request_from_id(&redeem_id)?; @@ -868,7 +884,7 @@ impl Pallet { // cancel issue request and slash collateral let slashed_amount = ext::issue::cancel_issue_request_and_slash_collateral::(&issue_id)?; - // cancel redeem request + // Set the status of the redeem request to "Cancelled" Self::set_redeem_status(redeem_id, RedeemRequestStatus::Cancelled); // release event diff --git a/parachain/runtime/runtime-tests/src/parachain/fee_pool.rs b/parachain/runtime/runtime-tests/src/parachain/fee_pool.rs index b469586195..4dfb63db32 100644 --- a/parachain/runtime/runtime-tests/src/parachain/fee_pool.rs +++ b/parachain/runtime/runtime-tests/src/parachain/fee_pool.rs @@ -1014,9 +1014,7 @@ fn accrued_lend_token_interest_increases_reward_share() { } activate_lending_and_mint(Token(DOT), LendToken(1)); let vault_id = PrimitiveVaultId::new(account_of(VAULT), LendToken(1), DEFAULT_WRAPPED_CURRENCY); - println!(" \n---- 1 ---- "); CoreVaultData::force_to(&vault_id, default_vault_state(&vault_id)); - println!(" \n---- 2 ---- "); // Borrow some lend_tokens so interest starts accruing in the market let initial_lend_token_stake: u128 = CapacityRewardsPallet::get_stake(&(), &vault_id.collateral_currency()).unwrap();