vegaprotocol / vega

A Go implementation of the Vega Protocol, a protocol for creating and trading derivatives on a fully decentralised network.
https://vega.xyz
GNU Affero General Public License v3.0
37 stars 22 forks source link

[Bug]: Liquidity provision cancelled when market moves from opening auction to continuous trading #9183

Closed Sohill-Patel closed 1 year ago

Sohill-Patel commented 1 year ago

Problem encountered

When a market moves from opening auction > continuous trading, lp commitment was cancelled by vega (the cancellation was not requested by ST)

Observed behaviour

md events show the market in opening auction with supplied stake = 390500000000000000000000

{"id":"2789-57","block":"4EDC58706C5E6BA5BC7AA87122B514F0EBF28D48E2897B482AC064192677C177","type":"BUS_EVENT_TYPE_MARKET_DATA","marketData":{"markPrice":"0","bestBidPrice":"100100000","bestBidVolume":"500000","bestOfferPrice":"95100000","bestOfferVolume":"500000","bestStaticBidPrice":"100100000","bestStaticBidVolume":"500000","bestStaticOfferPrice":"95100000","bestStaticOfferVolume":"500000","midPrice":"97600000","staticMidPrice":"97600000","market":"3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45","timestamp":"1692840492472578623","auctionEnd":"1692840482000000000","auctionStart":"1692840481000000000","indicativePrice":"97600000","indicativeVolume":"500000","marketTradingMode":"TRADING_MODE_OPENING_AUCTION","trigger":"AUCTION_TRIGGER_OPENING","targetStake":"1349076000000000000000","suppliedStake":"390500000000000000000000","marketValueProxy":"0","liquidityProviderFeeShare":[{"party":"02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f","equityLikeShare":"1","averageEntryValuation":"390500000000000000000000","averageScore":"1","virtualStake":"390500000000000000000000.0000000000000000"}],"marketState":"STATE_PENDING","nextMarkToMarket":"-6795364578871345152","lastTradedPrice":"0","marketGrowth":"0"},"version":1,"chainId":"testnet-001","txHash":"4EDC58706C5E6BA5BC7AA87122B514F0EBF28D48E2897B482AC064192677C177"}

The next event is shows the market moves to continuous trading and now the supplied stake=0

{"id":"2790-227","block":"ACDB030EBA09323AEEC0CADC5E246626574C237BC07418E66F26C4267A51529C","type":"BUS_EVENT_TYPE_MARKET_DATA","marketData":{"markPrice":"97600000","bestBidPrice":"90000000","bestBidVolume":"500000","bestOfferPrice":"120000000","bestOfferVolume":"500000","bestStaticBidPrice":"90000000","bestStaticBidVolume":"500000","bestStaticOfferPrice":"120000000","bestStaticOfferVolume":"500000","midPrice":"105000000","staticMidPrice":"105000000","market":"3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45","timestamp":"1692840492875692564","openInterest":"500000","indicativePrice":"0","marketTradingMode":"TRADING_MODE_CONTINUOUS","targetStake":"1349076000000000000000","suppliedStake":"0","marketValueProxy":"0","marketState":"STATE_ACTIVE","nextMarkToMarket":"1692840492875692564","lastTradedPrice":"97600000","marketGrowth":"0"},"version":1,"chainId":"testnet-001","txHash":"ACDB030EBA09323AEEC0CADC5E246626574C237BC07418E66F26C4267A51529C"}

and this is because in the same block lp commitment got cancelled. The "updatedAt":"1692840492875692564"=01:28:12.875 and looking in the ST logs at that time test was submitting normal orders to move market to continuous trading. There was no lp cancellation requests around that time

{"id":"2790-195","block":"ACDB030EBA09323AEEC0CADC5E246626574C237BC07418E66F26C4267A51529C","type":"BUS_EVENT_TYPE_LIQUIDITY_PROVISION","liquidityProvision":{"id":"ef7abd4c5d006346588d48157fda869b6d9343d3a9e4773e6ead04f396c7c03e","partyId":"02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f","createdAt":"1692840484947254816","updatedAt":"1692840492875692564","marketId":"3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45","commitmentAmount":"390500000000000000000000","fee":"0.3","version":"1","status":"STATUS_CANCELLED","reference":"EFC50251F5CD4A3"},"version":1,"chainId":"testnet-001","txHash":"ACDB030EBA09323AEEC0CADC5E246626574C237BC07418E66F26C4267A51529C"}

Expected behaviour

LP provision should not be cancelled when market moves from opening auction > continuous trading

Steps to reproduce

This issue appeared in nightly run of ST

The test is `test_settlement_trigger_priority` and test parameter `before_termination`

Software version

dev branch

Failing test

test_settlement_trigger_priority

Jenkins run

https://jenkins.ops.vega.xyz/job/common/job/system-tests-nightly/1056/testReport/junit/tests.cash_settled_futures/csf_test/Call_tests___full_a_f___test_settlement_trigger_priority_before_termination_/

Configuration used

No response

Relevant log output

Here is the log of when we submit the lp provision

2023-08-24 01:28:04.884:[INFO] [MainProcess] [MainThread] [   clientv2.py]:[   signtx_v2]:[ 388] Calling jsonrpc client.send_transaction for wallet: B3A701055A, pubkey: 02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f: req: {'liquidityProvisionSubmission': {'marketId': '3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45', 'commitmentAmount': '390500000000000000000000', 'fee': '0.3', 'reference': 'EFC50251F5CD4A3', 'sells': [], 'buys': []}, 'propagate': True, 'pubKey': '02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f'}, epoch: 261, block: 2771
2023-08-24 01:28:04.884:[INFO] [MainProcess] [MainThread] [   clientv2.py]:[     _header]:[  57] using token: 4OtYcR7kSsFynTJgNIk8yKoPtRFixtb9RvejK0uH0DmSxItDB1gAISTlrTYULjil
2023-08-24 01:28:04.887:[INFO] [MainProcess] [MainThread] [   clientv2.py]:[   signtx_v2]:[ 403] response: {'jsonrpc': '2.0', 'result': {'receivedAt': '2023-08-24T01:28:04.886148935Z', 'sentAt': '2023-08-24T01:28:04.886913318Z', 'transactionHash': '046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D', 'transaction': {'input_data': 'CPic8/q54IKnowEQ0xX6PnIKQDM1NjBjNDQ1MDVmNThmNWFiMGNkZjgxNTliZjdjMTQyOGZhMjUxYzg1NjA5ODI1YjBhZjAzZTE3NTZkYjJmNDUSGDM5MDUwMDAwMDAwMDAwMDAwMDAwMDAwMBoDMC4zMg9FRkM1MDI1MUY1Q0Q0QTM=', 'signature': {'value': '428ae6b1cd2f0a71e2da4076886ac4e68c2effb9031527aac4662e31d8a1c06c19361eb7c55cd5d560677fd7b78542d32fc38d1fe56bf70688a19d1516167c0c', 'algo': 'vega/ed25519', 'version': 1}, 'From': {'PubKey': '02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f'}, 'version': 3, 'pow': {'tid': '6a0f7665527edae7c47d26f6538a89fb789eb9a3f4fb407735667fa27c50c295', 'nonce': 3}}}, 'id': 'request'}
2023-08-24 01:28:04.887:[INFO] [MainProcess] [MainThread] [ liqProvMod.py]:[new_provision]:[ 198] New liquidity provision response is: self.new_prov_wallet_response={'receivedAt': '2023-08-24T01:28:04.886148935Z', 'sentAt': '2023-08-24T01:28:04.886913318Z', 'transactionHash': '046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D', 'transaction': {'input_data': 'CPic8/q54IKnowEQ0xX6PnIKQDM1NjBjNDQ1MDVmNThmNWFiMGNkZjgxNTliZjdjMTQyOGZhMjUxYzg1NjA5ODI1YjBhZjAzZTE3NTZkYjJmNDUSGDM5MDUwMDAwMDAwMDAwMDAwMDAwMDAwMBoDMC4zMg9FRkM1MDI1MUY1Q0Q0QTM=', 'signature': {'value': '428ae6b1cd2f0a71e2da4076886ac4e68c2effb9031527aac4662e31d8a1c06c19361eb7c55cd5d560677fd7b78542d32fc38d1fe56bf70688a19d1516167c0c', 'algo': 'vega/ed25519', 'version': 1}, 'From': {'PubKey': '02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f'}, 'version': 3, 'pow': {'tid': '6a0f7665527edae7c47d26f6538a89fb789eb9a3f4fb407735667fa27c50c295', 'nonce': 3}}, 'txHash': '046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D', 'tx': {'input_data': 'CPic8/q54IKnowEQ0xX6PnIKQDM1NjBjNDQ1MDVmNThmNWFiMGNkZjgxNTliZjdjMTQyOGZhMjUxYzg1NjA5ODI1YjBhZjAzZTE3NTZkYjJmNDUSGDM5MDUwMDAwMDAwMDAwMDAwMDAwMDAwMBoDMC4zMg9FRkM1MDI1MUY1Q0Q0QTM=', 'signature': {'value': '428ae6b1cd2f0a71e2da4076886ac4e68c2effb9031527aac4662e31d8a1c06c19361eb7c55cd5d560677fd7b78542d32fc38d1fe56bf70688a19d1516167c0c', 'algo': 'vega/ed25519', 'version': 1}, 'From': {'PubKey': '02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f'}, 'version': 3, 'pow': {'tid': '6a0f7665527edae7c47d26f6538a89fb789eb9a3f4fb407735667fa27c50c295', 'nonce': 3}}} 
2023-08-24 01:28:04.888:[INFO] [MainProcess] [MainThread] [      utils.py]:[get_transaction_result_by_hash]:[1074] Running TX Error check
2023-08-24 01:28:04.888:[INFO] [MainProcess] [MainThread] [      utils.py]:[ scan_events]:[ 517] BUS_EVENT_TYPE_TRANSACTION_RESULT scan - timeout is 9s - filters=nodeID None epoch=None marketId=None trader=None pubkey=None ref=None prop=None state=None tx_hash=None OnlyFirstOrder=True version=None search_list=['046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D'] rows_reqd=None
2023-08-24 01:28:05.718:[INFO] [MainProcess] [MainThread] [      utils.py]:[ scan_events]:[ 810] BUS_EVENT_TYPE_TRANSACTION_RESULT scan - completed - found 1 event(s) in 0.8299453258514404secs
2023-08-24 01:28:05.718:[INFO] [MainProcess] [MainThread] [      utils.py]:[find_failed_transaction_by_hash]:[1086] Transaction result found for hash 046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D, which did not fail.
2023-08-24 01:28:05.718:[INFO] [MainProcess] [MainThread] [      utils.py]:[find_failed_transaction_by_hash]:[1089] No TX Error events with txHash 046A19D82826EE75E26CD969844750D3EAE1403D1DB0B982A39693A383D0EC0D
EVODelavega commented 1 year ago

Does the party have sufficient balance to cover the margin required for their LP orders? This could be one of the cases where we check for provided liquidity, uncross the book, then deploy the LP orders, and only then know/determine that the LP has insufficient balance.

Sohill-Patel commented 1 year ago

@EVODelavega thats exactly whats going on at that time! Everything from General account is transferred to margin then from bond to margin. This is ST issue

{
  "id": "2790-184",
  "block": "ACDB030EBA09323AEEC0CADC5E246626574C237BC07418E66F26C4267A51529C",
  "type": "BUS_EVENT_TYPE_LEDGER_MOVEMENTS",
  "ledgerMovements": {
    "ledgerMovements": [
      {
        "entries": [
          {
            "fromAccount": {
              "assetId": "c5ac040edb250b1a3c5ee536c26c87177303c30d767bacf1f103f0a31ed485c2",
              "type": "ACCOUNT_TYPE_GENERAL",
              "owner": "02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f"
            },
            "toAccount": {
              "assetId": "c5ac040edb250b1a3c5ee536c26c87177303c30d767bacf1f103f0a31ed485c2",
              "type": "ACCOUNT_TYPE_MARGIN",
              "owner": "02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f",
              "marketId": "3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45"
            },
            "amount": "5550067936690876510828000",
            "type": "TRANSFER_TYPE_MARGIN_LOW",
            "timestamp": "1692840492875692564",
            "fromAccountBalance": "0",
            "toAccountBalance": "5574729628651904617648000"
          },
          {
            "fromAccount": {
              "assetId": "c5ac040edb250b1a3c5ee536c26c87177303c30d767bacf1f103f0a31ed485c2",
              "type": "ACCOUNT_TYPE_BOND",
              "owner": "02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f",
              "marketId": "3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45"
            },
            "toAccount": {
              "assetId": "c5ac040edb250b1a3c5ee536c26c87177303c30d767bacf1f103f0a31ed485c2",
              "type": "ACCOUNT_TYPE_MARGIN",
              "owner": "02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f",
              "marketId": "3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45"
            },
            "amount": "390500000000000000000000",
            "type": "TRANSFER_TYPE_MARGIN_LOW",
            "timestamp": "1692840492875692564",
            "fromAccountBalance": "0",
            "toAccountBalance": "5965229628651904617648000"
          }
        ],
        "balances": [
          {
            "account": {
              "assetId": "c5ac040edb250b1a3c5ee536c26c87177303c30d767bacf1f103f0a31ed485c2",
              "type": "ACCOUNT_TYPE_MARGIN",
              "owner": "02232b62c41d3f82432eb780bb72d0c0282492dbe1ce040bdd7e50f38ecbb24f",
              "marketId": "3560c44505f58f5ab0cdf8159bf7c1428fa251c85609825b0af03e1756db2f45"
            },
            "balance": "5940567936690876510828000"
          }
        ]
      },