Skip to content

Commit 8d4b190

Browse files
committed
WIP: on chain swaps
1 parent 793a689 commit 8d4b190

File tree

15 files changed

+513
-207
lines changed

15 files changed

+513
-207
lines changed

state-chain/chains/src/lib.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,55 @@ pub struct ChannelRefundParameters<A> {
905905
pub min_price: Price,
906906
}
907907

908+
#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen, PartialOrd, Ord)]
909+
pub struct RefundParametersExtendedGeneric<Address, AccountId> {
910+
pub retry_duration: cf_primitives::BlockNumber,
911+
pub refund_destination: RefundDestination<Address, AccountId>,
912+
pub min_price: Price,
913+
}
914+
915+
pub type RefundParametersExtended<AccountId> =
916+
RefundParametersExtendedGeneric<ForeignChainAddress, AccountId>;
917+
pub type RefundParametersExtendedEncoded<AccountId> =
918+
RefundParametersExtendedGeneric<EncodedAddress, AccountId>;
919+
920+
impl<AccountId> RefundParametersExtended<AccountId> {
921+
pub fn to_encoded<Converter: AddressConverter>(
922+
self,
923+
) -> RefundParametersExtendedEncoded<AccountId> {
924+
RefundParametersExtendedEncoded {
925+
retry_duration: self.retry_duration,
926+
refund_destination: match self.refund_destination {
927+
RefundDestination::ExternalAddress(address) =>
928+
RefundDestination::ExternalAddress(Converter::to_encoded_address(address)),
929+
RefundDestination::OnChainAccount(account_id) =>
930+
RefundDestination::OnChainAccount(account_id),
931+
},
932+
min_price: self.min_price,
933+
}
934+
}
935+
936+
pub fn min_output_amount(&self, input_amount: AssetAmount) -> AssetAmount {
937+
use sp_runtime::traits::UniqueSaturatedInto;
938+
cf_amm_math::output_amount_ceil(input_amount.into(), self.min_price).unique_saturated_into()
939+
}
940+
}
941+
942+
#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen, PartialOrd, Ord)]
943+
pub enum RefundDestination<Address, AccountId> {
944+
ExternalAddress(Address),
945+
OnChainAccount(AccountId),
946+
}
947+
948+
#[cfg(feature = "runtime-benchmarks")]
949+
impl<Address: BenchmarkValue, AccountId: BenchmarkValue> BenchmarkValue
950+
for RefundDestination<Address, AccountId>
951+
{
952+
fn benchmark_value() -> Self {
953+
RefundDestination::ExternalAddress(BenchmarkValue::benchmark_value())
954+
}
955+
}
956+
908957
#[cfg(feature = "runtime-benchmarks")]
909958
impl<A: BenchmarkValue> BenchmarkValue for ChannelRefundParameters<A> {
910959
fn benchmark_value() -> Self {
@@ -915,6 +964,7 @@ impl<A: BenchmarkValue> BenchmarkValue for ChannelRefundParameters<A> {
915964
}
916965
}
917966
}
967+
918968
#[cfg(feature = "std")]
919969
pub type RefundParametersRpc = ChannelRefundParameters<crate::address::AddressString>;
920970
pub type ChannelRefundParametersDecoded = ChannelRefundParameters<ForeignChainAddress>;
@@ -938,10 +988,6 @@ impl<A: Clone> ChannelRefundParameters<A> {
938988
min_price: self.min_price,
939989
})
940990
}
941-
pub fn min_output_amount(&self, input_amount: AssetAmount) -> AssetAmount {
942-
use sp_runtime::traits::UniqueSaturatedInto;
943-
cf_amm_math::output_amount_ceil(input_amount.into(), self.min_price).unique_saturated_into()
944-
}
945991
}
946992

947993
pub enum RequiresSignatureRefresh<C: ChainCrypto, Api: ApiCall<C>> {

state-chain/pallets/cf-ingress-egress/src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use cf_chains::{
3030
AllBatch, AllBatchError, CcmAdditionalData, CcmChannelMetadata, CcmDepositMetadata, CcmMessage,
3131
Chain, ChainCrypto, ChannelLifecycleHooks, ChannelRefundParametersDecoded, ConsolidateCall,
3232
DepositChannel, DepositDetailsToTransactionInId, DepositOriginType, ExecutexSwapAndCall,
33-
FetchAssetParams, ForeignChainAddress, IntoTransactionInIdForAnyChain, RejectCall, SwapOrigin,
34-
TransferAssetParams,
33+
FetchAssetParams, ForeignChainAddress, IntoTransactionInIdForAnyChain,
34+
RefundParametersExtended, RejectCall, SwapOrigin, TransferAssetParams,
3535
};
3636
use cf_primitives::{
3737
AccountRole, AffiliateShortId, Affiliates, Asset, AssetAmount, BasisPoints, Beneficiaries,
@@ -46,7 +46,7 @@ use cf_traits::{
4646
ChannelIdAllocator, DepositApi, EgressApi, EpochInfo, FeePayment,
4747
FetchesTransfersLimitProvider, GetBlockHeight, IngressEgressFeeApi, IngressSink, IngressSource,
4848
NetworkEnvironmentProvider, OnDeposit, PoolApi, ScheduledEgressDetails, SwapLimitsProvider,
49-
SwapRequestHandler, SwapRequestType,
49+
SwapOutputAction, SwapRequestHandler, SwapRequestType,
5050
};
5151
use frame_support::{
5252
pallet_prelude::{OptionQuery, *},
@@ -1910,11 +1910,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
19101910
amount_after_fees.into(),
19111911
destination_asset,
19121912
SwapRequestType::Regular {
1913-
ccm_deposit_metadata: deposit_metadata,
1914-
output_address: destination_address,
1913+
output_action: SwapOutputAction::Egress {
1914+
ccm_deposit_metadata: deposit_metadata,
1915+
output_address: destination_address,
1916+
},
19151917
},
19161918
broker_fees,
1917-
refund_params,
1919+
refund_params.map(|params| RefundParametersExtended {
1920+
retry_duration: params.retry_duration,
1921+
refund_destination: cf_chains::RefundDestination::ExternalAddress(
1922+
params.refund_address,
1923+
),
1924+
min_price: params.min_price,
1925+
}),
19181926
dca_params,
19191927
origin.into(),
19201928
);

state-chain/pallets/cf-ingress-egress/src/tests.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use cf_traits::{
4141
swap_request_api::{MockSwapRequest, MockSwapRequestHandler},
4242
},
4343
BalanceApi, DepositApi, EgressApi, EpochInfo, FetchesTransfersLimitProvider, FundingInfo,
44-
GetBlockHeight, SafeMode, ScheduledEgressDetails, SwapRequestType,
44+
GetBlockHeight, SafeMode, ScheduledEgressDetails, SwapOutputAction, SwapRequestType,
4545
};
4646
use frame_support::{
4747
assert_err, assert_noop, assert_ok,
@@ -1865,7 +1865,12 @@ fn can_request_swap_via_extrinsic() {
18651865
input_asset: INPUT_ASSET,
18661866
output_asset: OUTPUT_ASSET,
18671867
input_amount: INPUT_AMOUNT,
1868-
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
1868+
swap_type: SwapRequestType::Regular {
1869+
output_action: SwapOutputAction::Egress {
1870+
output_address,
1871+
ccm_deposit_metadata: None
1872+
}
1873+
},
18691874
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 0 }],
18701875
origin: SwapOrigin::Vault {
18711876
tx_id: TransactionInIdForAnyChain::Evm(H256::default()),
@@ -1925,7 +1930,12 @@ fn vault_swaps_support_affiliate_fees() {
19251930
input_asset: INPUT_ASSET,
19261931
output_asset: OUTPUT_ASSET,
19271932
input_amount: INPUT_AMOUNT,
1928-
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
1933+
swap_type: SwapRequestType::Regular {
1934+
output_action: SwapOutputAction::Egress {
1935+
output_address,
1936+
ccm_deposit_metadata: None
1937+
}
1938+
},
19291939
broker_fees: bounded_vec![
19301940
Beneficiary { account: BROKER, bps: BROKER_FEE },
19311941
// Only one affiliate is used (short id for affiliate 2 has not been
@@ -1982,7 +1992,12 @@ fn charge_no_broker_fees_on_unknown_primary_broker() {
19821992
input_asset: INPUT_ASSET,
19831993
output_asset: OUTPUT_ASSET,
19841994
input_amount: INPUT_AMOUNT,
1985-
swap_type: SwapRequestType::Regular { output_address, ccm_deposit_metadata: None },
1995+
swap_type: SwapRequestType::Regular {
1996+
output_action: SwapOutputAction::Egress {
1997+
output_address,
1998+
ccm_deposit_metadata: None
1999+
}
2000+
},
19862001
broker_fees: Default::default(),
19872002
origin: SwapOrigin::Vault {
19882003
tx_id: cf_chains::TransactionInIdForAnyChain::Evm(H256::default()),
@@ -2040,8 +2055,10 @@ fn can_request_ccm_swap_via_extrinsic() {
20402055
output_asset: OUTPUT_ASSET,
20412056
input_amount: INPUT_AMOUNT,
20422057
swap_type: SwapRequestType::Regular {
2043-
output_address,
2044-
ccm_deposit_metadata: Some(ccm_deposit_metadata)
2058+
output_action: SwapOutputAction::Egress {
2059+
output_address,
2060+
ccm_deposit_metadata: Some(ccm_deposit_metadata)
2061+
}
20452062
},
20462063
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 0 }],
20472064
origin: SwapOrigin::Vault {

state-chain/pallets/cf-ingress-egress/src/tests/boost.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,8 @@ fn taking_network_fee_from_boost_fee() {
11501150

11511151
mod vault_swaps {
11521152

1153+
use cf_traits::SwapOutputAction;
1154+
11531155
use crate::BoostedVaultTransactions;
11541156

11551157
use super::*;
@@ -1229,8 +1231,10 @@ mod vault_swaps {
12291231
// Note that ingress fee is not charged:
12301232
input_amount: DEPOSIT_AMOUNT - BOOST_FEE,
12311233
swap_type: SwapRequestType::Regular {
1232-
output_address,
1233-
ccm_deposit_metadata: None
1234+
output_action: SwapOutputAction::Egress {
1235+
output_address,
1236+
ccm_deposit_metadata: None
1237+
}
12341238
},
12351239
broker_fees: bounded_vec![Beneficiary { account: BROKER, bps: 5 }],
12361240
origin: SwapOrigin::Vault {

0 commit comments

Comments
 (0)