Skip to content

Commit

Permalink
Update rollup/down policy (#1851)
Browse files Browse the repository at this point in the history
* Update rollup/down policy

* Fix unit tests

* fix bench

Signed-off-by: Cheng JIANG <[email protected]>

* fix fee check

Signed-off-by: Cheng JIANG <[email protected]>

Signed-off-by: Cheng JIANG <[email protected]>
Co-authored-by: Cheng JIANG <[email protected]>
  • Loading branch information
alannotnerd and GopherJ authored Aug 18, 2022
1 parent 4f040fd commit 4feffcc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
9 changes: 6 additions & 3 deletions pallets/amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> Result<BalanceOf<T, I>, DispatchError> {
let fees = T::LpFee::get()
.checked_add(&T::ProtocolFee::get())
.map(|r| r.mul_floor(amount_in))
.map(|r| r.mul_ceil(amount_in))
.ok_or(ArithmeticError::Overflow)?;

let amount_in = amount_in
Expand Down Expand Up @@ -664,7 +664,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
);

Ok(fee_percent
.saturating_reciprocal_mul_floor(amount_in)
.saturating_reciprocal_mul_ceil(amount_in)
.checked_add(One::one())
.ok_or(ArithmeticError::Overflow)?)
}
Expand Down Expand Up @@ -1007,7 +1007,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
};

ensure!(
amount_in >= T::LpFee::get().saturating_reciprocal_mul_floor(One::one()),
amount_in
>= T::ProtocolFee::get()
.saturating_add(T::LpFee::get())
.saturating_reciprocal_mul_ceil(One::one()),
Error::<T, I>::InsufficientAmountIn
);
ensure!(!supply_out.is_zero(), Error::<T, I>::InsufficientAmountOut);
Expand Down
16 changes: 8 additions & 8 deletions pallets/amm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ fn trade_should_work_flipped_currencies() {

assert_eq!(
Assets::balance(SDOT, trader),
1_000_000_000 + 248 //
1_000_000_000 + 247 //
);

// pools values should be updated - we should have less DOT in the pool
assert_eq!(AMM::pools(SDOT, DOT).unwrap().quote_amount, 100_000 + 500);

// pools values should be updated - we should have more SDOT
assert_eq!(AMM::pools(SDOT, DOT).unwrap().base_amount, 50_000 - 248);
assert_eq!(AMM::pools(SDOT, DOT).unwrap().base_amount, 50_000 - 247);
})
}

Expand Down Expand Up @@ -671,7 +671,7 @@ fn amounts_out_should_work() {

let amounts_out = AMM::get_amounts_out(amount_in, path).unwrap();

assert_eq!(amounts_out, [1000, 332, 249]);
assert_eq!(amounts_out, [1000, 332, 248]);
})
}

Expand Down Expand Up @@ -700,7 +700,7 @@ fn long_route_amounts_in_should_work() {

let amounts_in = AMM::get_amounts_in(amount_out, path).unwrap();

assert_eq!(amounts_in, [2517, 1115, 1000]);
assert_eq!(amounts_in, [2521, 1116, 1000]);
})
}

Expand All @@ -721,7 +721,7 @@ fn short_route_amounts_in_should_work() {

let amounts_in = AMM::get_amounts_in(amount_out, path).unwrap();

assert_eq!(amounts_in, [1004, 1000]);
assert_eq!(amounts_in, [1005, 1000]);
})
}

Expand All @@ -736,7 +736,7 @@ fn amount_in_should_work() {

// actual value == 1004.0190572718165
// TODO: assumes we round down to int
assert_eq!(amount_in, 1004);
assert_eq!(amount_in, 1005);
})
}

Expand All @@ -749,7 +749,7 @@ fn amount_in_uneven_should_work() {

let amount_in = AMM::get_amount_in(amount_out, supply_in, supply_out).unwrap();

assert_eq!(amount_in, 75);
assert_eq!(amount_in, 76);
})
}

Expand Down Expand Up @@ -777,7 +777,7 @@ fn amount_out_and_in_should_work() {

let amount_in = AMM::get_amount_in(amount_out, supply_in, supply_out).unwrap();

assert_eq!(amount_in, 1004);
assert_eq!(amount_in, 1005);

let amount_out = AMM::get_amount_out(amount_in, supply_in, supply_out).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion pallets/router/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ benchmarks_instance_pallet! {
let routes: Vec<_> = vec![DOT, SDOT];
let balance_after_trade: BalanceOf<T, I> = <T as crate::Config<I>>::Assets::balance(DOT, &caller);
let amount_in = balance_before_trade - balance_after_trade;
let expected = 983u128;
let expected = 984u128;

assert_eq!(amount_in, expected);
assert_last_event::<T, I>(Event::Traded(caller, expected, routes, amount_out).into());
Expand Down
16 changes: 8 additions & 8 deletions pallets/router/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn swap_tokens_for_exact_tokens_should_work() {
.unwrap();

// check balances after swap
assert_eq!(Assets::balance(DOT, trader), 10_000 - 1_004);
assert_eq!(Assets::balance(DOT, trader), 10_000 - 1_005);
assert_eq!(Assets::balance(SDOT, trader), 10_000 + 1_000);
});
}
Expand Down Expand Up @@ -180,7 +180,7 @@ fn pool_as_bridge_swap_tokens_for_exact_tokens_should_work() {
.unwrap();

// check balances after swap
assert_eq!(Assets::balance(DOT, trader), 10_000 - 504);
assert_eq!(Assets::balance(DOT, trader), 10_000 - 506);
assert_eq!(Assets::balance(SDOT, trader), 10_000);
assert_eq!(Assets::balance(USDT, trader), 0 + 20_000 + 29);
});
Expand Down Expand Up @@ -360,7 +360,7 @@ fn trade_should_work_more_than_one_route() {
assert_eq!(Assets::balance(tokens::KSM, &ALICE), 10_000);

// Alice should now have some USDT!
assert_eq!(Assets::balance(tokens::USDT, &ALICE), 990);
assert_eq!(Assets::balance(tokens::USDT, &ALICE), 988);

// First Pool

Expand All @@ -387,21 +387,21 @@ fn trade_should_work_more_than_one_route() {
// we should have less KSM
assert_eq!(
DefaultAMM::pools(SDOT, KSM).unwrap().quote_amount,
100_000_000 - 993
100_000_000 - 992
);

// Third Pool

// we should have more KSM since were trading it for USDT
assert_eq!(
DefaultAMM::pools(USDT, KSM).unwrap().quote_amount,
100_000_000 + 993
100_000_000 + 992
);

// we should have less USDT since its the token the trader is receiving
assert_eq!(
DefaultAMM::pools(USDT, KSM).unwrap().base_amount,
100_000_000 - 990
100_000_000 - 988
);
})
}
Expand Down Expand Up @@ -447,7 +447,7 @@ fn get_all_routes_should_work() {
// Returns descending order `highest` value first.
assert_eq!(
routes,
vec![(vec![101, 1001, 100], 894), (vec![101, 100], 697)]
vec![(vec![101, 1001, 100], 893), (vec![101, 100], 697)]
);
})
}
Expand Down Expand Up @@ -491,7 +491,7 @@ fn get_best_route_should_work() {
.unwrap();

// Returns descending order `highest` value first.
assert_eq!(best_route, (vec![101, 1001, 100], 894));
assert_eq!(best_route, (vec![101, 1001, 100], 893));
})
}

Expand Down

0 comments on commit 4feffcc

Please sign in to comment.