Skip to content

Commit

Permalink
final tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maltekliemann committed Oct 11, 2024
1 parent 6523c39 commit 7007808
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 21 deletions.
2 changes: 2 additions & 0 deletions zrml/combinatorial-tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ mod pallet {
})
.collect::<Result<Vec<_>, _>>()?;
for &position in positions.iter() {
println!("{:?}", position);
T::MultiCurrency::withdraw(position, &who, amount)?;
}
println!("after");

// Destroy/store the tokens to be split.
let merged_token = if !free_index_set.iter().any(|&i| i) {
Expand Down
146 changes: 125 additions & 21 deletions zrml/combinatorial-tokens/src/tests/merge_position.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,126 @@
use super::*;
use test_case::test_case;

#[test_case(
Asset::Ztg,
CombinatorialToken([207, 168, 160, 93, 238, 221, 197, 1, 171, 102, 28, 24, 18, 107, 205, 231, 227, 98, 220, 105, 211, 29, 181, 30, 53, 7, 200, 154, 134, 246, 38, 139]),
CombinatorialToken([101, 210, 61, 196, 5, 247, 150, 41, 186, 49, 11, 63, 139, 53, 25, 65, 161, 83, 24, 142, 225, 102, 57, 241, 199, 18, 226, 137, 68, 3, 219, 131])
)]
#[test_case(
Asset::ForeignAsset(1),
CombinatorialToken([97, 71, 129, 186, 219, 73, 163, 242, 183, 111, 224, 26, 45, 104, 11, 229, 241, 31, 154, 126, 118, 218, 142, 191, 3, 255, 156, 77, 32, 1, 66, 227]),
CombinatorialToken([156, 42, 42, 43, 18, 242, 8, 247, 100, 196, 173, 111, 167, 225, 207, 149, 166, 194, 255, 1, 238, 128, 72, 199, 188, 57, 236, 168, 26, 58, 104, 156])
)]
fn merge_position_works_no_parent(
collateral: Asset<MarketId>,
ct_001: Asset<MarketId>,
ct_110: Asset<MarketId>,
) {
ExtBuilder::build().execute_with(|| {
let amount = _100;
let alice =
Account::new(0).deposit(ct_001, amount).unwrap().deposit(ct_110, amount).unwrap();
// Mock a deposit into the pallet's account.
let pallet =
Account::new(Pallet::<Runtime>::account_id()).deposit(collateral, amount).unwrap();

let market_id = create_market(collateral, MarketType::Categorical(3));

assert_ok!(CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
vec![vec![B0, B0, B1], vec![B1, B1, B0]],
amount,
));

assert_eq!(alice.free_balance(ct_001), 0);
assert_eq!(alice.free_balance(ct_110), 0);
assert_eq!(alice.free_balance(collateral), _100);
assert_eq!(pallet.free_balance(collateral), 0);
});
}

#[test]
fn merge_position_works_parent() {
ExtBuilder::build().execute_with(|| {
let ct_001 = CombinatorialToken([
207, 168, 160, 93, 238, 221, 197, 1, 171, 102, 28, 24, 18, 107, 205, 231, 227, 98, 220,
105, 211, 29, 181, 30, 53, 7, 200, 154, 134, 246, 38, 139,
]);
let ct_001_0101 = CombinatorialToken([
38, 14, 141, 152, 199, 40, 88, 165, 208, 236, 195, 198, 208, 75, 93, 85, 114, 4, 175,
225, 211, 72, 142, 210, 98, 202, 168, 193, 245, 217, 239, 28,
]);
let ct_001_1010 = CombinatorialToken([
107, 142, 3, 38, 49, 137, 237, 239, 1, 131, 197, 221, 236, 46, 246, 93, 185, 197, 228,
184, 75, 79, 107, 73, 89, 19, 22, 124, 15, 58, 110, 100,
]);

let amount = _100;
let alice = Account::new(0)
.deposit(ct_001_0101, amount)
.unwrap()
.deposit(ct_001_1010, amount)
.unwrap();

let _ = create_market(Asset::Ztg, MarketType::Categorical(3));
let market_id = create_market(Asset::Ztg, MarketType::Categorical(4));

// Collection ID of [0, 0, 1].
let parent_collection_id = [
6, 44, 173, 50, 122, 106, 144, 185, 253, 19, 252, 218, 215, 241, 218, 37, 196, 112, 45,
133, 165, 48, 231, 189, 87, 123, 131, 18, 190, 5, 110, 93,
];

assert_ok!(CombinatorialTokens::merge_position(
alice.signed(),
Some(parent_collection_id),
market_id,
vec![vec![B0, B1, B0, B1], vec![B1, B0, B1, B0]],
amount,
));

assert_eq!(alice.free_balance(ct_001), amount);
assert_eq!(alice.free_balance(ct_001_0101), 0);
assert_eq!(alice.free_balance(ct_001_1010), 0);
});
}

#[test]
fn merge_position_horizontal_works() {
ExtBuilder::build().execute_with(|| {
let ct_100 = CombinatorialToken([
63, 95, 93, 48, 199, 160, 113, 178, 33, 24, 52, 193, 247, 121, 229, 30, 231, 100, 209,
14, 57, 98, 193, 214, 34, 251, 53, 51, 136, 146, 93, 26,
]);
let ct_010 = CombinatorialToken([
23, 108, 101, 109, 145, 51, 201, 192, 240, 28, 43, 57, 53, 4, 75, 101, 116, 20, 184,
25, 227, 71, 149, 136, 59, 82, 81, 105, 41, 160, 39, 142,
]);
let ct_110 = CombinatorialToken([
101, 210, 61, 196, 5, 247, 150, 41, 186, 49, 11, 63, 139, 53, 25, 65, 161, 83, 24, 142,
225, 102, 57, 241, 199, 18, 226, 137, 68, 3, 219, 131,
]);

let amount = _100;
let alice = Account::new(0).deposit(ct_100, _100).unwrap().deposit(ct_010, _100).unwrap();

let market_id = create_market(Asset::Ztg, MarketType::Categorical(3));

assert_ok!(CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
vec![vec![B0, B1, B0], vec![B1, B0, B0]],
amount,
));

assert_eq!(alice.free_balance(ct_110), amount);
assert_eq!(alice.free_balance(ct_100), 0);
assert_eq!(alice.free_balance(ct_010), 0);
});
}

#[test]
fn merge_position_fails_if_market_not_found() {
Expand Down Expand Up @@ -28,13 +150,7 @@ fn merge_position_fails_on_invalid_partition_length() {
let partition = vec![vec![B1, B0, B1], vec![B0, B1]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
CombinatorialTokens::merge_position(alice.signed(), None, market_id, partition, _1,),
Error::<Runtime>::InvalidPartition
);
});
Expand All @@ -50,13 +166,7 @@ fn merge_position_fails_on_trivial_partition_member() {
let partition = vec![vec![B1, B0, B1], vec![B0, B0, B0]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
CombinatorialTokens::merge_position(alice.signed(), None, market_id, partition, _1,),
Error::<Runtime>::InvalidPartition
);
});
Expand All @@ -72,13 +182,7 @@ fn merge_position_fails_on_overlapping_partition_members() {
let partition = vec![vec![B1, B0, B1], vec![B0, B0, B1]];

assert_noop!(
CombinatorialTokens::merge_position(
alice.signed(),
None,
market_id,
partition,
_1,
),
CombinatorialTokens::merge_position(alice.signed(), None, market_id, partition, _1,),
Error::<Runtime>::InvalidPartition
);
});
Expand Down

0 comments on commit 7007808

Please sign in to comment.