Skip to content

Commit

Permalink
Feat: Liquidity pools inter domain tranche token transfer (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer authored Jun 6, 2024
1 parent 1e8c994 commit 3769a0b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 330 deletions.
30 changes: 23 additions & 7 deletions pallets/liquidity-pools/src/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use cfg_types::{
};
use frame_support::{
ensure,
traits::{fungibles::Mutate, tokens::Preservation},
traits::{fungibles::Mutate, tokens::Preservation, OriginTrait},
};
use sp_core::Get;
use sp_runtime::{
Expand All @@ -32,7 +32,7 @@ use crate::{pallet::Error, Config, GeneralCurrencyIndexOf, Message, MessageOf, P

impl<T: Config> Pallet<T>
where
T::AccountId: Into<[u8; 32]>,
<T as frame_system::Config>::AccountId: From<[u8; 32]> + Into<[u8; 32]>,
{
/// Executes a transfer from another domain exclusively for
/// non-tranche-tokens.
Expand All @@ -59,16 +59,19 @@ where
pub fn handle_tranche_tokens_transfer(
pool_id: T::PoolId,
tranche_id: T::TrancheId,
sending_domain: DomainAddress,
receiver: T::AccountId,
sending_domain: Domain,
receiver: DomainAddress,
amount: <T as Config>::Balance,
) -> DispatchResult {
ensure!(!amount.is_zero(), Error::<T>::InvalidTransferAmount);

let local_representation_of_receiver =
T::DomainAddressToAccountId::convert(receiver.clone());

ensure!(
T::Permission::has(
PermissionScope::Pool(pool_id),
receiver.clone(),
local_representation_of_receiver.clone(),
Role::PoolRole(PoolRole::TrancheInvestor(tranche_id, T::Time::now())),
),
Error::<T>::UnauthorizedTransfer
Expand All @@ -78,12 +81,25 @@ where

T::Tokens::transfer(
invest_id.into(),
&Domain::convert(sending_domain.domain()),
&receiver,
&Domain::convert(sending_domain),
&local_representation_of_receiver,
amount,
Preservation::Expendable,
)?;

// If the receiver is not on the Centrifuge domain we need to forward it now
// to the right domain from the holdings of the receiver we just transferred
// them to.
if receiver.domain() != Domain::Centrifuge {
Pallet::<T>::transfer_tranche_tokens(
T::RuntimeOrigin::signed(local_representation_of_receiver),
pool_id,
tranche_id,
receiver,
amount,
)?;
}

Ok(())
}

Expand Down
11 changes: 8 additions & 3 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,13 @@ pub mod pallet {
/// The converter from a DomainAddress to a Substrate AccountId.
type DomainAddressToAccountId: Convert<DomainAddress, Self::AccountId>;

/// The converter from a Domain 32 byte array to Substrate AccountId.
/// The converter from a Domain and 32 byte array to Substrate
/// AccountId.
type DomainAccountToAccountId: Convert<(Domain, [u8; 32]), Self::AccountId>;

/// The converter from a Domain and a 32 byte array to DomainAddress.
type DomainAccountToDomainAddress: Convert<(Domain, [u8; 32]), DomainAddress>;

/// The type for processing outgoing messages.
type OutboundQueue: OutboundQueue<
Sender = Self::AccountId,
Expand Down Expand Up @@ -973,14 +977,15 @@ pub mod pallet {
Message::TransferTrancheTokens {
pool_id,
tranche_id,
domain,
receiver,
amount,
..
} => Self::handle_tranche_tokens_transfer(
pool_id,
tranche_id,
sender,
receiver.into(),
sender.domain(),
T::DomainAccountToDomainAddress::convert((domain, receiver)),
amount,
),
Message::IncreaseInvestOrder {
Expand Down
1 change: 1 addition & 0 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ impl pallet_liquidity_pools::Config for Runtime {
type BalanceRatio = Ratio;
type CurrencyId = CurrencyId;
type DomainAccountToAccountId = AccountConverter;
type DomainAccountToDomainAddress = AccountConverter;
type DomainAddressToAccountId = AccountConverter;
type ForeignInvestment = ForeignInvestments;
type GeneralCurrencyPrefix = GeneralCurrencyPrefix;
Expand Down
99 changes: 0 additions & 99 deletions runtime/altair/src/liquidity_pools.rs

This file was deleted.

1 change: 1 addition & 0 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,7 @@ impl pallet_liquidity_pools::Config for Runtime {
type BalanceRatio = Ratio;
type CurrencyId = CurrencyId;
type DomainAccountToAccountId = AccountConverter;
type DomainAccountToDomainAddress = AccountConverter;
type DomainAddressToAccountId = AccountConverter;
type ForeignInvestment = ForeignInvestments;
type GeneralCurrencyPrefix = GeneralCurrencyPrefix;
Expand Down
116 changes: 0 additions & 116 deletions runtime/centrifuge/src/liquidity_pools.rs

This file was deleted.

21 changes: 17 additions & 4 deletions runtime/common/src/account_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use cfg_primitives::AccountId;
use cfg_types::domain_address::{Domain, DomainAddress};
use pallet_evm::AddressMapping;
use sp_core::{Get, H160};
use sp_core::{crypto::AccountId32, Get, H160};
use sp_runtime::traits::Convert;
use staging_xcm::v3;
use staging_xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -77,10 +77,23 @@ impl Convert<DomainAddress, AccountId> for AccountConverter {
}
}

impl Convert<(Domain, [u8; 32]), AccountId> for AccountConverter {
fn convert((domain, account): (Domain, [u8; 32])) -> AccountId {
impl Convert<(Domain, [u8; 32]), DomainAddress> for AccountConverter {
fn convert((domain, account): (Domain, [u8; 32])) -> DomainAddress {
match domain {
Domain::Centrifuge => AccountId::new(account),
Domain::Centrifuge => DomainAddress::Centrifuge(account),
Domain::EVM(chain_id) => {
let mut bytes20 = [0; 20];
bytes20.copy_from_slice(&account[..20]);
DomainAddress::EVM(chain_id, bytes20)
}
}
}
}

impl Convert<(Domain, [u8; 32]), AccountId32> for AccountConverter {
fn convert((domain, account): (Domain, [u8; 32])) -> AccountId32 {
match domain {
Domain::Centrifuge => AccountId32::new(account),
// EVM AccountId20 addresses are right-padded to 32 bytes
Domain::EVM(chain_id) => {
let mut bytes20 = [0; 20];
Expand Down
1 change: 1 addition & 0 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@ impl pallet_liquidity_pools::Config for Runtime {
type BalanceRatio = Ratio;
type CurrencyId = CurrencyId;
type DomainAccountToAccountId = AccountConverter;
type DomainAccountToDomainAddress = AccountConverter;
type DomainAddressToAccountId = AccountConverter;
type ForeignInvestment = ForeignInvestments;
type GeneralCurrencyPrefix = GeneralCurrencyPrefix;
Expand Down
Loading

0 comments on commit 3769a0b

Please sign in to comment.