Skip to content

Commit

Permalink
feat: use encoded address in ccm deposit metadata in events (#5192)
Browse files Browse the repository at this point in the history
* feat: use encoded address in ccm deposit metadata for swap requested event

* refactor: address PR comments

* feat: add fok and dca params to SwapRequested event (#5193)

* feat: add fok and dca params to SwapRequested event

* chore: test dca and fok parameters are in event

* refactor: use impl instead of utility function

---------

Co-authored-by: Maxim Shishmarev <msgmaxim@gmail.com>
  • Loading branch information
j4m1ef0rd and msgmaxim authored Aug 23, 2024
1 parent 428583b commit 18debf4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
21 changes: 17 additions & 4 deletions state-chain/chains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
};
pub use address::ForeignChainAddress;
use address::{
AddressDerivationApi, AddressDerivationError, EncodedAddress, IntoForeignChainAddress,
ToHumanreadableAddress,
AddressConverter, AddressDerivationApi, AddressDerivationError, EncodedAddress,
IntoForeignChainAddress, ToHumanreadableAddress,
};
use cf_primitives::{
AssetAmount, BroadcastId, ChannelId, EgressId, EthAmount, Price, TransactionHash,
Expand Down Expand Up @@ -628,12 +628,25 @@ pub struct CcmChannelMetadata {
}

#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, Serialize, Deserialize)]
pub struct CcmDepositMetadata {
pub struct CcmDepositMetadataGeneric<Address> {
pub source_chain: ForeignChain,
pub source_address: Option<ForeignChainAddress>,
pub source_address: Option<Address>,
pub channel_metadata: CcmChannelMetadata,
}

pub type CcmDepositMetadata = CcmDepositMetadataGeneric<ForeignChainAddress>;
pub type CcmDepositMetadataEncoded = CcmDepositMetadataGeneric<EncodedAddress>;

impl CcmDepositMetadata {
pub fn to_encoded<Converter: AddressConverter>(self) -> CcmDepositMetadataEncoded {
CcmDepositMetadataEncoded {
source_chain: self.source_chain,
source_address: self.source_address.map(Converter::to_encoded_address),
channel_metadata: self.channel_metadata,
}
}
}

#[derive(
PartialEqNoBound,
EqNoBound,
Expand Down
18 changes: 14 additions & 4 deletions state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cf_amm::common::Side;
use cf_chains::{
address::{AddressConverter, ForeignChainAddress},
ccm_checker::CcmValidityCheck,
CcmChannelMetadata, CcmDepositMetadata, ChannelRefundParameters,
CcmChannelMetadata, CcmDepositMetadata, CcmDepositMetadataEncoded, ChannelRefundParameters,
ChannelRefundParametersEncoded, SwapOrigin, SwapRefundParameters,
};
use cf_primitives::{
Expand Down Expand Up @@ -536,6 +536,8 @@ pub mod pallet {
output_asset: Asset,
origin: SwapOrigin,
request_type: SwapRequestTypeEncoded,
refund_parameters: Option<ChannelRefundParametersEncoded>,
dca_parameters: Option<DcaParameters>,
},
SwapRequestCompleted {
swap_request_id: SwapRequestId,
Expand Down Expand Up @@ -615,7 +617,7 @@ pub mod pallet {
CcmFailed {
reason: CcmFailReason,
destination_address: EncodedAddress,
deposit_metadata: CcmDepositMetadata,
deposit_metadata: CcmDepositMetadataEncoded,
origin: SwapOrigin,
},
MaximumSwapAmountSet {
Expand Down Expand Up @@ -2091,10 +2093,16 @@ pub mod pallet {
output_address: T::AddressConverter::to_encoded_address(
output_address.clone(),
),
ccm_deposit_metadata: ccm_deposit_metadata.clone(),
ccm_deposit_metadata: ccm_deposit_metadata
.clone()
.to_encoded::<T::AddressConverter>(),
},
},
origin: origin.clone(),
refund_parameters: refund_params
.clone()
.map(|params| params.map_address(T::AddressConverter::to_encoded_address)),
dca_parameters: dca_params.clone(),
});

match request_type {
Expand Down Expand Up @@ -2200,7 +2208,9 @@ pub mod pallet {
Self::deposit_event(Event::<T>::CcmFailed {
reason,
destination_address: encoded_destination_address,
deposit_metadata: ccm_deposit_metadata.clone(),
deposit_metadata: ccm_deposit_metadata
.clone()
.to_encoded::<T::AddressConverter>(),
origin: origin.clone(),
});

Expand Down
13 changes: 11 additions & 2 deletions state-chain/pallets/cf-swapping/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn assert_failed_ccm(
destination_address: ref address_in_event,
deposit_metadata: ref metadata_in_event,
..
}) if reason_in_event == &reason && address_in_event == &MockAddressConverter::to_encoded_address(destination_address) && metadata_in_event == &ccm,
}) if reason_in_event == &reason && address_in_event == &MockAddressConverter::to_encoded_address(destination_address) && metadata_in_event == &ccm.to_encoded::<<Test as pallet::Config>::AddressConverter>(),
RuntimeEvent::Swapping(Event::SwapRequestCompleted { .. }),
);
}
Expand Down Expand Up @@ -731,9 +731,12 @@ mod ccm {
output_asset,
input_amount,
request_type: SwapRequestTypeEncoded::Ccm {
ccm_deposit_metadata,
ccm_deposit_metadata: ccm_deposit_metadata
.to_encoded::<<Test as pallet::Config>::AddressConverter>(),
output_address: encoded_output_address,
},
dca_parameters: None,
refund_parameters: None,
origin,
}));
}
Expand Down Expand Up @@ -1112,6 +1115,8 @@ fn swap_by_witnesser_happy_path() {
request_type: SwapRequestTypeEncoded::Regular {
output_address: encoded_output_address,
},
refund_parameters: None,
dca_parameters: None,
origin: ORIGIN,
}));

Expand Down Expand Up @@ -2365,6 +2370,8 @@ fn network_fee_swap_gets_burnt() {
input_amount: AMOUNT,
output_asset: OUTPUT_ASSET,
request_type: SwapRequestTypeEncoded::NetworkFee,
refund_parameters: None,
dca_parameters: None,
origin: SwapOrigin::Internal,
}));
assert_has_matching_event!(Test, RuntimeEvent::Swapping(Event::SwapScheduled { .. }),);
Expand Down Expand Up @@ -2405,6 +2412,8 @@ fn transaction_fees_are_collected() {
input_amount: AMOUNT,
output_asset: OUTPUT_ASSET,
request_type: SwapRequestTypeEncoded::IngressEgressFee,
refund_parameters: None,
dca_parameters: None,
origin: SwapOrigin::Internal,
}));

Expand Down
10 changes: 5 additions & 5 deletions state-chain/pallets/cf-swapping/src/tests/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ fn dca_happy_path() {
.execute_with(|| {
assert_eq!(System::block_number(), INIT_BLOCK);

insert_swaps(&[params(
Some(DcaParameters { number_of_chunks: 2, chunk_interval: CHUNK_INTERVAL }),
None,
false,
)]);
const DCA_PARAMS: DcaParameters =
DcaParameters { number_of_chunks: 2, chunk_interval: CHUNK_INTERVAL };

insert_swaps(&[params(Some(DCA_PARAMS), None, false)]);

assert_has_matching_event!(
Test,
RuntimeEvent::Swapping(Event::SwapRequested {
swap_request_id: SWAP_REQUEST_ID,
input_amount: INPUT_AMOUNT,
dca_parameters: Some(DCA_PARAMS),
..
})
);
Expand Down
27 changes: 20 additions & 7 deletions state-chain/pallets/cf-swapping/src/tests/fill_or_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,26 @@ fn both_fok_and_regular_swaps_succeed_first_try() {
.execute_with(|| {
assert_eq!(System::block_number(), INIT_BLOCK);

insert_swaps(&[
fok_swap(None),
fok_swap(Some(TestRefundParams {
retry_duration: DEFAULT_SWAP_RETRY_DELAY_BLOCKS,
min_output: (INPUT_AMOUNT - BROKER_FEE) * DEFAULT_SWAP_RATE,
})),
]);
const REFUND_PARAMS: TestRefundParams = TestRefundParams {
retry_duration: DEFAULT_SWAP_RETRY_DELAY_BLOCKS,
min_output: (INPUT_AMOUNT - BROKER_FEE) * DEFAULT_SWAP_RATE,
};

let refund_parameters_encoded =
REFUND_PARAMS.into_channel_params(INPUT_AMOUNT).map_address(|refund_address| {
MockAddressConverter::to_encoded_address(refund_address)
});

insert_swaps(&vec![fok_swap(None), fok_swap(Some(REFUND_PARAMS))]);

assert_has_matching_event!(
Test,
RuntimeEvent::Swapping(Event::SwapRequested {
swap_request_id: FOK_REQUEST_ID,
refund_parameters,
..
}) if refund_parameters.as_ref() == Some(&refund_parameters_encoded),
);

assert_swaps_scheduled_for_block(
&[REGULAR_SWAP_ID, FOK_SWAP_ID],
Expand Down
6 changes: 4 additions & 2 deletions state-chain/traits/src/swapping.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use cf_chains::{CcmDepositMetadata, ChannelRefundParameters, ForeignChainAddress, SwapOrigin};
use cf_chains::{
CcmDepositMetadataGeneric, ChannelRefundParameters, ForeignChainAddress, SwapOrigin,
};
use cf_primitives::{Asset, AssetAmount, Beneficiaries, DcaParameters, SwapRequestId};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::sp_runtime::DispatchError;
Expand All @@ -18,7 +20,7 @@ pub enum SwapRequestTypeGeneric<Address> {
NetworkFee,
IngressEgressFee,
Regular { output_address: Address },
Ccm { output_address: Address, ccm_deposit_metadata: CcmDepositMetadata },
Ccm { output_address: Address, ccm_deposit_metadata: CcmDepositMetadataGeneric<Address> },
}

pub type SwapRequestType = SwapRequestTypeGeneric<ForeignChainAddress>;
Expand Down

0 comments on commit 18debf4

Please sign in to comment.