Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deduplicate event finding logic #5412

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 42 additions & 58 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ impl fmt::Display for WithdrawFeesDetail {
}
}

macro_rules! extract_event {
($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!($runtime_event_variant), stringify!($pallet_event_variant));
}
};
}

#[async_trait]
pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'static {
async fn request_swap_deposit_address(
Expand Down Expand Up @@ -390,24 +402,19 @@ 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,
state_chain_runtime::RuntimeEvent::Swapping,
pallet_cf_swapping::Event::SwapDepositAddressReady,
{
deposit_address,
channel_id,
source_chain_expiry_block,
channel_opening_fee,
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,
Expand All @@ -418,10 +425,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,
Expand All @@ -439,32 +444,25 @@ 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,
state_chain_runtime::RuntimeEvent::Swapping,
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<H256> {
self.simple_submission_with_dry_run(pallet_cf_swapping::Call::register_as_broker {})
Expand All @@ -484,20 +482,13 @@ 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,
state_chain_runtime::RuntimeEvent::Swapping,
pallet_cf_swapping::Event::PrivateBrokerChannelOpened,
{ channel_id, .. },
*channel_id
)
}

async fn close_private_btc_channel(&self) -> Result<ChannelId> {
Expand All @@ -509,20 +500,13 @@ 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,
state_chain_runtime::RuntimeEvent::Swapping,
pallet_cf_swapping::Event::PrivateBrokerChannelClosed,
{ channel_id, .. },
*channel_id
)
}
}

Expand Down
Loading