Skip to content

Commit

Permalink
Fix add liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed May 20, 2021
1 parent 312f5d5 commit 71583a4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 71 deletions.
66 changes: 27 additions & 39 deletions zenlink-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,22 @@ pub mod pallet {

/// Swap
/// Create a trading pair. \[creator, asset_id, asset_id\]
/// Create a trading pair. \[creator, asset_0, asset_1\]
PairCreated(T::AccountId, AssetId, AssetId),
/// Add liquidity. \[owner, asset_id, asset_id\]
LiquidityAdded(T::AccountId, AssetId, AssetId),
/// Remove liquidity. \[owner, recipient, asset_id, asset_id, amount\]
LiquidityRemoved(T::AccountId, T::AccountId, AssetId, AssetId, AssetBalance),
/// Transact in trading \[owner, recipient, swap_path\]
AssetSwap(T::AccountId, T::AccountId, Vec<AssetId>),
/// Add liquidity. \[owner, asset_0, asset_1, add_balance_0, add_balance_1, mint_balance_lp\]
LiquidityAdded(T::AccountId, AssetId, AssetId, AssetBalance, AssetBalance, AssetBalance),
/// Remove liquidity. \[owner, recipient, asset_0, asset_1, rm_balance_0, rm_balance_1, burn_balance_lp\]
LiquidityRemoved(
T::AccountId,
T::AccountId,
AssetId,
AssetId,
AssetBalance,
AssetBalance,
AssetBalance,
),
/// Transact in trading \[owner, recipient, swap_path, balance_in, balance_out\]
AssetSwap(T::AccountId, T::AccountId, Vec<AssetId>, AssetBalance, AssetBalance),

/// Transfer by xcm
Expand Down Expand Up @@ -381,7 +389,6 @@ pub mod pallet {
ensure!(asset_0.is_support() && asset_1.is_support(), Error::<T>::UnsupportedAssetType);
let who = ensure_signed(origin)?;
let now = frame_system::Pallet::<T>::block_number();
let (asset_0, asset_1) = Self::sort_asset_id(asset_0, asset_1);
ensure!(deadline > now, Error::<T>::Deadline);

Self::inner_add_liquidity(
Expand All @@ -392,11 +399,7 @@ pub mod pallet {
amount_1_desired,
amount_0_min,
amount_1_min,
)?;

Self::deposit_event(Event::LiquidityAdded(who, asset_0, asset_1));

Ok(())
)
}

/// Extract liquidity.
Expand All @@ -419,33 +422,26 @@ pub mod pallet {
asset_0: AssetId,
asset_1: AssetId,
#[pallet::compact] liquidity: AssetBalance,
#[pallet::compact] amount_asset_0_min: AssetBalance,
#[pallet::compact] amount_asset_1_min: AssetBalance,
#[pallet::compact] amount_0_min: AssetBalance,
#[pallet::compact] amount_1_min: AssetBalance,
recipient: <T::Lookup as StaticLookup>::Source,
#[pallet::compact] deadline: T::BlockNumber,
) -> DispatchResult {
ensure!(asset_0.is_support() && asset_1.is_support(), Error::<T>::UnsupportedAssetType);
let who = ensure_signed(origin)?;
let recipient = T::Lookup::lookup(recipient)?;
let now = frame_system::Pallet::<T>::block_number();
let (asset_0, asset_1) = Self::sort_asset_id(asset_0, asset_1);
ensure!(deadline > now, Error::<T>::Deadline);

Self::inner_remove_liquidity(
&who,
asset_0,
asset_1,
liquidity,
amount_asset_0_min,
amount_asset_1_min,
amount_0_min,
amount_1_min,
&recipient,
)?;

Self::deposit_event(Event::LiquidityRemoved(
who, recipient, asset_0, asset_1, liquidity,
));

Ok(())
)
}

/// Sell amount of foreign by path.
Expand All @@ -459,7 +455,7 @@ pub mod pallet {
/// - `deadline`: Height of the cutoff block of this transaction
#[pallet::weight(1_000_000)]
#[frame_support::transactional]
pub fn swap_exact_tokens_for_tokens(
pub fn swap_exact_assets_for_assets(
origin: OriginFor<T>,
#[pallet::compact] amount_in: AssetBalance,
#[pallet::compact] amount_out_min: AssetBalance,
Expand All @@ -473,17 +469,13 @@ pub mod pallet {
let now = frame_system::Pallet::<T>::block_number();
ensure!(deadline > now, Error::<T>::Deadline);

Self::inner_swap_exact_tokens_for_tokens(
Self::inner_swap_exact_assets_for_assets(
&who,
amount_in,
amount_out_min,
&path,
&recipient,
)?;

Self::deposit_event(Event::AssetSwap(who, recipient, path));

Ok(())
)
}

/// Buy amount of foreign by path.
Expand All @@ -497,7 +489,7 @@ pub mod pallet {
/// - `deadline`: Height of the cutoff block of this transaction
#[pallet::weight(1_000_000)]
#[frame_support::transactional]
pub fn swap_tokens_for_exact_tokens(
pub fn swap_assets_for_exact_assets(
origin: OriginFor<T>,
#[pallet::compact] amount_out: AssetBalance,
#[pallet::compact] amount_in_max: AssetBalance,
Expand All @@ -511,17 +503,13 @@ pub mod pallet {
let now = frame_system::Pallet::<T>::block_number();
ensure!(deadline > now, Error::<T>::Deadline);

Self::inner_swap_tokens_for_exact_tokens(
Self::inner_swap_assets_for_exact_assets(
&who,
amount_out,
amount_in_max,
&path,
&recipient,
)?;

Self::deposit_event(Event::AssetSwap(who, recipient, path));

Ok(())
)
}
}
}
74 changes: 56 additions & 18 deletions zenlink-protocol/src/swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<T: Config> Pallet<T> {
amount_0_min: AssetBalance,
amount_1_min: AssetBalance,
) -> DispatchResult {
LiquidityMeta::<T>::try_mutate((asset_0, asset_1), |meta| {
LiquidityMeta::<T>::try_mutate(Self::sort_asset_id(asset_0, asset_1), |meta| {
if let Some((pair_account, total_liquidity)) = meta {
let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, pair_account);
let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, pair_account);
Expand Down Expand Up @@ -99,8 +99,17 @@ impl<T: Config> Pallet<T> {
total_liquidity.checked_add(mint_liquidity).ok_or(Error::<T>::Overflow)?;
Self::mutate_liquidity(asset_0, asset_1, who, mint_liquidity, true)?;

T::MultiAssetsHandler::transfer(asset_0, &who, &pair_account, amount_0)?;
T::MultiAssetsHandler::transfer(asset_1, &who, &pair_account, amount_1)?;
T::MultiAssetsHandler::transfer(asset_0, who, &pair_account, amount_0)?;
T::MultiAssetsHandler::transfer(asset_1, who, &pair_account, amount_1)?;

Self::deposit_event(Event::LiquidityAdded(
who.clone(),
asset_0,
asset_1,
amount_0,
amount_1,
mint_liquidity,
));

Ok(())
} else {
Expand All @@ -115,16 +124,16 @@ impl<T: Config> Pallet<T> {
asset_0: AssetId,
asset_1: AssetId,
remove_liquidity: AssetBalance,
amount_token_0_min: AssetBalance,
amount_token_1_min: AssetBalance,
amount_0_min: AssetBalance,
amount_1_min: AssetBalance,
recipient: &T::AccountId,
) -> DispatchResult {
ensure!(
Self::lp_ledger(((asset_0, asset_1), who)) >= remove_liquidity,
Error::<T>::InsufficientLiquidity
);

LiquidityMeta::<T>::try_mutate((asset_0, asset_1), |meta| {
LiquidityMeta::<T>::try_mutate(Self::sort_asset_id(asset_0, asset_1), |meta| {
if let Some((pair_account, total_liquidity)) = meta {
let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, &pair_account);
let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, &pair_account);
Expand All @@ -135,7 +144,7 @@ impl<T: Config> Pallet<T> {
Self::calculate_share_amount(remove_liquidity, *total_liquidity, reserve_1);

ensure!(
amount_0 >= amount_token_0_min && amount_1 >= amount_token_1_min,
amount_0 >= amount_0_min && amount_1 >= amount_1_min,
Error::<T>::InsufficientTargetAmount
);

Expand All @@ -147,6 +156,16 @@ impl<T: Config> Pallet<T> {
T::MultiAssetsHandler::transfer(asset_0, &pair_account, recipient, amount_0)?;
T::MultiAssetsHandler::transfer(asset_1, &pair_account, recipient, amount_1)?;

Self::deposit_event(Event::LiquidityRemoved(
who.clone(),
recipient.clone(),
asset_0,
asset_1,
amount_0,
amount_1,
remove_liquidity,
));

Ok(())
} else {
Err(Error::<T>::PairNotExists.into())
Expand All @@ -155,7 +174,7 @@ impl<T: Config> Pallet<T> {
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn inner_swap_exact_tokens_for_tokens(
pub(crate) fn inner_swap_exact_assets_for_assets(
who: &T::AccountId,
amount_in: AssetBalance,
amount_out_min: AssetBalance,
Expand All @@ -171,11 +190,19 @@ impl<T: Config> Pallet<T> {
T::MultiAssetsHandler::transfer(path[0], who, &pair_account, amount_in)?;
Self::swap(&amounts, &path, &recipient)?;

Self::deposit_event(Event::AssetSwap(
who.clone(),
recipient.clone(),
Vec::from(path),
amount_in,
amounts[amounts.len() - 1],
));

Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn inner_swap_tokens_for_exact_tokens(
pub(crate) fn inner_swap_assets_for_exact_assets(
who: &T::AccountId,
amount_out: AssetBalance,
amount_in_max: AssetBalance,
Expand All @@ -192,6 +219,14 @@ impl<T: Config> Pallet<T> {
T::MultiAssetsHandler::transfer(path[0], who, &pair_account, amounts[0])?;
Self::swap(&amounts, &path, recipient)?;

Self::deposit_event(Event::AssetSwap(
who.clone(),
recipient.clone(),
Vec::from(path),
amounts[0],
amount_out,
));

Ok(())
}

Expand Down Expand Up @@ -255,16 +290,19 @@ impl<T: Config> Pallet<T> {
amount: AssetBalance,
is_mint: bool,
) -> DispatchResult {
LiquidityLedger::<T>::try_mutate(((asset_0, asset_1), who), |liquidity| {
if is_mint {
*liquidity = liquidity.checked_add(amount).ok_or(Error::<T>::Overflow)?;
} else {
*liquidity =
liquidity.checked_sub(amount).ok_or(Error::<T>::InsufficientLiquidity)?;
}
LiquidityLedger::<T>::try_mutate(
(Self::sort_asset_id(asset_0, asset_1), who),
|liquidity| {
if is_mint {
*liquidity = liquidity.checked_add(amount).ok_or(Error::<T>::Overflow)?;
} else {
*liquidity =
liquidity.checked_sub(amount).ok_or(Error::<T>::InsufficientLiquidity)?;
}

Ok(())
})
Ok(())
},
)
}

fn get_amount_in(
Expand Down
Loading

0 comments on commit 71583a4

Please sign in to comment.