Skip to content

Commit

Permalink
WIP: on chain swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
msgmaxim committed Jan 30, 2025
1 parent 793a689 commit 8d4b190
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 207 deletions.
54 changes: 50 additions & 4 deletions state-chain/chains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,55 @@ pub struct ChannelRefundParameters<A> {
pub min_price: Price,
}

#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen, PartialOrd, Ord)]
pub struct RefundParametersExtendedGeneric<Address, AccountId> {
pub retry_duration: cf_primitives::BlockNumber,
pub refund_destination: RefundDestination<Address, AccountId>,
pub min_price: Price,
}

pub type RefundParametersExtended<AccountId> =
RefundParametersExtendedGeneric<ForeignChainAddress, AccountId>;
pub type RefundParametersExtendedEncoded<AccountId> =
RefundParametersExtendedGeneric<EncodedAddress, AccountId>;

impl<AccountId> RefundParametersExtended<AccountId> {
pub fn to_encoded<Converter: AddressConverter>(
self,
) -> RefundParametersExtendedEncoded<AccountId> {
RefundParametersExtendedEncoded {
retry_duration: self.retry_duration,
refund_destination: match self.refund_destination {
RefundDestination::ExternalAddress(address) =>
RefundDestination::ExternalAddress(Converter::to_encoded_address(address)),
RefundDestination::OnChainAccount(account_id) =>
RefundDestination::OnChainAccount(account_id),
},
min_price: self.min_price,
}
}

pub fn min_output_amount(&self, input_amount: AssetAmount) -> AssetAmount {
use sp_runtime::traits::UniqueSaturatedInto;
cf_amm_math::output_amount_ceil(input_amount.into(), self.min_price).unique_saturated_into()
}
}

#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen, PartialOrd, Ord)]
pub enum RefundDestination<Address, AccountId> {
ExternalAddress(Address),
OnChainAccount(AccountId),
}

#[cfg(feature = "runtime-benchmarks")]
impl<Address: BenchmarkValue, AccountId: BenchmarkValue> BenchmarkValue
for RefundDestination<Address, AccountId>
{
fn benchmark_value() -> Self {
RefundDestination::ExternalAddress(BenchmarkValue::benchmark_value())
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<A: BenchmarkValue> BenchmarkValue for ChannelRefundParameters<A> {
fn benchmark_value() -> Self {
Expand All @@ -915,6 +964,7 @@ impl<A: BenchmarkValue> BenchmarkValue for ChannelRefundParameters<A> {
}
}
}

#[cfg(feature = "std")]
pub type RefundParametersRpc = ChannelRefundParameters<crate::address::AddressString>;
pub type ChannelRefundParametersDecoded = ChannelRefundParameters<ForeignChainAddress>;
Expand All @@ -938,10 +988,6 @@ impl<A: Clone> ChannelRefundParameters<A> {
min_price: self.min_price,
})
}
pub fn min_output_amount(&self, input_amount: AssetAmount) -> AssetAmount {
use sp_runtime::traits::UniqueSaturatedInto;
cf_amm_math::output_amount_ceil(input_amount.into(), self.min_price).unique_saturated_into()
}
}

pub enum RequiresSignatureRefresh<C: ChainCrypto, Api: ApiCall<C>> {
Expand Down
20 changes: 14 additions & 6 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use cf_chains::{
AllBatch, AllBatchError, CcmAdditionalData, CcmChannelMetadata, CcmDepositMetadata, CcmMessage,
Chain, ChainCrypto, ChannelLifecycleHooks, ChannelRefundParametersDecoded, ConsolidateCall,
DepositChannel, DepositDetailsToTransactionInId, DepositOriginType, ExecutexSwapAndCall,
FetchAssetParams, ForeignChainAddress, IntoTransactionInIdForAnyChain, RejectCall, SwapOrigin,
TransferAssetParams,
FetchAssetParams, ForeignChainAddress, IntoTransactionInIdForAnyChain,
RefundParametersExtended, RejectCall, SwapOrigin, TransferAssetParams,
};
use cf_primitives::{
AccountRole, AffiliateShortId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiaries,
Expand All @@ -46,7 +46,7 @@ use cf_traits::{
ChannelIdAllocator, DepositApi, EgressApi, EpochInfo, FeePayment,
FetchesTransfersLimitProvider, GetBlockHeight, IngressEgressFeeApi, IngressSink, IngressSource,
NetworkEnvironmentProvider, OnDeposit, PoolApi, ScheduledEgressDetails, SwapLimitsProvider,
SwapRequestHandler, SwapRequestType,
SwapOutputAction, SwapRequestHandler, SwapRequestType,
};
use frame_support::{
pallet_prelude::{OptionQuery, *},
Expand Down Expand Up @@ -1910,11 +1910,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
amount_after_fees.into(),
destination_asset,
SwapRequestType::Regular {
ccm_deposit_metadata: deposit_metadata,
output_address: destination_address,
output_action: SwapOutputAction::Egress {
ccm_deposit_metadata: deposit_metadata,
output_address: destination_address,
},
},
broker_fees,
refund_params,
refund_params.map(|params| RefundParametersExtended {
retry_duration: params.retry_duration,
refund_destination: cf_chains::RefundDestination::ExternalAddress(
params.refund_address,
),
min_price: params.min_price,
}),
dca_params,
origin.into(),
);
Expand Down
29 changes: 23 additions & 6 deletions state-chain/pallets/cf-ingress-egress/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use cf_traits::{
swap_request_api::{MockSwapRequest, MockSwapRequestHandler},
},
BalanceApi, DepositApi, EgressApi, EpochInfo, FetchesTransfersLimitProvider, FundingInfo,
GetBlockHeight, SafeMode, ScheduledEgressDetails, SwapRequestType,
GetBlockHeight, SafeMode, ScheduledEgressDetails, SwapOutputAction, SwapRequestType,
};
use frame_support::{
assert_err, assert_noop, assert_ok,
Expand Down Expand Up @@ -1865,7 +1865,12 @@ fn can_request_swap_via_extrinsic() {
input_asset: INPUT_ASSET,
output_asset: OUTPUT_ASSET,
input_amount: INPUT_AMOUNT,
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
swap_type: SwapRequestType::Regular {
output_action: SwapOutputAction::Egress {
output_address,
ccm_deposit_metadata: None
}
},
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 0 }],
origin: SwapOrigin::Vault {
tx_id: TransactionInIdForAnyChain::Evm(H256::default()),
Expand Down Expand Up @@ -1925,7 +1930,12 @@ fn vault_swaps_support_affiliate_fees() {
input_asset: INPUT_ASSET,
output_asset: OUTPUT_ASSET,
input_amount: INPUT_AMOUNT,
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
swap_type: SwapRequestType::Regular {
output_action: SwapOutputAction::Egress {
output_address,
ccm_deposit_metadata: None
}
},
broker_fees: bounded_vec![
Beneficiary { account: BROKER, bps: BROKER_FEE },
// Only one affiliate is used (short id for affiliate 2 has not been
Expand Down Expand Up @@ -1982,7 +1992,12 @@ fn charge_no_broker_fees_on_unknown_primary_broker() {
input_asset: INPUT_ASSET,
output_asset: OUTPUT_ASSET,
input_amount: INPUT_AMOUNT,
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
swap_type: SwapRequestType::Regular {
output_action: SwapOutputAction::Egress {
output_address,
ccm_deposit_metadata: None
}
},
broker_fees: Default::default(),
origin: SwapOrigin::Vault {
tx_id: cf_chains::TransactionInIdForAnyChain::Evm(H256::default()),
Expand Down Expand Up @@ -2040,8 +2055,10 @@ fn can_request_ccm_swap_via_extrinsic() {
output_asset: OUTPUT_ASSET,
input_amount: INPUT_AMOUNT,
swap_type: SwapRequestType::Regular {
output_address,
ccm_deposit_metadata: Some(ccm_deposit_metadata)
output_action: SwapOutputAction::Egress {
output_address,
ccm_deposit_metadata: Some(ccm_deposit_metadata)
}
},
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 0 }],
origin: SwapOrigin::Vault {
Expand Down
8 changes: 6 additions & 2 deletions state-chain/pallets/cf-ingress-egress/src/tests/boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ fn taking_network_fee_from_boost_fee() {

mod vault_swaps {

use cf_traits::SwapOutputAction;

use crate::BoostedVaultTransactions;

use super::*;
Expand Down Expand Up @@ -1229,8 +1231,10 @@ mod vault_swaps {
// Note that ingress fee is not charged:
input_amount: DEPOSIT_AMOUNT - BOOST_FEE,
swap_type: SwapRequestType::Regular {
output_address,
ccm_deposit_metadata: None
output_action: SwapOutputAction::Egress {
output_address,
ccm_deposit_metadata: None
}
},
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 5 }],
origin: SwapOrigin::Vault {
Expand Down
Loading

0 comments on commit 8d4b190

Please sign in to comment.