Skip to content

Commit

Permalink
feat: add external expiry block to event [WEB-496] (#4097)
Browse files Browse the repository at this point in the history
* feat: add external expiry block to event

* clippy allow

* add expiry to broker response
  • Loading branch information
acdibble authored Oct 10, 2023
1 parent 3b72edd commit c7e75b7
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 32 deletions.
3 changes: 3 additions & 0 deletions api/bin/chainflip-broker-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::Parser;
use futures::FutureExt;
use jsonrpsee::{core::async_trait, proc_macros::rpc, server::ServerBuilder};
use serde::{Deserialize, Serialize};
use sp_rpc::number::NumberOrHex;
use std::path::PathBuf;
use tracing::log;

Expand All @@ -23,6 +24,7 @@ pub struct BrokerSwapDepositAddress {
pub address: String,
pub issued_block: BlockNumber,
pub channel_id: ChannelId,
pub source_chain_expiry_block: NumberOrHex,
}

impl From<chainflip_api::SwapDepositAddress> for BrokerSwapDepositAddress {
Expand All @@ -31,6 +33,7 @@ impl From<chainflip_api::SwapDepositAddress> for BrokerSwapDepositAddress {
address: value.address,
issued_block: value.issued_block,
channel_id: value.channel_id,
source_chain_expiry_block: NumberOrHex::from(value.source_chain_expiry_block),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cf_chains::{
address::EncodedAddress,
dot::PolkadotAccountId,
evm::{to_evm_address, Address as EthereumAddress},
CcmChannelMetadata, ForeignChain,
AnyChain, CcmChannelMetadata, ForeignChain,
};
use cf_primitives::{AccountRole, Asset, BasisPoints, ChannelId};
use codec::Encode;
Expand Down Expand Up @@ -316,6 +316,7 @@ pub struct SwapDepositAddress {
pub address: String,
pub issued_block: state_chain_runtime::BlockNumber,
pub channel_id: ChannelId,
pub source_chain_expiry_block: <AnyChain as cf_chains::Chain>::ChainBlockNumber,
}

#[async_trait]
Expand All @@ -342,7 +343,10 @@ pub trait BrokerApi: SignedExtrinsicApi {

if let Some(state_chain_runtime::RuntimeEvent::Swapping(
pallet_cf_swapping::Event::SwapDepositAddressReady {
deposit_address, channel_id, ..
deposit_address,
channel_id,
source_chain_expiry_block,
..
},
)) = events.iter().find(|event| {
matches!(
Expand All @@ -356,6 +360,7 @@ pub trait BrokerApi: SignedExtrinsicApi {
address: deposit_address.to_string(),
issued_block: header.number,
channel_id: *channel_id,
source_chain_expiry_block: *source_chain_expiry_block,
})
} else {
panic!("SwapDepositAddressReady must have been generated");
Expand Down
18 changes: 11 additions & 7 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,12 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Opens a channel for the given asset and registers it with the given action.
///
/// May re-use an existing deposit address, depending on chain configuration.
#[allow(clippy::type_complexity)]
fn open_channel(
source_asset: TargetChainAsset<T, I>,
action: ChannelAction<T::AccountId>,
) -> Result<(ChannelId, TargetChainAccount<T, I>), DispatchError> {
) -> Result<(ChannelId, TargetChainAccount<T, I>, TargetChainBlockNumber<T, I>), DispatchError>
{
let (deposit_channel, channel_id) = if let Some((channel_id, mut deposit_channel)) =
DepositChannelPool::<T, I>::drain().next()
{
Expand Down Expand Up @@ -933,7 +935,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
},
);

Ok((channel_id, deposit_address))
Ok((channel_id, deposit_address, expiry_height))
}
}

Expand Down Expand Up @@ -987,13 +989,12 @@ impl<T: Config<I>, I: 'static> EgressApi<T::TargetChain> for Pallet<T, I> {

impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
type AccountId = T::AccountId;
type BlockNumber = BlockNumberFor<T>;
// This should be callable by the LP pallet.
fn request_liquidity_deposit_address(
lp_account: T::AccountId,
source_asset: TargetChainAsset<T, I>,
) -> Result<(ChannelId, ForeignChainAddress), DispatchError> {
let (channel_id, deposit_address) =
let (channel_id, deposit_address, ..) =
Self::open_channel(source_asset, ChannelAction::LiquidityProvision { lp_account })?;

Ok((channel_id, deposit_address.into()))
Expand All @@ -1007,8 +1008,11 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
broker_commission_bps: BasisPoints,
broker_id: T::AccountId,
channel_metadata: Option<CcmChannelMetadata>,
) -> Result<(ChannelId, ForeignChainAddress), DispatchError> {
let (channel_id, deposit_address) = Self::open_channel(
) -> Result<
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber),
DispatchError,
> {
let (channel_id, deposit_address, expiry_height) = Self::open_channel(
source_asset,
match channel_metadata {
Some(msg) => ChannelAction::CcmTransfer {
Expand All @@ -1025,6 +1029,6 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
},
)?;

Ok((channel_id, deposit_address.into()))
Ok((channel_id, deposit_address.into(), expiry_height))
}
}
2 changes: 1 addition & 1 deletion state-chain/pallets/cf-ingress-egress/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<Ctx: Clone> RequestAddress for TestExternalities<Test, Ctx> {
BROKER,
None,
)
.map(|(channel_id, deposit_address)| {
.map(|(channel_id, deposit_address, ..)| {
(request, channel_id, TestChainAccount::try_from(deposit_address).unwrap())
})
.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions state-chain/pallets/cf-ingress-egress/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ fn reused_address_channel_id_matches() {
>(CHANNEL_ID, eth::Asset::Eth)
.unwrap();
DepositChannelPool::<Test, _>::insert(CHANNEL_ID, new_channel.clone());
let (reused_channel_id, reused_address) = IngressEgress::open_channel(
let (reused_channel_id, reused_address, ..) = IngressEgress::open_channel(
eth::Asset::Eth,
ChannelAction::LiquidityProvision { lp_account: 0 },
)
Expand Down Expand Up @@ -461,7 +461,7 @@ fn can_process_ccm_deposit() {

// Register swap deposit with CCM

let (_, deposit_address) = IngressEgress::request_swap_deposit_address(
let (_, deposit_address, ..) = IngressEgress::request_swap_deposit_address(
from_asset,
to_asset,
destination_address.clone(),
Expand Down
1 change: 0 additions & 1 deletion state-chain/pallets/cf-lp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub mod pallet {
type DepositHandler: DepositApi<
AnyChain,
AccountId = <Self as frame_system::Config>::AccountId,
BlockNumber = BlockNumberFor<Self>,
>;

/// API for handling asset egress.
Expand Down
22 changes: 12 additions & 10 deletions state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl_pallet_safe_mode! {
#[frame_support::pallet]
pub mod pallet {

use cf_chains::{address::EncodedAddress, AnyChain};
use cf_chains::{address::EncodedAddress, AnyChain, Chain};
use cf_primitives::{Asset, AssetAmount, BasisPoints, EgressId};
use cf_traits::{AccountRoleRegistry, Chainflip, EgressApi, SwapDepositHandler};

Expand All @@ -191,7 +191,6 @@ pub mod pallet {
type DepositHandler: DepositApi<
AnyChain,
AccountId = <Self as frame_system::Config>::AccountId,
BlockNumber = BlockNumberFor<Self>,
>;

/// API for handling asset egress.
Expand Down Expand Up @@ -268,6 +267,7 @@ pub mod pallet {
channel_id: ChannelId,
broker_commission_rate: BasisPoints,
channel_metadata: Option<CcmChannelMetadata>,
source_chain_expiry_block: <AnyChain as Chain>::ChainBlockNumber,
},
/// A swap deposit has been received.
SwapScheduled {
Expand Down Expand Up @@ -519,14 +519,15 @@ pub mod pallet {
);
}

let (channel_id, deposit_address) = T::DepositHandler::request_swap_deposit_address(
source_asset,
destination_asset,
destination_address_internal,
broker_commission_bps,
broker,
channel_metadata.clone(),
)?;
let (channel_id, deposit_address, expiry_height) =
T::DepositHandler::request_swap_deposit_address(
source_asset,
destination_asset,
destination_address_internal,
broker_commission_bps,
broker,
channel_metadata.clone(),
)?;

Self::deposit_event(Event::<T>::SwapDepositAddressReady {
deposit_address: T::AddressConverter::to_encoded_address(deposit_address),
Expand All @@ -536,6 +537,7 @@ pub mod pallet {
channel_id,
broker_commission_rate: broker_commission_bps,
channel_metadata,
source_chain_expiry_block: expiry_height,
});

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions state-chain/runtime/src/chainflip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ macro_rules! impl_deposit_api_for_anychain {
( $t: ident, $(($chain: ident, $pallet: ident)),+ ) => {
impl DepositApi<AnyChain> for $t {
type AccountId = <Runtime as frame_system::Config>::AccountId;
type BlockNumber = frame_system::pallet_prelude::BlockNumberFor<Runtime>;

fn request_liquidity_deposit_address(
lp_account: Self::AccountId,
Expand All @@ -467,7 +466,7 @@ macro_rules! impl_deposit_api_for_anychain {
broker_commission_bps: BasisPoints,
broker_id: Self::AccountId,
channel_metadata: Option<CcmChannelMetadata>,
) -> Result<(ChannelId, ForeignChainAddress), DispatchError> {
) -> Result<(ChannelId, ForeignChainAddress, <AnyChain as cf_chains::Chain>::ChainBlockNumber), DispatchError> {
match source_asset.into() {
$(
ForeignChain::$chain => $pallet::request_swap_deposit_address(
Expand All @@ -477,7 +476,7 @@ macro_rules! impl_deposit_api_for_anychain {
broker_commission_bps,
broker_id,
channel_metadata,
),
).map(|(channel, address, block_number)| (channel, address, block_number.into())),
)+
}
}
Expand Down
3 changes: 1 addition & 2 deletions state-chain/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ pub trait FundingInfo {
/// Allow pallets to open and expire deposit addresses.
pub trait DepositApi<C: Chain> {
type AccountId;
type BlockNumber;

/// Issues a channel id and deposit address for a new liquidity deposit.
fn request_liquidity_deposit_address(
Expand All @@ -650,7 +649,7 @@ pub trait DepositApi<C: Chain> {
broker_commission_bps: BasisPoints,
broker_id: Self::AccountId,
channel_metadata: Option<CcmChannelMetadata>,
) -> Result<(ChannelId, ForeignChainAddress), DispatchError>;
) -> Result<(ChannelId, ForeignChainAddress, C::ChainBlockNumber), DispatchError>;
}

pub trait AccountRoleRegistry<T: frame_system::Config> {
Expand Down
9 changes: 5 additions & 4 deletions state-chain/traits/src/mocks/deposit_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use cf_chains::{
};
use cf_primitives::{chains::assets::any, BasisPoints, ChannelId};
use codec::{Decode, Encode};
use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::TypeInfo;
use sp_std::marker::PhantomData;

Expand Down Expand Up @@ -78,7 +77,6 @@ impl<C: Chain, T: Chainflip> MockDepositHandler<C, T> {

impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
type AccountId = T::AccountId;
type BlockNumber = BlockNumberFor<T>;

fn request_liquidity_deposit_address(
lp_account: Self::AccountId,
Expand Down Expand Up @@ -108,7 +106,10 @@ impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
broker_commission_bps: BasisPoints,
broker_id: Self::AccountId,
channel_metadata: Option<CcmChannelMetadata>,
) -> Result<(cf_primitives::ChannelId, ForeignChainAddress), sp_runtime::DispatchError> {
) -> Result<
(cf_primitives::ChannelId, ForeignChainAddress, C::ChainBlockNumber),
sp_runtime::DispatchError,
> {
let (channel_id, deposit_address) =
Self::get_new_deposit_address(SwapOrLp::Swap, source_asset);
<Self as MockPalletStorage>::mutate_value(b"SWAP_INGRESS_CHANNELS", |swap_channels| {
Expand All @@ -127,6 +128,6 @@ impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
});
};
});
Ok((channel_id, deposit_address))
Ok((channel_id, deposit_address, 0u32.into()))
}
}

0 comments on commit c7e75b7

Please sign in to comment.