Skip to content

Commit

Permalink
fix: added comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Aug 14, 2023
1 parent 20dbbd1 commit 66151f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
23 changes: 20 additions & 3 deletions crates/issue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,46 +305,63 @@ impl<T: Config> Pallet<T> {
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<BalanceOf<T>, 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::<T>(
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<DefaultVaultId<T>, 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::<T>(&issue.vault, &total)?;

// distribute rewards
// Distribute rewards based on the issue fee
ext::fee::distribute_rewards::<T>(&issue.fee())?;

// release griefing collateral
// Release the locked griefing collateral to the requester's account
let griefing_collateral: Amount<T> = 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(())
Expand Down
18 changes: 17 additions & 1 deletion crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(<T as Config>::WeightInfo::request_replace())]
#[transactional]
Expand Down Expand Up @@ -845,6 +859,8 @@ impl<T: Config> Pallet<T> {
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)?;

Expand All @@ -868,7 +884,7 @@ impl<T: Config> Pallet<T> {
// cancel issue request and slash collateral
let slashed_amount = ext::issue::cancel_issue_request_and_slash_collateral::<T>(&issue_id)?;

// cancel redeem request
// Set the status of the redeem request to "Cancelled"
Self::set_redeem_status(redeem_id, RedeemRequestStatus::Cancelled);

// release event
Expand Down
2 changes: 0 additions & 2 deletions parachain/runtime/runtime-tests/src/parachain/fee_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 66151f4

Please sign in to comment.