code.vegaprotocol.io/vega@v0.79.0/core/integration/features/verified/network_pnl_funding.feature (about)

     1  Feature: Network profit and loss consideres funding
     2  
     3    Background:
     4  
     5      Given time is updated to "2020-10-16T00:00:00Z"
     6  
     7      # Configure the network
     8      Given the following network parameters are set:
     9        | name                                    | value |
    10        | network.markPriceUpdateMaximumFrequency | 0s    |
    11      And the following assets are registered:
    12        | id  | decimal places |
    13        | ETH | 0              |
    14      And the perpetual oracles from "0xCAFECAFE1":
    15        | name        | asset | settlement property | settlement type | schedule property | schedule type  | margin funding factor | interest rate | clamp lower bound | clamp upper bound | quote name | settlement decimals |
    16        | perp-oracle | ETH   | perp.ETH.value      | TYPE_INTEGER    | perp.funding.cue  | TYPE_TIMESTAMP | 0.9                   | 0.1           | 0                 | 0                 | ETH        | 0                   |
    17      Given the liquidation strategies:
    18        | name                | disposal step | disposal fraction | full disposal size | max fraction consumed | disposal slippage range |
    19        | liquidation-strat-1 | 3600          | 0.5               | 0                  | 1                     | 0.1                     |
    20      And the markets:
    21        | id        | quote name | asset | risk model                    | margin calculator         | auction duration | fees         | price monitoring | data source config | linear slippage factor | quadratic slippage factor | liquidation strategy | sla params    | market type |
    22        | ETH/MAR22 | ETH        | ETH   | default-log-normal-risk-model | default-margin-calculator | 1                | default-none | default-none     | perp-oracle        | 0.001                  | 0                         | liquidation-strat-1  | default-basic | perp        |
    23  
    24  
    25    @Perpetual
    26    @NetPNL
    27    Scenario: Network has long position when funding rate negative, gains paid into market insurance pool (0053-PERP-037)
    28  
    29      # Setup the market
    30      Given the initial insurance pool balance is "10000" for all the markets
    31      And the parties deposit on asset's general account the following amount:
    32        | party | asset | amount       |
    33        | lp1   | ETH   | 100000000000 |
    34        | aux1  | ETH   | 10000000000  |
    35        | aux2  | ETH   | 10000000000  |
    36      And the parties submit the following liquidity provision:
    37        | id  | party | market id | commitment amount | fee | lp type    |
    38        | lp1 | lp1   | ETH/MAR22 | 500000            | 0   | submission |
    39      When the parties place the following orders:
    40        | party | market id | side | volume | price | resulting trades | type       | tif     | reference |
    41        | lp1   | ETH/MAR22 | buy  | 1000   | 199   | 0                | TYPE_LIMIT | TIF_GTC | best-bid  |
    42        | lp1   | ETH/MAR22 | sell | 1000   | 201   | 0                | TYPE_LIMIT | TIF_GTC | best-ask  |
    43      When the parties place the following orders:
    44        | party | market id | side | volume | price | resulting trades | type       | tif     |
    45        | aux1  | ETH/MAR22 | buy  | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
    46        | aux2  | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
    47      And the opening auction period ends for market "ETH/MAR22"
    48      Then the market data for the market "ETH/MAR22" should be:
    49        | mark price | trading mode            |
    50        | 200        | TRADING_MODE_CONTINUOUS |
    51  
    52      # atRiskPary opens a long position
    53      Given the parties deposit on asset's general account the following amount:
    54        | party       | asset | amount |
    55        | atRiskParty | ETH   | 100    |
    56      And the parties place the following orders:
    57        | party       | market id | side | volume | price | resulting trades | type       | tif     |
    58        | aux1        | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
    59        | atRiskParty | ETH/MAR22 | buy  | 1      | 200   | 1                | TYPE_LIMIT | TIF_GTC |
    60      When the network moves ahead "1" blocks
    61      Then the parties should have the following profit and loss:
    62        | party       | volume | unrealised pnl | realised pnl |
    63        | atRiskParty | 1      | 0              | 0            |
    64      And the parties should have the following margin levels:
    65        | party       | market id | maintenance | search | initial | release |
    66        | atRiskParty | ETH/MAR22 | 15          | 16     | 18      | 21      |
    67      And the parties should have the following account balances:
    68        | party       | asset | market id | margin | general |
    69        | atRiskParty | ETH   | ETH/MAR22 | 16     | 84      |
    70  
    71      # Market moves against atRiskParty whom is liquidated
    72      Given the parties amend the following orders:
    73        | party | reference | price | size delta | tif     |
    74        | lp1   | best-bid  | 99    | 0          | TIF_GTC |
    75        | lp1   | best-ask  | 101   | 0          | TIF_GTC |
    76      And the parties place the following orders:
    77        | party | market id | side | volume | price | resulting trades | type       | tif     |
    78        | aux1  | ETH/MAR22 | buy  | 1      | 100   | 0                | TYPE_LIMIT | TIF_GTC |
    79        | aux2  | ETH/MAR22 | sell | 1      | 100   | 1                | TYPE_LIMIT | TIF_GTC |
    80      When the network moves ahead "1" blocks
    81      Then the mark price should be "100" for the market "ETH/MAR22"
    82      And the parties should have the following profit and loss:
    83        | party       | volume | unrealised pnl | realised pnl |
    84        | atRiskParty | 0      | 0              | -100         |
    85        | network     | 1      | 0              | 0            |
    86      And the insurance pool balance should be "10000" for the market "ETH/MAR22"
    87  
    88      # Start a new funding period 
    89      Given time is updated to "2020-10-16T00:05:00Z"
    90      And the oracles broadcast data with block time signed with "0xCAFECAFE1":
    91        | name             | value      | time offset |
    92        | perp.funding.cue | 1602806700 | 0s          |
    93        | perp.ETH.value   | 101        | 1s          |
    94  
    95      # Negative funding payment, shorts pay longs, gains paid into insurance pool
    96      Given time is updated to "2020-10-16T00:10:00Z"
    97      And the product data for the market "ETH/MAR22" should be:
    98        | internal twap | external twap | funding payment | funding rate        |
    99        | 100           | 101           | -1              | -0.0099009900990099 |
   100      And the parties should have the following profit and loss:
   101        | party   | volume | unrealised pnl | realised pnl |
   102        | network | 1      | 0              | 0            |
   103      And the insurance pool balance should be "10000" for the market "ETH/MAR22"
   104      When the oracles broadcast data with block time signed with "0xCAFECAFE1":
   105        | name             | value      | time offset |
   106        | perp.funding.cue | 1602807000 | 0s          |
   107      Then the parties should have the following profit and loss:
   108        | party   | volume | unrealised pnl | realised pnl |
   109        | network | 1      | 0              | 1            |
   110      And the insurance pool balance should be "10001" for the market "ETH/MAR22"
   111  
   112  
   113    @Perpetual
   114    @NetPNL
   115    Scenario: Network has long position when funding rate positive, losses paid from market insurance pool (0053-PERP-038)
   116  
   117      # Setup the market
   118      Given the initial insurance pool balance is "10000" for all the markets
   119      And the parties deposit on asset's general account the following amount:
   120        | party | asset | amount       |
   121        | lp1   | ETH   | 100000000000 |
   122        | aux1  | ETH   | 10000000000  |
   123        | aux2  | ETH   | 10000000000  |
   124      And the parties submit the following liquidity provision:
   125        | id  | party | market id | commitment amount | fee | lp type    |
   126        | lp1 | lp1   | ETH/MAR22 | 500000            | 0   | submission |
   127      When the parties place the following orders:
   128        | party | market id | side | volume | price | resulting trades | type       | tif     | reference |
   129        | lp1   | ETH/MAR22 | buy  | 1000   | 199   | 0                | TYPE_LIMIT | TIF_GTC | best-bid  |
   130        | lp1   | ETH/MAR22 | sell | 1000   | 201   | 0                | TYPE_LIMIT | TIF_GTC | best-ask  |
   131      When the parties place the following orders:
   132        | party | market id | side | volume | price | resulting trades | type       | tif     |
   133        | aux1  | ETH/MAR22 | buy  | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   134        | aux2  | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   135      And the opening auction period ends for market "ETH/MAR22"
   136      Then the market data for the market "ETH/MAR22" should be:
   137        | mark price | trading mode            |
   138        | 200        | TRADING_MODE_CONTINUOUS |
   139  
   140      # atRiskPary opens a long position
   141      Given the parties deposit on asset's general account the following amount:
   142        | party       | asset | amount |
   143        | atRiskParty | ETH   | 100    |
   144      And the parties place the following orders:
   145        | party       | market id | side | volume | price | resulting trades | type       | tif     |
   146        | aux1        | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   147        | atRiskParty | ETH/MAR22 | buy  | 1      | 200   | 1                | TYPE_LIMIT | TIF_GTC |
   148      When the network moves ahead "1" blocks
   149      Then the parties should have the following profit and loss:
   150        | party       | volume | unrealised pnl | realised pnl |
   151        | atRiskParty | 1      | 0              | 0            |
   152      And the parties should have the following margin levels:
   153        | party       | market id | maintenance | search | initial | release |
   154        | atRiskParty | ETH/MAR22 | 15          | 16     | 18      | 21      |
   155      And the parties should have the following account balances:
   156        | party       | asset | market id | margin | general |
   157        | atRiskParty | ETH   | ETH/MAR22 | 16     | 84      |
   158  
   159      # Market moves against atRiskParty whom is liquidated
   160      Given the parties amend the following orders:
   161        | party | reference | price | size delta | tif     |
   162        | lp1   | best-bid  | 99    | 0          | TIF_GTC |
   163        | lp1   | best-ask  | 101   | 0          | TIF_GTC |
   164      And the parties place the following orders:
   165        | party | market id | side | volume | price | resulting trades | type       | tif     |
   166        | aux1  | ETH/MAR22 | buy  | 1      | 100   | 0                | TYPE_LIMIT | TIF_GTC |
   167        | aux2  | ETH/MAR22 | sell | 1      | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   168      When the network moves ahead "1" blocks
   169      Then the mark price should be "100" for the market "ETH/MAR22"
   170      And the parties should have the following profit and loss:
   171        | party       | volume | unrealised pnl | realised pnl |
   172        | atRiskParty | 0      | 0              | -100         |
   173        | network     | 1      | 0              | 0            |
   174      And the insurance pool balance should be "10000" for the market "ETH/MAR22"
   175  
   176      # Start a new funding period 
   177      Given time is updated to "2020-10-16T00:05:00Z"
   178      And the oracles broadcast data with block time signed with "0xCAFECAFE1":
   179        | name             | value      | time offset |
   180        | perp.funding.cue | 1602806700 | 0s          |
   181        | perp.ETH.value   | 99         | 1s          |
   182  
   183      # Positive funding payment, longs pay shorts, losses paid from insurance pool
   184      Given time is updated to "2020-10-16T00:10:00Z"
   185      And the product data for the market "ETH/MAR22" should be:
   186        | internal twap | external twap | funding payment | funding rate       |
   187        | 100           | 99            | 1               | 0.0101010101010101 |
   188      And the parties should have the following profit and loss:
   189        | party   | volume | unrealised pnl | realised pnl |
   190        | network | 1      | 0              | 0            |
   191      And the insurance pool balance should be "10000" for the market "ETH/MAR22"
   192      When the oracles broadcast data with block time signed with "0xCAFECAFE1":
   193        | name             | value      | time offset |
   194        | perp.funding.cue | 1602807000 | 0s          |
   195      Then the parties should have the following profit and loss:
   196        | party   | volume | unrealised pnl | realised pnl |
   197        | network | 1      | 0              | -1           |
   198      And the insurance pool balance should be "9999" for the market "ETH/MAR22"
   199  
   200  
   201    @Perpetual
   202    @NetPNL
   203    Scenario: If a market insurance pool does not have enough funds to cover a funding payment, loss socialisation occurs and the total balances across the network remains constant (0053-PERP-039)
   204  
   205      # Setup the market
   206      And the parties deposit on asset's general account the following amount:
   207        | party | asset | amount       |
   208        | lp1   | ETH   | 100000000000 |
   209        | aux1  | ETH   | 10000000000  |
   210        | aux2  | ETH   | 10000000000  |
   211      And the parties submit the following liquidity provision:
   212        | id  | party | market id | commitment amount | fee | lp type    |
   213        | lp1 | lp1   | ETH/MAR22 | 500000            | 0   | submission |
   214      When the parties place the following orders:
   215        | party | market id | side | volume | price | resulting trades | type       | tif     | reference |
   216        | lp1   | ETH/MAR22 | buy  | 1000   | 199   | 0                | TYPE_LIMIT | TIF_GTC | best-bid  |
   217        | lp1   | ETH/MAR22 | sell | 1000   | 201   | 0                | TYPE_LIMIT | TIF_GTC | best-ask  |
   218      When the parties place the following orders:
   219        | party | market id | side | volume | price | resulting trades | type       | tif     |
   220        | aux1  | ETH/MAR22 | buy  | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   221        | aux2  | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   222      And the opening auction period ends for market "ETH/MAR22"
   223      Then the market data for the market "ETH/MAR22" should be:
   224        | mark price | trading mode            |
   225        | 200        | TRADING_MODE_CONTINUOUS |
   226  
   227      # atRiskPary opens a long position
   228      Given the parties deposit on asset's general account the following amount:
   229        | party       | asset | amount |
   230        | atRiskParty | ETH   | 100    |
   231      And the parties place the following orders:
   232        | party       | market id | side | volume | price | resulting trades | type       | tif     |
   233        | aux1        | ETH/MAR22 | sell | 1      | 200   | 0                | TYPE_LIMIT | TIF_GTC |
   234        | atRiskParty | ETH/MAR22 | buy  | 1      | 200   | 1                | TYPE_LIMIT | TIF_GTC |
   235      When the network moves ahead "1" blocks
   236      Then the parties should have the following profit and loss:
   237        | party       | volume | unrealised pnl | realised pnl |
   238        | atRiskParty | 1      | 0              | 0            |
   239      And the parties should have the following margin levels:
   240        | party       | market id | maintenance | search | initial | release |
   241        | atRiskParty | ETH/MAR22 | 15          | 16     | 18      | 21      |
   242      And the parties should have the following account balances:
   243        | party       | asset | market id | margin | general |
   244        | atRiskParty | ETH   | ETH/MAR22 | 16     | 84      |
   245  
   246      # Market moves against atRiskParty whom is liquidated
   247      Given the parties amend the following orders:
   248        | party | reference | price | size delta | tif     |
   249        | lp1   | best-bid  | 99    | 0          | TIF_GTC |
   250        | lp1   | best-ask  | 101   | 0          | TIF_GTC |
   251      And the parties place the following orders:
   252        | party | market id | side | volume | price | resulting trades | type       | tif     |
   253        | aux1  | ETH/MAR22 | buy  | 1      | 100   | 0                | TYPE_LIMIT | TIF_GTC |
   254        | aux2  | ETH/MAR22 | sell | 1      | 100   | 1                | TYPE_LIMIT | TIF_GTC |
   255      When the network moves ahead "1" blocks
   256      Then the mark price should be "100" for the market "ETH/MAR22"
   257      And the parties should have the following profit and loss:
   258        | party       | volume | unrealised pnl | realised pnl |
   259        | atRiskParty | 0      | 0              | -100         |
   260        | network     | 1      | 0              | 0            |
   261      And the insurance pool balance should be "0" for the market "ETH/MAR22"
   262  
   263      # Start a new funding period 
   264      Given time is updated to "2020-10-16T00:05:00Z"
   265      And the oracles broadcast data with block time signed with "0xCAFECAFE1":
   266        | name             | value      | time offset |
   267        | perp.funding.cue | 1602806700 | 0s          |
   268        | perp.ETH.value   | 99         | 1s          |
   269  
   270      # Positive funding payment, longs pay shorts, losses covered with loss socialisation
   271      Given time is updated to "2020-10-16T00:10:00Z"
   272      And the product data for the market "ETH/MAR22" should be:
   273        | internal twap | external twap | funding payment | funding rate       |
   274        | 100           | 99            | 1               | 0.0101010101010101 |
   275      And the parties should have the following profit and loss:
   276        | party   | volume | unrealised pnl | realised pnl |
   277        | network | 1      | 0              | 0            |
   278      And the insurance pool balance should be "0" for the market "ETH/MAR22"
   279      And the cumulated balance for all accounts should be worth "120000000100"
   280      When the oracles broadcast data with block time signed with "0xCAFECAFE1":
   281        | name             | value      | time offset |
   282        | perp.funding.cue | 1602807000 | 0s          |
   283      Then the parties should have the following profit and loss:
   284        | party   | volume | unrealised pnl | realised pnl |
   285        | network | 1      | 0              | -1           |
   286      And the insurance pool balance should be "0" for the market "ETH/MAR22"
   287      And the cumulated balance for all accounts should be worth "120000000100"
   288  
   289