From 7abb11dfce89dcee5e5b741367375c65cfd009a6 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 2 Oct 2023 11:49:47 +0100 Subject: [PATCH] fix: update optional fields for tests --- tests/integration/test_trading.py | 34 ++++++-- tests/integration/utils/fixtures.py | 2 +- vega_sim/service.py | 121 ++++++++++++++++++++++------ 3 files changed, 124 insertions(+), 33 deletions(-) diff --git a/tests/integration/test_trading.py b/tests/integration/test_trading.py index 54409fe80..537d8b66b 100644 --- a/tests/integration/test_trading.py +++ b/tests/integration/test_trading.py @@ -286,25 +286,47 @@ def test_recurring_transfer(vega_service_with_market: VegaServiceNull): @pytest.mark.integration def test_funding_reward_pool(vega_service_with_market: VegaServiceNull): vega = vega_service_with_market - vega.wait_for_total_catchup() + market_id = vega.all_markets()[0].id + asset_id = vega.find_asset_id(symbol=ASSET_NAME, raise_on_missing=True) create_and_faucet_wallet(vega=vega, wallet=PARTY_A, amount=1e3) - create_and_faucet_wallet(vega=vega, wallet=LIQ, amount=1e5) create_and_faucet_wallet(vega=vega, wallet=PARTY_B, amount=1e5) create_and_faucet_wallet(vega=vega, wallet=PARTY_C, amount=1e5) vega.wait_for_total_catchup() - asset_id = vega.find_asset_id(symbol=ASSET_NAME, raise_on_missing=True) + # Forward one epoch + next_epoch(vega=vega) vega.recurring_transfer( from_key_name=PARTY_A.name, - to_key_name=PARTY_B.name, from_account_type=vega_protos.vega.ACCOUNT_TYPE_GENERAL, - to_account_type=vega_protos.vega.ACCOUNT_TYPE_GENERAL, + to_account_type=vega_protos.vega.ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES, asset=asset_id, + asset_for_metric=asset_id, + metric=vega_protos.vega.DISPATCH_METRIC_MAKER_FEES_PAID, amount=100, factor=1.0, ) + # Generate trades for non-zero metrics + vega.submit_order( + trading_key=PARTY_B.name, + market_id=market_id, + order_type="TYPE_LIMIT", + time_in_force="TIME_IN_FORCE_GTC", + side="SIDE_SELL", + price=0.30, + volume=10000, + ) + vega.submit_order( + trading_key=PARTY_C.name, + market_id=market_id, + order_type="TYPE_LIMIT", + time_in_force="TIME_IN_FORCE_GTC", + side="SIDE_BUY", + price=0.30, + volume=10000, + ) + vega.wait_for_total_catchup() party_a_accounts_t0 = vega.list_accounts(key_name=PARTY_A.name, asset_id=asset_id) @@ -322,7 +344,7 @@ def test_funding_reward_pool(vega_service_with_market: VegaServiceNull): party_a_accounts_t2 = vega.list_accounts(key_name=PARTY_A.name, asset_id=asset_id) - assert party_a_accounts_t2[0].balance == 799.8 + assert party_a_accounts_t2[0].balance == 899.9 @pytest.mark.integration diff --git a/tests/integration/utils/fixtures.py b/tests/integration/utils/fixtures.py index 2342c67c6..9762f1f47 100644 --- a/tests/integration/utils/fixtures.py +++ b/tests/integration/utils/fixtures.py @@ -167,7 +167,7 @@ def vega_service(): retain_log_files=True, transactions_per_block=1, listen_for_high_volume_stream_updates=False, - use_full_vega_wallet=False, + use_full_vega_wallet=True, ) as vega: yield vega logging.debug("vega_service teardown") diff --git a/vega_sim/service.py b/vega_sim/service.py index 7114c4157..f94691fa7 100644 --- a/vega_sim/service.py +++ b/vega_sim/service.py @@ -2274,15 +2274,15 @@ def recurring_transfer( asset_for_metric: Optional[str] = None, metric: Optional[vega_protos.vega.DispatchMetric] = None, markets: Optional[List[str]] = None, - entity_scope: vega_protos.vega.EntityScope = vega_protos.vega.ENTITY_SCOPE_INDIVIDUALS, - individual_scope: vega_protos.vega.IndividualScope = vega_protos.vega.INDIVIDUAL_SCOPE_ALL, + entity_scope: Optional[vega_protos.vega.EntityScope] = None, + individual_scope: Optional[vega_protos.vega.IndividualScope] = None, team_scope: Optional[List[str]] = None, n_top_performers: Optional[float] = None, staking_requirement: Optional[float] = None, notional_time_weighted_average_position_requirement: Optional[float] = None, - window_length: int = 1, - lock_period: int = 1, - distribution_strategy: vega_protos.vega.DistributionStrategy = vega_protos.vega.DISTRIBUTION_STRATEGY_PRO_RATA, + window_length: Optional[int] = None, + lock_period: Optional[int] = None, + distribution_strategy: Optional[vega_protos.vega.DistributionStrategy] = None, rank_table: Optional[List[vega_protos.vega.Rank]] = None, ): """Create a recurring transfer of funds. @@ -2348,9 +2348,97 @@ def recurring_transfer( if val is not None: setattr(recurring_transfer, attr, val) - # Set the mandatory DispatchStrategy fields + # If any dispatch strategy field is set, try and create a dispatch strategy + if any( + [ + arg is not None + for arg in [ + entity_scope, + window_length, + lock_period, + distribution_strategy, + metric, + asset_for_metric, + individual_scope, + n_top_performers, + staking_requirement, + notional_time_weighted_average_position_requirement, + markets, + team_scope, + ] + ] + ): + dispatch_strategy = self.dispatch_strategy( + asset_for_metric=asset_for_metric, + metric=metric, + markets=markets, + entity_scope=entity_scope, + individual_scope=individual_scope, + team_scope=team_scope, + n_top_performers=n_top_performers, + staking_requirement=staking_requirement, + notional_time_weighted_average_position_requirement=notional_time_weighted_average_position_requirement, + window_length=window_length, + lock_period=lock_period, + distribution_strategy=distribution_strategy, + rank_table=rank_table, + ) + recurring_transfer.dispatch_strategy.CopyFrom(dispatch_strategy) + + trading.transfer( + wallet=self.wallet, + wallet_name=from_wallet_name, + key_name=from_key_name, + from_account_type=from_account_type, + to=( + self.wallet.public_key(wallet_name=to_wallet_name, name=to_key_name) + if to_key_name is not None + else "0000000000000000000000000000000000000000000000000000000000000000" + ), + to_account_type=to_account_type, + asset=asset, + amount=str(num_to_padded_int(amount, self.asset_decimals[asset])), + reference=reference, + recurring=recurring_transfer, + ) + + def dispatch_strategy( + self, + metric: vega_protos.vega.DispatchMetric, + asset_for_metric: Optional[str] = None, + markets: Optional[List[str]] = None, + entity_scope: Optional[vega_protos.vega.EntityScope] = None, + individual_scope: Optional[vega_protos.vega.IndividualScope] = None, + team_scope: Optional[List[str]] = None, + n_top_performers: Optional[float] = None, + staking_requirement: Optional[float] = None, + notional_time_weighted_average_position_requirement: Optional[float] = None, + window_length: Optional[int] = None, + lock_period: Optional[int] = None, + distribution_strategy: Optional[vega_protos.vega.DistributionStrategy] = None, + rank_table: Optional[List[vega_protos.vega.Rank]] = None, + ) -> vega_protos.vega.DispatchStrategy: + # Set defaults for mandatory fields + if entity_scope is None: + entity_scope = vega_protos.vega.ENTITY_SCOPE_INDIVIDUALS + if individual_scope is None: + individual_scope = vega_protos.vega.INDIVIDUAL_SCOPE_ALL + if distribution_strategy is None: + distribution_strategy = vega_protos.vega.DISTRIBUTION_STRATEGY_PRO_RATA + if window_length is None: + window_length = 1 + if lock_period is None: + lock_period = 1 + + # Set the mandatory (and conditionally mandatory) DispatchStrategy fields dispatch_strategy = vega_protos.vega.DispatchStrategy( + asset_for_metric=None + if metric in [vega_protos.vega.DISPATCH_METRIC_VALIDATOR_RANKING] + else asset_for_metric, entity_scope=entity_scope, + individual_scope=individual_scope + if entity_scope is vega_protos.vega.ENTITY_SCOPE_INDIVIDUALS + else None, window_length=None if metric in [vega_protos.vega.DISPATCH_METRIC_MARKET_VALUE] else window_length, @@ -2362,8 +2450,6 @@ def recurring_transfer( # Set the optional DispatchStrategy fields for attr, val in [ ("metric", metric), - ("asset_for_metric", asset_for_metric), - ("individual_scope", individual_scope), ("n_top_performers", n_top_performers), ("staking_requirement", staking_requirement), ( @@ -2379,24 +2465,7 @@ def recurring_transfer( dispatch_strategy.team_scope.extend(team_scope) if rank_table is not None: dispatch_strategy.rank_table.extend(rank_table) - recurring_transfer.dispatch_strategy.CopyFrom(dispatch_strategy) - - trading.transfer( - wallet=self.wallet, - wallet_name=from_wallet_name, - key_name=from_key_name, - from_account_type=from_account_type, - to=( - self.wallet.public_key(wallet_name=to_wallet_name, name=to_key_name) - if to_key_name is not None - else "0000000000000000000000000000000000000000000000000000000000000000" - ), - to_account_type=to_account_type, - asset=asset, - amount=str(num_to_padded_int(amount, self.asset_decimals[asset])), - reference=reference, - recurring=recurring_transfer, - ) + return dispatch_strategy def list_transfers( self,