Skip to content

Commit

Permalink
Merge branch 'master' into feat/adjust_fee
Browse files Browse the repository at this point in the history
  • Loading branch information
jak-pan authored Jul 2, 2021
2 parents d8c4aca + e634d4e commit 6a5bad0
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 12 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ jobs:
run: rustup component add clippy
- name: Run clippy
run: cargo clippy --release -- -D warnings
- name: Tests
run: time cargo test --all --release --verbose --locked
- name: Benchmarking tests
run: cd node && cargo test --release --features runtime-benchmarks -p pallet-exchange-benchmarking -p pallet-xyk -p pallet-multi-payment-benchmarking -p pallet-claims

- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Test && Generate code coverage
run: cargo tarpaulin --all-features --workspace --locked --exclude-files node/* --exclude-files runtime/* --exclude-files utils/* --exclude-files **/weights.rs --ignore-tests -o Xml -o lcov
- name: Upload to codecov.io
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
- name: Build release
run: time cargo build --release --verbose
run: time cargo build --release --quiet --locked
- name: Version info
run: ./target/release/hydra-dx --version
- name: Upload release binary
Expand Down
7 changes: 0 additions & 7 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,6 @@ pub mod pallet {
.checked_add(shares_added)
.ok_or(Error::<T>::InvalidLiquidityAmount)?;

let asset_b_balance = T::Currency::free_balance(asset_b, &who);

ensure!(
asset_b_balance >= amount_b_required,
Error::<T>::InsufficientAssetBalance
);

T::Currency::transfer(asset_a, &who, &pair_account, amount_a)?;
T::Currency::transfer(asset_b, &who, &pair_account, amount_b_required)?;

Expand Down
264 changes: 264 additions & 0 deletions pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ fn create_pool_overflowing_amount_should_not_work() {
});
}

#[test]
fn create_pool_with_insufficient_balance_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = HDX;

assert_noop!(
XYK::create_pool(
Origin::signed(user),
4000,
asset_a,
100_000_000_000_000,
Price::from(10)
),
Error::<Test>::InsufficientAssetBalance
);

assert_noop!(
XYK::create_pool(
Origin::signed(user),
asset_a,
4000,
100_000_000_000_000,
Price::from(10)
),
Error::<Test>::InsufficientAssetBalance
);
});
}

#[test]
fn add_liquidity_should_work() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -254,6 +284,105 @@ fn remove_liquidity_should_work() {
});
}

#[test]
fn remove_liquidity_without_shares_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = HDX;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
100_000_000,
Price::from(1)
));

let pair_account = XYK::get_pair_id(AssetPair {
asset_in: asset_a,
asset_out: asset_b,
});
let share_token = XYK::share_token(pair_account);
let shares = Currency::free_balance(share_token, &user);

assert_ok!(Currency::transfer(Origin::signed(ALICE), BOB, share_token, shares));

assert_noop!(
XYK::remove_liquidity(Origin::signed(user), asset_a, asset_b, 355_000),
Error::<Test>::InsufficientAssetBalance
);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000).into(),
orml_tokens::Event::Endowed(share_token, BOB, shares).into(),
orml_tokens::Event::Transfer(share_token, ALICE, BOB, shares).into(),
]);
});
}

// events in the following test do not occur during standard chain operation
#[test]
fn remove_liquidity_from_reduced_pool_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = HDX;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
100_000_000,
Price::from(1)
));

let pair_account = XYK::get_pair_id(AssetPair {
asset_in: asset_a,
asset_out: asset_b,
});

// remove some amount from the pool
assert_ok!(Currency::transfer(
Origin::signed(pair_account),
BOB,
asset_a,
90_000_000
));

assert_noop!(
XYK::remove_liquidity(Origin::signed(user), asset_a, asset_b, 200_000_000),
Error::<Test>::InsufficientAssetBalance
);

// return it back to the pool
assert_ok!(Currency::transfer(
Origin::signed(BOB),
pair_account,
asset_a,
90_000_000
));
// do it again with asset_b
assert_ok!(Currency::transfer(
Origin::signed(pair_account),
BOB,
asset_b,
90_000_000
));

assert_noop!(
XYK::remove_liquidity(Origin::signed(user), asset_a, asset_b, 200_000_000),
Error::<Test>::InsufficientAssetBalance
);
expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000).into(),
orml_tokens::Event::Transfer(asset_a, pair_account, BOB, 90_000_000).into(),
orml_tokens::Event::Transfer(asset_a, BOB, pair_account, 90_000_000).into(),
orml_tokens::Event::Transfer(asset_b, pair_account, BOB, 90_000_000).into(),
]);
});
}

#[test]
fn add_liquidity_more_than_owner_should_not_work() {
new_test_ext().execute_with(|| {
Expand All @@ -271,6 +400,11 @@ fn add_liquidity_more_than_owner_should_not_work() {
XYK::add_liquidity(Origin::signed(ALICE), HDX, ACA, 200_000_000_000_000_000, 600_000_000),
Error::<Test>::InsufficientAssetBalance
);

assert_noop!(
XYK::add_liquidity(Origin::signed(ALICE), HDX, ACA, 600_000_000, 200_000_000_000_000_000),
Error::<Test>::InsufficientAssetBalance
);
});
}

Expand All @@ -291,6 +425,24 @@ fn add_zero_liquidity_should_not_work() {
});
}

#[test]
fn add_liquidity_exceeding_max_limit_should_not_work() {
new_test_ext().execute_with(|| {
assert_ok!(XYK::create_pool(
Origin::signed(ALICE),
HDX,
ACA,
100_000_000_000_000,
Price::from(1)
));

assert_noop!(
XYK::add_liquidity(Origin::signed(ALICE), HDX, ACA, 10_000_000, 1_000_000),
Error::<Test>::AssetAmountExceededLimit
);
});
}

#[test]
fn remove_zero_liquidity_should_not_work() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -684,6 +836,118 @@ fn discount_sell_fees_should_work() {
});
}

#[test]
fn sell_without_sufficient_balance_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = ACA;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
1_000_000_000,
Price::from(1)
));

assert_ok!(Currency::transfer(Origin::signed(user), BOB, ACA, 999_998_999_999_999));

assert_noop!(
XYK::sell(Origin::signed(user), ACA, DOT, 1_000, 100, false),
Error::<Test>::InsufficientAssetBalance
);
});
}

#[test]
fn sell_without_sufficient_discount_balance_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = ACA;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
1_000_000_000_000,
Price::from(1)
));

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
HDX,
1_000_000_000_000,
Price::from(1)
));

assert_ok!(Currency::transfer(Origin::signed(user), BOB, HDX, 998_999_999_999_999));

assert_noop!(
XYK::sell(Origin::signed(user), ACA, DOT, 1_000_000_000, 100, true),
Error::<Test>::InsufficientNativeCurrencyBalance
);
});
}

#[test]
fn buy_without_sufficient_balance_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = ACA;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
1_000_000_000,
Price::from(1)
));

assert_ok!(Currency::transfer(Origin::signed(user), BOB, ACA, 999_998_999_999_999));

assert_noop!(
XYK::buy(Origin::signed(user), DOT, ACA, 1_000, 10_000, false),
Error::<Test>::InsufficientAssetBalance
);
});
}

#[test]
fn buy_without_sufficient_discount_balance_should_not_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = ACA;
let asset_b = DOT;

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_a,
asset_b,
1_000_000_000_000,
Price::from(1)
));

assert_ok!(XYK::create_pool(
Origin::signed(user),
asset_b,
HDX,
1_000_000_000_000,
Price::from(1)
));

assert_ok!(Currency::transfer(Origin::signed(user), BOB, HDX, 998_999_999_999_999));

assert_noop!(
XYK::buy(Origin::signed(user), DOT, ACA, 1_000_000_000, 10_000_000_000, true),
Error::<Test>::InsufficientNativeCurrencyBalance
);
});
}

#[test]
fn single_buy_should_work() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit 6a5bad0

Please sign in to comment.