Skip to content

Commit

Permalink
Add typed balances to FI and OrderBook. (#1719)
Browse files Browse the repository at this point in the history
* typed balances in foreing-investment

* typed balances in pallet-order-book

* order-book in runtimes

* fix pool mocks

* fix IT
  • Loading branch information
lemunozm authored Feb 7, 2024
1 parent 3a1aecf commit 1858039
Show file tree
Hide file tree
Showing 28 changed files with 391 additions and 279 deletions.
11 changes: 7 additions & 4 deletions libs/mocks/src/investment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type Amount;
type TrancheAmount;
type CurrencyId;
type InvestmentId;
}
Expand Down Expand Up @@ -42,7 +43,7 @@ pub mod pallet {
}

pub fn mock_update_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId, T::Amount) -> DispatchResult + 'static,
f: impl Fn(&T::AccountId, T::InvestmentId, T::TrancheAmount) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
Expand All @@ -54,7 +55,8 @@ pub mod pallet {
}

pub fn mock_redemption(
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::Amount, DispatchError> + 'static,
f: impl Fn(&T::AccountId, T::InvestmentId) -> Result<T::TrancheAmount, DispatchError>
+ 'static,
) {
register_call!(move |(a, b)| f(a, b));
}
Expand Down Expand Up @@ -89,6 +91,7 @@ pub mod pallet {
type CurrencyId = T::CurrencyId;
type Error = DispatchError;
type InvestmentId = T::InvestmentId;
type TrancheAmount = T::TrancheAmount;

fn update_investment(
a: &T::AccountId,
Expand All @@ -112,7 +115,7 @@ pub mod pallet {
fn update_redemption(
a: &T::AccountId,
b: Self::InvestmentId,
c: Self::Amount,
c: Self::TrancheAmount,
) -> DispatchResult {
execute_call!((a, b, c))
}
Expand All @@ -124,7 +127,7 @@ pub mod pallet {
fn redemption(
a: &T::AccountId,
b: Self::InvestmentId,
) -> Result<Self::Amount, Self::Error> {
) -> Result<Self::TrancheAmount, Self::Error> {
execute_call!((a, b))
}

Expand Down
24 changes: 15 additions & 9 deletions libs/mocks/src/token_swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type CurrencyId;
type Balance;
type BalanceIn;
type BalanceOut;
type Ratio;
type OrderId;
type OrderDetails;
Expand All @@ -30,7 +31,7 @@ pub mod pallet {
T::AccountId,
T::CurrencyId,
T::CurrencyId,
T::Balance,
T::BalanceOut,
OrderRatio<T::Ratio>,
) -> Result<T::OrderId, DispatchError>
+ 'static,
Expand All @@ -39,7 +40,7 @@ pub mod pallet {
}

pub fn mock_update_order(
f: impl Fn(T::OrderId, T::Balance, OrderRatio<T::Ratio>) -> DispatchResult + 'static,
f: impl Fn(T::OrderId, T::BalanceOut, OrderRatio<T::Ratio>) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
Expand All @@ -63,15 +64,20 @@ pub mod pallet {
}

pub fn mock_convert_by_market(
f: impl Fn(T::CurrencyId, T::CurrencyId, T::Balance) -> Result<T::Balance, DispatchError>
f: impl Fn(
T::CurrencyId,
T::CurrencyId,
T::BalanceOut,
) -> Result<T::BalanceIn, DispatchError>
+ 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
}

impl<T: Config> TokenSwaps<T::AccountId> for Pallet<T> {
type Balance = T::Balance;
type BalanceIn = T::BalanceOut;
type BalanceOut = T::BalanceIn;
type CurrencyId = T::CurrencyId;
type OrderDetails = T::OrderDetails;
type OrderId = T::OrderId;
Expand All @@ -81,15 +87,15 @@ pub mod pallet {
a: T::AccountId,
b: Self::CurrencyId,
c: Self::CurrencyId,
d: Self::Balance,
d: Self::BalanceOut,
e: OrderRatio<Self::Ratio>,
) -> Result<Self::OrderId, DispatchError> {
execute_call!((a, b, c, d, e))
}

fn update_order(
a: Self::OrderId,
b: Self::Balance,
b: Self::BalanceOut,
c: OrderRatio<Self::Ratio>,
) -> DispatchResult {
execute_call!((a, b, c))
Expand All @@ -110,8 +116,8 @@ pub mod pallet {
fn convert_by_market(
a: Self::CurrencyId,
b: Self::CurrencyId,
c: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
c: Self::BalanceOut,
) -> Result<Self::BalanceIn, DispatchError> {
execute_call!((a, b, c))
}
}
Expand Down
12 changes: 7 additions & 5 deletions libs/traits/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub trait TrancheCurrency<PoolId, TrancheId> {
/// investment classes
pub trait Investment<AccountId> {
type Amount;
type TrancheAmount;
type CurrencyId;
type Error: Debug;
type InvestmentId;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub trait Investment<AccountId> {
fn update_redemption(
who: &AccountId,
investment_id: Self::InvestmentId,
amount: Self::Amount,
amount: Self::TrancheAmount,
) -> Result<(), Self::Error>;

/// Checks whether a currency is accepted as a payout for the given
Expand All @@ -84,7 +85,7 @@ pub trait Investment<AccountId> {
fn redemption(
who: &AccountId,
investment_id: Self::InvestmentId,
) -> Result<Self::Amount, Self::Error>;
) -> Result<Self::TrancheAmount, Self::Error>;

/// Checks whether an investment requires to be collected before it can be
/// updated.
Expand Down Expand Up @@ -225,6 +226,7 @@ pub trait InvestmentAccountant<AccountId> {
/// NOTE: Has many similarities with the [Investment] trait.
pub trait ForeignInvestment<AccountId> {
type Amount;
type TrancheAmount;
type CurrencyId;
type Error: Debug;
type InvestmentId;
Expand Down Expand Up @@ -268,7 +270,7 @@ pub trait ForeignInvestment<AccountId> {
fn increase_foreign_redemption(
who: &AccountId,
investment_id: Self::InvestmentId,
amount: Self::Amount,
amount: Self::TrancheAmount,
foreign_payout_currency: Self::CurrencyId,
) -> Result<(), Self::Error>;

Expand All @@ -282,7 +284,7 @@ pub trait ForeignInvestment<AccountId> {
fn decrease_foreign_redemption(
who: &AccountId,
investment_id: Self::InvestmentId,
amount: Self::Amount,
amount: Self::TrancheAmount,
foreign_payout_currency: Self::CurrencyId,
) -> Result<(), Self::Error>;

Expand Down Expand Up @@ -326,7 +328,7 @@ pub trait ForeignInvestment<AccountId> {
fn redemption(
who: &AccountId,
investment_id: Self::InvestmentId,
) -> Result<Self::Amount, Self::Error>;
) -> Result<Self::TrancheAmount, Self::Error>;

/// Checks whether a currency can be used for buying the given investment.
fn accepted_payment_currency(
Expand Down
18 changes: 10 additions & 8 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ pub enum OrderRatio<Ratio> {

pub trait TokenSwaps<Account> {
type CurrencyId;
type Balance;
type BalanceOut;
type BalanceIn;
type Ratio;
type OrderId;
type OrderDetails;
Expand All @@ -523,14 +524,14 @@ pub trait TokenSwaps<Account> {
account: Account,
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
amount_out: Self::Balance,
amount_out: Self::BalanceOut,
ratio: OrderRatio<Self::Ratio>,
) -> Result<Self::OrderId, DispatchError>;

/// Update an existing active order.
fn update_order(
order_id: Self::OrderId,
amount_out: Self::Balance,
amount_out: Self::BalanceOut,
ratio: OrderRatio<Self::Ratio>,
) -> DispatchResult;

Expand All @@ -550,8 +551,8 @@ pub trait TokenSwaps<Account> {
fn convert_by_market(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
amount_out: Self::Balance,
) -> Result<Self::Balance, DispatchError>;
amount_out: Self::BalanceOut,
) -> Result<Self::BalanceIn, DispatchError>;
}

/// Trait to transmit a change of status for anything uniquely identifiable.
Expand Down Expand Up @@ -621,9 +622,10 @@ pub trait TryConvert<A, B> {
// TODO: Remove usage for the one from frame_support::traits::tokens once we are
// on the same Polkadot version
pub trait ConversionToAssetBalance<InBalance, AssetId, AssetBalance> {
type Error;
fn to_asset_balance(balance: InBalance, asset_id: AssetId)
-> Result<AssetBalance, Self::Error>;
fn to_asset_balance(
balance: InBalance,
asset_id: AssetId,
) -> Result<AssetBalance, DispatchError>;
}

/// Converts an asset balance value into balance.
Expand Down
44 changes: 19 additions & 25 deletions libs/types/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{EnsureAddAssign, Zero},
ArithmeticError, DispatchError,
DispatchError, DispatchResult,
};
use sp_std::cmp::PartialEq;

Expand Down Expand Up @@ -116,36 +116,30 @@ impl<Balance: Zero + Copy> RedeemCollection<Balance> {

/// The collected investment/redemption amount for an account
#[derive(Encode, Default, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct CollectedAmount<Balance> {
pub struct CollectedAmount<Collected, Payment> {
/// The amount which was collected
/// * If investment: Tranche tokens
/// * If redemption: Payment currency
pub amount_collected: Balance,
pub amount_collected: Collected,

/// The amount which was converted during processing based on the
/// fulfillment price(s)
/// * If investment: Payment currency
/// * If redemption: Tranche tokens
pub amount_payment: Balance,
pub amount_payment: Payment,
}

impl<Balance: EnsureAddAssign + Copy> CollectedAmount<Balance> {
pub fn increase(&mut self, other: &Self) -> Result<(), ArithmeticError> {
impl<Collected: EnsureAddAssign + Copy, Payment: EnsureAddAssign + Copy>
CollectedAmount<Collected, Payment>
{
pub fn increase(&mut self, other: &Self) -> DispatchResult {
self.amount_collected
.ensure_add_assign(other.amount_collected)?;
self.amount_payment.ensure_add_assign(other.amount_payment)
}
}

/// A representation of an investment identifier and the corresponding owner.
///
/// NOTE: Trimmed version of `InvestmentInfo` required for foreign investments.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
self.amount_payment
.ensure_add_assign(other.amount_payment)?;

pub struct ForeignInvestmentInfo<AccountId, InvestmentId, TokenSwapReason> {
pub owner: AccountId,
pub id: InvestmentId,
pub last_swap_reason: Option<TokenSwapReason>,
Ok(())
}
}

/// A simple representation of a currency swap.
Expand Down Expand Up @@ -177,14 +171,14 @@ impl<Balance, Currency: PartialEq> Swap<Balance, Currency> {

/// A representation of a currency swap in process.
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct SwapState<Balance, Currency> {
pub struct SwapState<BalanceIn, BalanceOut, Currency> {
/// Swap not yet processed with the pending outcomming amount
pub remaining: Swap<Balance, Currency>,
pub remaining: Swap<BalanceOut, Currency>,
/// Amount of incoming currency already swapped
pub swapped_in: Balance,
pub swapped_in: BalanceIn,
/// Amount of incoming currency already swapped denominated in outgoing
/// currency
pub swapped_out: Balance,
pub swapped_out: BalanceOut,
}

/// A representation of an executed investment decrement.
Expand All @@ -203,7 +197,7 @@ pub struct ExecutedForeignDecreaseInvest<Balance, Currency> {

/// A representation of an executed collected foreign investment or redemption.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
pub struct ExecutedForeignCollect<Balance, Currency> {
pub struct ExecutedForeignCollect<Balance, TrancheTokens, Remaining, Currency> {
/// The foreign currency in which ...
/// * If investment: the payment took place
/// * If redemption: the payout takes place
Expand All @@ -217,14 +211,14 @@ pub struct ExecutedForeignCollect<Balance, Currency> {
/// The amount of tranche tokens...
/// * If investment: received for the investment made
/// * If redemption: which were actually redeemed
pub amount_tranche_tokens_payout: Balance,
pub amount_tranche_tokens_payout: TrancheTokens,

/// The unprocessed ...
/// * If investment: investment amount of `currency` (denominated in foreign
/// currency)
/// * If redemption: redemption amount of tranche tokens (denominated in
/// pool currency)
pub amount_remaining: Balance,
pub amount_remaining: Remaining,
}

/// A representation of an investment portfolio consisting of free, pending and
Expand Down
Loading

0 comments on commit 1858039

Please sign in to comment.