Skip to content

Commit

Permalink
generic AssetId in swap router
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybaoo committed Nov 2, 2022
1 parent 26ea50a commit 3216103
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
5 changes: 4 additions & 1 deletion example/runtime/src/weights/block_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ pub mod constants {
let w = super::constants::BlockExecutionWeight::get();

// At least 100 µs.
assert!(w >= 100u64 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs.");
assert!(
w >= 100u64 * constants::WEIGHT_PER_MICROS,
"Weight should be at least 100 µs."
);
// At most 50 ms.
assert!(w <= 50u64 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms.");
}
Expand Down
3 changes: 2 additions & 1 deletion example/runtime/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ impl zenlink_swap_router::Config for Runtime {
type Event = super::Event;
type StablePoolId = PoolId;
type Balance = Balance;
type CurrencyId = CurrencyId;
type StableCurrencyId = CurrencyId;
type NormalCurrencyId = AssetId;
type NormalAmm = ZenlinkProtocol;
type StableAMM = ZenlinkStableAmm;
type WeightInfo = ();
Expand Down
32 changes: 21 additions & 11 deletions zenlink-swap-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::{
transactional,
};

use zenlink_protocol::{AssetBalance, AssetId, ExportZenlink};
use zenlink_protocol::{AssetBalance, ExportZenlink};
use zenlink_stable_amm::traits::StableAmmApi;

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
Expand All @@ -47,9 +47,9 @@ pub enum StableSwapMode {
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
pub enum Route<PoolId, CurrencyId> {
Stable(StablePath<PoolId, CurrencyId>),
Normal(Vec<AssetId>),
pub enum Route<PoolId, StableCurrencyId, NormalCurrencyId> {
Stable(StablePath<PoolId, StableCurrencyId>),
Normal(Vec<NormalCurrencyId>),
}

pub use pallet::*;
Expand Down Expand Up @@ -88,19 +88,29 @@ pub mod pallet {
+ Into<AssetBalance>
+ TypeInfo;

type CurrencyId: Parameter
// The currency id use in stable amm
type StableCurrencyId: Parameter
+ Member
+ Copy
+ MaybeSerializeDeserialize
+ Ord
+ TypeInfo
+ MaxEncodedLen;

type NormalAmm: ExportZenlink<AccountIdOf<Self>, AssetId>;
// The currency id use in standard amm
type NormalCurrencyId: Parameter
+ Member
+ Copy
+ MaybeSerializeDeserialize
+ Ord
+ TypeInfo
+ MaxEncodedLen;

type NormalAmm: ExportZenlink<AccountIdOf<Self>, Self::NormalCurrencyId>;

type StableAMM: StableAmmApi<
Self::StablePoolId,
Self::CurrencyId,
Self::StableCurrencyId,
AccountIdOf<Self>,
Self::Balance,
>;
Expand Down Expand Up @@ -135,7 +145,7 @@ pub mod pallet {
origin: OriginFor<T>,
amount_in: T::Balance,
amount_out_min: T::Balance,
routes: Vec<Route<T::StablePoolId, T::CurrencyId>>,
routes: Vec<Route<T::StablePoolId, T::StableCurrencyId, T::NormalCurrencyId>>,
to: T::AccountId,
deadline: T::BlockNumber,
) -> DispatchResult {
Expand Down Expand Up @@ -175,7 +185,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
fn stable_swap(
who: &T::AccountId,
path: &StablePath<T::StablePoolId, T::CurrencyId>,
path: &StablePath<T::StablePoolId, T::StableCurrencyId>,
amount_in: T::Balance,
to: &T::AccountId,
) -> Result<T::Balance, DispatchError> {
Expand Down Expand Up @@ -235,7 +245,7 @@ impl<T: Config> Pallet<T> {
fn swap(
who: &T::AccountId,
amount_in: T::Balance,
path: &[AssetId],
path: &[T::NormalCurrencyId],
to: &T::AccountId,
) -> DispatchResult {
T::NormalAmm::inner_swap_exact_assets_for_assets(
Expand All @@ -249,7 +259,7 @@ impl<T: Config> Pallet<T> {

fn currency_index_from_stable_pool(
pool_id: T::StablePoolId,
currency_id: T::CurrencyId,
currency_id: T::StableCurrencyId,
) -> Result<u32, DispatchError> {
T::StableAMM::currency_index(pool_id, currency_id)
.ok_or_else(|| Error::<T>::MismatchPoolAndCurrencyId.into())
Expand Down
3 changes: 2 additions & 1 deletion zenlink-swap-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ impl Config for Test {
type Event = Event;
type StablePoolId = PoolId;
type Balance = Balance;
type CurrencyId = CurrencyId;
type StableCurrencyId = CurrencyId;
type NormalCurrencyId = AssetId;
type NormalAmm = Zenlink;
type StableAMM = StableAMM;
type WeightInfo = ();
Expand Down

0 comments on commit 3216103

Please sign in to comment.