From 4ec3603a5a10fd4f1b2f12a155f1cdfc1f4ed847 Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Fri, 13 Dec 2024 17:17:12 +0200 Subject: [PATCH] fix: add tests for refunds --- pallets/services/src/lib.rs | 4 +- pallets/services/src/tests.rs | 125 ++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/pallets/services/src/lib.rs b/pallets/services/src/lib.rs index 1435adc1..005a84f1 100644 --- a/pallets/services/src/lib.rs +++ b/pallets/services/src/lib.rs @@ -1214,7 +1214,7 @@ pub mod module { &Self::account_id(), &refund_to, payment.amount, - ExistenceRequirement::KeepAlive, + ExistenceRequirement::AllowDeath, )?; }, Asset::Custom(asset_id) => { @@ -1227,7 +1227,7 @@ pub mod module { &Self::account_id(), &refund_to, payment.amount, - Preservation::Preserve, + Preservation::Expendable, )?; }, Asset::Erc20(token) => { diff --git a/pallets/services/src/tests.rs b/pallets/services/src/tests.rs index 1855313a..d90ebcc8 100644 --- a/pallets/services/src/tests.rs +++ b/pallets/services/src/tests.rs @@ -651,6 +651,131 @@ fn request_service_with_payment_token() { }); } +#[test] +fn reject_service_with_payment_token() { + new_test_ext(vec![ALICE, BOB, CHARLIE, DAVE, EVE]).execute_with(|| { + System::set_block_number(1); + assert_ok!(Services::update_master_blueprint_service_manager(RuntimeOrigin::root(), MBSM)); + let alice = mock_pub_key(ALICE); + let blueprint = cggmp21_blueprint(); + + assert_ok!(Services::create_blueprint( + RuntimeOrigin::signed(alice.clone()), + blueprint.clone() + )); + let bob = mock_pub_key(BOB); + assert_ok!(Services::register( + RuntimeOrigin::signed(bob.clone()), + 0, + OperatorPreferences { key: zero_key(), price_targets: Default::default() }, + Default::default(), + 0, + )); + + let payment = 5 * 10u128.pow(6); // 5 USDC + let charlie_address = mock_address(CHARLIE); + let charlie_evm_account_id = address_to_account_id(charlie_address); + let before_balance = Services::query_erc20_balance_of(USDC_ERC20, charlie_address) + .map(|(b, _)| b) + .unwrap_or_default(); + assert_ok!(Services::request( + RuntimeOrigin::signed(charlie_evm_account_id), + Some(charlie_address), + 0, + vec![], + vec![bob.clone()], + Default::default(), + vec![TNT, USDC, WETH], + 100, + Asset::Erc20(USDC_ERC20), + payment, + )); + + assert_eq!(ServiceRequests::::iter_keys().collect::>().len(), 1); + + // The Pallet address now has 5 USDC + assert_ok!( + Services::query_erc20_balance_of(USDC_ERC20, Services::address()).map(|(b, _)| b), + U256::from(payment) + ); + // Charlie Balance should be decreased by 5 USDC + assert_ok!( + Services::query_erc20_balance_of(USDC_ERC20, charlie_address).map(|(b, _)| b), + before_balance - U256::from(payment) + ); + + // Bob rejects the request + assert_ok!(Services::reject(RuntimeOrigin::signed(bob.clone()), 0)); + + // The Payment should be now refunded to the requester. + // Pallet account should have 0 USDC + assert_ok!( + Services::query_erc20_balance_of(USDC_ERC20, Services::address()).map(|(b, _)| b), + U256::from(0) + ); + // Charlie Balance should be back to the original + assert_ok!( + Services::query_erc20_balance_of(USDC_ERC20, charlie_address).map(|(b, _)| b), + before_balance + ); + }); +} + +#[test] +fn reject_service_with_payment_asset() { + new_test_ext(vec![ALICE, BOB, CHARLIE, DAVE, EVE]).execute_with(|| { + System::set_block_number(1); + assert_ok!(Services::update_master_blueprint_service_manager(RuntimeOrigin::root(), MBSM)); + let alice = mock_pub_key(ALICE); + let blueprint = cggmp21_blueprint(); + + assert_ok!(Services::create_blueprint( + RuntimeOrigin::signed(alice.clone()), + blueprint.clone() + )); + let bob = mock_pub_key(BOB); + assert_ok!(Services::register( + RuntimeOrigin::signed(bob.clone()), + 0, + OperatorPreferences { key: zero_key(), price_targets: Default::default() }, + Default::default(), + 0, + )); + + let payment = 5 * 10u128.pow(6); // 5 USDC + let charlie = mock_pub_key(CHARLIE); + let before_balance = Assets::balance(USDC, charlie.clone()); + assert_ok!(Services::request( + RuntimeOrigin::signed(charlie.clone()), + None, + 0, + vec![], + vec![bob.clone()], + Default::default(), + vec![TNT, USDC, WETH], + 100, + Asset::Custom(USDC), + payment, + )); + + assert_eq!(ServiceRequests::::iter_keys().collect::>().len(), 1); + + // The Pallet account now has 5 USDC + assert_eq!(Assets::balance(USDC, Services::account_id()), payment); + // Charlie Balance should be decreased by 5 USDC + assert_eq!(Assets::balance(USDC, charlie.clone()), before_balance - payment); + + // Bob rejects the request + assert_ok!(Services::reject(RuntimeOrigin::signed(bob.clone()), 0)); + + // The Payment should be now refunded to the requester. + // Pallet account should have 0 USDC + assert_eq!(Assets::balance(USDC, Services::account_id()), 0); + // Charlie Balance should be back to the original + assert_eq!(Assets::balance(USDC, charlie), before_balance); + }); +} + #[test] fn job_calls() { new_test_ext(vec![ALICE, BOB, CHARLIE, DAVE, EVE]).execute_with(|| {