Skip to content

Commit

Permalink
feat: Adding ability to cancel liquidity provision (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomMcL authored Nov 16, 2023
1 parent e93f7df commit 720d9d8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 20 deletions.
12 changes: 8 additions & 4 deletions examples/nullchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,17 @@

# suspend market
print(
f"market state: {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
"market state:"
f" {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
)
update_type = MarketStateUpdateType.Suspend
print(f"submitting proposal: {update_type}")
vega.update_market_state(
proposal_key=MM_WALLET.name, market_id=market_id, market_state=update_type
)
print(
f"market state: {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
"market state:"
f" {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
)
# resume market
vega.wait_for_total_catchup()
Expand All @@ -229,7 +231,8 @@
proposal_key=MM_WALLET.name, market_id=market_id, market_state=update_type
)
print(
f"market state: {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
"market state:"
f" {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
)

input("Pausing to observe the market, press Enter to continue.")
Expand All @@ -251,7 +254,8 @@
)
vega.wait_for_total_catchup()
print(
f"market state: {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
"market state:"
f" {vega_protos.markets.Market.State.Name(vega.get_latest_market_data(market_id).market_state)}"
)

vega.forward("10s")
Expand Down
36 changes: 36 additions & 0 deletions vega_sim/api/trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,42 @@ def submit_liquidity(
logger.debug(f"Submitted liquidity on market {market_id}")


def cancel_liquidity(
wallet_name: str,
wallet: Wallet,
market_id: str,
key_name: Optional[str] = None,
):
"""Cancel a custom liquidity profile.
Args:
wallet_name:
str, the wallet name performing the action
wallet:
Wallet, wallet client
market_id:
str, The ID of the market to place the commitment on
commitment_amount:
int, The amount in asset decimals of market asset to commit
to liquidity provision
fee:
float, The fee level at which to set the LP fee
(in %, e.g. 0.01 == 1% and 1 == 100%)
key_name:
Optional[str], key name stored in metadata. Defaults to None.
"""

wallet.submit_transaction(
transaction=vega_protos.commands.v1.commands.LiquidityProvisionCancellation(
market_id=market_id,
),
wallet_name=wallet_name,
transaction_type="liquidity_provision_cancellation",
key_name=key_name,
)
logger.debug(f"Cancelled liquidity on market {market_id}")


def pegged_order(
reference: vega_protos.vega.PeggedReference,
offset: str,
Expand Down
23 changes: 19 additions & 4 deletions vega_sim/scenario/fuzzed_markets/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ def __init__(
commitment_factor_min: float = 0.1,
commitment_factor_max: float = 0.6,
initial_asset_mint: float = 1e5,
probability_cancel: float = 0.01,
):
super().__init__(key_name, tag, wallet_name, state_update_freq)

Expand All @@ -621,6 +622,7 @@ def __init__(

self.commitment_factor_min = commitment_factor_min
self.commitment_factor_max = commitment_factor_max
self.probability_cancel = probability_cancel
self.random_state = random_state if random_state is not None else RandomState()
self.initial_asset_mint = initial_asset_mint

Expand Down Expand Up @@ -668,6 +670,14 @@ def _gen_spec(self, side: vega_protos.vega.Side, is_valid: bool):
)

def step(self, vega_state):
if self.random_state.random() < self.probability_cancel:
self.vega.cancel_liquidity(
key_name=self.key_name,
wallet_name=self.wallet_name,
market_id=self.market_id,
)
return

commitment_factor = (
self.random_state.random_sample()
* (self.commitment_factor_max - self.commitment_factor_min)
Expand Down Expand Up @@ -850,7 +860,8 @@ def __init__(

if self.market_config.is_perp() and perp_settlement_data_generator == None:
raise ValueError(
"'perp_settlement_data_generator' must be supplied when 'market_config' indicates a perp market"
"'perp_settlement_data_generator' must be supplied when 'market_config'"
" indicates a perp market"
)

self.settlement_price = settlement_price
Expand Down Expand Up @@ -1147,7 +1158,8 @@ def step(self, vega_state):
):
continue
logging.info(
"All fuzzed UpdateReferralProgram proposals failed, submitting sensible proposal."
"All fuzzed UpdateReferralProgram proposals failed, submitting sensible"
" proposal."
)
self._sensible_proposal()

Expand Down Expand Up @@ -1274,7 +1286,8 @@ def step(self, vega_state):
):
continue
logging.info(
"All fuzzed UpdateReferralProgram proposals failed, submitting sensible proposal."
"All fuzzed UpdateReferralProgram proposals failed, submitting sensible"
" proposal."
)
self._sensible_proposal()

Expand Down Expand Up @@ -1512,5 +1525,7 @@ def step(self, vega_state):

def finalise(self):
logging.debug(
f"Agent {self.name()} proposed {self.accepted_proposals}/{self.proposals} valid governance transfer proposals."
f"Agent {self.name()} proposed"
f" {self.accepted_proposals}/{self.proposals} valid governance transfer"
" proposals."
)
65 changes: 53 additions & 12 deletions vega_sim/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,13 @@ def try_enable_perp_markets(
if not self.get_network_parameter(key=perps_netparam, to_type="int"):
if raise_on_failure:
raise ValueError(
"perps market proposals not allowed by default, allowing via network parameter change failed"
"perps market proposals not allowed by default, allowing via"
" network parameter change failed"
)
else:
logger.info(
f"successfully updated network parameter '{perps_netparam}' to '{desired_value}'"
f"successfully updated network parameter '{perps_netparam}' to"
f" '{desired_value}'"
)

def create_simple_perps_market(
Expand Down Expand Up @@ -1420,7 +1422,8 @@ def submit_termination_and_settlement_data(
oracle_name = filter_key.name

logger.info(
f"Submitting market termination signal and settlement price {settlement_price} for {oracle_name}"
"Submitting market termination signal and settlement price"
f" {settlement_price} for {oracle_name}"
)

gov.submit_termination_and_settlement_data(
Expand Down Expand Up @@ -1865,6 +1868,38 @@ def submit_liquidity(
key_name=key_name,
)

def cancel_liquidity(
self,
key_name: str,
market_id: str,
wallet_name: Optional[str] = None,
):
"""Cancel a custom liquidity profile.
Args:
key_name:
str, the key name performing the action
market_id:
str, The ID of the market to place the commitment on
commitment_amount:
int, The amount in asset decimals of market asset to commit
to liquidity provision
fee:
float, The fee level at which to set the LP fee
(in %, e.g. 0.01 == 1% and 1 == 100%)
is_amendment:
Optional bool, Is the submission an amendment to an existing provision
If None, will query the network to check.
wallet_name:
optional, str name of wallet to use
"""
return trading.cancel_liquidity(
market_id=market_id,
wallet=self.wallet,
wallet_name=wallet_name,
key_name=key_name,
)

def find_market_id(self, name: str, raise_on_missing: bool = False) -> str:
"""Looks up the Market ID of a given market name
Expand Down Expand Up @@ -2578,9 +2613,11 @@ def one_off_transfer(
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=(
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, adp)),
Expand Down Expand Up @@ -3164,9 +3201,11 @@ def get_fees_stats(
market_id=market_id,
asset_id=asset_id,
epoch_seq=epoch_seq,
party_id=self.wallet.public_key(key_name, wallet_name)
if key_name is not None
else None,
party_id=(
self.wallet.public_key(key_name, wallet_name)
if key_name is not None
else None
),
asset_decimals=self.asset_decimals,
)

Expand Down Expand Up @@ -3254,9 +3293,11 @@ def list_teams(
return data.list_teams(
data_client=self.trading_data_client_v2,
team_id=team_id,
party_id=None
if key_name is None
else self.wallet.public_key(name=key_name, wallet_name=wallet_name),
party_id=(
None
if key_name is None
else self.wallet.public_key(name=key_name, wallet_name=wallet_name)
),
)

def list_team_referees(
Expand Down

0 comments on commit 720d9d8

Please sign in to comment.