-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Connectors v2 message PoC #1292
Changes from 25 commits
65a3d17
690430e
b5eb58c
4588b91
0839851
394fd4e
6a86151
3ba9230
9c84f3a
ccc22b0
af67315
492fdb3
7726985
93662a1
3db1233
6cc8863
7cacb2b
7f60bd4
c064dca
dd2356e
51542c5
af3ea8e
572fb34
4ecddf9
2ab8593
618f7c2
bdbf65d
14b3f09
6a06ba4
d774926
05dd755
6b06a9d
e045d0e
787cc53
2f22dae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,15 +94,15 @@ pub type TrancheIdOf<T> = <<T as Config>::PoolInspect as PoolInspect< | |
pub type MessageOf<T> = | ||
Message<Domain, PoolIdOf<T>, TrancheIdOf<T>, <T as Config>::Balance, <T as Config>::Rate>; | ||
|
||
pub type CurrencyIdOf<T> = <T as pallet_xcm_transactor::Config>::CurrencyId; | ||
pub type CurrencyIdOf<T> = <T as Config>::CurrencyId; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use cfg_primitives::Moment; | ||
use cfg_traits::{Permissions, PoolInspect, TrancheCurrency}; | ||
use cfg_types::{ | ||
permissions::{PermissionScope, PoolRole, Role}, | ||
tokens::CustomMetadata, | ||
tokens::{CustomMetadata, GeneralCurrencyIndex}, | ||
}; | ||
use frame_support::{error::BadOrigin, pallet_prelude::*, traits::UnixTime}; | ||
use frame_system::pallet_prelude::*; | ||
|
@@ -162,6 +162,24 @@ pub mod pallet { | |
Balance = <Self as Config>::Balance, | ||
CustomMetadata = CustomMetadata, | ||
>; | ||
|
||
/// The currency type of transferrable token. | ||
type CurrencyId: Parameter | ||
+ Member | ||
+ Copy | ||
+ MaybeSerializeDeserialize | ||
+ Ord | ||
+ TypeInfo | ||
+ MaxEncodedLen | ||
+ Into<<Self as pallet_xcm_transactor::Config>::CurrencyId> | ||
+ TryInto< | ||
GeneralCurrencyIndex<u128, <Self as Config>::GeneralCurrencyPrefix>, | ||
Error = DispatchError, | ||
>; | ||
|
||
/// The prefix for currencies added via Connectors. | ||
#[pallet::constant] | ||
type GeneralCurrencyPrefix: Get<[u8; 12]>; | ||
} | ||
|
||
#[pallet::event] | ||
|
@@ -196,6 +214,8 @@ pub mod pallet { | |
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
/// Failed to map the asset to its u128 representation | ||
wischli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AssetNotFound, | ||
/// A pool could not be found | ||
PoolNotFound, | ||
/// A tranche could not be found | ||
|
@@ -276,6 +296,7 @@ pub mod pallet { | |
origin: OriginFor<T>, | ||
pool_id: PoolIdOf<T>, | ||
tranche_id: TrancheIdOf<T>, | ||
decimals: u8, | ||
domain: Domain, | ||
) -> DispatchResult { | ||
let who = ensure_signed(origin.clone())?; | ||
|
@@ -301,6 +322,7 @@ pub mod pallet { | |
Message::AddTranche { | ||
pool_id, | ||
tranche_id, | ||
decimals, | ||
token_name, | ||
token_symbol, | ||
price, | ||
|
@@ -328,7 +350,7 @@ pub mod pallet { | |
|
||
Self::do_send_message( | ||
who, | ||
Message::UpdateTokenPrice { | ||
Message::UpdateTrancheTokenPrice { | ||
pool_id, | ||
tranche_id, | ||
price, | ||
|
@@ -382,7 +404,7 @@ pub mod pallet { | |
pool_id, | ||
tranche_id, | ||
valid_until, | ||
address: domain_address.address(), | ||
member: domain_address.address(), | ||
}, | ||
domain_address.domain(), | ||
)?; | ||
|
@@ -393,7 +415,7 @@ pub mod pallet { | |
/// Transfer tranche tokens to a given address | ||
#[pallet::weight(< T as Config >::WeightInfo::transfer())] | ||
#[pallet::call_index(6)] | ||
pub fn transfer( | ||
pub fn transfer_tranche_tokens( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block local domain! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! 05dd755 |
||
origin: OriginFor<T>, | ||
pool_id: PoolIdOf<T>, | ||
tranche_id: TrancheIdOf<T>, | ||
|
@@ -427,13 +449,83 @@ pub mod pallet { | |
)?; | ||
|
||
Self::do_send_message( | ||
who, | ||
Message::Transfer { | ||
who.clone(), | ||
Message::TransferTrancheTokens { | ||
pool_id, | ||
tranche_id, | ||
amount, | ||
domain: domain_address.domain(), | ||
address: domain_address.address(), | ||
sender: who | ||
wischli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.encode() | ||
.try_into() | ||
.map_err(|_| DispatchError::Other("Conversion to 32 bytes failed"))?, | ||
wischli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
receiver: domain_address.address(), | ||
}, | ||
domain_address.domain(), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Transfer non-tranche tokens to a given address | ||
#[pallet::weight(< T as Config >::WeightInfo::transfer())] | ||
#[pallet::call_index(7)] | ||
pub fn transfer( | ||
origin: OriginFor<T>, | ||
asset_id: CurrencyIdOf<T>, | ||
domain_address: DomainAddress, | ||
amount: <T as pallet::Config>::Balance, | ||
) -> DispatchResult { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block local domain! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! 05dd755 |
||
let who = ensure_signed(origin.clone())?; | ||
|
||
ensure!(!amount.is_zero(), Error::<T>::InvalidTransferAmount); | ||
|
||
// Transfer to the domain account for bookkeeping | ||
T::Tokens::transfer( | ||
asset_id, | ||
&who, | ||
&DomainLocator::<Domain> { | ||
domain: domain_address.domain(), | ||
} | ||
.into_account_truncating(), | ||
amount, | ||
false, | ||
)?; | ||
|
||
Self::do_send_message( | ||
who.clone(), | ||
Message::Transfer { | ||
amount, | ||
currency: Self::try_get_general_index(asset_id)?, | ||
sender: who | ||
.encode() | ||
.try_into() | ||
.map_err(|_| DispatchError::Other("Conversion to 32 bytes failed"))?, | ||
receiver: domain_address.address(), | ||
}, | ||
domain_address.domain(), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Add a `CurrencyId` to the set of known currencies on a given Domain. | ||
// TODO: Replace weight after benchmarking | ||
#[pallet::weight(< T as Config >::WeightInfo::add_connector())] | ||
#[pallet::call_index(8)] | ||
pub fn add_currency( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have an issue for tracking that. This would be really important! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the FIXME There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not have an existing Github issue but this is part of the asset metadata update which is owned by @NunoAlexandre There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improved message in 05dd755 |
||
origin: OriginFor<T>, | ||
currency_id: CurrencyIdOf<T>, | ||
domain_address: DomainAddress, | ||
) -> DispatchResult { | ||
let who = ensure_signed(origin)?; | ||
|
||
Self::do_send_message( | ||
who, | ||
Message::AddCurrency { | ||
currency: Self::try_get_general_index(currency_id)?, | ||
// FIXME: In PR which adds evm_address to AssetRegistry | ||
evm_address: [0u8; 20], | ||
}, | ||
domain_address.domain(), | ||
)?; | ||
|
@@ -491,7 +583,7 @@ pub mod pallet { | |
fee_payer, | ||
// The currency in which we want to pay fees | ||
CurrencyPayment { | ||
currency: Currency::AsCurrencyId(xcm_domain.fee_currency), | ||
currency: Currency::AsCurrencyId(xcm_domain.fee_currency.into()), | ||
fee_amount: None, | ||
}, | ||
// The call to be executed in the destination chain | ||
|
@@ -550,6 +642,26 @@ pub mod pallet { | |
|
||
encoded | ||
} | ||
|
||
/// Returns the `u128` general index of a currency as the concatenation of the | ||
/// configured `GeneralCurrencyPrefix` and its local currency identifier. | ||
/// | ||
/// Assumes the currency to be registered in the `AssetRegistry`. | ||
pub fn try_get_general_index( | ||
currency: <T as pallet::Config>::CurrencyId, | ||
) -> Result<u128, DispatchError> { | ||
ensure!( | ||
<T as Config>::AssetRegistry::metadata(¤cy).is_some(), | ||
wischli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Error::<T>::AssetNotFound | ||
); | ||
|
||
let general_index: GeneralCurrencyIndex< | ||
u128, | ||
<T as pallet::Config>::GeneralCurrencyPrefix, | ||
wischli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> = CurrencyIdOf::<T>::try_into(currency)?; | ||
|
||
Ok(general_index.index) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to not have clashes with external currencies and their representation in our
CurrencyId::ForeignAssets(u128)
I would like to have this prefix be a trimmed hash of the given bytes. Given that the alphabet is at most 52 values and u8 has room for 255 this will increase the probability of not having colissions. But maybe I am too worried there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be overkill but I am always supporting safer approaches. Unfortunately we lose out on the
const
property due to usingblake2_128
and splitting. WDYT? d774926