Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/open-gov-2
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Jun 25, 2024
2 parents 7175fe9 + de2886f commit 4beb919
Show file tree
Hide file tree
Showing 63 changed files with 6,401 additions and 8,311 deletions.
10 changes: 1 addition & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions libs/mocks/src/try_convert.rs → libs/mocks/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ pub mod pallet {
use cfg_traits::TryConvert;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call_instance, register_call_instance};
use sp_runtime::traits::Convert;

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
type From;

type To;

type Error;
}

#[pallet::pallet]
Expand All @@ -20,16 +18,26 @@ pub mod pallet {
type CallIds<T: Config<I>, I: 'static = ()> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn mock_try_convert(f: impl Fn(T::From) -> Result<T::To, T::Error> + 'static) {
pub fn mock_try_convert(f: impl Fn(T::From) -> Result<T::To, DispatchError> + 'static) {
register_call_instance!(f);
}

pub fn mock_convert(f: impl Fn(T::From) -> T::To + 'static) {
register_call_instance!(f);
}
}

impl<T: Config<I>, I: 'static> TryConvert<T::From, T::To> for Pallet<T, I> {
type Error = T::Error;
type Error = DispatchError;

fn try_convert(from: T::From) -> Result<T::To, Self::Error> {
execute_call_instance!(from)
}
}

impl<T: Config<I>, I: 'static> Convert<T::From, T::To> for Pallet<T, I> {
fn convert(from: T::From) -> T::To {
execute_call_instance!(from)
}
}
}
159 changes: 159 additions & 0 deletions libs/mocks/src/foreign_investment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::investments::ForeignInvestment;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Amount;
type TrancheAmount;
type CurrencyId;
type InvestmentId;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_increase_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount, T::CurrencyId) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_decrease_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount, T::CurrencyId) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_increase_foreign_redemption(
f: impl Fn(
&T::AccountId,
T::InvestmentId,
T::TrancheAmount,
T::CurrencyId,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_decrease_foreign_redemption(
f: impl Fn(
&T::AccountId,
T::InvestmentId,
T::TrancheAmount,
T::CurrencyId,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_collect_foreign_investment(
f: impl Fn(&T::AccountId, T::InvestmentId, T::CurrencyId) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_collect_foreign_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId, T::CurrencyId) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_investment(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::Amount, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::TrancheAmount, DispatchError>
+ 'static,
) {
register_call!(move |(a, b)| f(a, b));
}
}

impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> {
type Amount = T::Amount;
type CurrencyId = T::CurrencyId;
type Error = DispatchError;
type InvestmentId = T::InvestmentId;
type TrancheAmount = T::TrancheAmount;

fn increase_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn decrease_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn increase_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::TrancheAmount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn decrease_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::TrancheAmount,
d: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c, d))
}

fn collect_foreign_investment(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn collect_foreign_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::CurrencyId,
) -> DispatchResult {
execute_call!((a, b, c))
}

fn investment(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Amount, DispatchError> {
execute_call!((a, b))
}

fn redemption(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::TrancheAmount, DispatchError> {
execute_call!((a, b))
}
}
}
5 changes: 3 additions & 2 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pub mod asset_registry;
pub mod change_guard;
pub mod converter;
pub mod currency_conversion;
pub mod data;
pub mod fees;
pub mod foreign_investment;
pub mod investment;
pub mod liquidity_pools;
pub mod liquidity_pools_gateway_routers;
pub mod outbound_queue;
pub mod pay_fee;
pub mod permissions;
pub mod pools;
Expand All @@ -14,7 +17,6 @@ pub mod rewards;
pub mod status_notification;
pub mod time;
pub mod token_swaps;
pub mod try_convert;
pub mod value_provider;
pub mod write_off_policy;

Expand All @@ -33,7 +35,6 @@ pub use rewards::pallet as pallet_mock_rewards;
pub use status_notification::pallet as pallet_mock_status_notification;
pub use time::pallet as pallet_mock_time;
pub use token_swaps::pallet as pallet_mock_token_swaps;
pub use try_convert::pallet as pallet_mock_try_convert;
pub use value_provider::pallet as pallet_mock_value_provider;
pub use write_off_policy::pallet as pallet_mock_write_off_policy;

Expand Down
37 changes: 37 additions & 0 deletions libs/mocks/src/outbound_queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use cfg_traits::liquidity_pools::OutboundQueue;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Sender;
type Destination;
type Message;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
type CallIds<T: Config> = StorageMap<_, _, String, mock_builder::CallId>;

impl<T: Config> Pallet<T> {
pub fn mock_submit(
f: impl Fn(T::Sender, T::Destination, T::Message) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
}

impl<T: Config> OutboundQueue for Pallet<T> {
type Destination = T::Destination;
type Message = T::Message;
type Sender = T::Sender;

fn submit(a: Self::Sender, b: Self::Destination, c: Self::Message) -> DispatchResult {
execute_call!((a, b, c))
}
}
}
7 changes: 5 additions & 2 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ pub mod constants {
/// The maximum number of pool fees per pool fee bucket
pub const MAX_POOL_FEES_PER_BUCKET: u32 = 100;

/// Identification of the native token of the chain. Used in XCM locations.
pub const NATIVE_KEY: &[u8] = &[0, 1];

/// The index of the root OpenGov track
pub const TRACK_INDEX_ROOT: u16 = 0;
/// The index of the whitelisted caller OpenGov track
Expand Down Expand Up @@ -293,7 +296,7 @@ pub mod parachains {

pub mod altair {
pub const ID: u32 = 2088;
pub const AIR_KEY: &[u8] = &[0, 1];
pub const AIR_KEY: &[u8] = crate::NATIVE_KEY;
}
}

Expand All @@ -305,7 +308,7 @@ pub mod parachains {

pub mod centrifuge {
pub const ID: u32 = 2031;
pub const CFG_KEY: &[u8] = &[0, 1];
pub const CFG_KEY: &[u8] = crate::NATIVE_KEY;
}
}

Expand Down
36 changes: 29 additions & 7 deletions libs/types/src/domain_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cfg_utils::{decode_be_bytes, vec_to_fixed_array};
use frame_support::pallet_prelude::RuntimeDebug;
use parity_scale_codec::{Decode, Encode, Input, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::traits::{AccountIdConversion, Convert};
use sp_runtime::traits::AccountIdConversion;
use sp_std::{vec, vec::Vec};

use crate::EVMChainId;
Expand Down Expand Up @@ -63,12 +63,12 @@ impl Codec for Domain {
}
}

impl<AccountId> Convert<Domain, AccountId> for Domain
where
AccountId: Encode + Decode,
{
fn convert(domain: Domain) -> AccountId {
DomainLocator { domain }.into_account_truncating()
impl Domain {
pub fn into_account<AccountId: Encode + Decode>(&self) -> AccountId {
DomainLocator {
domain: self.clone(),
}
.into_account_truncating()
}
}

Expand Down Expand Up @@ -118,3 +118,25 @@ impl DomainAddress {
self.clone().into()
}
}

#[cfg(test)]
mod tests {
use parity_scale_codec::{Decode, Encode};

use super::*;

#[test]
fn test_domain_encode_decode() {
test_domain_identity(Domain::Centrifuge);
test_domain_identity(Domain::EVM(1284));
test_domain_identity(Domain::EVM(1));
}

/// Test that (decode . encode) results in the original value
fn test_domain_identity(domain: Domain) {
let encoded = domain.encode();
let decoded = Domain::decode(&mut encoded.as_slice()).unwrap();

assert_eq!(domain, decoded);
}
}
Loading

0 comments on commit 4beb919

Please sign in to comment.