Skip to content

Commit

Permalink
Use Balance instead of u128 as type for Balancer weights (#1251)
Browse files Browse the repository at this point in the history
* Replace hard-coded `u128` fo weights with balance types

* Replace modulo operator with `checked_rem_res`

* Update copyright notices

* Revert changes to modulo operations
  • Loading branch information
maltekliemann authored Feb 15, 2024
1 parent f793cf3 commit be75fa9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
3 changes: 1 addition & 2 deletions primitives/src/traits/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use frame_support::dispatch::{DispatchError, Weight};
pub trait Swaps<AccountId> {
type Asset;
type Balance;
// TODO(#1216): Add weight type which implements `Into<Balance>` and `From<Balance>`

/// Creates an initial active pool.
///
Expand All @@ -44,7 +43,7 @@ pub trait Swaps<AccountId> {
assets: Vec<Self::Asset>,
swap_fee: Self::Balance,
amount: Self::Balance,
weights: Vec<u128>,
weights: Vec<Self::Balance>,
) -> Result<PoolId, DispatchError>;

/// Close the specified pool.
Expand Down
6 changes: 3 additions & 3 deletions zrml/swaps/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn bench_create_pool<T>(
caller: T::AccountId,
asset_count: usize,
opt_asset_amount: Option<BalanceOf<T>>,
opt_weights: Option<Vec<u128>>,
opt_weights: Option<Vec<BalanceOf<T>>>,
open: bool,
) -> (u128, Vec<AssetOf<T>>, BalanceOf<T>)
where
Expand Down Expand Up @@ -169,7 +169,7 @@ benchmarks! {
let balance: BalanceOf<T> = LIQUIDITY.saturated_into();
let asset_amount_in: BalanceOf<T> = balance.bmul(T::MaxInRatio::get()).unwrap();
let weight_in = T::MinWeight::get();
let weight_out = 2 * weight_in;
let weight_out = weight_in * 2u8.into();
let mut weights = vec![weight_in; asset_count as usize];
weights[asset_count as usize - 1] = weight_out;
let caller: T::AccountId = whitelisted_caller();
Expand Down Expand Up @@ -205,7 +205,7 @@ benchmarks! {
let balance: BalanceOf<T> = LIQUIDITY.saturated_into();
let asset_amount_out: BalanceOf<T> = balance.bmul(T::MaxOutRatio::get()).unwrap();
let weight_out = T::MinWeight::get();
let weight_in = 4 * weight_out;
let weight_in = weight_out * 4u8.into();
let mut weights = vec![weight_out; asset_count as usize];
weights[0] = weight_in;
let caller: T::AccountId = whitelisted_caller();
Expand Down
35 changes: 19 additions & 16 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mod pallet {
ensure!(pool_amount <= mul, Error::<T>::MaxInRatio);
let asset_amount: BalanceOf<T> = crate::math::calc_single_out_given_pool_in(
asset_balance.saturated_into(),
Self::pool_weight_rslt(&pool, &asset)?,
Self::pool_weight_rslt(&pool, &asset)?.saturated_into(),
total_supply.saturated_into(),
pool.total_weight.saturated_into(),
pool_amount.saturated_into(),
Expand Down Expand Up @@ -372,7 +372,7 @@ mod pallet {
ensure!(pool_amount <= mul, Error::<T>::MaxOutRatio);
let asset_amount: BalanceOf<T> = crate::math::calc_single_in_given_pool_out(
asset_balance.saturated_into(),
Self::pool_weight_rslt(&pool, &asset)?,
Self::pool_weight_rslt(&pool, &asset)?.saturated_into(),
total_supply.saturated_into(),
pool.total_weight.saturated_into(),
pool_amount.saturated_into(),
Expand Down Expand Up @@ -537,17 +537,17 @@ mod pallet {
type MaxSwapFee: Get<BalanceOf<Self>>;

#[pallet::constant]
type MaxTotalWeight: Get<u128>;
type MaxTotalWeight: Get<BalanceOf<Self>>;

#[pallet::constant]
type MaxWeight: Get<u128>;
type MaxWeight: Get<BalanceOf<Self>>;

#[pallet::constant]
/// The minimum amount of assets in a pool.
type MinAssets: Get<u16>;

#[pallet::constant]
type MinWeight: Get<u128>;
type MinWeight: Get<BalanceOf<Self>>;

/// The module identifier.
#[pallet::constant]
Expand Down Expand Up @@ -784,9 +784,9 @@ mod pallet {

Ok(crate::math::calc_spot_price(
balance_in.saturated_into(),
in_weight,
in_weight.saturated_into(),
balance_out.saturated_into(),
out_weight,
out_weight.saturated_into(),
swap_fee,
)?
.saturated_into())
Expand Down Expand Up @@ -938,7 +938,10 @@ mod pallet {
})
}

fn pool_weight_rslt(pool: &PoolOf<T>, asset: &AssetOf<T>) -> Result<u128, Error<T>> {
fn pool_weight_rslt(
pool: &PoolOf<T>,
asset: &AssetOf<T>,
) -> Result<BalanceOf<T>, Error<T>> {
pool.weights.get(asset).cloned().ok_or(Error::<T>::AssetNotBound)
}

Expand Down Expand Up @@ -980,15 +983,15 @@ mod pallet {
assets: Vec<AssetOf<T>>,
swap_fee: BalanceOf<T>,
amount: BalanceOf<T>,
weights: Vec<u128>,
weights: Vec<BalanceOf<T>>,
) -> Result<PoolId, DispatchError> {
ensure!(assets.len() <= usize::from(T::MaxAssets::get()), Error::<T>::TooManyAssets);
ensure!(assets.len() >= usize::from(T::MinAssets::get()), Error::<T>::TooFewAssets);
let next_pool_id = Self::inc_next_pool_id()?;
let pool_shares_id = Self::pool_shares_id(next_pool_id);
let pool_account = Self::pool_account_id(&next_pool_id);
let mut map = BTreeMap::new();
let mut total_weight = 0;
let mut total_weight: BalanceOf<T> = Zero::zero();
let mut sorted_assets = assets.clone();
sorted_assets.sort();
let has_duplicates = sorted_assets
Expand Down Expand Up @@ -1132,7 +1135,7 @@ mod pallet {
pool_amount: |asset_balance: BalanceOf<T>, total_supply: BalanceOf<T>| {
let pool_amount: BalanceOf<T> = crate::math::calc_pool_in_given_single_out(
asset_balance.saturated_into(),
Self::pool_weight_rslt(pool_ref, &asset)?,
Self::pool_weight_rslt(pool_ref, &asset)?.saturated_into(),
total_supply.saturated_into(),
pool_ref.total_weight.saturated_into(),
asset_amount.saturated_into(),
Expand Down Expand Up @@ -1190,7 +1193,7 @@ mod pallet {
ensure!(asset_amount <= mul, Error::<T>::MaxInRatio);
let pool_amount: BalanceOf<T> = crate::math::calc_pool_out_given_single_in(
asset_balance.saturated_into(),
Self::pool_weight_rslt(pool_ref, &asset_in)?,
Self::pool_weight_rslt(pool_ref, &asset_in)?.saturated_into(),
total_supply.saturated_into(),
pool_ref.total_weight.saturated_into(),
asset_amount.saturated_into(),
Expand Down Expand Up @@ -1258,9 +1261,9 @@ mod pallet {
);
let asset_amount_out: BalanceOf<T> = crate::math::calc_out_given_in(
balance_in.saturated_into(),
Self::pool_weight_rslt(&pool, &asset_in)?,
Self::pool_weight_rslt(&pool, &asset_in)?.saturated_into(),
balance_out.saturated_into(),
Self::pool_weight_rslt(&pool, &asset_out)?,
Self::pool_weight_rslt(&pool, &asset_out)?.saturated_into(),
asset_amount_in.saturated_into(),
pool.swap_fee.saturated_into(),
)?
Expand Down Expand Up @@ -1329,9 +1332,9 @@ mod pallet {
let balance_in = T::AssetManager::free_balance(asset_in, &pool_account_id);
let asset_amount_in: BalanceOf<T> = crate::math::calc_in_given_out(
balance_in.saturated_into(),
Self::pool_weight_rslt(&pool, &asset_in)?,
Self::pool_weight_rslt(&pool, &asset_in)?.saturated_into(),
balance_out.saturated_into(),
Self::pool_weight_rslt(&pool, &asset_out)?,
Self::pool_weight_rslt(&pool, &asset_out)?.saturated_into(),
asset_amount_out.saturated_into(),
pool.swap_fee.saturated_into(),
)?
Expand Down
6 changes: 2 additions & 4 deletions zrml/swaps/src/types/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ impl Get<u32> for MaxAssets {
}
}

// TODO(#1213): Replace `u128` with `PoolWeight` type which implements `Into<Balance>` and
// `From<Balance>`! Or just replace it with `Balance`.
#[derive(TypeInfo, Clone, Encode, Eq, Decode, MaxEncodedLen, PartialEq, RuntimeDebug)]
pub struct Pool<Asset, Balance> {
pub assets: BoundedVec<Asset, MaxAssets>,
pub status: PoolStatus,
pub swap_fee: Balance,
pub total_weight: u128,
pub weights: BoundedBTreeMap<Asset, u128, MaxAssets>,
pub total_weight: Balance,
pub weights: BoundedBTreeMap<Asset, Balance, MaxAssets>,
}

impl<Asset, Balance> Pool<Asset, Balance>
Expand Down

0 comments on commit be75fa9

Please sign in to comment.