Skip to content

Commit 482c012

Browse files
committed
feat: add events for on-chain swapping
1 parent bd860b9 commit 482c012

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

state-chain/pallets/cf-swapping/src/lib.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,20 @@ pub mod pallet {
703703
MinimumNetworkFeeSet {
704704
min_fee: AssetAmount,
705705
},
706+
// Account credited as a result of an on-chain swap
707+
CreditedOnChain {
708+
swap_request_id: SwapRequestId,
709+
account_id: T::AccountId,
710+
asset: Asset,
711+
amount: AssetAmount,
712+
},
713+
// Account received a refund as a result of an on-chain swap
714+
RefundedOnChain {
715+
swap_request_id: SwapRequestId,
716+
account_id: T::AccountId,
717+
asset: Asset,
718+
amount: AssetAmount,
719+
},
706720
}
707721
#[pallet::error]
708722
pub enum Error<T> {
@@ -1538,6 +1552,13 @@ pub mod pallet {
15381552
);
15391553
},
15401554
RefundDestination::OnChainAccount(account_id) => {
1555+
Self::deposit_event(Event::<T>::RefundedOnChain {
1556+
swap_request_id,
1557+
account_id: account_id.clone(),
1558+
asset: request.input_asset,
1559+
amount: amount_to_refund,
1560+
});
1561+
15411562
T::BalanceApi::credit_account(
15421563
account_id,
15431564
request.input_asset,
@@ -1552,7 +1573,7 @@ pub mod pallet {
15521573
match output_action {
15531574
SwapOutputAction::Egress { ccm_deposit_metadata, output_address } => {
15541575
Self::egress_for_swap(
1555-
swap.swap_request_id,
1576+
swap_request_id,
15561577
*accumulated_output_amount,
15571578
request.output_asset,
15581579
output_address.clone(),
@@ -1561,7 +1582,13 @@ pub mod pallet {
15611582
);
15621583
},
15631584
SwapOutputAction::CreditOnChain { account_id } => {
1564-
// TODO: a new event for when LP is credited after a swap?
1585+
Self::deposit_event(Event::<T>::CreditedOnChain {
1586+
swap_request_id,
1587+
account_id: account_id.clone(),
1588+
asset: request.output_asset,
1589+
amount: *accumulated_output_amount,
1590+
});
1591+
15651592
T::BalanceApi::credit_account(
15661593
account_id,
15671594
request.output_asset,
@@ -1655,9 +1682,16 @@ pub mod pallet {
16551682
);
16561683
},
16571684
SwapOutputAction::CreditOnChain { account_id } => {
1685+
Self::deposit_event(Event::<T>::CreditedOnChain {
1686+
swap_request_id,
1687+
account_id: account_id.clone(),
1688+
asset: request.output_asset,
1689+
amount: dca_state.accumulated_output_amount,
1690+
});
1691+
16581692
T::BalanceApi::credit_account(
16591693
account_id,
1660-
swap.output_asset(),
1694+
request.output_asset,
16611695
dca_state.accumulated_output_amount,
16621696
);
16631697
},

state-chain/pallets/cf-swapping/src/tests.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ mod on_chain_swapping {
16891689
input_asset: INPUT_ASSET,
16901690
input_amount: INPUT_AMOUNT,
16911691
output_asset: OUTPUT_ASSET,
1692-
origin: SwapOrigin::Internal,
1692+
origin: SwapOrigin::OnChainAccount(LP_ACCOUNT),
16931693
request_type: SwapRequestTypeEncoded::Regular {
16941694
output_action: SwapOutputActionEncoded::CreditOnChain {
16951695
account_id: LP_ACCOUNT
@@ -1704,9 +1704,21 @@ mod on_chain_swapping {
17041704
})
17051705
.then_process_blocks_until_block(SWAP_BLOCK)
17061706
.then_execute_with(|_| {
1707-
assert_has_matching_event!(
1707+
assert_event_sequence!(
17081708
Test,
1709-
RuntimeEvent::Swapping(Event::SwapExecuted { .. }),
1709+
RuntimeEvent::Swapping(Event::SwapExecuted {
1710+
swap_request_id: SWAP_REQUEST_ID,
1711+
..
1712+
}),
1713+
RuntimeEvent::Swapping(Event::CreditedOnChain {
1714+
swap_request_id: SWAP_REQUEST_ID,
1715+
account_id: LP_ACCOUNT,
1716+
asset: OUTPUT_ASSET,
1717+
amount: EXPECTED_OUTPUT_AMOUNT,
1718+
}),
1719+
RuntimeEvent::Swapping(Event::SwapRequestCompleted {
1720+
swap_request_id: SWAP_REQUEST_ID
1721+
}),
17101722
);
17111723

17121724
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), 0);
@@ -1746,7 +1758,7 @@ mod on_chain_swapping {
17461758
input_asset: INPUT_ASSET,
17471759
input_amount: INPUT_AMOUNT,
17481760
output_asset: OUTPUT_ASSET,
1749-
origin: SwapOrigin::Internal,
1761+
origin: SwapOrigin::OnChainAccount(LP_ACCOUNT),
17501762
request_type: SwapRequestTypeEncoded::Regular {
17511763
output_action: SwapOutputActionEncoded::CreditOnChain {
17521764
account_id: LP_ACCOUNT
@@ -1771,12 +1783,29 @@ mod on_chain_swapping {
17711783
})
17721784
.then_process_blocks_until_block(CHUNK_2_BLOCK)
17731785
.then_execute_with(|_| {
1774-
// There is no "SwapRefunded" event...
1775-
17761786
// Only one chunk is expected to be swapped:
17771787
const EXPECTED_OUTPUT_AMOUNT: AssetAmount =
17781788
CHUNK_AMOUNT * DEFAULT_SWAP_RATE * DEFAULT_SWAP_RATE;
17791789

1790+
assert_event_sequence!(
1791+
Test,
1792+
RuntimeEvent::Swapping(Event::RefundedOnChain {
1793+
swap_request_id: SWAP_REQUEST_ID,
1794+
account_id: LP_ACCOUNT,
1795+
asset: INPUT_ASSET,
1796+
amount: CHUNK_AMOUNT,
1797+
}),
1798+
RuntimeEvent::Swapping(Event::CreditedOnChain {
1799+
swap_request_id: SWAP_REQUEST_ID,
1800+
account_id: LP_ACCOUNT,
1801+
asset: OUTPUT_ASSET,
1802+
amount: EXPECTED_OUTPUT_AMOUNT
1803+
}),
1804+
RuntimeEvent::Swapping(Event::SwapRequestCompleted {
1805+
swap_request_id: SWAP_REQUEST_ID
1806+
}),
1807+
);
1808+
17801809
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), CHUNK_AMOUNT);
17811810
assert_eq!(
17821811
MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET),

0 commit comments

Comments
 (0)