Skip to content

Commit bd860b9

Browse files
committed
chore: address review
1 parent a876c17 commit bd860b9

File tree

9 files changed

+51
-42
lines changed

9 files changed

+51
-42
lines changed

state-chain/chains/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ pub enum SwapOrigin<AccountId> {
682682
broker_id: Option<AccountId>,
683683
},
684684
Internal,
685+
OnChainAccount(AccountId),
685686
}
686687

687688
impl<AccountId> SwapOrigin<AccountId> {
@@ -690,6 +691,7 @@ impl<AccountId> SwapOrigin<AccountId> {
690691
Self::DepositChannel { ref broker_id, .. } => Some(broker_id),
691692
Self::Vault { ref broker_id, .. } => broker_id.as_ref(),
692693
Self::Internal => None,
694+
Self::OnChainAccount(_) => None,
693695
}
694696
}
695697
}

state-chain/pallets/cf-lp/src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,12 @@ impl<T: Config> LpRegistration for Pallet<T> {
424424
LiquidityRefundAddress::<T>::insert(account_id, address.chain(), address);
425425
}
426426

427-
fn ensure_has_refund_address_for_pair(
427+
fn ensure_has_refund_address_for_asset(
428428
account_id: &Self::AccountId,
429-
base_asset: Asset,
430-
quote_asset: Asset,
429+
asset: Asset,
431430
) -> DispatchResult {
432431
ensure!(
433-
LiquidityRefundAddress::<T>::contains_key(account_id, ForeignChain::from(base_asset)) &&
434-
LiquidityRefundAddress::<T>::contains_key(
435-
account_id,
436-
ForeignChain::from(quote_asset)
437-
),
432+
LiquidityRefundAddress::<T>::contains_key(account_id, ForeignChain::from(asset)),
438433
Error::<T>::NoLiquidityRefundAddressRegistered
439434
);
440435
Ok(())

state-chain/pallets/cf-pools/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,8 @@ impl<T: Config> Pallet<T> {
16981698
quote_asset: any::Asset,
16991699
f: F,
17001700
) -> Result<R, DispatchError> {
1701-
T::LpRegistrationApi::ensure_has_refund_address_for_pair(lp, base_asset, quote_asset)?;
1701+
T::LpRegistrationApi::ensure_has_refund_address_for_asset(lp, base_asset)?;
1702+
T::LpRegistrationApi::ensure_has_refund_address_for_asset(lp, quote_asset)?;
17021703
let asset_pair = AssetPair::try_new::<T>(base_asset, quote_asset)?;
17031704
Self::inner_sweep(lp)?;
17041705
Self::try_mutate_pool(asset_pair, f)

state-chain/pallets/cf-swapping/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use cf_primitives::{
1717
use cf_runtime_utilities::log_or_panic;
1818
use cf_traits::{
1919
impl_pallet_safe_mode, AffiliateRegistry, BalanceApi, Bonding, ChannelIdAllocator, DepositApi,
20-
FundingInfo, IngressEgressFeeApi, SwapLimitsProvider, SwapOutputAction, SwapRequestHandler,
21-
SwapRequestType, SwapRequestTypeEncoded, SwapType, SwappingApi,
20+
FundingInfo, IngressEgressFeeApi, LpRegistration, SwapLimitsProvider, SwapOutputAction,
21+
SwapRequestHandler, SwapRequestType, SwapRequestTypeEncoded, SwapType, SwappingApi,
2222
};
2323
use frame_support::{
2424
pallet_prelude::*,
@@ -391,7 +391,7 @@ pub mod pallet {
391391
AffiliateShortId, Asset, AssetAmount, BasisPoints, BlockNumber, DcaParameters, EgressId,
392392
SwapId, SwapOutput, SwapRequestId,
393393
};
394-
use cf_traits::{AccountRoleRegistry, Chainflip, EgressApi, ScheduledEgressDetails};
394+
use cf_traits::{AccountRoleRegistry, Chainflip, EgressApi, PoolApi, ScheduledEgressDetails};
395395
use frame_system::WeightInfo as SystemWeightInfo;
396396
use sp_runtime::SaturatedConversion;
397397

@@ -442,6 +442,10 @@ pub mod pallet {
442442
/// The balance API for interacting with the asset-balance pallet.
443443
type BalanceApi: BalanceApi<AccountId = <Self as frame_system::Config>::AccountId>;
444444

445+
type LpRegistrationApi: LpRegistration<AccountId = Self::AccountId>;
446+
447+
type PoolApi: PoolApi<AccountId = <Self as frame_system::Config>::AccountId>;
448+
445449
type ChannelIdAllocator: ChannelIdAllocator;
446450

447451
type Bonder: Bonding<
@@ -1220,9 +1224,11 @@ pub mod pallet {
12201224
min_price: Price,
12211225
dca_params: Option<DcaParameters>,
12221226
) -> DispatchResult {
1223-
let account_id = ensure_signed(origin)?;
1227+
let account_id = T::AccountRoleRegistry::ensure_liquidity_provider(origin)?;
1228+
1229+
T::LpRegistrationApi::ensure_has_refund_address_for_asset(&account_id, output_asset)?;
12241230

1225-
// Do we need to sweep?
1231+
T::PoolApi::sweep(&account_id)?;
12261232

12271233
T::BalanceApi::try_debit_account(&account_id, input_asset, amount)?;
12281234

@@ -1238,11 +1244,11 @@ pub mod pallet {
12381244
Default::default(), /* no broker fees */
12391245
Some(RefundParametersExtended {
12401246
retry_duration,
1241-
refund_destination: RefundDestination::OnChainAccount(account_id),
1247+
refund_destination: RefundDestination::OnChainAccount(account_id.clone()),
12421248
min_price,
12431249
}),
12441250
dca_params,
1245-
SwapOrigin::Internal,
1251+
SwapOrigin::OnChainAccount(account_id),
12461252
);
12471253

12481254
Ok(())

state-chain/pallets/cf-swapping/src/mock.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ use cf_traits::mocks::fee_payment::MockFeePayment;
88
use cf_traits::{
99
impl_mock_chainflip, impl_mock_runtime_safe_mode,
1010
mocks::{
11-
address_converter::MockAddressConverter, balance_api::MockBalance, bonding::MockBonderFor,
12-
deposit_handler::MockDepositHandler, egress_handler::MockEgressHandler,
11+
address_converter::MockAddressConverter,
12+
balance_api::{MockBalance, MockLpRegistration},
13+
bonding::MockBonderFor,
14+
deposit_handler::MockDepositHandler,
15+
egress_handler::MockEgressHandler,
1316
ingress_egress_fee_handler::MockIngressEgressFeeHandler,
1417
},
1518
AccountRoleRegistry, ChannelIdAllocator, SwappingApi,
@@ -180,6 +183,8 @@ impl pallet_cf_swapping::Config for Test {
180183
type FeePayment = MockFeePayment<Self>;
181184
type IngressEgressFeeHandler = MockIngressEgressFeeHandler<AnyChain>;
182185
type BalanceApi = MockBalance;
186+
type PoolApi = Self;
187+
type LpRegistrationApi = MockLpRegistration;
183188
type CcmValidityChecker = AlwaysValid;
184189
type NetworkFee = NetworkFee;
185190
type ChannelIdAllocator = MockChannelIdAllocator;
@@ -189,11 +194,15 @@ impl pallet_cf_swapping::Config for Test {
189194
pub const ALICE: <Test as frame_system::Config>::AccountId = 123u64;
190195
pub const BOB: <Test as frame_system::Config>::AccountId = 789u64;
191196
pub const BROKER: <Test as frame_system::Config>::AccountId = 456u64;
197+
pub const LP_ACCOUNT: u64 = 119;
192198

193199
cf_test_utilities::impl_test_helpers! {
194200
Test,
195201
RuntimeGenesisConfig::default(),
196202
|| {
197203
<MockAccountRoleRegistry as AccountRoleRegistry<Test>>::register_as_broker(&BROKER).unwrap();
204+
<MockAccountRoleRegistry as AccountRoleRegistry<Test>>::register_as_liquidity_provider(
205+
&LP_ACCOUNT.into(),
206+
).unwrap();
198207
},
199208
}

state-chain/pallets/cf-swapping/src/tests.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ mod swap_batching {
16491649
}
16501650
}
16511651

1652+
#[cfg(test)]
16521653
mod on_chain_swapping {
16531654

16541655
use cf_traits::{mocks::balance_api::MockBalance, SwapOutputActionEncoded};
@@ -1658,8 +1659,6 @@ mod on_chain_swapping {
16581659
const INPUT_ASSET: Asset = Asset::Eth;
16591660
const OUTPUT_ASSET: Asset = Asset::Flip;
16601661

1661-
const ACCOUNT_ID: u64 = 3;
1662-
16631662
const INPUT_AMOUNT: AssetAmount = 1000;
16641663

16651664
#[test]
@@ -1672,10 +1671,10 @@ mod on_chain_swapping {
16721671

16731672
new_test_ext()
16741673
.execute_with(|| {
1675-
MockBalance::credit_account(&ACCOUNT_ID, INPUT_ASSET, INPUT_AMOUNT);
1674+
MockBalance::credit_account(&LP_ACCOUNT, INPUT_ASSET, INPUT_AMOUNT);
16761675

16771676
assert_ok!(Swapping::internal_swap(
1678-
RuntimeOrigin::signed(ACCOUNT_ID),
1677+
RuntimeOrigin::signed(LP_ACCOUNT),
16791678
INPUT_AMOUNT,
16801679
INPUT_ASSET,
16811680
OUTPUT_ASSET,
@@ -1693,15 +1692,15 @@ mod on_chain_swapping {
16931692
origin: SwapOrigin::Internal,
16941693
request_type: SwapRequestTypeEncoded::Regular {
16951694
output_action: SwapOutputActionEncoded::CreditOnChain {
1696-
account_id: ACCOUNT_ID
1695+
account_id: LP_ACCOUNT
16971696
}
16981697
},
16991698
..
17001699
})
17011700
);
17021701

1703-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, INPUT_ASSET), 0);
1704-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, OUTPUT_ASSET), 0);
1702+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), 0);
1703+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET), 0);
17051704
})
17061705
.then_process_blocks_until_block(SWAP_BLOCK)
17071706
.then_execute_with(|_| {
@@ -1710,9 +1709,9 @@ mod on_chain_swapping {
17101709
RuntimeEvent::Swapping(Event::SwapExecuted { .. }),
17111710
);
17121711

1713-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, INPUT_ASSET), 0);
1712+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), 0);
17141713
assert_eq!(
1715-
MockBalance::get_balance(&ACCOUNT_ID, OUTPUT_ASSET),
1714+
MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET),
17161715
EXPECTED_OUTPUT_AMOUNT
17171716
);
17181717
});
@@ -1729,10 +1728,10 @@ mod on_chain_swapping {
17291728

17301729
new_test_ext()
17311730
.execute_with(|| {
1732-
MockBalance::credit_account(&ACCOUNT_ID, INPUT_ASSET, INPUT_AMOUNT);
1731+
MockBalance::credit_account(&LP_ACCOUNT, INPUT_ASSET, INPUT_AMOUNT);
17331732

17341733
assert_ok!(Swapping::internal_swap(
1735-
RuntimeOrigin::signed(ACCOUNT_ID),
1734+
RuntimeOrigin::signed(LP_ACCOUNT),
17361735
INPUT_AMOUNT,
17371736
INPUT_ASSET,
17381737
OUTPUT_ASSET,
@@ -1750,15 +1749,15 @@ mod on_chain_swapping {
17501749
origin: SwapOrigin::Internal,
17511750
request_type: SwapRequestTypeEncoded::Regular {
17521751
output_action: SwapOutputActionEncoded::CreditOnChain {
1753-
account_id: ACCOUNT_ID
1752+
account_id: LP_ACCOUNT
17541753
}
17551754
},
17561755
..
17571756
})
17581757
);
17591758

1760-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, INPUT_ASSET), 0);
1761-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, OUTPUT_ASSET), 0);
1759+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), 0);
1760+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET), 0);
17621761
})
17631762
.then_process_blocks_until_block(CHUNK_1_BLOCK)
17641763
.then_execute_with(|_| {
@@ -1778,9 +1777,9 @@ mod on_chain_swapping {
17781777
const EXPECTED_OUTPUT_AMOUNT: AssetAmount =
17791778
CHUNK_AMOUNT * DEFAULT_SWAP_RATE * DEFAULT_SWAP_RATE;
17801779

1781-
assert_eq!(MockBalance::get_balance(&ACCOUNT_ID, INPUT_ASSET), CHUNK_AMOUNT);
1780+
assert_eq!(MockBalance::get_balance(&LP_ACCOUNT, INPUT_ASSET), CHUNK_AMOUNT);
17821781
assert_eq!(
1783-
MockBalance::get_balance(&ACCOUNT_ID, OUTPUT_ASSET),
1782+
MockBalance::get_balance(&LP_ACCOUNT, OUTPUT_ASSET),
17841783
EXPECTED_OUTPUT_AMOUNT
17851784
);
17861785
});

state-chain/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ impl pallet_cf_swapping::Config for Runtime {
310310
type CcmValidityChecker = CcmValidityChecker;
311311
type NetworkFee = NetworkFee;
312312
type BalanceApi = AssetBalances;
313+
type PoolApi = LiquidityPools;
314+
type LpRegistrationApi = LiquidityProvider;
313315
type ChannelIdAllocator = BitcoinIngressEgress;
314316
type Bonder = Bonder<Runtime>;
315317
}

state-chain/traits/src/liquidity.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ pub trait LpRegistration {
2424
);
2525

2626
/// Ensure that the given account has a refund address set for the given asset.
27-
fn ensure_has_refund_address_for_pair(
28-
who: &Self::AccountId,
29-
base_asset: Asset,
30-
quote_asset: Asset,
31-
) -> DispatchResult;
27+
fn ensure_has_refund_address_for_asset(who: &Self::AccountId, asset: Asset) -> DispatchResult;
3228
}
3329

3430
pub trait HistoricalFeeMigration {

state-chain/traits/src/mocks/balance_api.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ impl LpRegistration for MockLpRegistration {
9191
#[cfg(feature = "runtime-benchmarks")]
9292
fn register_liquidity_refund_address(_: &Self::AccountId, _: cf_chains::ForeignChainAddress) {}
9393

94-
fn ensure_has_refund_address_for_pair(
94+
fn ensure_has_refund_address_for_asset(
9595
_who: &Self::AccountId,
96-
_base_asset: Asset,
97-
_quote_asset: Asset,
96+
_asset: Asset,
9897
) -> DispatchResult {
9998
Ok(())
10099
}

0 commit comments

Comments
 (0)