code.vegaprotocol.io/vega@v0.79.0/core/integration/features/verified/Funding-insurance_pool.feature (about) 1 Feature: Position resolution case 5 lognormal risk model 2 3 Background: 4 5 Given the log normal risk model named "lognormal-risk-model-fish": 6 | risk aversion | tau | mu | r | sigma | 7 | 0.001 | 0.01 | 0 | 0.0 | 1.2 | 8 #calculated risk factor long: 0.336895684; risk factor short: 0.4878731 9 10 And the price monitoring named "price-monitoring-1": 11 | horizon | probability | auction extension | 12 | 1 | 0.99999999 | 300 | 13 14 And the margin calculator named "margin-calculator-1": 15 | search factor | initial factor | release factor | 16 | 1.2 | 1.5 | 2 | 17 18 And the oracle spec for settlement data filtering data from "0xCAFECAFE" named "ethDec20Oracle": 19 | property | type | binding | 20 | prices.ETH.value | TYPE_INTEGER | settlement data | 21 22 And the oracle spec for trading termination filtering data from "0xCAFECAFE" named "ethDec20Oracle": 23 | property | type | binding | 24 | trading.terminated | TYPE_BOOLEAN | trading termination | 25 26 And the markets: 27 | id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params | 28 | ETH/DEC19 | ETH | USD | lognormal-risk-model-fish | margin-calculator-1 | 1 | default-none | default-none | ethDec20Oracle | 0.74667 | 0 | default-futures | 29 30 And the following network parameters are set: 31 | name | value | 32 | market.auction.minimumDuration | 1 | 33 | network.markPriceUpdateMaximumFrequency | 0s | 34 | market.liquidity.successorLaunchWindowLength | 1s | 35 | limits.markets.maxPeggedOrders | 2 | 36 37 @Liquidation 38 Scenario: 001 using lognormal risk model, setup a scenario where designatedLoser gets closed out; 0012-POSR-002, 0012-POSR-005, 0013-ACCT-001 39 40 # setup accounts 41 Given the parties deposit on asset's general account the following amount: 42 | party | asset | amount | 43 | sellSideProvider | USD | 1000000000000 | 44 | buySideProvider | USD | 1000000000000 | 45 | designatedLoser | USD | 22000 | 46 | aux | USD | 1000000000000 | 47 | aux2 | USD | 1000000000000 | 48 | lpprov | USD | 1000000000000 | 49 50 When the parties submit the following liquidity provision: 51 | id | party | market id | commitment amount | fee | lp type | 52 | lp1 | lpprov | ETH/DEC19 | 9000 | 0.1 | submission | 53 | lp1 | lpprov | ETH/DEC19 | 9000 | 0.1 | amendment | 54 55 # place auxiliary orders so we always have best bid and best offer as to not trigger the liquidity auction 56 Then the parties place the following orders: 57 | party | market id | side | volume | price | resulting trades | type | tif | 58 | aux | ETH/DEC19 | buy | 10 | 1 | 0 | TYPE_LIMIT | TIF_GTC | 59 | aux | ETH/DEC19 | sell | 10 | 2000 | 0 | TYPE_LIMIT | TIF_GTC | 60 | aux | ETH/DEC19 | buy | 1 | 150 | 0 | TYPE_LIMIT | TIF_GTC | 61 | aux2 | ETH/DEC19 | sell | 1 | 150 | 0 | TYPE_LIMIT | TIF_GTC | 62 | lpprov | ETH/DEC19 | buy | 225 | 40 | 0 | TYPE_LIMIT | TIF_GTC | 63 | lpprov | ETH/DEC19 | sell | 36 | 250 | 0 | TYPE_LIMIT | TIF_GTC | 64 Then the opening auction period ends for market "ETH/DEC19" 65 And the mark price should be "150" for the market "ETH/DEC19" 66 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 67 68 # insurance pool generation - setup orderbook 69 When the parties place the following orders with ticks: 70 | party | market id | side | volume | price | resulting trades | type | tif | reference | 71 | sellSideProvider | ETH/DEC19 | sell | 290 | 150 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 72 | buySideProvider | ETH/DEC19 | buy | 1 | 140 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-1 | 73 74 And the market data for the market "ETH/DEC19" should be: 75 | mark price | trading mode | target stake | supplied stake | open interest | 76 | 150 | TRADING_MODE_CONTINUOUS | 731 | 9000 | 1 | 77 #target_stake = mark_price x max_oi x target_stake_scaling_factor x rf=150*10*1*0.4878731=731 78 79 Then the order book should have the following volumes for market "ETH/DEC19": 80 | side | volume | price | 81 | buy | 10 | 1 | 82 | buy | 225 | 40 | 83 | buy | 1 | 140 | 84 | sell | 290 | 150 | 85 | sell | 36 | 250 | 86 | sell | 10 | 2000 | 87 88 Then the parties should have the following profit and loss: 89 | party | volume | unrealised pnl | realised pnl | 90 | aux | 1 | 0 | 0 | 91 | aux2 | -1 | 0 | 0 | 92 93 # insurance pool generation - trade 94 When the parties place the following orders with ticks: 95 | party | market id | side | volume | price | resulting trades | type | tif | reference | 96 | designatedLoser | ETH/DEC19 | buy | 290 | 150 | 1 | TYPE_LIMIT | TIF_GTC | ref-1 | 97 98 Then the parties should have the following account balances: 99 | party | asset | market id | margin | general | 100 | designatedLoser | USD | ETH/DEC19 | 0 | 0 | 101 102 Then the parties should have the following margin levels: 103 | party | market id | maintenance | search | initial | release | 104 | designatedLoser | ETH/DEC19 | 0 | 0 | 0 | 0 | 105 106 Then the order book should have the following volumes for market "ETH/DEC19": 107 | side | volume | price | 108 | buy | 0 | 1 | 109 | buy | 0 | 40 | 110 | buy | 0 | 140 | 111 | sell | 0 | 150 | 112 | sell | 36 | 250 | 113 | sell | 10 | 2000 | 114 | sell | 0 | 2100 | 115 116 #designatedLoser has position of vol 290; price 150; calculated risk factor long: 0.336895684; risk factor short: 0.4878731 117 #what's on the order book to cover the position is shown above, which makes the exit price 38.77118644 =(140*1+40*225+1*10)/236, slippage per unit is 150-38.77118644=111.2288136 118 #margin level is PositionVol*(markPrice*RiskFactor+SlippagePerUnit) = 290*(150*0.336895684+111.2288136)=46911.3182 119 120 When the parties place the following orders with ticks: 121 | party | market id | side | volume | price | resulting trades | type | tif | reference | 122 | buySideProvider | ETH/DEC19 | buy | 290 | 120 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-2 | 123 124 # insurance pool generation - set new mark price (and trigger closeout) 125 #When the parties place the following orders with ticks: 126 When the parties place the following orders with ticks: 127 | party | market id | side | volume | price | resulting trades | type | tif | reference | 128 | sellSideProvider | ETH/DEC19 | sell | 1 | 140 | 0 | TYPE_LIMIT | TIF_GTC | ref-1 | 129 | buySideProvider | ETH/DEC19 | buy | 1 | 140 | 1 | TYPE_LIMIT | TIF_GTC | ref-2 | 130 And the network moves ahead "2" blocks 131 132 Then the following trades should be executed: 133 | buyer | price | size | seller | 134 | network | 150 | 290 | designatedLoser | 135 | buySideProvider | 140 | 1 | network | 136 | lpprov | 40 | 225 | network | 137 | aux | 1 | 10 | network | 138 | buySideProvider | 120 | 54 | network | 139 | buySideProvider | 140 | 1 | sellSideProvider | 140 141 And the following network trades should be executed: 142 | party | aggressor side | volume | 143 | designatedLoser | buy | 290 | 144 | buySideProvider | sell | 1 | 145 | lpprov | sell | 225 | 146 | aux | sell | 10 | 147 | buySideProvider | sell | 54 | 148 | buySideProvider | buy | 1 | 149 150 # check positions 151 And the parties should have the following profit and loss: 152 | party | volume | unrealised pnl | realised pnl | 153 | designatedLoser | 0 | 0 | -17650 | 154 | sellSideProvider | -291 | 2900 | 0 | 155 | buySideProvider | 56 | 1080 | -1622 | 156 | aux | 11 | 1380 | -541 | 157 | aux2 | -1 | 10 | 0 | 158 159 And the parties should have the following account balances: 160 | party | asset | market id | margin | general | 161 | designatedLoser | USD | ETH/DEC19 | 0 | 0 | 162 163 # check margin levels 164 Then the parties should have the following margin levels: 165 | party | market id | maintenance | search | initial | release | 166 | designatedLoser | ETH/DEC19 | 0 | 0 | 0 | 0 | 167 # checking margins 168 Then the parties should have the following account balances: 169 | party | asset | market id | margin | general | 170 | designatedLoser | USD | ETH/DEC19 | 0 | 0 | 171 172 # then we make sure the insurance pool collected the funds (however they get later spent on MTM payment to closeout-facilitating party) 173 Then the following transfers should happen: 174 | from | to | from account | to account | market id | amount | asset | 175 | designatedLoser | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_FEES_MAKER | ETH/DEC19 | 0 | USD | 176 | designatedLoser | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_FEES_LIQUIDITY | ETH/DEC19 | 4331 | USD | 177 | designatedLoser | | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_FEES_INFRASTRUCTURE | ETH/DEC19 | 0 | USD | 178 | market | buySideProvider | ACCOUNT_TYPE_FEES_MAKER | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 0 | USD | 179 | designatedLoser | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_INSURANCE | ETH/DEC19 | 17650 | USD | 180 | market | market | ACCOUNT_TYPE_INSURANCE | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 16735 | USD | 181 | market | buySideProvider | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 8 | USD | 182 | market | buySideProvider | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 0 | USD | 183 184 And the insurance pool balance should be "0" for the market "ETH/DEC19" 185 186 When the parties place the following orders with ticks: 187 | party | market id | side | volume | price | resulting trades | type | tif | reference | 188 | aux2 | ETH/DEC19 | buy | 1 | 120 | 0 | TYPE_LIMIT | TIF_GTC | ref-2 | 189 | aux | ETH/DEC19 | sell | 1 | 120 | 1 | TYPE_LIMIT | TIF_GTC | ref-1 | 190 191 And the market data for the market "ETH/DEC19" should be: 192 | mark price | trading mode | target stake | supplied stake | open interest | 193 | 120 | TRADING_MODE_CONTINUOUS | 170949 | 9000 | 292 | 194 195 And the insurance pool balance should be "0" for the market "ETH/DEC19" 196 197 # Double entry accounting is maintained at all points 198 # i.e. every transfer event has a source account and destination account and the balance of the source account before the transfer equals to the balance of source account minus the transfer amount after the transfer and balance of the destination account before the transfer plus the transfer amount equals to the balance of the destination account after the transfer. 199 # source account(before transfer)- transfer amount = destination account: 81259-5820=75439 200 # destination account (before transfer) + transfer amount = destination account: 839594+5820=845414 201 202 Then the following transfers should happen: 203 | from | to | from account | to account | market id | amount | asset | 204 | buySideProvider | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1120 | USD | 205 | market | sellSideProvider | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 5820 | USD | 206 207 Then the parties should have the following profit and loss: 208 | party | volume | unrealised pnl | realised pnl | 209 | designatedLoser | 0 | 0 | -17650 | 210 | sellSideProvider | -291 | 8720 | 0 | 211 | buySideProvider | 57 | -40 | -1622 | 212 | aux | 10 | 1055 | -436 | 213 | aux2 | -1 | 30 | 0 | 214 215 Then the parties should have the following account balances: 216 | party | asset | market id | margin | general | 217 | designatedLoser | USD | ETH/DEC19 | 0 | 0 | 218 219 And the insurance pool balance should be "0" for the market "ETH/DEC19" 220 When the oracles broadcast data signed with "0xCAFECAFE": 221 | name | value | 222 | trading.terminated | true | 223 And time is updated to "2020-01-01T01:01:01Z" 224 Then the market state should be "STATE_TRADING_TERMINATED" for the market "ETH/DEC19" 225 Then the oracles broadcast data signed with "0xCAFECAFE": 226 | name | value | 227 | prices.ETH.value | 80 | 228 229 When the network moves ahead "3" blocks 230 # When a market is closed, the insurance pool account has its outstanding funds transferred to the [network treasury] 231 Then the global insurance pool balance should be "0" for the asset "USD" 232 And the insurance pool balance should be "0" for the market "ETH/DEC19" 233 234 @Liquidation 235 Scenario: 002 create a suicidal trade from "designatedLoser" to get closeout immediately after trade 236 237 # setup accounts 238 Given the parties deposit on asset's general account the following amount: 239 | party | asset | amount | 240 | sellSideProvider | USD | 1000000000000 | 241 | buySideProvider | USD | 1000000000000 | 242 | designatedLoser | USD | 22000 | 243 | aux | USD | 1000000000000 | 244 | aux2 | USD | 1000000000000 | 245 | lpprov | USD | 1000000000000 | 246 247 When the parties submit the following liquidity provision: 248 | id | party | market id | commitment amount | fee | lp type | 249 | lp1 | lpprov | ETH/DEC19 | 9000 | 0.1 | submission | 250 | lp1 | lpprov | ETH/DEC19 | 9000 | 0.1 | amendment | 251 Then the parties place the following orders: 252 | party | market id | side | volume | price | resulting trades | type | tif | 253 | lpprov | ETH/DEC19 | buy | 225 | 40 | 0 | TYPE_LIMIT | TIF_GTC | 254 | lpprov | ETH/DEC19 | sell | 36 | 250 | 0 | TYPE_LIMIT | TIF_GTC | 255 256 # place auxiliary orders so we always have best bid and best offer as to not trigger the liquidity auction 257 Then the parties place the following orders: 258 | party | market id | side | volume | price | resulting trades | type | tif | 259 | aux | ETH/DEC19 | buy | 10 | 1 | 0 | TYPE_LIMIT | TIF_GTC | 260 | aux | ETH/DEC19 | sell | 10 | 2000 | 0 | TYPE_LIMIT | TIF_GTC | 261 | aux | ETH/DEC19 | buy | 1 | 150 | 0 | TYPE_LIMIT | TIF_GTC | 262 | aux2 | ETH/DEC19 | sell | 1 | 150 | 0 | TYPE_LIMIT | TIF_GTC | 263 Then the opening auction period ends for market "ETH/DEC19" 264 And the mark price should be "150" for the market "ETH/DEC19" 265 And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19" 266 267 When the parties place the following orders with ticks: 268 | party | market id | side | volume | price | resulting trades | type | tif | reference | 269 | sellSideProvider | ETH/DEC19 | sell | 290 | 150 | 0 | TYPE_LIMIT | TIF_GTC | sell-provider-1 | 270 | buySideProvider | ETH/DEC19 | buy | 100 | 140 | 0 | TYPE_LIMIT | TIF_GTC | buy-provider-1 | 271 272 And the market data for the market "ETH/DEC19" should be: 273 | mark price | trading mode | target stake | supplied stake | open interest | 274 | 150 | TRADING_MODE_CONTINUOUS | 731 | 9000 | 1 | 275 276 Then the order book should have the following volumes for market "ETH/DEC19": 277 | side | volume | price | 278 | buy | 10 | 1 | 279 | buy | 225 | 40 | 280 | buy | 100 | 140 | 281 | sell | 290 | 150 | 282 | sell | 36 | 250 | 283 | sell | 10 | 2000 | 284 285 Then the parties should have the following profit and loss: 286 | party | volume | unrealised pnl | realised pnl | 287 | aux | 1 | 0 | 0 | 288 | aux2 | -1 | 0 | 0 | 289 290 When the parties place the following orders with ticks: 291 | party | market id | side | volume | price | resulting trades | type | tif | reference | 292 | designatedLoser | ETH/DEC19 | buy | 290 | 150 | 1 | TYPE_LIMIT | TIF_GTC | ref-1 | 293 And the network moves ahead "1" blocks 294 295 Then the parties should have the following account balances: 296 | party | asset | market id | margin | general | 297 | designatedLoser | USD | ETH/DEC19 | 0 | 0 | 298 299 Then the parties should have the following margin levels: 300 | party | market id | maintenance | search | initial | release | 301 | designatedLoser | ETH/DEC19 | 0 | 0 | 0 | 0 | 302 303 Then the parties should have the following profit and loss: 304 | party | volume | unrealised pnl | realised pnl | 305 | designatedLoser | 0 | 0 | -17650 | 306 | sellSideProvider | -290 | 0 | 0 | 307 308 And the insurance pool balance should be "0" for the market "ETH/DEC19" 309 310 311