Skip to content

Commit

Permalink
Merge pull request #10268 from vegaprotocol/10154
Browse files Browse the repository at this point in the history
feat: move remaining insurance pool into network treasury rather than…
  • Loading branch information
ze97286 authored Dec 19, 2023
2 parents 1b15989 + e133ca6 commit 6cceff6
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 179 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- [10218](https://github.com/vegaprotocol/vega/issues/10218) - Volume discount stats shows volumes even if party doesn't qualify for a discount tier.
- [9880](https://github.com/vegaprotocol/vega/issues/9880) - Add support for batch proposals.
- [10159](https://github.com/vegaprotocol/vega/issues/10159) - Add additional funding period data to market data API to allow streaming funding period data.
-[10154](https://github.com/vegaprotocol/vega/issues/10154) - Move remaining insurance pool balance into the network treasury rather than splitting between other markets and global insurance.

### 🐛 Fixes

Expand Down
17 changes: 1 addition & 16 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3294,24 +3294,10 @@ func (e *Engine) ClearInsurancepool(ctx context.Context, mktID, asset string, cl
return nil, nil
}

// get all other market insurance accounts for the same asset
sortedAccKeys := maps.Keys(e.accs)
sort.Strings(sortedAccKeys)

var insuranceAccounts []*types.Account
for _, accKey := range sortedAccKeys {
acc := e.accs[accKey]
if acc.ID != marketInsuranceID && acc.Asset == asset && acc.Type == types.AccountTypeInsurance {
insuranceAccounts = append(insuranceAccounts, acc.Clone())
}
}

// add the global insurance account
globalIns, _ := e.GetGlobalInsuranceAccount(asset)
insuranceAccounts = append(insuranceAccounts, globalIns)
// redistribute market insurance funds between the global and other markets equally
req.FromAccount[0] = marketInsuranceAcc
req.ToAccount = insuranceAccounts
req.ToAccount[0] = globalIns
req.Amount = marketInsuranceAcc.Balance.Clone()
insuranceLedgerEntries, err := e.getLedgerEntries(ctx, req)
if err != nil {
Expand All @@ -3327,7 +3313,6 @@ func (e *Engine) ClearInsurancepool(ctx context.Context, mktID, asset string, cl
}
resp = append(resp, insuranceLedgerEntries)
e.removeAccount(marketInsuranceID)

return resp, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Feature: Test settlement at expiry with decimal places for asset and market (dif
And the global insurance pool balance should be "2000000000" for the asset "ETH"
And the insurance pool balance should be "0" for the market "ETH/DEC21"


Scenario: Same as above, but the other market already terminated before the end of scenario, expecting 0 balances in per market insurance pools - all should go to per asset insurance pool (0002-STTL-additional-tests, 0005-COLL-002, 0015-INSR-002, 0032-PRIM-018)

Given the initial insurance pool balance is "1000000000" for all the markets
Expand Down Expand Up @@ -335,8 +336,8 @@ Feature: Test settlement at expiry with decimal places for asset and market (dif
Then the market state should be "STATE_SETTLED" for the market "ETH/DEC21"
And the network moves ahead "6" blocks
And the insurance pool balance should be "0" for the market "ETH/DEC21"
And the insurance pool balance should be "1500000000" for the market "ETH/DEC19"
And the global insurance pool balance should be "500000000" for the asset "ETH"
And the insurance pool balance should be "1000000000" for the market "ETH/DEC19"
And the global insurance pool balance should be "1000000000" for the asset "ETH"

Then the market state should be "STATE_ACTIVE" for the market "ETH/DEC19"

Expand Down Expand Up @@ -466,9 +467,9 @@ Feature: Test settlement at expiry with decimal places for asset and market (dif
When the network moves ahead "2" blocks
And the cumulated balance for all accounts should be worth "10021300000000"
And the insurance pool balance should be "0" for the market "ETH/DEC19"
# 916 were taken from the insurance pool to cover the losses of party 2, the remaining is split between global and the other market
And the global insurance pool balance should be "4200000" for the asset "ETH"
And the insurance pool balance should be "104200000" for the market "ETH/DEC21"
# 916 were taken from the insurance pool to cover the losses of party 2, the remaining is moved to the global insurance account
And the global insurance pool balance should be "8400000" for the asset "ETH"
And the insurance pool balance should be "100000000" for the market "ETH/DEC21"

@SLABug
Scenario: Settlement happened when market is being closed - loss socialisation in action - insurance doesn't cover all losses (0002-STTL-009)
Expand Down Expand Up @@ -649,8 +650,8 @@ Feature: Test settlement at expiry with decimal places for asset and market (dif

And the cumulated balance for all accounts should be worth "10201200000000"
And the insurance pool balance should be "0" for the market "ETH/DEC21"
And the global insurance pool balance should be "25000000" for the asset "ETH"
And the insurance pool balance should be "75000000" for the market "ETH/DEC19"
And the global insurance pool balance should be "50000000" for the asset "ETH"
And the insurance pool balance should be "50000000" for the market "ETH/DEC19"



Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ Feature: Test the transfers to and from the insurance pools when markets termina
And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC21"
And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC22"
And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC23"
And the insurance pool balance should be "1200000" for the market "ETH/DEC19"
And the insurance pool balance should be "1200000" for the market "ETH/DEC20"
And the insurance pool balance should be "1200000" for the market "ETH/DEC21"
And the insurance pool balance should be "1200000" for the market "ETH/DEC22"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "1000000" for the market "ETH/DEC21"
And the insurance pool balance should be "1000000" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "200000" for the asset "ETH"
And the global insurance pool balance should be "1000000" for the asset "ETH"

# OK, let's terminate a market via governance that is in continuous trading
# The successor time window now comes in to play
Expand All @@ -195,12 +195,12 @@ Feature: Test the transfers to and from the insurance pools when markets termina
And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC22"
And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC23"
# insurance pool is not yet distributed
And the insurance pool balance should be "1200000" for the market "ETH/DEC19"
And the insurance pool balance should be "1200000" for the market "ETH/DEC20"
And the insurance pool balance should be "1200000" for the market "ETH/DEC21"
And the insurance pool balance should be "1200000" for the market "ETH/DEC22"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "1000000" for the market "ETH/DEC21"
And the insurance pool balance should be "1000000" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "200000" for the asset "ETH"
And the global insurance pool balance should be "1000000" for the asset "ETH"
# pass the successor time window
When the network moves ahead "10" blocks
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC20"
Expand All @@ -209,50 +209,50 @@ Feature: Test the transfers to and from the insurance pools when markets termina
#And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC22"
#And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC23"
## now we should see the update
And the insurance pool balance should be "1500000" for the market "ETH/DEC19"
And the insurance pool balance should be "1500000" for the market "ETH/DEC20"
And the insurance pool balance should be "1500000" for the market "ETH/DEC21"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "1000000" for the market "ETH/DEC21"
## Nothing in the drained pool, nothing goes to the insurance pool of the old market
And the insurance pool balance should be "0" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "500000" for the asset "ETH"
And the global insurance pool balance should be "2000000" for the asset "ETH"

## Now settle a market via the oracle
When the oracles broadcast data signed with "0xCAFECAFE2":
| name | value |
| trading.terminated | true |
And the network moves ahead "1" blocks
Then the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC21"
And the insurance pool balance should be "1500000" for the market "ETH/DEC19"
And the insurance pool balance should be "1500000" for the market "ETH/DEC20"
And the insurance pool balance should be "1500000" for the market "ETH/DEC21"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "1000000" for the market "ETH/DEC21"
## Nothing in the drained pool, nothing goes to the insurance pool of the old market
And the insurance pool balance should be "0" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "500000" for the asset "ETH"
And the global insurance pool balance should be "2000000" for the asset "ETH"
# Moving past the successor window means nothing here, market is not settled
When the network moves ahead "10" blocks
Then the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC21"
And the insurance pool balance should be "1500000" for the market "ETH/DEC19"
And the insurance pool balance should be "1500000" for the market "ETH/DEC20"
And the insurance pool balance should be "1500000" for the market "ETH/DEC21"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "1000000" for the market "ETH/DEC21"
## Nothing in the drained pool, nothing goes to the insurance pool of the old market
And the insurance pool balance should be "0" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "500000" for the asset "ETH"
And the global insurance pool balance should be "2000000" for the asset "ETH"

## Now settle the terminated market
When the oracles broadcast data signed with "0xCAFECAFE2":
| name | value |
| prices.ETH.value | 150 |
And the network moves ahead "10" blocks
Then the insurance pool balance should be "2000000" for the market "ETH/DEC19"
And the insurance pool balance should be "2000000" for the market "ETH/DEC20"
Then the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the insurance pool balance should be "0" for the market "ETH/DEC21"
## Nothing in the drained pool, nothing goes to the insurance pool of the old market
And the insurance pool balance should be "0" for the market "ETH/DEC22"
And the insurance pool balance should be "0" for the market "ETH/DEC23"
And the global insurance pool balance should be "1000000" for the asset "ETH"
And the global insurance pool balance should be "3000000" for the asset "ETH"

## Now terminate both of the other markets
When the oracles broadcast data signed with "0xCAFECAFE1":
Expand All @@ -263,17 +263,17 @@ Feature: Test the transfers to and from the insurance pools when markets termina
| trading.terminated | true |
Then the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC20"
And the trading mode should be "TRADING_MODE_NO_TRADING" for the market "ETH/DEC19"
And the insurance pool balance should be "2000000" for the market "ETH/DEC19"
And the insurance pool balance should be "2000000" for the market "ETH/DEC20"
And the global insurance pool balance should be "1000000" for the asset "ETH"
And the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the insurance pool balance should be "1000000" for the market "ETH/DEC20"
And the global insurance pool balance should be "3000000" for the asset "ETH"

## Now settle one market, pass the successor window and ensure the insurance pool is divided between the global insurance pool and the terminated market
When the oracles broadcast data signed with "0xCAFECAFE1":
| name | value |
| prices.ETH.value | 150 |
And the network moves ahead "10" blocks
Then the insurance pool balance should be "3000000" for the market "ETH/DEC19"
And the global insurance pool balance should be "2000000" for the asset "ETH"
Then the insurance pool balance should be "1000000" for the market "ETH/DEC19"
And the global insurance pool balance should be "4000000" for the asset "ETH"
## the insurance pools from the settled/cancelled markets are all drained
And the insurance pool balance should be "0" for the market "ETH/DEC20"
And the insurance pool balance should be "0" for the market "ETH/DEC21"
Expand All @@ -285,7 +285,7 @@ Feature: Test the transfers to and from the insurance pools when markets termina
| name | value |
| prices.ETH.value | 150 |
And the network moves ahead "10" blocks
And the global insurance pool balance should be "5000000" for the asset "ETH"
And the global insurance pool balance should be "5000000" for the asset "ETH"
## the insurance pools from the settled/cancelled markets are all drained
Then the insurance pool balance should be "0" for the market "ETH/DEC19"
And the insurance pool balance should be "0" for the market "ETH/DEC20"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ Feature: Test settlement at expiry time from internal oracle

And the network moves ahead "3" blocks
And the insurance pool balance should be "0" for the market "ETH/DEC21"
And the insurance pool balance should be "15000" for the market "ETH/DEC19"

And the insurance pool balance should be "10000" for the market "ETH/DEC19"
Then the market state should be "STATE_ACTIVE" for the market "ETH/DEC19"

When the parties place the following orders with ticks:
Expand Down Expand Up @@ -421,8 +420,9 @@ Feature: Test settlement at expiry time from internal oracle
When the network moves ahead "2" blocks
Then the cumulated balance for all accounts should be worth "100213000"
And the insurance pool balance should be "0" for the market "ETH/DEC19"
# 916 were taken from the insurance pool to cover the losses of party 2, the remaining is split between global and the other market
And the insurance pool balance should be "1042" for the market "ETH/DEC21"
# 916 were taken from the insurance pool to cover the losses of party 2, the remaining is moved to the global insurance pool
And the insurance pool balance should be "1000" for the market "ETH/DEC21"
And the global insurance pool balance should be "84" for the asset "ETH"

@SLABug
Scenario: Settlement happened when market is being closed - loss socialisation in action - insurance doesn't cover all losses
Expand Down Expand Up @@ -598,7 +598,8 @@ Feature: Test settlement at expiry time from internal oracle

And the cumulated balance for all accounts should be worth "102012000"
And the insurance pool balance should be "0" for the market "ETH/DEC21"
And the insurance pool balance should be "750" for the market "ETH/DEC19"
And the insurance pool balance should be "500" for the market "ETH/DEC19"
And the global insurance pool balance should be "500" for the asset "ETH"

@Oracle
Scenario: Orders can still be placed if the order expiry was sent with the wrong pub key
Expand Down
Loading

0 comments on commit 6cceff6

Please sign in to comment.