code.vegaprotocol.io/vega@v0.79.0/core/integration/features/capped-futures/0016-PFUT-022.feature (about)

     1  Feature: When max_price is specified and the market is ran in a fully-collateralised mode and it has parties with open positions settling it at a price of 0 works correctly
     2  
     3    Background:
     4      Given time is updated to "2019-11-30T00:00:00Z"
     5      And the average block duration is "1"
     6  
     7      And the oracle spec for settlement data filtering data from "0xCAFECAFE1" named "ethDec21Oracle":
     8        | property         | type         | binding         |
     9        | prices.ETH.value | TYPE_INTEGER | settlement data |
    10  
    11      And the oracle spec for trading termination filtering data from "0xCAFECAFE1" named "ethDec21Oracle":
    12        | property           | type         | binding             |
    13        | trading.terminated | TYPE_BOOLEAN | trading termination |
    14  
    15      And the settlement data decimals for the oracle named "ethDec21Oracle" is given in "0" decimal places
    16  
    17      And the following network parameters are set:
    18        | name                                         | value |
    19        | market.auction.minimumDuration               | 1     |
    20        | network.markPriceUpdateMaximumFrequency      | 1s    |
    21        | market.liquidity.successorLaunchWindowLength | 1s    |
    22        | limits.markets.maxPeggedOrders               | 4     |
    23  
    24      And the fees configuration named "fees-config-1":
    25        | maker fee | infrastructure fee |
    26        | 0.005     | 0.02               |
    27      And the price monitoring named "price-monitoring-1":
    28        | horizon | probability | auction extension |
    29        | 3600000 | 0.99        | 300               |
    30      And the log normal risk model named "lognormal-risk-model-1":
    31        | risk aversion | tau  | mu | r   | sigma |
    32        | 0.0002        | 0.01 | 0  | 0.0 | 1.2   |
    33  
    34      And the markets:
    35        | id        | quote name | asset | risk model             | margin calculator                | auction duration | fees          | price monitoring   | data source config | linear slippage factor | quadratic slippage factor | sla params      | max price cap | fully collateralised | binary |
    36        | ETH/DEC21 | ETH        | USD   | lognormal-risk-model-1 | default-capped-margin-calculator | 1                | fees-config-1 | price-monitoring-1 | ethDec21Oracle     | 0.25                   | 0                         | default-futures | 1500          | true                 | false  |
    37  
    38    @SLABug @NoPerp @Capped @CSettle @CappedBug
    39    Scenario: 0016-PFUT-022: The sum of all final settlement cashflows equals 0 (loss socialisation does not happen). Assuming general account balances of all parties were 0 after opening the positions and all of their funds were in the margin accounts: short parties end up with balances equal to abs(position size) * max_price and long parties end up with 0 balances.
    40      Given the initial insurance pool balance is "10000" for all the markets
    41      And the parties deposit on asset's general account the following amount:
    42        | party    | asset | amount    |
    43        | party1   | USD   | 10000     |
    44        | party2   | USD   | 10000     |
    45        | aux1     | USD   | 100000    |
    46        | aux2     | USD   | 100000    |
    47        | aux3     | USD   | 100000    |
    48        | aux4     | USD   | 100000    |
    49        | aux5     | USD   | 100000    |
    50        | party-lp | USD   | 100000000 |
    51  
    52      And the parties submit the following liquidity provision:
    53        | id  | party    | market id | commitment amount | fee | lp type    |
    54        | lp2 | party-lp | ETH/DEC21 | 30000             | 0   | submission |
    55  
    56      When the parties place the following orders:
    57        | party  | market id | side | volume | price | resulting trades | type       | tif     | reference | error |
    58        | aux1   | ETH/DEC21 | buy  | 2      | 999   | 0                | TYPE_LIMIT | TIF_GTC | ref-1     |       |
    59        | aux2   | ETH/DEC21 | sell | 2      | 1499  | 0                | TYPE_LIMIT | TIF_GTC | ref-2     |       |
    60        | party1 | ETH/DEC21 | buy  | 5      | 1000  | 0                | TYPE_LIMIT | TIF_GTC | ref-3     |       |
    61        | party2 | ETH/DEC21 | sell | 5      | 1000  | 0                | TYPE_LIMIT | TIF_GTC | ref-4     |       |
    62      And the network moves ahead "2" blocks
    63  
    64      Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC21"
    65      And the market state should be "STATE_ACTIVE" for the market "ETH/DEC21"
    66      And the mark price should be "1000" for the market "ETH/DEC21"
    67      And the parties should have the following account balances:
    68        | party  | asset | market id | margin | general |
    69        | party1 | USD   | ETH/DEC21 | 5000   | 5000    |
    70        | party2 | USD   | ETH/DEC21 | 2500   | 7500    |
    71  
    72      #order margin for aux1: limit price * size = 999*2=1998
    73      #order margin for aux2: (max price - limit price) * size = (1500-1301)*2=398
    74      # party1 maintenance margin level: position size * average entry price = 5*1000=5000
    75      # party2 maintenance margin level: position size * (max price - average entry price)=5*(1500-1000)=2500
    76      # Aux1: potential position * average price on book = 2 * 999 = 1998, but due to the MTM settlement the margin level
    77      And the parties should have the following margin levels:
    78        | party  | market id | maintenance | search | initial | release | margin mode  |
    79        | party1 | ETH/DEC21 | 5000        | 5000   | 5000    | 5000    | cross margin |
    80        | party2 | ETH/DEC21 | 2500        | 2500   | 2500    | 2500    | cross margin |
    81        | aux2   | ETH/DEC21 | 2           | 2      | 2       | 2       | cross margin |
    82        | aux1   | ETH/DEC21 | 1998        | 1998   | 1998    | 1998    | cross margin |
    83  
    84      #update mark price
    85      When the parties place the following orders:
    86        | party | market id | side | volume | price | resulting trades | type       | tif     | reference |
    87        | aux1  | ETH/DEC21 | buy  | 1      | 1100  | 0                | TYPE_LIMIT | TIF_GTC | aux1-2    |
    88        | aux2  | ETH/DEC21 | sell | 1      | 1100  | 1                | TYPE_LIMIT | TIF_GTC | aux2-2    |
    89  
    90      And the network moves ahead "2" blocks
    91      Then the mark price should be "1100" for the market "ETH/DEC21"
    92  
    93      # MTM settlement 5 long makes a profit of 500, 5 short loses 500
    94      # Now for aux1 and 2, the calculations from above still hold but more margin is required due to the open positions:
    95      # aux1: position * 1100 + 999*2 = 1100 + 1998 = 3098
    96      # aux2: then placing the order (max price - average order price) * 3 = (1500 - (1301 + 1301 + 1100)/3) * 3 = (1500 - 1234) * 3 = 266 * 3 = 798
    97      # aux2's short position and potential margins are calculated separately as 2 * (1500-1301) + 1 * (1500 - 1100) = 398 + 400 = 798
    98      And the parties should have the following account balances:
    99        | party  | asset | market id | margin | general |
   100        | party1 | USD   | ETH/DEC21 | 5500   | 5000    |
   101        | party2 | USD   | ETH/DEC21 | 2000   | 7500    |
   102        | aux1   | USD   | ETH/DEC21 | 3098   | 96908   |
   103        | aux2   | USD   | ETH/DEC21 | 402    | 99570   |
   104      # The market is fully collateralised, switching to isolated margin is not supported
   105      When the parties submit update margin mode:
   106        | party  | market    | margin_mode     | margin_factor | error                                                                                                                                                 |
   107        | party1 | ETH/DEC21 | isolated margin | 0.5           | margin factor (0.5) must be greater than max(riskFactorLong (0.3696680542085883), riskFactorShort (0.5650462045113667)) + linearSlippageFactor (0.25) |
   108        | party1 | ETH/DEC21 | isolated margin | 0.9           | isolated margin not permitted on fully collateralised markets                                                                                         |
   109  
   110      #update mark price to max_price
   111      When the parties place the following orders:
   112        | party | market id | side | volume | price | resulting trades | type       | tif     | reference | error |
   113        | aux3  | ETH/DEC21 | buy  | 2      | 1499  | 1                | TYPE_LIMIT | TIF_GTC | aux3-1    |       |
   114  
   115      And the following trades should be executed:
   116        | buyer | price | size | seller |
   117        | aux3  | 1499  | 2    | aux2   |
   118  
   119      And the network moves ahead "2" blocks
   120      Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC21"
   121      Then the mark price should be "1499" for the market "ETH/DEC21"
   122  
   123      # MTM settlement 5 long makes a profit of 2000, 5 short loses 2000
   124      # Now for aux1 and 2, the calculations from above still hold but more margin is required due to the open positions:
   125      # aux1: position * 1100 + 999*2 = 1100 + 1998 = 3098
   126      # aux2: short position of size 2, traded price at 1500, then margin: postion size * (max price - average entry price) = 3*(1100+1500*2)/3
   127      And the parties should have the following account balances:
   128        | party  | asset | market id | margin | general |
   129        | party1 | USD   | ETH/DEC21 | 7495   | 5000    |
   130        | party2 | USD   | ETH/DEC21 | 5      | 7500    |
   131        | aux1   | USD   | ETH/DEC21 | 3497   | 96908   |
   132        | aux2   | USD   | ETH/DEC21 | 3      | 99585   |
   133        | aux3   | USD   | ETH/DEC21 | 2998   | 96927   |
   134  
   135      And the parties should have the following margin levels:
   136        | party  | market id | maintenance | search | initial | release | margin mode  |
   137        | party1 | ETH/DEC21 | 7495        | 7495   | 7495    | 7495    | cross margin |
   138        | party2 | ETH/DEC21 | 5           | 5      | 5       | 5       | cross margin |
   139        | aux2   | ETH/DEC21 | 3           | 3      | 3       | 3       | cross margin |
   140        | aux1   | ETH/DEC21 | 3497        | 3497   | 3497    | 3497    | cross margin |
   141      #trade at max_price
   142      When the parties place the following orders:
   143        | party | market id | side | volume | price | resulting trades | type       | tif     | reference |
   144        | aux4  | ETH/DEC21 | buy  | 2      | 1499  | 0                | TYPE_LIMIT | TIF_GTC | aux4-1    |
   145        | aux5  | ETH/DEC21 | sell | 2      | 1499  | 1                | TYPE_LIMIT | TIF_GTC | aux5-1    |
   146  
   147      And the network moves ahead "2" blocks
   148      Then the parties should have the following account balances:
   149        | party | asset | market id | margin | general |
   150        | aux4  | USD   | ETH/DEC21 | 2998   | 97017   |
   151        | aux5  | USD   | ETH/DEC21 | 2      | 99923   |
   152      And the following transfers should happen:
   153        | from   | to   | from account            | to account                       | market id | amount | asset |
   154        | aux5   |      | ACCOUNT_TYPE_GENERAL    | ACCOUNT_TYPE_FEES_MAKER          | ETH/DEC21 | 15     | USD   |
   155        | aux5   |      | ACCOUNT_TYPE_GENERAL    | ACCOUNT_TYPE_FEES_INFRASTRUCTURE | ETH/DEC21 | 60     | USD   |
   156        | aux5   |      | ACCOUNT_TYPE_GENERAL    | ACCOUNT_TYPE_FEES_LIQUIDITY      | ETH/DEC21 | 0      | USD   |
   157        | market | aux4 | ACCOUNT_TYPE_FEES_MAKER | ACCOUNT_TYPE_GENERAL             | ETH/DEC21 | 15     | USD   |
   158  
   159      # Terminate trading
   160      When the oracles broadcast data signed with "0xCAFECAFE1":
   161        | name               | value |
   162        | trading.terminated | true  |
   163      And the network moves ahead "2" blocks
   164      Then the market state should be "STATE_TRADING_TERMINATED" for the market "ETH/DEC21"
   165  
   166      # Trigger settlement at price 0.
   167      When the oracles broadcast data signed with "0xCAFECAFE1":
   168        | name             | value |
   169        | prices.ETH.value | 0     |
   170      And the network moves ahead "2" blocks
   171      Then the last market state should be "STATE_SETTLED" for the market "ETH/DEC21"
   172      And the following transfers should happen:
   173        | from     | to       | from account            | to account              | market id | amount | asset | type                        |
   174        | aux1     |          | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC21 | 1499   | USD   | TRANSFER_TYPE_LOSS          |
   175        | aux3     |          | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC21 | 2998   | USD   | TRANSFER_TYPE_LOSS          |
   176        | aux4     |          | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC21 | 2998   | USD   | TRANSFER_TYPE_LOSS          |
   177        | party1   |          | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC21 | 7495   | USD   | TRANSFER_TYPE_LOSS          |
   178        | market   | aux2     | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN     | ETH/DEC21 | 4497   | USD   | TRANSFER_TYPE_WIN           |
   179        | market   | aux5     | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN     | ETH/DEC21 | 2998   | USD   | TRANSFER_TYPE_WIN           |
   180        | market   | party2   | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN     | ETH/DEC21 | 7495   | USD   | TRANSFER_TYPE_WIN           |
   181        | aux1     | aux1     | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_GENERAL    | ETH/DEC21 | 1998   | USD   | TRANSFER_TYPE_CLEAR_ACCOUNT |
   182        | aux2     | aux2     | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_GENERAL    | ETH/DEC21 | 4500   | USD   | TRANSFER_TYPE_CLEAR_ACCOUNT |
   183        | aux5     | aux5     | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_GENERAL    | ETH/DEC21 | 3000   | USD   | TRANSFER_TYPE_CLEAR_ACCOUNT |
   184        | party2   | party2   | ACCOUNT_TYPE_MARGIN     | ACCOUNT_TYPE_GENERAL    | ETH/DEC21 | 7500   | USD   | TRANSFER_TYPE_CLEAR_ACCOUNT |
   185        | party-lp | party-lp | ACCOUNT_TYPE_BOND       | ACCOUNT_TYPE_GENERAL    | ETH/DEC21 | 30000  | USD   | TRANSFER_TYPE_CLEAR_ACCOUNT |
   186      And the parties should have the following account balances:
   187        | party  | asset | market id | margin | general |
   188        | party1 | USD   | ETH/DEC21 | 0      | 5000    |
   189        | party2 | USD   | ETH/DEC21 | 0      | 15000   |
   190        | aux1   | USD   | ETH/DEC21 | 0      | 98906   |
   191        | aux2   | USD   | ETH/DEC21 | 0      | 104085  |
   192        | aux3   | USD   | ETH/DEC21 | 0      | 96927   |
   193        | aux4   | USD   | ETH/DEC21 | 0      | 97017   |
   194        | aux5   | USD   | ETH/DEC21 | 0      | 102923  |