Skip to content

Commit

Permalink
feat: add events for on-chain swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
msgmaxim committed Feb 3, 2025
1 parent bd860b9 commit 482c012
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
40 changes: 37 additions & 3 deletions state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -1538,6 +1552,13 @@ pub mod pallet {
);
},
RefundDestination::OnChainAccount(account_id) => {
Self::deposit_event(Event::<T>::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,
Expand All @@ -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(),
Expand All @@ -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::<T>::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,
Expand Down Expand Up @@ -1655,9 +1682,16 @@ pub mod pallet {
);
},
SwapOutputAction::CreditOnChain { account_id } => {
Self::deposit_event(Event::<T>::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,
);
},
Expand Down
41 changes: 35 additions & 6 deletions state-chain/pallets/cf-swapping/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit 482c012

Please sign in to comment.