Skip to content

Commit

Permalink
feat: LPv2 message reorder (#1892)
Browse files Browse the repository at this point in the history
* wip: add new msgs + reorder

* refactor: apply renaming

* fix: ITs

* feat: UpdateRestriction message

* fix: cancel_unprocessed_investment IT

* fix: fmt

* fix: clippy

* tests: reanchor solidity to reorder branch

* feat: apply hook to AddTranche msg

* tests: fix pool_management ITs

* wip: fix lp investments

* fmt

* fix: Tranche namespace

* refactor: remove fulfilled from FulfilledRedeemRequest

* chore: update lp submodule

* minor cleanup

* chore: update lp submodule

* chore: add missing cleanup

* fixes + ignore failing tests

* fmt

* tests: fix message indices
  • Loading branch information
wischli committed Jul 12, 2024
1 parent 2710a5f commit e95db39
Show file tree
Hide file tree
Showing 18 changed files with 1,162 additions and 1,723 deletions.
9 changes: 9 additions & 0 deletions libs/types/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ pub mod rewards {
/// assigned to any member of the only group in block rewards.
pub const DEFAULT_COLLATOR_STAKE: Balance = CFG;
}

pub mod liquidity_pools {
/// The account id of the solidity restriction manager interface required
/// for the `hook` param of the `AddTranche` LP message.
///
/// NOTE: Temporarily hardcoded.
pub const SOLIDITY_RESTRICTION_MANAGER_ADDRESS: [u8; 32] =
hex_literal::hex!("96ffc875c1fb9d072c6357920b27e894d2bac2ac000000000000000045564d00");
}
10 changes: 4 additions & 6 deletions pallets/liquidity-pools/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ where
Fortitude::Polite,
)?;

let message = Message::ExecutedDecreaseInvestOrder {
let message = Message::FulfilledCancelDepositRequest {
pool_id: investment_id.of_pool().into(),
tranche_id: investment_id.of_tranche().into(),
investor: investor.into(),
currency,
currency_payout: status.amount_decreased.into(),
remaining_invest_amount: status.amount_remaining.into(),
fulfilled_invest_amount: status.amount_remaining.into(),
};
T::OutboundQueue::submit(T::TreasuryAccount::get(), domain_address.domain(), message)?;

Expand Down Expand Up @@ -102,14 +102,13 @@ where
Fortitude::Polite,
)?;

let message = Message::ExecutedCollectRedeem {
let message = Message::FulfilledRedeemRequest {
pool_id: investment_id.of_pool().into(),
tranche_id: investment_id.of_tranche().into(),
investor: investor.into(),
currency,
currency_payout: status.amount_currency_payout.into(),
tranche_tokens_payout: status.amount_tranche_tokens_payout.into(),
remaining_redeem_amount: status.amount_remaining.into(),
};

T::OutboundQueue::submit(T::TreasuryAccount::get(), domain_address.domain(), message)?;
Expand Down Expand Up @@ -146,14 +145,13 @@ where
Preservation::Expendable,
)?;

let message = Message::ExecutedCollectInvest {
let message = Message::FulfilledDepositRequest {
pool_id: investment_id.of_pool().into(),
tranche_id: investment_id.of_tranche().into(),
investor: investor.into(),
currency,
currency_payout: status.amount_currency_payout.into(),
tranche_tokens_payout: status.amount_tranche_tokens_payout.into(),
remaining_invest_amount: status.amount_remaining.into(),
};

T::OutboundQueue::submit(T::TreasuryAccount::get(), domain_address.domain(), message)?;
Expand Down
94 changes: 20 additions & 74 deletions pallets/liquidity-pools/src/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ where
///
/// If the provided currency does not match the pool currency, a token swap
/// is initiated.
pub fn handle_increase_invest_order(
pub fn handle_deposit_request(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
Expand All @@ -133,23 +133,23 @@ where
Ok(())
}

/// Initiates the decrement of an existing investment order of the investor.
/// Cancels an invest order by decreasing by the entire unprocessed
/// investment amount.
///
/// On success, the unprocessed investment amount is decremented and a swap
/// back into the provided foreign currency initiated.
/// On success, initiates a swap back into the provided foreign currency.
///
/// The finalization of this call (fulfillment of the swap) is assumed to be
/// asynchronous. In any case, it is handled by
/// `DecreasedForeignInvestOrderHook` which burns the corresponding amount
/// in foreign currency and dispatches `ExecutedDecreaseInvestOrder`.
pub fn handle_decrease_invest_order(
/// in foreign currency and dispatches `FulfilledCancelDepositRequest`.
pub fn handle_cancel_deposit_request(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
currency_index: GeneralCurrencyIndexOf<T>,
amount: <T as Config>::Balance,
) -> DispatchResult {
let invest_id: T::TrancheCurrency = Self::derive_invest_id(pool_id, tranche_id)?;
let amount = T::ForeignInvestment::investment(&investor, invest_id.clone())?;
let payout_currency = Self::try_get_currency_id(currency_index)?;

T::ForeignInvestment::decrease_foreign_investment(
Expand All @@ -162,27 +162,6 @@ where
Ok(())
}

/// Cancels an invest order by decreasing by the entire unprocessed
/// investment amount.
///
/// On success, initiates a swap back into the provided foreign currency.
///
/// The finalization of this call (fulfillment of the swap) is assumed to be
/// asynchronous. In any case, it is handled by
/// `DecreasedForeignInvestOrderHook` which burns the corresponding amount
/// in foreign currency and dispatches `ExecutedDecreaseInvestOrder`.
pub fn handle_cancel_invest_order(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
currency_index: GeneralCurrencyIndexOf<T>,
) -> DispatchResult {
let invest_id: T::TrancheCurrency = Self::derive_invest_id(pool_id, tranche_id)?;
let amount = T::ForeignInvestment::investment(&investor, invest_id)?;

Self::handle_decrease_invest_order(pool_id, tranche_id, investor, currency_index, amount)
}

/// Increases an existing redemption order of the investor.
///
/// Transfers the increase redemption amount from the holdings of the
Expand All @@ -191,7 +170,7 @@ where
///
/// Assumes that the amount of tranche tokens has been locked in the
/// `DomainLocator` account of the origination domain beforehand.
pub fn handle_increase_redeem_order(
pub fn handle_redeem_request(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
Expand Down Expand Up @@ -222,84 +201,51 @@ where
Ok(())
}

/// Decreases an existing redemption order of the investor.
/// Cancels an existing redemption order of the investor by decreasing the
/// redemption by the entire unprocessed amount.
///
/// Initiates a return `ExecutedDecreaseRedemption` message to refund the
/// Initiates a return `FulfilledCancelRedeemRequest` message to refund the
/// decreased amount on the source domain.
///
/// NOTE: In contrast to investments, redemption decrements happen
/// fully synchronously as they can only be called in between increasing a
/// redemption and its (full) processing.
pub fn handle_decrease_redeem_order(
pub fn handle_cancel_redeem_request(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
tranche_tokens_payout: <T as Config>::Balance,
currency_index: GeneralCurrencyIndexOf<T>,
destination: DomainAddress,
) -> DispatchResult {
let invest_id: T::TrancheCurrency = Self::derive_invest_id(pool_id, tranche_id)?;
let amount = T::ForeignInvestment::redemption(&investor, invest_id.clone())?;
let currency_u128 = currency_index.index;
let payout_currency = Self::try_get_currency_id(currency_index)?;

T::ForeignInvestment::decrease_foreign_redemption(
&investor,
invest_id.clone(),
tranche_tokens_payout,
amount,
payout_currency,
)?;

T::Tokens::transfer(
invest_id.clone().into(),
&investor,
&destination.domain().into_account(),
tranche_tokens_payout,
amount,
Preservation::Expendable,
)?;

let message: Message = Message::ExecutedDecreaseRedeemOrder {
let message = Message::FulfilledCancelRedeemRequest {
pool_id: pool_id.into(),
tranche_id: tranche_id.into(),
investor: investor.clone().into(),
currency: currency_u128,
tranche_tokens_payout: tranche_tokens_payout.into(),
remaining_redeem_amount: T::ForeignInvestment::redemption(
&investor,
invest_id.clone(),
)?
.into(),
tranche_tokens_payout: amount.into(),
};

T::OutboundQueue::submit(T::TreasuryAccount::get(), destination.domain(), message)?;

Ok(())
}

/// Cancels an existing redemption order of the investor by decreasing the
/// redemption by the entire unprocessed amount.
///
/// Initiates a return `ExecutedDecreaseRedemption` message to refund the
/// decreased amount on the source domain.
pub fn handle_cancel_redeem_order(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
investor: T::AccountId,
currency_index: GeneralCurrencyIndexOf<T>,
destination: DomainAddress,
) -> DispatchResult {
let invest_id: T::TrancheCurrency = Self::derive_invest_id(pool_id, tranche_id)?;
let amount = T::ForeignInvestment::redemption(&investor, invest_id)?;

Self::handle_decrease_redeem_order(
pool_id,
tranche_id,
investor,
amount,
currency_index,
destination,
)
}

/// Collect the results of a user's invest orders for the given investment
/// id. If any amounts are not fulfilled, they are directly appended to the
/// next active order for this investment.
Expand All @@ -320,8 +266,8 @@ where
let invest_id: T::TrancheCurrency = Self::derive_invest_id(pool_id, tranche_id)?;
let payment_currency = Self::try_get_currency_id(currency_index)?;

// NOTE: Dispatch of `ExecutedCollectInvest` is handled by
// `ExecutedCollectInvestHook`
// NOTE: Dispatch of `FulfilledDepositRequest` is handled by
// `FulfilledDepositRequestHook`
T::ForeignInvestment::collect_foreign_investment(&investor, invest_id, payment_currency)?;

Ok(())
Expand All @@ -337,7 +283,7 @@ where
/// The termination of this call (fulfillment of the swap) is assumed to be
/// asynchronous and handled by the `CollectedForeignRedemptionHook`. It
/// burns the return currency amount and dispatches
/// `Message::ExecutedCollectRedeem` to the destination domain.
/// `Message::FulfilledRedeemRequest` to the destination domain.
pub fn handle_collect_redemption(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
Expand Down
Loading

0 comments on commit e95db39

Please sign in to comment.