From 2c4de1084bfc0920d5759a45def9f55a45c0d091 Mon Sep 17 00:00:00 2001 From: kylezs Date: Wed, 13 Nov 2024 10:19:08 +0100 Subject: [PATCH 1/3] refactor: deduplicate event finding logic --- api/lib/src/lib.rs | 96 ++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index c9203ff8d3..b29c47c2de 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -345,6 +345,18 @@ impl fmt::Display for WithdrawFeesDetail { } } +macro_rules! extract_event { + ($events:expr, $event_variant:path, $pattern:tt, $result:expr) => { + if let Some(state_chain_runtime::RuntimeEvent::Swapping($event_variant $pattern)) = $events.iter().find(|event| { + matches!(event, state_chain_runtime::RuntimeEvent::Swapping($event_variant { .. })) + }) { + Ok($result) + } else { + bail!("No {} event was found", stringify!($event_variant)); + } + }; +} + #[async_trait] pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'static { async fn request_swap_deposit_address( @@ -390,8 +402,10 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st .until_in_block() .await?; - if let Some(state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::SwapDepositAddressReady { + extract_event!( + events, + pallet_cf_swapping::Event::SwapDepositAddressReady, + { deposit_address, channel_id, source_chain_expiry_block, @@ -399,15 +413,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st refund_parameters, .. }, - )) = events.iter().find(|event| { - matches!( - event, - state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::SwapDepositAddressReady { .. } - ) - ) - }) { - Ok(SwapDepositAddress { + SwapDepositAddress { address: AddressString::from_encoded_address(deposit_address), issued_block: header.number, channel_id: *channel_id, @@ -418,10 +424,8 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st AddressString::from_encoded_address(&refund_address) }) }), - }) - } else { - bail!("No SwapDepositAddressReady event was found"); - } + } + ) } async fn withdraw_fees( &self, @@ -439,32 +443,24 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st .until_in_block() .await?; - if let Some(state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::WithdrawalRequested { + extract_event!( + events, + pallet_cf_swapping::Event::WithdrawalRequested, + { egress_amount, egress_fee, destination_address, egress_id, .. }, - )) = events.iter().find(|event| { - matches!( - event, - state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::WithdrawalRequested { .. } - ) - ) - }) { - Ok(WithdrawFeesDetail { + WithdrawFeesDetail { tx_hash, egress_id: *egress_id, egress_amount: (*egress_amount).into(), egress_fee: (*egress_fee).into(), destination_address: AddressString::from_encoded_address(destination_address), - }) - } else { - bail!("No WithdrawalRequested event was found"); - } + } + ) } async fn register_account(&self) -> Result { self.simple_submission_with_dry_run(pallet_cf_swapping::Call::register_as_broker {}) @@ -484,20 +480,12 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st .until_in_block() .await?; - if let Some(state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::PrivateBrokerChannelOpened { channel_id, .. }, - )) = events.iter().find(|event| { - matches!( - event, - state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::PrivateBrokerChannelOpened { .. } - ) - ) - }) { - Ok(*channel_id) - } else { - bail!("No PrivateBrokerChannelOpened event was found"); - } + extract_event!( + &events, + pallet_cf_swapping::Event::PrivateBrokerChannelOpened, + { channel_id, .. }, + *channel_id + ) } async fn close_private_btc_channel(&self) -> Result { @@ -509,20 +497,12 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st .until_in_block() .await?; - if let Some(state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::PrivateBrokerChannelClosed { channel_id, .. }, - )) = events.iter().find(|event| { - matches!( - event, - state_chain_runtime::RuntimeEvent::Swapping( - pallet_cf_swapping::Event::PrivateBrokerChannelClosed { .. } - ) - ) - }) { - Ok(*channel_id) - } else { - bail!("No PrivateBrokerChannelClosed event was found"); - } + extract_event!( + &events, + pallet_cf_swapping::Event::PrivateBrokerChannelClosed, + { channel_id, .. }, + *channel_id + ) } } From 6eb618b654cf456e0844f7bea6ed8ba41f3f8ad7 Mon Sep 17 00:00:00 2001 From: kylezs Date: Wed, 13 Nov 2024 11:15:43 +0100 Subject: [PATCH 2/3] refactor: generalise macro for any runtime event --- api/lib/src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index b29c47c2de..502d149a38 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -346,13 +346,13 @@ impl fmt::Display for WithdrawFeesDetail { } macro_rules! extract_event { - ($events:expr, $event_variant:path, $pattern:tt, $result:expr) => { - if let Some(state_chain_runtime::RuntimeEvent::Swapping($event_variant $pattern)) = $events.iter().find(|event| { - matches!(event, state_chain_runtime::RuntimeEvent::Swapping($event_variant { .. })) + ($events:expr, $pallet_event:path, $event_variant:path, $pattern:tt, $result:expr) => { + if let Some($pallet_event($event_variant $pattern)) = $events.iter().find(|event| { + matches!(event, $pallet_event($event_variant { .. })) }) { Ok($result) } else { - bail!("No {} event was found", stringify!($event_variant)); + bail!("No {}({}) event was found", stringify!($pallet_event), stringify!($event_variant)); } }; } @@ -404,6 +404,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st extract_event!( events, + state_chain_runtime::RuntimeEvent::Swapping, pallet_cf_swapping::Event::SwapDepositAddressReady, { deposit_address, @@ -445,6 +446,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st extract_event!( events, + state_chain_runtime::RuntimeEvent::Swapping, pallet_cf_swapping::Event::WithdrawalRequested, { egress_amount, @@ -482,6 +484,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st extract_event!( &events, + state_chain_runtime::RuntimeEvent::Swapping, pallet_cf_swapping::Event::PrivateBrokerChannelOpened, { channel_id, .. }, *channel_id @@ -499,6 +502,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st extract_event!( &events, + state_chain_runtime::RuntimeEvent::Swapping, pallet_cf_swapping::Event::PrivateBrokerChannelClosed, { channel_id, .. }, *channel_id From 2385040b0bdceef64ef76d12c83f45e30421981f Mon Sep 17 00:00:00 2001 From: kylezs Date: Wed, 13 Nov 2024 11:37:41 +0100 Subject: [PATCH 3/3] chore: naming of macro bindings --- api/lib/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index 502d149a38..4396bd63c8 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -346,13 +346,13 @@ impl fmt::Display for WithdrawFeesDetail { } macro_rules! extract_event { - ($events:expr, $pallet_event:path, $event_variant:path, $pattern:tt, $result:expr) => { - if let Some($pallet_event($event_variant $pattern)) = $events.iter().find(|event| { - matches!(event, $pallet_event($event_variant { .. })) + ($events:expr, $runtime_event_variant:path, $pallet_event_variant:path, $pattern:tt, $result:expr) => { + if let Some($runtime_event_variant($pallet_event_variant $pattern)) = $events.iter().find(|event| { + matches!(event, $runtime_event_variant($pallet_event_variant { .. })) }) { Ok($result) } else { - bail!("No {}({}) event was found", stringify!($pallet_event), stringify!($event_variant)); + bail!("No {}({}) event was found", stringify!($runtime_event_variant), stringify!($pallet_event_variant)); } }; }