Skip to content

Commit

Permalink
Merge pull request #199 from galacticcouncil/audit-tests
Browse files Browse the repository at this point in the history
chore: additional audit tests
  • Loading branch information
enthusiastmartin authored Oct 28, 2021
2 parents ddfd11c + ae81f48 commit b673a02
Show file tree
Hide file tree
Showing 7 changed files with 1,094 additions and 132 deletions.
23 changes: 22 additions & 1 deletion pallets/duster/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::mock::{
Currencies, Duster, Event as TestEvent, ExtBuilder, Origin, System, Test, Tokens, ALICE, DUSTER, KILLED, TREASURY,
Currencies, Duster, Event as TestEvent, ExtBuilder, Origin, System, Test, Tokens, ALICE, BOB, DUSTER, KILLED,
TREASURY,
};
use frame_support::{assert_noop, assert_ok};
use primitives::AssetId;
Expand All @@ -23,6 +24,26 @@ fn dust_account_works() {
assert_eq!(Currencies::free_balance(0, &*DUSTER), 10_000);
});
}

#[test]
fn reward_duster_can_fail() {
ExtBuilder::default()
.with_balance(*ALICE, 1, 100)
.build()
.execute_with(|| {
assert_ok!(Currencies::transfer(Origin::signed(*TREASURY), *BOB, 0, 1_000_000));

assert_ok!(Duster::dust_account(Origin::signed(*DUSTER), *ALICE, 1));
assert_eq!(Tokens::free_balance(1, &*TREASURY), 100);

for (who, _, _) in orml_tokens::Accounts::<Test>::iter() {
assert_ne!(who, *ALICE, "Alice account should have been removed!");
}

assert_eq!(Currencies::free_balance(0, &*DUSTER), 0);
});
}

#[test]
fn dust_account_with_sufficient_balance_fails() {
ExtBuilder::default()
Expand Down
293 changes: 293 additions & 0 deletions pallets/exchange/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4403,3 +4403,296 @@ fn trade_limit_test() {
assert_eq!(Exchange::get_intentions_count((asset_a, asset_b)), 0);
});
}

#[test]
fn register_intention_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Exchange::register_intention(
&ALICE,
IntentionType::SELL,
AssetPair {
asset_in: HDX,
asset_out: DOT
},
1,
1,
1,
false
));
assert_ok!(Exchange::register_intention(
&ALICE,
IntentionType::BUY,
AssetPair {
asset_in: HDX,
asset_out: DOT
},
1,
1,
1,
false
));

assert_eq!(Exchange::get_intentions_count((HDX, DOT)), 2);
assert_eq!(Exchange::get_intentions((HDX, DOT)).len(), 2);
});
}

// #[test]
// fn register_intention_should_return_error_on_overflow() {
// new_test_ext().execute_with(|| {
// ExchangeAssetsIntentionCount::<Test>::insert((HDX, DOT), u32::MAX);
// assert_eq!(Exchange::get_intentions_count((HDX, DOT)), u32::MAX);

// assert!(Exchange::register_intention(&ALICE, IntentionType::SELL, AssetPair { asset_in: HDX, asset_out: DOT }, 1, 1, 1, false).is_err());
// });
// }

#[test]
fn execute_amm_transfer_should_work() {
new_test_ext().execute_with(|| {
let asset_a = HDX;
let asset_b = DOT;
let pool_amount = 1_000_000_000_000_000;
let initial_price = Price::from_float(0.072);
initialize_pool(asset_b, asset_a, ALICE, pool_amount, initial_price);

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

let alice_buy_intention_id = generate_intention_id(&ALICE, 0);
assert_ok!(Exchange::execute_amm_transfer(
IntentionType::BUY,
alice_buy_intention_id,
&AMMTransfer {
origin: ALICE,
assets: AssetPair {
asset_in: HDX,
asset_out: DOT
},
amount: 1_000_000_000_000,
amount_out: 1_000_000_000,
discount: false,
discount_amount: 0,
fee: (1000, 1_000_000),
}
));

let alice_sell_intention_id = generate_intention_id(&ALICE, 1);
assert_ok!(Exchange::execute_amm_transfer(
IntentionType::SELL,
alice_sell_intention_id,
&AMMTransfer {
origin: ALICE,
assets: AssetPair {
asset_in: HDX,
asset_out: DOT
},
amount: 1_000_000_000_000,
amount_out: 1_000_000_000,
discount: false,
discount_amount: 0,
fee: (1000, 1_000_000),
}
));

expect_events(vec![
xyk::Event::BuyExecuted(
ALICE,
DOT,
HDX,
1_000_000_000_000,
1_000_000_000,
HDX,
1000000,
pair_account,
)
.into(),
Event::IntentionResolvedAMMTrade(
ALICE,
IntentionType::BUY,
alice_buy_intention_id,
1_000_000_000_000,
1_001_000_000,
pair_account,
)
.into(),
xyk::Event::SellExecuted(
ALICE,
HDX,
DOT,
1_000_000_000_000,
1_000_000_000,
HDX,
1000000,
pair_account,
)
.into(),
Event::IntentionResolvedAMMTrade(
ALICE,
IntentionType::SELL,
alice_sell_intention_id,
1_000_000_000_000,
1_001_000_000,
pair_account,
)
.into(),
]);
});
}

#[test]
fn resolve_single_intention_should_work() {
new_test_ext().execute_with(|| {
let asset_a = HDX;
let asset_b = DOT;
let pool_amount = 1_000_000_000_000_000;
let initial_price = Price::from_float(0.072);
initialize_pool(asset_b, asset_a, ALICE, pool_amount, initial_price);

let alice_buy_intention_id = generate_intention_id(&ALICE, 0);
Exchange::resolve_single_intention(&ExchangeIntention {
who: ALICE,
assets: AssetPair {
asset_in: DOT,
asset_out: HDX,
},
amount_in: 150_000_000,
amount_out: 2_000_000_000,
trade_limit: 1_500_000_000_000,
discount: false,
sell_or_buy: IntentionType::BUY,
intention_id: alice_buy_intention_id,
});

let alice_sell_intention_id = generate_intention_id(&ALICE, 1);
Exchange::resolve_single_intention(&ExchangeIntention {
who: ALICE,
assets: AssetPair {
asset_in: DOT,
asset_out: HDX,
},
amount_in: 150_000_000,
amount_out: 2_000_000_000,
trade_limit: 101_000,
discount: false,
sell_or_buy: IntentionType::SELL,
intention_id: alice_sell_intention_id,
});

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

expect_events(vec![
xyk::Event::BuyExecuted(
ALICE,
HDX,
DOT,
2_000_000_000,
27_778_549_405,
DOT,
55_557_098,
pair_account,
)
.into(),
Event::IntentionResolvedAMMTrade(
ALICE,
IntentionType::BUY,
alice_buy_intention_id,
2_000_000_000,
27_834_106_503,
pair_account,
)
.into(),
xyk::Event::SellExecuted(ALICE, DOT, HDX, 150000000, 10777799, HDX, 21598, pair_account).into(),
Event::IntentionResolvedAMMTrade(
ALICE,
IntentionType::SELL,
alice_sell_intention_id,
150000000,
10799397,
pair_account,
)
.into(),
]);
});
}

#[test]
fn verify_intention_should_work() {
new_test_ext().execute_with(|| {
let user = ALICE;
let asset_a = HDX;
let asset_b = DOT;
let pool_amount = 1_000_000_000_000_000;
let initial_price = Price::from_float(2.0);
initialize_pool(asset_a, asset_b, user, pool_amount, initial_price);

assert!(
Exchange::verify_intention(&Intention::<Test> {
who: user,
assets: AssetPair {
asset_in: asset_a,
asset_out: asset_b,
},
amount_in: 1_000_000_000,
amount_out: 2_000_000_000,
trade_limit: 3_000_000_000,
discount: false,
sell_or_buy: IntentionType::BUY,
intention_id: generate_intention_id(&user, 0),
})
);

assert!(
Exchange::verify_intention(&Intention::<Test> {
who: user,
assets: AssetPair {
asset_in: asset_a,
asset_out: asset_b,
},
amount_in: 1_000_000_000,
amount_out: 2_000_000_000,
trade_limit: 100_000_000,
discount: false,
sell_or_buy: IntentionType::SELL,
intention_id: generate_intention_id(&user, 0),
})
);

assert!(
!Exchange::verify_intention(&Intention::<Test> {
who: user,
assets: AssetPair {
asset_in: asset_a,
asset_out: asset_b,
},
amount_in: 1_000_000_000,
amount_out: 2_000_000_000,
trade_limit: 100_000_000,
discount: false,
sell_or_buy: IntentionType::BUY,
intention_id: generate_intention_id(&user, 0),
})
);

assert!(
!Exchange::verify_intention(&Intention::<Test> {
who: user,
assets: AssetPair {
asset_in: asset_a,
asset_out: asset_b,
},
amount_in: 1_000_000_000,
amount_out: 2_000_000_000,
trade_limit: 10_000_000_000,
discount: false,
sell_or_buy: IntentionType::SELL,
intention_id: generate_intention_id(&user, 0),
})
);
});
}
Loading

0 comments on commit b673a02

Please sign in to comment.