Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Core DCA #5106

Merged
merged 31 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0b80187
fix: read and remove request state on refunding
msgmaxim Jul 30, 2024
cac0630
feat: DCA works for non-CCM swaps
msgmaxim Jul 30, 2024
73593e7
feat: DCA full refund
msgmaxim Jul 31, 2024
c031439
feat: DCA partial refund
msgmaxim Jul 31, 2024
d7f2c3f
refactor: use egress_for_swap for CCM
msgmaxim Jul 31, 2024
899cccf
test: dca_with_fok_fully_executed
msgmaxim Aug 1, 2024
e0dcf0b
feat: rename fee->egress_fee in SwapEgressScheduled
msgmaxim Aug 1, 2024
7a66f1f
feat: add dca_parameters to the extrinsic
msgmaxim Aug 1, 2024
30e1915
feat: dca parameters in channel actions + migration
msgmaxim Aug 2, 2024
e9dc8a8
chore: clippy fix
msgmaxim Aug 2, 2024
a132930
feat: DCA support for CCM swaps
msgmaxim Aug 6, 2024
cfb2c01
chore: address minor review comments
msgmaxim Aug 7, 2024
f450573
refactor: deduplicate SwapRequestComplited
msgmaxim Aug 7, 2024
bc02334
Merge branch 'main' into feat/dca
msgmaxim Aug 7, 2024
a74e647
fix: use chunk interval for DCA
msgmaxim Aug 8, 2024
c71f870
refactor: prepare_next_chunk returns Option
msgmaxim Aug 8, 2024
e3b7b88
refactor: prepare_next_chunks records prev chunk output
msgmaxim Aug 9, 2024
221f053
chore: refund_params getter
msgmaxim Aug 9, 2024
7d5a996
fix: ccm transfers should charge broker fee
dandanlen Aug 7, 2024
3edafdb
fix: migration for ccm broker fee
msgmaxim Aug 9, 2024
7af1e38
chore: address minor review comments
msgmaxim Aug 12, 2024
7589dac
feat: try-runtime migration test for DCA
msgmaxim Aug 12, 2024
f15f4fa
Merge branch 'main' into feat/dca
msgmaxim Aug 13, 2024
2719d61
test: check dca state in tests
msgmaxim Aug 13, 2024
5ebc809
test: use swap rate other than 1 in swapping tests
msgmaxim Aug 13, 2024
5ed5175
feat: FoK lifetime per chunk
msgmaxim Aug 14, 2024
4e844a2
Merge branch 'main' into feat/dca
msgmaxim Aug 15, 2024
5efcce6
refactor: aliases for ChannelRefundParameters
msgmaxim Aug 15, 2024
9064003
revert: refund_block
msgmaxim Aug 16, 2024
100467b
Merge branch 'main' into feat/dca
msgmaxim Aug 16, 2024
5e4bd9c
chore: fix migration
dandanlen Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/bin/chainflip-broker-api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cf_utilities::task_scope::{task_scope, Scope};
use chainflip_api::{
self,
primitives::{AccountRole, Affiliates, Asset, BasisPoints, CcmChannelMetadata},
primitives::{AccountRole, Affiliates, Asset, BasisPoints, CcmChannelMetadata, DcaParameters},
settings::StateChain,
AccountId32, AddressString, BrokerApi, OperatorApi, RefundParameters, StateChainApi,
SwapDepositAddress, WithdrawFeesDetail,
Expand Down Expand Up @@ -32,6 +32,7 @@ pub trait Rpc {
boost_fee: Option<BasisPoints>,
affiliate_fees: Option<Affiliates<AccountId32>>,
refund_parameters: Option<RefundParameters>,
dca_parameters: Option<DcaParameters>,
) -> RpcResult<SwapDepositAddress>;

#[method(name = "withdraw_fees", aliases = ["broker_withdrawFees"])]
Expand Down Expand Up @@ -79,6 +80,7 @@ impl RpcServer for RpcServerImpl {
boost_fee: Option<BasisPoints>,
affiliate_fees: Option<Affiliates<AccountId32>>,
refund_parameters: Option<RefundParameters>,
dca_parameters: Option<DcaParameters>,
) -> RpcResult<SwapDepositAddress> {
Ok(self
.api
Expand All @@ -92,6 +94,7 @@ impl RpcServer for RpcServerImpl {
boost_fee,
affiliate_fees.unwrap_or_default(),
refund_parameters,
dca_parameters,
)
.await?)
}
Expand Down
1 change: 1 addition & 0 deletions api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async fn run_cli() -> Result<()> {
params.boost_fee,
Default::default(),
None,
None,
Comment on lines 73 to +74
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
.await?;
println!("Deposit Address: {address}");
Expand Down
4 changes: 3 additions & 1 deletion api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cf_chains::{
CcmChannelMetadata, ChannelRefundParameters, ForeignChain, ForeignChainAddress,
};
pub use cf_primitives::{AccountRole, Affiliates, Asset, BasisPoints, ChannelId, SemVer};
use cf_primitives::{BlockNumber, NetworkEnvironment, Price};
use cf_primitives::{BlockNumber, DcaParameters, NetworkEnvironment, Price};
use futures::FutureExt;
use pallet_cf_account_roles::MAX_LENGTH_FOR_VANITY_NAME;
use pallet_cf_governance::ExecutionMode;
Expand Down Expand Up @@ -392,6 +392,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st
boost_fee: Option<BasisPoints>,
affiliate_fees: Affiliates<AccountId32>,
refund_parameters: Option<RefundParameters>,
dca_parameters: Option<DcaParameters>,
) -> Result<SwapDepositAddress> {
let destination_address =
destination_address.try_parse_to_encoded_address(destination_asset.into())?;
Expand Down Expand Up @@ -425,6 +426,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st
})
})
.transpose()?,
dca_parameters,
},
)
.await?
Expand Down
2 changes: 2 additions & 0 deletions state-chain/cf-integration-tests/src/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn schedule_deposit_to_swap(
0u16,
Default::default(),
None,
None,
));

let deposit_address = <AddressDerivation as AddressDerivationApi<Solana>>::generate_address(
Expand Down Expand Up @@ -377,6 +378,7 @@ fn solana_ccm_fails_with_invalid_input() {
0u16,
Default::default(),
None,
None,
),
pallet_cf_swapping::Error::<Runtime>::InvalidCcm,
);
Expand Down
2 changes: 2 additions & 0 deletions state-chain/cf-integration-tests/src/swapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ fn basic_pool_setup_provision_and_swap() {
0u16,
Default::default(),
None,
None,
));

let deposit_address =
Expand Down Expand Up @@ -392,6 +393,7 @@ fn can_process_ccm_via_swap_deposit_address() {
0u16,
Default::default(),
None,
None,
));

// Deposit funds for the ccm.
Expand Down
1 change: 0 additions & 1 deletion state-chain/chains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,6 @@ impl RetryPolicy for DefaultRetryPolicy {
)]
pub struct SwapRefundParameters {
pub refund_block: cf_primitives::BlockNumber,
pub refund_address: ForeignChainAddress,
pub min_output: cf_primitives::AssetAmount,
}

Expand Down
26 changes: 18 additions & 8 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use cf_chains::{
};
use cf_primitives::{
Asset, AssetAmount, BasisPoints, Beneficiaries, BlockNumber, BoostPoolTier, BroadcastId,
ChannelId, EgressCounter, EgressId, EpochIndex, ForeignChain, PrewitnessedDepositId,
SwapRequestId, ThresholdSignatureRequestId, SECONDS_PER_BLOCK,
ChannelId, DcaParameters, EgressCounter, EgressId, EpochIndex, ForeignChain,
PrewitnessedDepositId, SwapRequestId, ThresholdSignatureRequestId, SECONDS_PER_BLOCK,
};
use cf_runtime_utilities::log_or_panic;
use cf_traits::{
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<C: Chain> CrossChainMessage<C> {
}
}

pub const PALLET_VERSION: StorageVersion = StorageVersion::new(11);
pub const PALLET_VERSION: StorageVersion = StorageVersion::new(12);

impl_pallet_safe_mode! {
PalletSafeMode<I>;
Expand Down Expand Up @@ -294,6 +294,7 @@ pub mod pallet {
destination_address: ForeignChainAddress,
broker_fees: Beneficiaries<AccountId>,
refund_params: Option<ChannelRefundParameters>,
dca_params: Option<DcaParameters>,
},
LiquidityProvision {
lp_account: AccountId,
Expand All @@ -303,6 +304,7 @@ pub mod pallet {
destination_address: ForeignChainAddress,
channel_metadata: CcmChannelMetadata,
refund_params: Option<ChannelRefundParameters>,
dca_params: Option<DcaParameters>,
},
}

Expand Down Expand Up @@ -1577,6 +1579,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
destination_asset,
broker_fees,
refund_params,
dca_params,
dandanlen marked this conversation as resolved.
Show resolved Hide resolved
} => {
if let Ok(swap_request_id) = T::SwapRequestHandler::init_swap_request(
asset.into(),
Expand All @@ -1585,6 +1588,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
SwapRequestType::Regular { output_address: destination_address },
broker_fees,
refund_params,
dca_params,
swap_origin,
) {
DepositAction::Swap { swap_request_id }
Expand All @@ -1597,6 +1601,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
destination_address,
channel_metadata,
refund_params,
dca_params,
} => {
if let Ok(swap_request_id) = T::SwapRequestHandler::init_swap_request(
asset.into(),
Expand All @@ -1612,6 +1617,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
},
Default::default(),
refund_params,
dca_params,
swap_origin,
) {
DepositAction::CcmTransfer { swap_request_id }
Expand Down Expand Up @@ -1939,7 +1945,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
<T::TargetChain as Chain>::GAS_ASSET.into(),
SwapRequestType::IngressEgressFee,
Default::default(),
None, /* refund params */
None, /* no refund params */
None, /* no DCA */
SwapOrigin::Internal,
) {
log_or_panic!("Ingress-egress fee swap should never fail");
Expand Down Expand Up @@ -2078,13 +2085,14 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
channel_metadata: Option<CcmChannelMetadata>,
boost_fee: BasisPoints,
refund_params: Option<ChannelRefundParameters>,
dca_params: Option<DcaParameters>,
) -> Result<
(ChannelId, ForeignChainAddress, <T::TargetChain as Chain>::ChainBlockNumber, Self::Amount),
DispatchError,
> {
if let Some(refund_params) = &refund_params {
if let Some(params) = &refund_params {
ensure!(
refund_params.retry_duration <= MaxSwapRetryDurationBlocks::<T, I>::get(),
params.retry_duration <= MaxSwapRetryDurationBlocks::<T, I>::get(),
DispatchError::Other("Retry duration too long")
);
}
Expand All @@ -2093,17 +2101,19 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
&broker_id,
source_asset,
match channel_metadata {
Some(msg) => ChannelAction::CcmTransfer {
Some(channel_metadata) => ChannelAction::CcmTransfer {
destination_asset,
destination_address,
channel_metadata: msg,
channel_metadata,
refund_params,
dca_params,
},
None => ChannelAction::Swap {
destination_asset,
destination_address,
broker_fees,
refund_params,
dca_params,
},
},
boost_fee,
Expand Down
5 changes: 4 additions & 1 deletion state-chain/pallets/cf-ingress-egress/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ mod add_refund_params;
pub mod remove_prewitnessed_deposits;
pub mod withheld_transaction_fees;

mod add_dca_params;

pub type PalletMigration<T, I> = (
VersionedMigration<Pallet<T, I>, remove_prewitnessed_deposits::Migration<T, I>, 8, 9>,
VersionedMigration<Pallet<T, I>, add_refund_params::Migration<T, I>, 9, 10>,
VersionedMigration<Pallet<T, I>, withheld_transaction_fees::Migration<T, I>, 10, 11>,
PlaceholderMigration<Pallet<T, I>, 11>,
VersionedMigration<Pallet<T, I>, add_dca_params::Migration<T, I>, 11, 12>,
PlaceholderMigration<Pallet<T, I>, 12>,
);
Loading
Loading