diff --git a/api/bin/chainflip-broker-api/src/main.rs b/api/bin/chainflip-broker-api/src/main.rs index 62cd7cc8795..430ef783131 100644 --- a/api/bin/chainflip-broker-api/src/main.rs +++ b/api/bin/chainflip-broker-api/src/main.rs @@ -91,7 +91,7 @@ pub trait Rpc { channel_metadata: Option, boost_fee: Option, affiliate_fees: Option>, - refund_parameters: Option, + refund_parameters: RefundParametersRpc, dca_parameters: Option, ) -> RpcResult; @@ -189,7 +189,7 @@ impl RpcServer for RpcServerImpl { channel_metadata: Option, boost_fee: Option, affiliate_fees: Option>, - refund_parameters: Option, + refund_parameters: RefundParametersRpc, dca_parameters: Option, ) -> RpcResult { Ok(self diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index d18ff5a247e..c1da1c3af79 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -4,8 +4,8 @@ use anyhow::{anyhow, bail, Context, Result}; use async_trait::async_trait; pub use cf_chains::{address::AddressString, RefundParametersRpc}; use cf_chains::{ - evm::to_evm_address, CcmChannelMetadata, Chain, ChainCrypto, ChannelRefundParameters, - ChannelRefundParametersEncoded, ForeignChain, + evm::to_evm_address, CcmChannelMetadata, Chain, ChainCrypto, ChannelRefundParametersEncoded, + ForeignChain, }; use cf_primitives::DcaParameters; pub use cf_primitives::{AccountRole, Affiliates, Asset, BasisPoints, ChannelId, SemVer}; @@ -316,7 +316,7 @@ pub struct SwapDepositAddress { pub channel_id: ChannelId, pub source_chain_expiry_block: NumberOrHex, pub channel_opening_fee: U256, - pub refund_parameters: Option, + pub refund_parameters: RefundParametersRpc, } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -373,12 +373,21 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st channel_metadata: Option, boost_fee: Option, affiliate_fees: Option>, - refund_parameters: Option, + refund_parameters: RefundParametersRpc, dca_parameters: Option, ) -> Result { let destination_address = destination_address .try_parse_to_encoded_address(destination_asset.into()) .map_err(anyhow::Error::msg)?; + + let internal_refund_parameters = ChannelRefundParametersEncoded { + retry_duration: refund_parameters.retry_duration, + refund_address: refund_parameters + .refund_address + .try_parse_to_encoded_address(source_asset.into()) + .map_err(anyhow::Error::msg)?, + min_price: refund_parameters.min_price, + }; let (_tx_hash, events, header, ..) = self .submit_signed_extrinsic_with_dry_run( pallet_cf_swapping::Call::request_swap_deposit_address_with_affiliates { @@ -389,17 +398,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st channel_metadata, boost_fee: boost_fee.unwrap_or_default(), affiliate_fees: affiliate_fees.unwrap_or_default(), - refund_parameters: refund_parameters - .map(|rpc_params: ChannelRefundParameters| { - Ok::<_, anyhow::Error>(ChannelRefundParametersEncoded { - retry_duration: rpc_params.retry_duration, - refund_address: rpc_params - .refund_address - .try_parse_to_encoded_address(source_asset.into())?, - min_price: rpc_params.min_price, - }) - }) - .transpose()?, + refund_parameters: internal_refund_parameters, dca_parameters, }, ) @@ -425,11 +424,10 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st channel_id: *channel_id, source_chain_expiry_block: (*source_chain_expiry_block).into(), channel_opening_fee: (*channel_opening_fee).into(), - refund_parameters: refund_parameters.as_ref().map(|params| { - params.map_address(|refund_address| { + refund_parameters: refund_parameters + .map_address(|refund_address| { AddressString::from_encoded_address(&refund_address) - }) - }), + }), } ) } diff --git a/state-chain/pallets/cf-ingress-egress/src/lib.rs b/state-chain/pallets/cf-ingress-egress/src/lib.rs index 07f91b94734..d9575cb0a0f 100644 --- a/state-chain/pallets/cf-ingress-egress/src/lib.rs +++ b/state-chain/pallets/cf-ingress-egress/src/lib.rs @@ -238,7 +238,7 @@ use deposit_origin::DepositOrigin; #[derive(RuntimeDebug, PartialEq, Eq, Encode, Decode, TypeInfo, CloneNoBound)] #[scale_info(skip_type_params(T, I))] pub struct TransactionRejectionDetails, I: 'static> { - pub refund_address: Option, + pub refund_address: ForeignChainAddress, pub asset: TargetChainAsset, pub amount: TargetChainAmount, pub deposit_details: ::DepositDetails, @@ -469,12 +469,12 @@ pub mod pallet { destination_address: ForeignChainAddress, broker_fees: Beneficiaries, channel_metadata: Option, - refund_params: Option>, + refund_params: ChannelRefundParameters, dca_params: Option, }, LiquidityProvision { lp_account: AccountId, - refund_address: Option, + refund_address: ForeignChainAddress, }, } @@ -1112,7 +1112,7 @@ pub mod pallet { } for tx in ScheduledTransactionsForRejection::::take() { - if let Some(Ok(refund_address)) = tx.refund_address.clone().map(TryInto::try_into) { + if let Ok(refund_address) = tx.refund_address.clone().try_into() { if let Ok(api_call) = >::new_unsigned( tx.deposit_details.clone(), @@ -1132,11 +1132,6 @@ pub mod pallet { tx_id: tx.deposit_details, }); } - } else { - FailedRejections::::append(tx.clone()); - Self::deposit_event(Event::::TransactionRejectionFailed { - tx_id: tx.deposit_details, - }); } } } @@ -1887,7 +1882,7 @@ impl, I: 'static> Pallet { output_address: destination_address, }, broker_fees, - refund_params, + Some(refund_params), dca_params, origin.into(), ); @@ -2209,9 +2204,8 @@ impl, I: 'static> Pallet { destination_asset: output_asset, destination_address: destination_address_internal, broker_fees, - refund_params: Some( - refund_params.map_address(|address| address.into_foreign_chain_address()), - ), + refund_params: refund_params + .map_address(|address| address.into_foreign_chain_address()), dca_params, channel_metadata, }; @@ -2262,9 +2256,8 @@ impl, I: 'static> Pallet { // since by boosting the protocol is committing to accept the deposit. if TransactionsMarkedForRejection::::take(broker_id, &tx_id).is_some() { let refund_address = match &action { - ChannelAction::Swap { refund_params, .. } => refund_params - .as_ref() - .map(|refund_params| refund_params.refund_address.clone()), + ChannelAction::Swap { refund_params, .. } => + refund_params.refund_address.clone(), ChannelAction::LiquidityProvision { refund_address, .. } => refund_address.clone(), }; @@ -2517,9 +2510,8 @@ impl, I: 'static> Pallet { destination_address: destination_address_internal, broker_fees, channel_metadata: channel_metadata.clone(), - refund_params: Some( - refund_params.map_address(|address| address.into_foreign_chain_address()), - ), + refund_params: refund_params + .map_address(|address| address.into_foreign_chain_address()), dca_params: dca_params.clone(), }; @@ -2900,10 +2892,7 @@ impl, I: 'static> DepositApi for Pallet { let (channel_id, deposit_address, expiry_block, channel_opening_fee) = Self::open_channel( &lp_account, source_asset, - ChannelAction::LiquidityProvision { - lp_account: lp_account.clone(), - refund_address: Some(refund_address), - }, + ChannelAction::LiquidityProvision { lp_account: lp_account.clone(), refund_address }, boost_fee, )?; @@ -2924,15 +2913,13 @@ impl, I: 'static> DepositApi for Pallet { broker_id: T::AccountId, channel_metadata: Option, boost_fee: BasisPoints, - refund_params: Option, + refund_params: ChannelRefundParametersDecoded, dca_params: Option, ) -> Result< (ChannelId, ForeignChainAddress, ::ChainBlockNumber, Self::Amount), DispatchError, > { - if let Some(params) = &refund_params { - T::SwapLimitsProvider::validate_refund_params(params.retry_duration)?; - } + T::SwapLimitsProvider::validate_refund_params(refund_params.retry_duration)?; if let Some(params) = &dca_params { T::SwapLimitsProvider::validate_dca_params(params)?; } diff --git a/state-chain/pallets/cf-swapping/src/lib.rs b/state-chain/pallets/cf-swapping/src/lib.rs index a9ace7fb553..22cfb32e5c3 100644 --- a/state-chain/pallets/cf-swapping/src/lib.rs +++ b/state-chain/pallets/cf-swapping/src/lib.rs @@ -602,7 +602,7 @@ pub mod pallet { boost_fee: BasisPoints, channel_opening_fee: T::Amount, affiliate_fees: Affiliates, - refund_parameters: Option, + refund_parameters: ChannelRefundParametersEncoded, dca_parameters: Option, }, /// A swap is scheduled for the first time @@ -898,6 +898,7 @@ pub mod pallet { broker_commission: BasisPoints, channel_metadata: Option, boost_fee: BasisPoints, + refund_parameters: ChannelRefundParametersEncoded, ) -> DispatchResult { Self::request_swap_deposit_address_with_affiliates( origin, @@ -910,7 +911,7 @@ pub mod pallet { Default::default(), // This extrinsic is for backwards compatibility and does not support new // features like FoK or DCA - None, + refund_parameters, None, ) } @@ -1078,7 +1079,7 @@ pub mod pallet { channel_metadata: Option, boost_fee: BasisPoints, affiliate_fees: Affiliates, - refund_parameters: Option, + refund_parameters: ChannelRefundParametersEncoded, dca_parameters: Option, ) -> DispatchResult { let broker = T::AccountRoleRegistry::ensure_broker(origin)?; @@ -1105,15 +1106,10 @@ pub mod pallet { .map_err(address_error_to_pallet_error::)?; // Convert the refund parameter from `EncodedAddress` into `ForeignChainAddress` type. - let refund_params_internal = refund_parameters - .clone() - .map(|params| { - params.try_map_address(|addr| { - T::AddressConverter::try_from_encoded_address(addr) - .map_err(|_| Error::::InvalidRefundAddress.into()) - }) - }) - .transpose()?; + let refund_params_internal = refund_parameters.clone().try_map_address(|addr| { + T::AddressConverter::try_from_encoded_address(addr) + .map_err(|_| Error::::InvalidRefundAddress.into()) + })?; if let Some(ccm) = channel_metadata.as_ref() { let destination_chain: ForeignChain = destination_asset.into(); diff --git a/state-chain/runtime/src/chainflip.rs b/state-chain/runtime/src/chainflip.rs index 7ddb31e61f4..76341a0852a 100644 --- a/state-chain/runtime/src/chainflip.rs +++ b/state-chain/runtime/src/chainflip.rs @@ -696,7 +696,7 @@ macro_rules! impl_deposit_api_for_anychain { broker_id: Self::AccountId, channel_metadata: Option, boost_fee: BasisPoints, - refund_parameters: Option, + refund_parameters: ChannelRefundParametersDecoded, dca_parameters: Option, ) -> Result<(ChannelId, ForeignChainAddress, ::ChainBlockNumber, FlipBalance), DispatchError> { match source_asset.into() { diff --git a/state-chain/traits/src/lib.rs b/state-chain/traits/src/lib.rs index 85e5bc956a0..cf0038511ca 100644 --- a/state-chain/traits/src/lib.rs +++ b/state-chain/traits/src/lib.rs @@ -748,7 +748,7 @@ pub trait DepositApi { broker_id: Self::AccountId, channel_metadata: Option, boost_fee: BasisPoints, - refund_params: Option, + refund_params: ChannelRefundParametersDecoded, dca_params: Option, ) -> Result<(ChannelId, ForeignChainAddress, C::ChainBlockNumber, Self::Amount), DispatchError>; } diff --git a/state-chain/traits/src/mocks/deposit_handler.rs b/state-chain/traits/src/mocks/deposit_handler.rs index 997dccd819d..f16ecbfa1fb 100644 --- a/state-chain/traits/src/mocks/deposit_handler.rs +++ b/state-chain/traits/src/mocks/deposit_handler.rs @@ -125,7 +125,7 @@ impl DepositApi for MockDepositHandler { broker_id: Self::AccountId, channel_metadata: Option, boost_fee: BasisPoints, - _refund_params: Option, + _refund_params: ChannelRefundParametersDecoded, _dca_params: Option, ) -> Result< (cf_primitives::ChannelId, ForeignChainAddress, C::ChainBlockNumber, Self::Amount),