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

fix: stop LPs without refund addresses for both assets from creating orders in a pool (PRO-896) #4099

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions state-chain/cf-integration-tests/src/swapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ fn new_account(account_id: &AccountId, role: AccountRole) {
System::reset_events();
}

fn register_refund_addressses(account_id: &AccountId) {
for encoded_address in [
EncodedAddress::Eth(Default::default()),
EncodedAddress::Dot(Default::default()),
EncodedAddress::Btc("bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw".as_bytes().to_vec()),
] {
assert_ok!(LiquidityProvider::register_liquidity_refund_address(
RuntimeOrigin::signed(account_id.clone()),
encoded_address
));
}
}

fn credit_account(account_id: &AccountId, asset: Asset, amount: AssetAmount) {
let original_amount =
pallet_cf_lp::FreeBalances::<Runtime>::get(account_id, asset).unwrap_or_default();
Expand Down Expand Up @@ -173,6 +186,8 @@ fn set_limit_order(

fn setup_pool_and_accounts(assets: Vec<Asset>) {
new_account(&DORIS, AccountRole::LiquidityProvider);
register_refund_addressses(&DORIS);

new_account(&ZION, AccountRole::Broker);

for asset in assets {
Expand All @@ -190,6 +205,7 @@ fn basic_pool_setup_provision_and_swap() {
new_pool(Asset::Flip, 0u32, price_at_tick(0).unwrap());

new_account(&DORIS, AccountRole::LiquidityProvider);
register_refund_addressses(&DORIS);
credit_account(&DORIS, Asset::Eth, 1_000_000);
credit_account(&DORIS, Asset::Flip, 1_000_000);
credit_account(&DORIS, Asset::Usdc, 1_000_000);
Expand Down
24 changes: 24 additions & 0 deletions state-chain/pallets/cf-lp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ pub mod pallet {
impl<T: Config> LpBalanceApi for Pallet<T> {
type AccountId = <T as frame_system::Config>::AccountId;

#[cfg(feature = "runtime-benchmarks")]
fn register_liquidity_refund_address(
account_id: &Self::AccountId,
address: ForeignChainAddress,
) {
LiquidityRefundAddress::<T>::insert(account_id, address.chain(), address);
}

fn ensure_has_refund_address_for_pair(
account_id: &Self::AccountId,
base_asset: Asset,
pair_asset: Asset,
) -> DispatchResult {
ensure!(
LiquidityRefundAddress::<T>::contains_key(account_id, ForeignChain::from(base_asset)) &&
LiquidityRefundAddress::<T>::contains_key(
account_id,
ForeignChain::from(pair_asset)
),
Error::<T>::NoLiquidityRefundAddressRegistered
);
Ok(())
}

fn try_credit_account(
account_id: &Self::AccountId,
asset: Asset,
Expand Down
3 changes: 3 additions & 0 deletions state-chain/pallets/cf-pools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets = ['x86_64-unknown-linux-gnu']
[dependencies]
# Internal dependencies
cf-amm = { path = '../../amm', default-features = false }
cf-chains = { path = '../../chains', default-features = false }
cf-primitives = { path = '../../primitives', default-features = false }
cf-traits = { path = '../../traits', default-features = false }

Expand Down Expand Up @@ -48,6 +49,7 @@ cf-test-utilities = { path = '../../test-utilities' }
default = ['std']
std = [
'cf-amm/std',
'cf-chains/std',
'cf-primitives/std',
'cf-traits/std',
'codec/std',
Expand All @@ -61,6 +63,7 @@ std = [
'serde/std',
]
runtime-benchmarks = [
'cf-chains/runtime-benchmarks',
'cf-primitives/runtime-benchmarks',
'cf-traits/runtime-benchmarks',
'frame-benchmarking/runtime-benchmarks',
Expand Down
10 changes: 9 additions & 1 deletion state-chain/pallets/cf-pools/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::*;
use cf_amm::common::price_at_tick;
use cf_chains::ForeignChainAddress;
use cf_primitives::Asset;
use cf_traits::{AccountRoleRegistry, LpBalanceApi};
use frame_benchmarking::{benchmarks, whitelisted_caller};
Expand All @@ -13,10 +14,17 @@ use frame_support::{
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};

fn new_lp_account<T: Chainflip>() -> T::AccountId {
fn new_lp_account<T: Chainflip + Config>() -> T::AccountId {
let caller: T::AccountId = whitelisted_caller();
<T as frame_system::Config>::OnNewAccount::on_new_account(&caller);
T::AccountRoleRegistry::register_as_liquidity_provider(&caller).unwrap();
for address in [
ForeignChainAddress::Eth(Default::default()),
ForeignChainAddress::Dot(Default::default()),
ForeignChainAddress::Btc(cf_chains::btc::ScriptPubkey::P2PKH(Default::default())),
] {
T::LpBalance::register_liquidity_refund_address(&caller, address);
}
caller
}

Expand Down
4 changes: 4 additions & 0 deletions state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ pub mod pallet {
Error::<T>::UpdatingRangeOrdersDisabled
);
let lp = T::AccountRoleRegistry::ensure_liquidity_provider(origin)?;
T::LpBalance::ensure_has_refund_address_for_pair(&lp, base_asset, pair_asset)?;
Self::try_mutate_enabled_pool(base_asset, pair_asset, |asset_pair, pool| {
let tick_range = match (
pool.range_orders
Expand Down Expand Up @@ -742,6 +743,7 @@ pub mod pallet {
Error::<T>::UpdatingRangeOrdersDisabled
);
let lp = T::AccountRoleRegistry::ensure_liquidity_provider(origin)?;
T::LpBalance::ensure_has_refund_address_for_pair(&lp, base_asset, pair_asset)?;
Self::try_mutate_enabled_pool(base_asset, pair_asset, |asset_pair, pool| {
let tick_range = match (
pool.range_orders
Expand Down Expand Up @@ -814,6 +816,7 @@ pub mod pallet {
Error::<T>::UpdatingLimitOrdersDisabled
);
let lp = T::AccountRoleRegistry::ensure_liquidity_provider(origin)?;
T::LpBalance::ensure_has_refund_address_for_pair(&lp, sell_asset, buy_asset)?;
Self::try_mutate_enabled_pool(sell_asset, buy_asset, |asset_pair, pool| {
let tick = match (
pool.limit_orders[asset_pair.base_side]
Expand Down Expand Up @@ -889,6 +892,7 @@ pub mod pallet {
Error::<T>::UpdatingLimitOrdersDisabled
);
let lp = T::AccountRoleRegistry::ensure_liquidity_provider(origin)?;
T::LpBalance::ensure_has_refund_address_for_pair(&lp, sell_asset, buy_asset)?;
Self::try_mutate_enabled_pool(sell_asset, buy_asset, |asset_pair, pool| {
let tick = match (
pool.limit_orders[asset_pair.base_side]
Expand Down
23 changes: 23 additions & 0 deletions state-chain/traits/src/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ pub trait SwapDepositHandler {
pub trait LpBalanceApi {
type AccountId;

#[cfg(feature = "runtime-benchmarks")]
fn register_liquidity_refund_address(who: &Self::AccountId, address: ForeignChainAddress);

fn ensure_has_refund_address_for_pair(
who: &Self::AccountId,
base_asset: Asset,
pair_asset: Asset,
) -> DispatchResult;

/// Attempt to credit the account with the given asset and amount.
fn try_credit_account(
who: &Self::AccountId,
Expand Down Expand Up @@ -67,6 +76,20 @@ impl<T: frame_system::Config> SwappingApi for T {
impl<T: frame_system::Config> LpBalanceApi for T {
type AccountId = T::AccountId;

#[cfg(feature = "runtime-benchmarks")]
fn register_liquidity_refund_address(_who: &Self::AccountId, _address: ForeignChainAddress) {
// TODO
}

fn ensure_has_refund_address_for_pair(
_who: &Self::AccountId,
_base_asset: Asset,
_pair_asset: Asset,
) -> DispatchResult {
// TODO
Ok(())
}

fn try_credit_account(
_who: &Self::AccountId,
_asset: Asset,
Expand Down