Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MESH-1853/ add field for external id to settlements #1280

Merged
Merged
94 changes: 73 additions & 21 deletions pallets/runtime/tests/src/settlement_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use pallet_identity as identity;
use pallet_portfolio::MovePortfolioItem;
use pallet_scheduler as scheduler;
use pallet_settlement::{
AffirmationStatus, Instruction, InstructionId, InstructionStatus, Leg, LegId, LegStatus,
Receipt, ReceiptDetails, ReceiptMetadata, SettlementType, VenueDetails, VenueId,
AffirmationStatus, Instruction, InstructionId, InstructionMemo, InstructionStatus, Leg, LegId,
LegStatus, Receipt, ReceiptDetails, ReceiptMetadata, SettlementType, VenueDetails, VenueId,
VenueInstructions, VenueType,
};
use polymesh_common_utilities::constants::ERC1400_TRANSFER_SUCCESS;
Expand Down Expand Up @@ -318,7 +318,7 @@ fn basic_settlement() {
to: PortfolioId::default_portfolio(bob.did),
asset: TICKER,
amount: amount
}]
}],
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
Expand Down Expand Up @@ -416,7 +416,7 @@ fn overdraft_failure() {
to: PortfolioId::default_portfolio(bob.did),
asset: TICKER,
amount: amount
}]
}],
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
Expand Down Expand Up @@ -467,7 +467,7 @@ fn token_swap() {
SettlementType::SettleOnAffirmation,
None,
None,
legs.clone()
legs.clone(),
));

assert_user_affirms(instruction_id, &alice, AffirmationStatus::Pending);
Expand Down Expand Up @@ -598,7 +598,7 @@ fn claiming_receipt() {
SettlementType::SettleOnAffirmation,
None,
None,
legs.clone()
legs.clone(),
));

assert_user_affirms(instruction_id, &alice, AffirmationStatus::Pending);
Expand Down Expand Up @@ -824,7 +824,7 @@ fn settle_on_block() {
SettlementType::SettleOnBlock(block_number),
None,
None,
legs.clone()
legs.clone(),
));
assert_eq!(1, scheduler::Agenda::<TestStorage>::get(block_number).len());

Expand Down Expand Up @@ -943,7 +943,7 @@ fn failed_execution() {
SettlementType::SettleOnBlock(block_number),
None,
None,
legs.clone()
legs.clone(),
));
assert_eq!(1, scheduler::Agenda::<TestStorage>::get(block_number).len());

Expand Down Expand Up @@ -1069,7 +1069,7 @@ fn venue_filtering() {
SettlementType::SettleOnBlock(block_number),
None,
None,
legs.clone()
legs.clone(),
));
assert_ok!(Settlement::set_venue_filtering(
alice.origin(),
Expand All @@ -1083,7 +1083,7 @@ fn venue_filtering() {
SettlementType::SettleOnBlock(block_number),
None,
None,
legs.clone()
legs.clone(),
),
Error::UnauthorizedVenue
);
Expand All @@ -1099,7 +1099,7 @@ fn venue_filtering() {
None,
None,
legs.clone(),
default_portfolio_vec(alice.did)
default_portfolio_vec(alice.did),
));

assert_affirm_instruction_with_one_leg!(alice.origin(), instruction_id, alice.did);
Expand Down Expand Up @@ -1277,7 +1277,7 @@ fn basic_fuzzing() {
SettlementType::SettleOnBlock(block_number),
None,
None,
legs.clone()
legs.clone(),
));

// Authorize instructions and do a few authorize/deny in between
Expand Down Expand Up @@ -1478,7 +1478,7 @@ fn claim_multiple_receipts_during_authorization() {
SettlementType::SettleOnAffirmation,
None,
None,
legs.clone()
legs.clone(),
));

alice.assert_all_balances_unchanged();
Expand Down Expand Up @@ -1616,7 +1616,7 @@ fn overload_instruction() {
SettlementType::SettleOnAffirmation,
None,
None,
legs.clone()
legs.clone(),
),
Error::InstructionHasTooManyLegs
);
Expand All @@ -1627,7 +1627,7 @@ fn overload_instruction() {
SettlementType::SettleOnAffirmation,
None,
None,
legs
legs,
));
});
}
Expand Down Expand Up @@ -1754,7 +1754,7 @@ fn test_weights_for_settlement_transaction() {
SettlementType::SettleOnAffirmation,
None,
None,
legs.clone()
legs.clone(),
));

assert_affirm_instruction_with_one_leg!(
Expand Down Expand Up @@ -1806,7 +1806,7 @@ fn cross_portfolio_settlement() {
to: PortfolioId::user_portfolio(bob.did, num),
asset: TICKER,
amount: amount
}]
}],
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
Expand Down Expand Up @@ -1894,7 +1894,7 @@ fn multiple_portfolio_settlement() {
asset: TICKER,
amount: amount
}
]
],
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
Expand Down Expand Up @@ -2057,7 +2057,7 @@ fn multiple_custodian_settlement() {
asset: TICKER,
amount: amount
}
]
],
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
Expand Down Expand Up @@ -2264,7 +2264,7 @@ fn dirty_storage_with_tx() {
asset: TICKER,
amount: amount2
}
]
],
));

assert_affirm_instruction!(alice.origin(), instruction_id, alice.did, 2);
Expand Down Expand Up @@ -2461,6 +2461,58 @@ fn modify_venue_signers() {
});
}

#[test]
fn basic_settlement_with_memo() {
test_with_cdd_provider(|eve| {
let mut alice = UserWithBalance::new(AccountKeyring::Alice, &[TICKER]);
let mut bob = UserWithBalance::new(AccountKeyring::Bob, &[TICKER]);
let venue_counter = create_token_and_venue(TICKER, alice.user);
let instruction_id = Settlement::instruction_counter();
let amount = 100u128;
alice.refresh_init_balances();
bob.refresh_init_balances();

// Provide scope claim to sender and receiver of the transaction.
provide_scope_claim_to_multiple_parties(&[alice.did, bob.did], TICKER, eve);

assert_ok!(Settlement::add_instruction_with_memo(
alice.origin(),
venue_counter,
SettlementType::SettleOnAffirmation,
None,
None,
vec![Leg {
from: PortfolioId::default_portfolio(alice.did),
to: PortfolioId::default_portfolio(bob.did),
asset: TICKER,
amount: amount
}],
Some(InstructionMemo::default()),
));
alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();

// check that the memo was stored correctly
assert_eq!(
Settlement::memo(instruction_id).unwrap(),
InstructionMemo::default()
);

assert_affirm_instruction_with_one_leg!(alice.origin(), instruction_id, alice.did);

alice.assert_all_balances_unchanged();
bob.assert_all_balances_unchanged();
set_current_block_number(5);
// Instruction get scheduled to next block.
assert_affirm_instruction_with_zero_leg!(bob.origin(), instruction_id, bob.did);

// Advances the block no. to execute the instruction.
next_block();
alice.assert_balance_decreased(&TICKER, amount);
bob.assert_balance_increased(&TICKER, amount);
});
}

fn create_instruction(
alice: &User,
bob: &User,
Expand All @@ -2482,7 +2534,7 @@ fn create_instruction(
asset: ticker,
amount
}],
default_portfolio_vec(alice.did)
default_portfolio_vec(alice.did),
));
instruction_id
}
Expand Down
38 changes: 34 additions & 4 deletions pallets/settlement/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ benchmarks! {
// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did , portfolios, _, _) = emulate_add_instruction::<T>(l, true).unwrap();
// Add instruction
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone()).unwrap();
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone(), None).unwrap();
let instruction_id = InstructionId(1);
// Affirm an instruction
let portfolios_set = portfolios.clone().into_iter().collect::<BTreeSet<_>>();
Expand All @@ -703,7 +703,7 @@ benchmarks! {
// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did, _, _, account_id) = emulate_add_instruction::<T>(1, true).unwrap();
// Add instruction
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone()).unwrap();
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone(), None).unwrap();
let instruction_id = InstructionId(1);
let leg_id = LegId(0);

Expand Down Expand Up @@ -748,7 +748,7 @@ benchmarks! {
// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did , s_portfolios, r_portfolios, account_id) = emulate_add_instruction::<T>(1, true).unwrap();
// Add instruction
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone()).unwrap();
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone(), None).unwrap();
let instruction_id = InstructionId(1);
let ticker = Ticker::generate_into(1u64);
let receipt = create_receipt_details::<T>(0, legs.first().unwrap().clone());
Expand All @@ -773,7 +773,7 @@ benchmarks! {
// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did , s_portfolios, r_portfolios, account_id) = emulate_add_instruction::<T>(r, true).unwrap();
// Add instruction
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone()).unwrap();
Module::<T>::base_add_instruction(did, venue_id, SettlementType::SettleOnAffirmation, None, None, legs.clone(), None).unwrap();
let instruction_id = InstructionId(1);
let mut receipt_details = Vec::with_capacity(r as usize);
legs.clone().into_iter().enumerate().for_each(|(idx, l)| {
Expand Down Expand Up @@ -858,6 +858,36 @@ benchmarks! {
next_block::<T>();
assert_eq!(Module::<T>::instruction_details(instruction_id).status, InstructionStatus::Failed, "Settlement: reschedule_instruction didn't work");
}

add_instruction_with_memo_and_settle_on_block_type {
let l in 1 .. T::MaxLegsInInstruction::get(); // Variation for the MAX leg count.
// Define settlement type
let settlement_type = SettlementType::SettleOnBlock(100u32.into());
let instruction_id = InstructionId(1);
set_block_number::<T>(50);

// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did , _, _, _ ) = emulate_add_instruction::<T>(l, false).unwrap();

}: add_instruction_with_memo(origin, venue_id, settlement_type, Some(99999999u32.into()), Some(99999999u32.into()), legs, Some(InstructionMemo::default()))
verify {
assert_eq!(Module::<T>::memo(instruction_id).unwrap(), InstructionMemo::default());
}

add_and_affirm_instruction_with_memo_and_settle_on_block_type {
let l in 1 .. T::MaxLegsInInstruction::get();
// Define settlement type.
let settlement_type = SettlementType::SettleOnBlock(100u32.into());
let instruction_id = InstructionId(1);
set_block_number::<T>(50);
// Emulate the add instruction and get all the necessary arguments.
let (legs, venue_id, origin, did , portfolios, _, _) = emulate_add_instruction::<T>(l, true).unwrap();
let s_portfolios = portfolios.clone();
}: add_and_affirm_instruction_with_memo(origin, venue_id, settlement_type, Some(99999999u32.into()), Some(99999999u32.into()), legs, s_portfolios, Some(InstructionMemo::default()))
verify {
verify_add_and_affirm_instruction::<T>(venue_id, settlement_type, portfolios).unwrap();
assert_eq!(Module::<T>::memo(instruction_id).unwrap(), InstructionMemo::default());
}
}

pub fn next_block<T: Config + pallet_scheduler::Config>() {
Expand Down
Loading