Skip to content

Commit

Permalink
refactor: change panic to bail on LP and Broker API's (#4190)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd authored Nov 1, 2023
1 parent 2bfaea3 commit 6f5ec83
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub trait BrokerApi: SignedExtrinsicApi {
source_chain_expiry_block: *source_chain_expiry_block,
})
} else {
panic!("SwapDepositAddressReady must have been generated");
bail!("No SwapDepositAddressReady event was found");
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions api/lib/src/lp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Range;

use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use async_trait::async_trait;
pub use cf_amm::{
common::{Order, SideMap, Tick},
Expand Down Expand Up @@ -131,15 +131,15 @@ pub trait LpApi: SignedExtrinsicApi {
.until_in_block()
.await?;

Ok(events
events
.into_iter()
.find_map(|event| match event {
state_chain_runtime::RuntimeEvent::LiquidityProvider(
pallet_cf_lp::Event::LiquidityDepositAddressReady { deposit_address, .. },
) => Some(deposit_address),
_ => None,
})
.expect("DepositAddressReady must have been generated"))
.ok_or_else(|| anyhow::anyhow!("No LiquidityDepositAddressReady event was found"))
}

async fn withdraw_asset(
Expand All @@ -148,6 +148,10 @@ pub trait LpApi: SignedExtrinsicApi {
asset: Asset,
destination_address: EncodedAddress,
) -> Result<EgressId> {
if amount == 0 {
bail!("Withdrawal amount must be greater than 0");
}

let (_tx_hash, events, ..) = self
.submit_signed_extrinsic(pallet_cf_lp::Call::withdraw_asset {
amount,
Expand All @@ -158,15 +162,15 @@ pub trait LpApi: SignedExtrinsicApi {
.until_in_block()
.await?;

Ok(events
events
.into_iter()
.find_map(|event| match event {
state_chain_runtime::RuntimeEvent::LiquidityProvider(
pallet_cf_lp::Event::WithdrawalEgressScheduled { egress_id, .. },
) => Some(egress_id),
_ => None,
})
.expect("WithdrawalEgressScheduled must have been generated"))
.ok_or_else(|| anyhow::anyhow!("No WithdrawalEgressScheduled event was found"))
}

async fn update_range_order(
Expand Down

0 comments on commit 6f5ec83

Please sign in to comment.