Skip to content

Commit

Permalink
Updates with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vimukthi-git committed Mar 16, 2022
1 parent 00b2cfe commit 54dd9ab
Show file tree
Hide file tree
Showing 4 changed files with 571 additions and 236 deletions.
148 changes: 127 additions & 21 deletions frame/pablo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod stable_swap_tests;
#[cfg(test)]
mod uniswap_tests;

mod stable_swap;
mod uniswap;

#[frame_support::pallet]
pub mod pallet {
use crate::{uniswap::Uniswap, PoolConfiguration::ConstantProduct};
use crate::{stable_swap::StableSwap, uniswap::Uniswap, PoolConfiguration::ConstantProduct};
use codec::{Codec, FullCodec};
use composable_traits::{
currency::{CurrencyFactory, LocalAssets},
Expand All @@ -67,8 +68,6 @@ pub mod pallet {
Permill,
};

use crate::stable_swap::StableSwap;

#[derive(RuntimeDebug, Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, TypeInfo)]
pub enum PoolInitConfiguration<AssetId> {
StableSwap {
Expand All @@ -77,6 +76,11 @@ pub mod pallet {
fee: Permill,
protocol_fee: Permill,
},
ConstantProduct {
pair: CurrencyPair<AssetId>,
fee: Permill,
owner_fee: Permill,
},
}

#[derive(RuntimeDebug, Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, TypeInfo)]
Expand All @@ -93,6 +97,7 @@ pub mod pallet {
PoolConfiguration<<T as frame_system::Config>::AccountId, <T as Config>::AssetId>;
type PoolInitConfigurationOf<T> = PoolInitConfiguration<<T as Config>::AssetId>;

// TODO refactor event publishing with cu-23v2y3n
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -416,13 +421,11 @@ pub mod pallet {

Ok(pool_id)
},
PoolInitConfiguration::ConstantProduct(constant_product_pool_info) =>
Uniswap::<T>::do_create_pool(
&constant_product_pool_info.owner,
constant_product_pool_info.pair,
constant_product_pool_info.fee,
constant_product_pool_info.owner_fee,
),
PoolInitConfiguration::ConstantProduct { pair, fee, owner_fee } => {
let pool_id = Uniswap::<T>::do_create_pool(&who, pair, fee, owner_fee)?;
Self::deposit_event(Event::PoolCreated { owner: who.clone(), pool_id });
Ok(pool_id)
},
}
}

Expand All @@ -447,6 +450,10 @@ pub mod pallet {
let base_amount = Self::get_exchange_value(pool_id, pair.base, quote_amount)?;
let base_amount_u: u128 = T::Convert::convert(base_amount);

// https://uniswap.org/whitepaper.pdf
// 3.2.1
// we do not inflate the lp for the owner fees
// cut is done before enforcing the invariant
let (lp_fee, protocol_fee) = if apply_fees {
let lp_fee = fee.mul_floor(base_amount_u);
// protocol_fee is computed based on lp_fee
Expand Down Expand Up @@ -480,6 +487,7 @@ pub mod pallet {
match pool {
PoolConfiguration::StableSwap(stable_swap_pool_info) =>
Ok(stable_swap_pool_info.pair),
ConstantProduct(constant_product_pool_info) => Ok(constant_product_pool_info.pair),
}
}

Expand All @@ -498,6 +506,12 @@ pub mod pallet {
asset_id,
amount,
),
ConstantProduct(constant_product_pool_info) => Uniswap::<T>::get_exchange_value(
constant_product_pool_info,
pool_account,
asset_id,
amount,
),
}
}

Expand All @@ -523,15 +537,34 @@ pub mod pallet {
min_mint_amount,
keep_alive,
)?;
Self::deposit_event(Event::<T>::LiquidityAdded {
Self::deposit_event(Event::<T>::LiquidityAddedToStableSwapPool {
who: who.clone(),
pool_id,
base_amount,
quote_amount,
mint_amount,
});
},
ConstantProduct(constant_product_pool_info) => {
let mint_amount = Uniswap::<T>::add_liquidity(
who,
constant_product_pool_info,
pool_account,
base_amount,
quote_amount,
min_mint_amount,
keep_alive,
)?;
Self::deposit_event(Event::<T>::LiquidityAddedToConstantProductPool {
who: who.clone(),
pool_id,
base_amount,
quote_amount,
minted_lp: mint_amount,
});
},
}
// TODO refactor event publishing with cu-23v2y3n
Ok(())
}

Expand Down Expand Up @@ -564,7 +597,25 @@ pub mod pallet {
total_issuance: updated_lp,
});
},
ConstantProduct(constant_product_pool_info) => {
let (base_amount, quote_amount, updated_lp) = Uniswap::<T>::remove_liquidity(
who,
constant_product_pool_info,
pool_account,
lp_amount,
min_base_amount,
min_quote_amount,
)?;
Self::deposit_event(Event::<T>::LiquidityRemoved {
pool_id,
who: who.clone(),
base_amount,
quote_amount,
total_issuance: updated_lp,
});
},
}
// TODO refactor event publishing with cu-23v2y3n
Ok(())
}

Expand Down Expand Up @@ -627,7 +678,53 @@ pub mod pallet {

Ok(base_amount_excluding_fees)
},
ConstantProduct(constant_product_pool_info) => {
let (base_amount, quote_amount, lp_fees, owner_fees) = Self::do_compute_swap(
pool_id,
pair,
quote_amount,
true,
constant_product_pool_info.fee,
constant_product_pool_info.owner_fee,
)?;
let total_fees = lp_fees.safe_add(&owner_fees)?;
let quote_amount_including_fees = quote_amount.safe_add(&total_fees)?;

ensure!(base_amount >= min_receive, Error::<T>::CannotRespectMinimumRequested);

let pool_account = Self::account_id(&pool_id);
T::Assets::transfer(
pair.quote,
who,
&pool_account,
quote_amount_including_fees,
keep_alive,
)?;
// NOTE(hussein-aitlance): no need to keep alive the pool account
T::Assets::transfer(
pair.quote,
&pool_account,
&constant_product_pool_info.owner,
owner_fees,
false,
)?;
T::Assets::transfer(pair.base, &pool_account, who, base_amount, false)?;

Self::deposit_event(Event::<T>::Swapped {
pool_id,
who: who.clone(),
base_asset: pair.base,
quote_asset: pair.quote,
base_amount,
quote_amount,
fee: total_fees,
});

Ok(base_amount)
},
}

// TODO refactor event publishing with cu-23v2y3n
}

#[transactional]
Expand All @@ -651,6 +748,15 @@ pub mod pallet {
Self::exchange(who, pool_id, pair, dx, T::Balance::zero(), keep_alive)?;
Ok(amount)
},
ConstantProduct(constant_product_pool) => {
let pair = if asset_id == constant_product_pool.pair.base {
constant_product_pool.pair
} else {
constant_product_pool.pair.swap()
};
let quote_amount = Self::get_exchange_value(pool_id, asset_id, amount)?;
Self::exchange(who, pool_id, pair, quote_amount, T::Balance::zero(), keep_alive)
},
}
}

Expand All @@ -667,15 +773,15 @@ pub mod pallet {
PoolConfiguration::StableSwap(pool) => {
let pair =
if asset_id == pool.pair.base { pool.pair.swap() } else { pool.pair };
let dy = Self::exchange(
who,
pool_id,
pair,
amount,
Self::Balance::zero(),
keep_alive,
)?;
Ok(dy)
Self::exchange(who, pool_id, pair, amount, Self::Balance::zero(), keep_alive)
},
ConstantProduct(constant_product_pool) => {
let pair = if asset_id == constant_product_pool.pair.base {
constant_product_pool.pair.swap()
} else {
constant_product_pool.pair
};
Self::exchange(who, pool_id, pair, amount, T::Balance::zero(), keep_alive)
},
}
}
Expand Down
1 change: 1 addition & 0 deletions frame/pablo/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use system::EnsureRoot;

pub type CurrencyId = u128;

pub const BTC: AssetId = 0;
pub const USDT: CurrencyId = 2;
pub const USDC: CurrencyId = 4;

Expand Down
Loading

0 comments on commit 54dd9ab

Please sign in to comment.