add fee amount test
use super::*;
use frame_support::assert_noop;
+use sp_runtime::Percent;
#[test]
fn simple_sell_works() {
@@ -426,3 +427,52 @@ fn sell_not_allowed_asset_fails() {
assert_ok!(Omnipool::sell(Origin::signed(LP1), 100, 200, 50 * ONE, 10 * ONE));
});
}
+
+#[test]
+fn simple_sell_with_fee_works() {
+ ExtBuilder::default()
+ .with_endowed_accounts(vec![
+ (Omnipool::protocol_account(), DAI, 1000 * ONE),
+ (Omnipool::protocol_account(), HDX, NATIVE_AMOUNT),
+ (LP2, 100, 2000 * ONE),
+ (LP3, 200, 2000 * ONE),
+ (LP1, 100, 1000 * ONE),
+ ])
+ .with_registered_asset(100)
+ .with_registered_asset(200)
+ .with_asset_fee((1, 10))
+ .with_initial_pool(1000 * ONE, NATIVE_AMOUNT, FixedU128::from(1), FixedU128::from(1))
+ .build()
+ .execute_with(|| {
+ let token_amount = 2000 * ONE;
+ let token_price = FixedU128::from_float(1.0);
+
+ let fee = Percent::from_rational(1u8, 10u8);
+
+ assert_ok!(Omnipool::add_token(Origin::signed(LP2), 100, token_amount, token_price,));
+
+ assert_ok!(Omnipool::add_token(Origin::signed(LP3), 200, token_amount, token_price,));
+
+ assert_eq!(
+ Tokens::free_balance(200, &Omnipool::protocol_account()),
+ 2000000000000000
+ );
+
+ let sell_amount = 50 * ONE;
+ let min_limit = 10 * ONE;
+
+ let fee = Percent::from_percent(100).checked_sub(&fee).unwrap();
+
+ let expected_zero_fee = 47_619_047_619_047u128;
+ let expected_10_percent_fee = fee.mul_ceil(expected_zero_fee);
+
+ assert_ok!(Omnipool::sell(Origin::signed(LP1), 100, 200, sell_amount, min_limit));
+
+ assert_eq!(Tokens::free_balance(100, &LP1), 950_000_000_000_000);
+ assert_eq!(Tokens::free_balance(200, &LP1), expected_10_percent_fee);
+ assert_eq!(
+ Tokens::free_balance(200, &Omnipool::protocol_account()),
+ 2000000000000000 - expected_10_percent_fee,
+ );
+ });
+}
