Skip to content

Commit

Permalink
feat: Add channel opening fee to *DepositAddressReady Events (#4609)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellorigotti authored Mar 6, 2024
1 parent d946680 commit 4540356
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
2 changes: 2 additions & 0 deletions api/bin/chainflip-broker-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct BrokerSwapDepositAddress {
pub issued_block: BlockNumber,
pub channel_id: ChannelId,
pub source_chain_expiry_block: NumberOrHex,
pub channel_opening_fee: u128,
}

impl From<chainflip_api::SwapDepositAddress> for BrokerSwapDepositAddress {
Expand All @@ -38,6 +39,7 @@ impl From<chainflip_api::SwapDepositAddress> for BrokerSwapDepositAddress {
issued_block: value.issued_block,
channel_id: value.channel_id,
source_chain_expiry_block: NumberOrHex::from(value.source_chain_expiry_block),
channel_opening_fee: value.channel_opening_fee,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ pub struct SwapDepositAddress {
pub issued_block: state_chain_runtime::BlockNumber,
pub channel_id: ChannelId,
pub source_chain_expiry_block: <AnyChain as cf_chains::Chain>::ChainBlockNumber,
pub channel_opening_fee: u128,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -366,6 +367,7 @@ pub trait BrokerApi: SignedExtrinsicApi {
deposit_address,
channel_id,
source_chain_expiry_block,
channel_opening_fee,
..
},
)) = events.iter().find(|event| {
Expand All @@ -381,6 +383,7 @@ pub trait BrokerApi: SignedExtrinsicApi {
issued_block: header.number,
channel_id: *channel_id,
source_chain_expiry_block: *source_chain_expiry_block,
channel_opening_fee: *channel_opening_fee,
})
} else {
bail!("No SwapDepositAddressReady event was found");
Expand Down
22 changes: 13 additions & 9 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,8 +1310,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
source_asset: TargetChainAsset<T, I>,
action: ChannelAction<T::AccountId>,
boost_fee: BasisPoints,
) -> Result<(ChannelId, TargetChainAccount<T, I>, TargetChainBlockNumber<T, I>), DispatchError>
{
) -> Result<
(ChannelId, TargetChainAccount<T, I>, TargetChainBlockNumber<T, I>, T::Amount),
DispatchError,
> {
let channel_opening_fee = ChannelOpeningFee::<T, I>::get();
T::FeePayment::try_burn_fee(requester, channel_opening_fee)?;
Self::deposit_event(Event::<T, I>::ChannelOpeningFeePaid { fee: channel_opening_fee });
Expand Down Expand Up @@ -1359,7 +1361,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
},
);

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

pub fn get_failed_call(broadcast_id: BroadcastId) -> Option<FailedForeignChainCall> {
Expand Down Expand Up @@ -1498,23 +1500,25 @@ 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 Amount = T::Amount;

// This should be callable by the LP pallet.
fn request_liquidity_deposit_address(
lp_account: T::AccountId,
source_asset: TargetChainAsset<T, I>,
boost_fee: BasisPoints,
) -> Result<
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber),
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber, Self::Amount),
DispatchError,
> {
let (channel_id, deposit_address, expiry_block) = Self::open_channel(
let (channel_id, deposit_address, expiry_block, channel_opening_fee) = Self::open_channel(
&lp_account,
source_asset,
ChannelAction::LiquidityProvision { lp_account: lp_account.clone() },
boost_fee,
)?;

Ok((channel_id, deposit_address.into(), expiry_block))
Ok((channel_id, deposit_address.into(), expiry_block, channel_opening_fee))
}

// This should only be callable by the broker.
Expand All @@ -1527,10 +1531,10 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
channel_metadata: Option<CcmChannelMetadata>,
boost_fee: BasisPoints,
) -> Result<
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber),
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber, Self::Amount),
DispatchError,
> {
let (channel_id, deposit_address, expiry_height) = Self::open_channel(
let (channel_id, deposit_address, expiry_height, channel_opening_fee) = Self::open_channel(
&broker_id,
source_asset,
match channel_metadata {
Expand All @@ -1549,6 +1553,6 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
boost_fee,
)?;

Ok((channel_id, deposit_address.into(), expiry_height))
Ok((channel_id, deposit_address.into(), expiry_height, channel_opening_fee))
}
}
5 changes: 4 additions & 1 deletion state-chain/pallets/cf-lp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod pallet {
type DepositHandler: DepositApi<
AnyChain,
AccountId = <Self as frame_system::Config>::AccountId,
Amount = <Self as Chainflip>::Amount,
>;

/// API for handling asset egress.
Expand Down Expand Up @@ -116,6 +117,7 @@ pub mod pallet {
account_id: T::AccountId,
deposit_chain_expiry_block: <AnyChain as Chain>::ChainBlockNumber,
boost_fee: BasisPoints,
channel_opening_fee: T::Amount,
},
WithdrawalEgressScheduled {
egress_id: EgressId,
Expand Down Expand Up @@ -182,7 +184,7 @@ pub mod pallet {
Error::<T>::NoLiquidityRefundAddressRegistered
);

let (channel_id, deposit_address, expiry_block) =
let (channel_id, deposit_address, expiry_block, channel_opening_fee) =
T::DepositHandler::request_liquidity_deposit_address(
account_id.clone(),
asset,
Expand All @@ -196,6 +198,7 @@ pub mod pallet {
account_id,
deposit_chain_expiry_block: expiry_block,
boost_fee,
channel_opening_fee,
});

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub mod pallet {
type DepositHandler: DepositApi<
AnyChain,
AccountId = <Self as frame_system::Config>::AccountId,
Amount = <Self as Chainflip>::Amount,
>;

/// API for handling asset egress.
Expand Down Expand Up @@ -311,6 +312,7 @@ pub mod pallet {
channel_metadata: Option<CcmChannelMetadata>,
source_chain_expiry_block: <AnyChain as Chain>::ChainBlockNumber,
boost_fee: BasisPoints,
channel_opening_fee: T::Amount,
},
/// A swap deposit has been received.
SwapScheduled {
Expand Down Expand Up @@ -493,7 +495,7 @@ pub mod pallet {
);
}

let (channel_id, deposit_address, expiry_height) =
let (channel_id, deposit_address, expiry_height, channel_opening_fee) =
T::DepositHandler::request_swap_deposit_address(
source_asset,
destination_asset,
Expand All @@ -514,6 +516,7 @@ pub mod pallet {
channel_metadata,
source_chain_expiry_block: expiry_height,
boost_fee,
channel_opening_fee,
});

Ok(())
Expand Down
9 changes: 5 additions & 4 deletions state-chain/runtime/src/chainflip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,20 +436,21 @@ 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 Amount = <Runtime as Chainflip>::Amount;

fn request_liquidity_deposit_address(
lp_account: Self::AccountId,
source_asset: Asset,
boost_fee: BasisPoints
) -> Result<(ChannelId, ForeignChainAddress, <AnyChain as cf_chains::Chain>::ChainBlockNumber), DispatchError> {
) -> Result<(ChannelId, ForeignChainAddress, <AnyChain as cf_chains::Chain>::ChainBlockNumber, FlipBalance), DispatchError> {
match source_asset.into() {
$(
ForeignChainAndAsset::$chain(source_asset) =>
$pallet::request_liquidity_deposit_address(
lp_account,
source_asset,
boost_fee
).map(|(channel, address, block_number)| (channel, address, block_number.into())),
).map(|(channel, address, block_number, channel_opening_fee)| (channel, address, block_number.into(), channel_opening_fee)),
)+
}
}
Expand All @@ -462,7 +463,7 @@ macro_rules! impl_deposit_api_for_anychain {
broker_id: Self::AccountId,
channel_metadata: Option<CcmChannelMetadata>,
boost_fee: BasisPoints
) -> Result<(ChannelId, ForeignChainAddress, <AnyChain as cf_chains::Chain>::ChainBlockNumber), DispatchError> {
) -> Result<(ChannelId, ForeignChainAddress, <AnyChain as cf_chains::Chain>::ChainBlockNumber, FlipBalance), DispatchError> {
match source_asset.into() {
$(
ForeignChainAndAsset::$chain(source_asset) => $pallet::request_swap_deposit_address(
Expand All @@ -473,7 +474,7 @@ macro_rules! impl_deposit_api_for_anychain {
broker_id,
channel_metadata,
boost_fee
).map(|(channel, address, block_number)| (channel, address, block_number.into())),
).map(|(channel, address, block_number, channel_opening_fee)| (channel, address, block_number.into(), channel_opening_fee)),
)+
}
}
Expand Down
5 changes: 3 additions & 2 deletions state-chain/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,14 @@ pub trait FundingInfo {
/// Allow pallets to open and expire deposit addresses.
pub trait DepositApi<C: Chain> {
type AccountId;
type Amount;

/// Issues a channel id and deposit address for a new liquidity deposit.
fn request_liquidity_deposit_address(
lp_account: Self::AccountId,
source_asset: C::ChainAsset,
boost_fee: BasisPoints,
) -> Result<(ChannelId, ForeignChainAddress, C::ChainBlockNumber), DispatchError>;
) -> Result<(ChannelId, ForeignChainAddress, C::ChainBlockNumber, Self::Amount), DispatchError>;

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

pub trait AccountRoleRegistry<T: frame_system::Config> {
Expand Down
21 changes: 14 additions & 7 deletions state-chain/traits/src/mocks/deposit_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<C: Chain, T: Chainflip> MockDepositHandler<C, T> {
fn get_new_deposit_address(
swap_or_lp: SwapOrLp,
asset: <C as Chain>::ChainAsset,
) -> (ChannelId, ForeignChainAddress) {
) -> (ChannelId, ForeignChainAddress, T::Amount) {
let channel_id = <Self as MockPalletStorage>::mutate_value(
match swap_or_lp {
SwapOrLp::Swap => b"SWAP_INTENT_ID",
Expand All @@ -65,6 +65,7 @@ impl<C: Chain, T: Chainflip> MockDepositHandler<C, T> {
),
ForeignChain::Bitcoin => todo!("Bitcoin address"),
},
Default::default(),
)
}

Expand All @@ -79,16 +80,22 @@ impl<C: Chain, T: Chainflip> MockDepositHandler<C, T> {

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

fn request_liquidity_deposit_address(
lp_account: Self::AccountId,
source_asset: <C as cf_chains::Chain>::ChainAsset,
boost_fee: BasisPoints,
) -> Result<
(cf_primitives::ChannelId, ForeignChainAddress, <C as cf_chains::Chain>::ChainBlockNumber),
(
cf_primitives::ChannelId,
ForeignChainAddress,
<C as cf_chains::Chain>::ChainBlockNumber,
Self::Amount,
),
sp_runtime::DispatchError,
> {
let (channel_id, deposit_address) =
let (channel_id, deposit_address, channel_opening_fee) =
Self::get_new_deposit_address(SwapOrLp::Lp, source_asset);
<Self as MockPalletStorage>::mutate_value(b"LP_INGRESS_CHANNELS", |lp_channels| {
if lp_channels.is_none() {
Expand All @@ -103,7 +110,7 @@ impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
});
}
});
Ok((channel_id, deposit_address, 0u32.into()))
Ok((channel_id, deposit_address, 0u32.into(), channel_opening_fee))
}

fn request_swap_deposit_address(
Expand All @@ -115,10 +122,10 @@ impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
channel_metadata: Option<CcmChannelMetadata>,
boost_fee: BasisPoints,
) -> Result<
(cf_primitives::ChannelId, ForeignChainAddress, C::ChainBlockNumber),
(cf_primitives::ChannelId, ForeignChainAddress, C::ChainBlockNumber, Self::Amount),
sp_runtime::DispatchError,
> {
let (channel_id, deposit_address) =
let (channel_id, deposit_address, channel_opening_fee) =
Self::get_new_deposit_address(SwapOrLp::Swap, source_asset);
<Self as MockPalletStorage>::mutate_value(b"SWAP_INGRESS_CHANNELS", |swap_channels| {
if swap_channels.is_none() {
Expand All @@ -137,6 +144,6 @@ impl<C: Chain, T: Chainflip> DepositApi<C> for MockDepositHandler<C, T> {
});
};
});
Ok((channel_id, deposit_address, 0u32.into()))
Ok((channel_id, deposit_address, 0u32.into(), channel_opening_fee))
}
}

0 comments on commit 4540356

Please sign in to comment.