Skip to content

Commit

Permalink
refactor: deduplicate event finding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Nov 13, 2024
1 parent c22b70a commit dca5255
Showing 1 changed file with 42 additions and 58 deletions.
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, $event_variant:path, $err_msg:literal, $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 { .. }))
}) {
$result
} else {
bail!($err_msg);
}
};
}

#[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 {
Ok(extract_event!(
events,
pallet_cf_swapping::Event::SwapDepositAddressReady,
"No SwapDepositAddressReady event was found",
{
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 {
Ok(extract_event!(
events,
pallet_cf_swapping::Event::WithdrawalRequested,
"No WithdrawalRequested event was found",
{
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");
}
Ok(extract_event!(
&events,
pallet_cf_swapping::Event::PrivateBrokerChannelOpened,
"No PrivateBrokerChannelOpened event was found",
{ 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");
}
Ok(extract_event!(
&events,
pallet_cf_swapping::Event::PrivateBrokerChannelClosed,
"No PrivateBrokerChannelClosed event was found",
{ channel_id, .. },
*channel_id
))
}
}

Expand Down

0 comments on commit dca5255

Please sign in to comment.