diff --git a/state-chain/pallets/cf-swapping/src/lib.rs b/state-chain/pallets/cf-swapping/src/lib.rs index b38fc6260d..82a12c5378 100644 --- a/state-chain/pallets/cf-swapping/src/lib.rs +++ b/state-chain/pallets/cf-swapping/src/lib.rs @@ -703,6 +703,20 @@ pub mod pallet { MinimumNetworkFeeSet { min_fee: AssetAmount, }, + // Account credited as a result of an on-chain swap + CreditedOnChain { + swap_request_id: SwapRequestId, + account_id: T::AccountId, + asset: Asset, + amount: AssetAmount, + }, + // Account received a refund as a result of an on-chain swap + RefundedOnChain { + swap_request_id: SwapRequestId, + account_id: T::AccountId, + asset: Asset, + amount: AssetAmount, + }, } #[pallet::error] pub enum Error { @@ -1538,6 +1552,13 @@ pub mod pallet { ); }, RefundDestination::OnChainAccount(account_id) => { + Self::deposit_event(Event::::RefundedOnChain { + swap_request_id, + account_id: account_id.clone(), + asset: request.input_asset, + amount: amount_to_refund, + }); + T::BalanceApi::credit_account( account_id, request.input_asset, @@ -1552,7 +1573,7 @@ pub mod pallet { match output_action { SwapOutputAction::Egress { ccm_deposit_metadata, output_address } => { Self::egress_for_swap( - swap.swap_request_id, + swap_request_id, *accumulated_output_amount, request.output_asset, output_address.clone(), @@ -1561,7 +1582,13 @@ pub mod pallet { ); }, SwapOutputAction::CreditOnChain { account_id } => { - // TODO: a new event for when LP is credited after a swap? + Self::deposit_event(Event::::CreditedOnChain { + swap_request_id, + account_id: account_id.clone(), + asset: request.output_asset, + amount: *accumulated_output_amount, + }); + T::BalanceApi::credit_account( account_id, request.output_asset, @@ -1655,9 +1682,16 @@ pub mod pallet { ); }, SwapOutputAction::CreditOnChain { account_id } => { + Self::deposit_event(Event::::CreditedOnChain { + swap_request_id, + account_id: account_id.clone(), + asset: request.output_asset, + amount: dca_state.accumulated_output_amount, + }); + T::BalanceApi::credit_account( account_id, - swap.output_asset(), + request.output_asset, dca_state.accumulated_output_amount, ); }, diff --git a/state-chain/pallets/cf-swapping/src/tests.rs b/state-chain/pallets/cf-swapping/src/tests.rs index 8b0b6cff39..3763abc6d3 100644 --- a/state-chain/pallets/cf-swapping/src/tests.rs +++ b/state-chain/pallets/cf-swapping/src/tests.rs @@ -1689,7 +1689,7 @@ mod on_chain_swapping { input_asset: INPUT_ASSET, input_amount: INPUT_AMOUNT, output_asset: OUTPUT_ASSET, - origin: SwapOrigin::Internal, + origin: SwapOrigin::OnChainAccount(LP_ACCOUNT), request_type: SwapRequestTypeEncoded::Regular { output_action: SwapOutputActionEncoded::CreditOnChain { account_id: LP_ACCOUNT @@ -1704,9 +1704,21 @@ mod on_chain_swapping { }) .then_process_blocks_until_block(SWAP_BLOCK) .then_execute_with(|_| { - assert_has_matching_event!( + assert_event_sequence!( Test, - RuntimeEvent::Swapping(Event::SwapExecuted { .. }), + RuntimeEvent::Swapping(Event::SwapExecuted { + swap_request_id: SWAP_REQUEST_ID, + .. + }), + RuntimeEvent::Swapping(Event::CreditedOnChain { + swap_request_id: SWAP_REQUEST_ID, + account_id: LP_ACCOUNT, + asset: OUTPUT_ASSET, + amount: EXPECTED_OUTPUT_AMOUNT, + }), + RuntimeEvent::Swapping(Event::SwapRequestCompleted { + swap_request_id: SWAP_REQUEST_ID + }), ); assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), 0); @@ -1746,7 +1758,7 @@ mod on_chain_swapping { input_asset: INPUT_ASSET, input_amount: INPUT_AMOUNT, output_asset: OUTPUT_ASSET, - origin: SwapOrigin::Internal, + origin: SwapOrigin::OnChainAccount(LP_ACCOUNT), request_type: SwapRequestTypeEncoded::Regular { output_action: SwapOutputActionEncoded::CreditOnChain { account_id: LP_ACCOUNT @@ -1771,12 +1783,29 @@ mod on_chain_swapping { }) .then_process_blocks_until_block(CHUNK_2_BLOCK) .then_execute_with(|_| { - // There is no "SwapRefunded" event... - // Only one chunk is expected to be swapped: const EXPECTED_OUTPUT_AMOUNT: AssetAmount = CHUNK_AMOUNT * DEFAULT_SWAP_RATE * DEFAULT_SWAP_RATE; + assert_event_sequence!( + Test, + RuntimeEvent::Swapping(Event::RefundedOnChain { + swap_request_id: SWAP_REQUEST_ID, + account_id: LP_ACCOUNT, + asset: INPUT_ASSET, + amount: CHUNK_AMOUNT, + }), + RuntimeEvent::Swapping(Event::CreditedOnChain { + swap_request_id: SWAP_REQUEST_ID, + account_id: LP_ACCOUNT, + asset: OUTPUT_ASSET, + amount: EXPECTED_OUTPUT_AMOUNT + }), + RuntimeEvent::Swapping(Event::SwapRequestCompleted { + swap_request_id: SWAP_REQUEST_ID + }), + ); + assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), CHUNK_AMOUNT); assert_eq!( MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET),