Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Oct 18, 2024
1 parent a116bdf commit be46ac3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
16 changes: 8 additions & 8 deletions substrate/client/tests/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 16599866399465
amount_out: 16611018363939
}]
);

Expand Down Expand Up @@ -144,7 +144,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 17166744497317
amount_out: 17207430166736
}]
);
})
Expand Down Expand Up @@ -210,7 +210,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 12406166091918,
amount_out: 12421816676180,
}]
);
})
Expand Down Expand Up @@ -264,8 +264,8 @@ serai_test!(
mint_to: pair.public().into(),
pool_id: coin,
coin_amount: 10_000_000_000_000, // half of sent amount
sri_amount: 111669009482,
lp_token_minted: 1055147701082
sri_amount: 111631562261,
lp_token_minted: 1055499886564
}]
);
})
Expand Down Expand Up @@ -349,7 +349,7 @@ serai_test!(
send_to: IN_INSTRUCTION_EXECUTOR,
path,
amount_in: 200_000_000_000_000,
amount_out: 18933113030
amount_out: 18970355346
}]
);
}
Expand Down Expand Up @@ -388,7 +388,7 @@ serai_test!(
send_to: out_address.as_native().unwrap(),
path,
amount_in: 200_000_000_000,
amount_out: 1473437558561637
amount_out: 1482888317565764
}]
);
}
Expand Down Expand Up @@ -426,7 +426,7 @@ serai_test!(
send_to: out_address.as_native().unwrap(),
path,
amount_in: 100_000_000_000_000,
amount_out: 1751430396
amount_out: 1755477054
}]
);
}
Expand Down
26 changes: 12 additions & 14 deletions substrate/dex/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,32 +992,30 @@ pub mod pallet {
let (reserve_in, reserve_out) = Self::get_reserves(coin1, coin2)?;
let prev_amount = amounts.last().expect("Always has at least one element");
let amount_out = Self::get_amount_out(*prev_amount, reserve_in, reserve_out)?;
amounts.push(amount_out);

// now that we got swap fee from the user, burn half of it.
Self::burn_half_of_swap_fee(Self::get_pool_id(*coin1, *coin2)?, *coin1)?;
Self::burn_half_of_swap_fee(Self::get_pool_id(*coin1, *coin2)?, *coin1, *prev_amount)?;

amounts.push(amount_out);
}
}

Ok(amounts)
}

fn burn_half_of_swap_fee(pool: PoolId, coin: Coin) -> Result<(), DispatchError> {
fn burn_half_of_swap_fee(
pool: PoolId,
coin: Coin,
amount: SubstrateAmount,
) -> Result<(), DispatchError> {
let pool_account = Self::get_pool_account(pool);
let origin = RawOrigin::Signed(pool_account);

let balance = Coins::<T>::balance(pool_account, coin).0;
let burn_percent =
HigherPrecisionBalance::from(T::LPFee::get()).checked_div(2).ok_or(Error::<T>::Overflow)?;

let burn_amount = HigherPrecisionBalance::from(balance)
.checked_mul(burn_percent)
.ok_or(Error::<T>::Overflow)?
.checked_div(1000)
.ok_or(Error::<T>::Overflow)?;
// half of the taken fee
let burn_percent = T::LPFee::get().checked_div(2).ok_or(Error::<T>::Overflow)?;
let burn_amount = Self::mul_div(amount, burn_percent.into(), 1000)?;

Coins::<T>::burn(
origin.into(),
RawOrigin::Signed(pool_account).into(),
Balance { coin, amount: Amount(burn_amount.try_into().map_err(|_| Error::<T>::Overflow)?) },
)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions substrate/dex/pallet/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,10 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() {
));

// burn half of the taken fees
let burn_amount = get_burn_amount(liquidity2);
let burn_amount = get_burn_amount(input_amount);
liquidity2 -= burn_amount;

let burn_amount = get_burn_amount(liquidity1_pool2);
let burn_amount = get_burn_amount(expect_out2);
liquidity1_pool2 -= burn_amount;

let pool_id1 = Dex::get_pool_id(coin1, coin2).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl coins::Config<coins::Instance1> for Runtime {
impl dex::Config for Runtime {
type RuntimeEvent = RuntimeEvent;

// 0.6% in total but only half will go to LPs(0.3%) other half to be burned.
type LPFee = ConstU32<6>;
// 0.5% in total but only half will go to LPs(0.25%) other half to be burned.
type LPFee = ConstU32<5>;
type MintMinLiquidity = ConstU64<10000>;

type MaxSwapPathLength = ConstU32<3>; // coin1 -> SRI -> coin2
Expand Down

0 comments on commit be46ac3

Please sign in to comment.