Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type to loss socialisation event #11625

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
- [11577](https://github.com/vegaprotocol/vega/issues/11577) - Add API for party discounts and rewards.
- [10716](https://github.com/vegaprotocol/vega/issues/10716) - Set Tendermint defaults during init.
- [11624](https://github.com/vegaprotocol/vega/issues/11624) - prevent creation of rewards with no payout, but with high computational cost.
- [11512](https://github.com/vegaprotocol/vega/issues/11512) - Add loss socialisation amounts to funding payment API.

### 🐛 Fixes

Expand Down
11 changes: 6 additions & 5 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers
logging.String("market-id", settle.MarketID))

brokerEvts = append(brokerEvts,
events.NewLossSocializationEvent(ctx, transfer.Owner, marketID, delta, false, now))
events.NewLossSocializationEvent(ctx, transfer.Owner, marketID, delta, false, now, types.LossTypeUnspecified))
}
}
}
Expand Down Expand Up @@ -1476,16 +1476,16 @@ func (e *Engine) getMTMPartyAccounts(party, marketID, asset string) (gen, margin
// PerpsFundingSettlement will run a funding settlement over given positions.
// This works exactly the same as a MTM settlement, but uses different transfer types.
func (e *Engine) PerpsFundingSettlement(ctx context.Context, marketID string, transfers []events.Transfer, asset string, round *num.Uint, useGeneralAccountForMarginSearch func(string) bool) ([]events.Margin, []*types.LedgerMovement, error) {
return e.mtmOrFundingSettlement(ctx, marketID, transfers, asset, types.TransferTypePerpFundingWin, round, useGeneralAccountForMarginSearch)
return e.mtmOrFundingSettlement(ctx, marketID, transfers, asset, types.TransferTypePerpFundingWin, round, useGeneralAccountForMarginSearch, types.LossTypeFunding)
}

// MarkToMarket will run the mark to market settlement over a given set of positions
// return ledger move stuff here, too (separate return value, because we need to stream those).
func (e *Engine) MarkToMarket(ctx context.Context, marketID string, transfers []events.Transfer, asset string, useGeneralAccountForMarginSearch func(string) bool) ([]events.Margin, []*types.LedgerMovement, error) {
return e.mtmOrFundingSettlement(ctx, marketID, transfers, asset, types.TransferTypeMTMWin, nil, useGeneralAccountForMarginSearch)
return e.mtmOrFundingSettlement(ctx, marketID, transfers, asset, types.TransferTypeMTMWin, nil, useGeneralAccountForMarginSearch, types.LossTypeSettlement)
}

func (e *Engine) mtmOrFundingSettlement(ctx context.Context, marketID string, transfers []events.Transfer, asset string, winType types.TransferType, round *num.Uint, useGeneralAccountForMarginSearch func(string) bool) ([]events.Margin, []*types.LedgerMovement, error) {
func (e *Engine) mtmOrFundingSettlement(ctx context.Context, marketID string, transfers []events.Transfer, asset string, winType types.TransferType, round *num.Uint, useGeneralAccountForMarginSearch func(string) bool, lType types.LossType) ([]events.Margin, []*types.LedgerMovement, error) {
// stop immediately if there aren't any transfers, channels are closed
if len(transfers) == 0 {
return nil, nil, nil
Expand Down Expand Up @@ -1616,7 +1616,7 @@ func (e *Engine) mtmOrFundingSettlement(ctx context.Context, marketID string, tr
logging.String("market-id", settle.MarketID))

brokerEvts = append(brokerEvts,
events.NewLossSocializationEvent(ctx, party, settle.MarketID, delta, false, now))
events.NewLossSocializationEvent(ctx, party, settle.MarketID, delta, false, now, lType))
}
marginEvts = append(marginEvts, marginEvt)
} else {
Expand Down Expand Up @@ -1652,6 +1652,7 @@ func (e *Engine) mtmOrFundingSettlement(ctx context.Context, marketID string, tr
collected: settle.Balance,
requests: make([]request, 0, len(transfers)-winidx),
ts: now,
lType: lType,
}

if distr.LossSocializationEnabled() {
Expand Down
7 changes: 5 additions & 2 deletions core/collateral/simple_distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type simpleDistributor struct {
collected *num.Uint
requests []request
ts int64
lType types.LossType
}

func (s *simpleDistributor) LossSocializationEnabled() bool {
Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *simpleDistributor) Run(ctx context.Context) []events.Event {
v := v
netReq = &v
}
evt = events.NewLossSocializationEvent(ctx, v.request.Owner, s.marketID, loss, true, s.ts)
evt = events.NewLossSocializationEvent(ctx, v.request.Owner, s.marketID, loss, true, s.ts, s.lType)
s.log.Warn("loss socialization missing funds to be distributed",
logging.String("party-id", evt.PartyID()),
logging.BigInt("amount", evt.Amount()),
Expand All @@ -99,7 +100,9 @@ func (s *simpleDistributor) Run(ctx context.Context) []events.Event {
evt.MarketID(),
loss,
!profit, // true if party still lost out, false if mismatch > shortfall
s.ts)
s.ts,
s.lType,
)
}
return evts
}
22 changes: 18 additions & 4 deletions core/events/loss_socialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package events
import (
"context"

"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
Expand All @@ -28,9 +29,10 @@ type LossSoc struct {
marketID string
amount *num.Int
ts int64
lType types.LossType
}

func NewLossSocializationEvent(ctx context.Context, partyID, marketID string, amount *num.Uint, neg bool, ts int64) *LossSoc {
func NewLossSocializationEvent(ctx context.Context, partyID, marketID string, amount *num.Uint, neg bool, ts int64, lType types.LossType) *LossSoc {
signedAmount := num.NewIntFromUint(amount)
if neg {
signedAmount.FlipSign()
Expand All @@ -41,9 +43,18 @@ func NewLossSocializationEvent(ctx context.Context, partyID, marketID string, am
marketID: marketID,
amount: signedAmount,
ts: ts,
lType: lType,
}
}

func (l LossSoc) LossType() types.LossType {
return l.lType
}

func (l LossSoc) IsFunding() bool {
return l.lType == types.LossTypeFunding
}

func (l LossSoc) IsParty(id string) bool {
return l.partyID == id
}
Expand Down Expand Up @@ -73,6 +84,7 @@ func (l LossSoc) Proto() eventspb.LossSocialization {
MarketId: l.marketID,
PartyId: l.partyID,
Amount: l.amount.String(),
LossType: l.lType,
}
}

Expand All @@ -88,12 +100,14 @@ func (l LossSoc) StreamMessage() *eventspb.BusEvent {
}

func LossSocializationEventFromStream(ctx context.Context, be *eventspb.BusEvent) *LossSoc {
ls := be.GetLossSocialization()
lse := &LossSoc{
Base: newBaseFromBusEvent(ctx, LossSocializationEvent, be),
partyID: be.GetLossSocialization().PartyId,
marketID: be.GetLossSocialization().MarketId,
partyID: ls.PartyId,
marketID: ls.MarketId,
lType: ls.LossType,
}

lse.amount, _ = num.IntFromString(be.GetLossSocialization().Amount, 10)
lse.amount, _ = num.IntFromString(ls.Amount, 10)
return lse
}
8 changes: 8 additions & 0 deletions core/integration/features/perpetual.feature
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ Feature: Simple test creating a perpetual market.
| start | end | internal twap | external twap | funding payment | funding rate |
| 1575072007 | 1575072014 | 9820000000000000 | 9750000000000000 | 70000000000000 | 0.0071794871794872 |
| 1575072014 | | 9890000000000000 | 9720000000000000 | | |
And the following funding payment events should be emitted:
| party | market | amount |
| trader2 | ETH/DEC19 | -100000000 |
| trader2 | ETH/DEC19 | 700000000 |
| trader1 | ETH/DEC19 | 100000000 |
| trader1 | ETH/DEC19 | -700000000 |
| trader3 | ETH/DEC19 | -1400000000 |
| trader4 | ETH/DEC19 | 1400000000 |
# payments for trader3 and trader4 should be twice those of trader1 and trader2
And the following transfers should happen:
| type | from | to | from account | to account | market id | amount | asset |
Expand Down
20 changes: 14 additions & 6 deletions core/integration/features/settlement/0053-PERP-039.feature
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,34 @@ Feature: If a market insurance pool does not have enough funds to cover a fundin
And the mark price should be "1200" for the market "ETH/DEC19"

When time is updated to "2021-08-12T11:04:12Z"
Then system unix time is "1628766252"
And system unix time is "1628766252"

When the oracles broadcast data with block time signed with "0xCAFECAFE1":
And the oracles broadcast data with block time signed with "0xCAFECAFE1":
| name | value | time offset |
| perp.funding.cue | 1628766252 | 0s |

And the following funding period events should be emitted:
Then the following funding period events should be emitted:
| start | end | internal twap | external twap | funding payment |
| 1612998252 | 1628766252 | 1200 | 6200 | -5000 |
| 1612998252 | 1628766252 | 1200 | 6200 | -5000 |

# funding payment is 5000000 but party "aux" only has 4793200
# check that loss socialisation has happened and that the insurance pool has been cleared to indicate
# that there wasn't enough in there to cover the funding payment hence the winning parties received a haircut
#And debug funding payment events
And the following funding payment events should be emitted:
| party | market | amount | loss amount | loss type |
| party2 | ETH/DEC19 | 5000000 | -68867 | TYPE_FUNDING_PAYMENT |
| party3 | ETH/DEC19 | 5000000 | -68866 | TYPE_FUNDING_PAYMENT |
| aux2 | ETH/DEC19 | 5000000 | -68867 | TYPE_FUNDING_PAYMENT |
| aux | ETH/DEC19 | -5000000 | 206600 | TYPE_FUNDING_PAYMENT |
| party1 | ETH/DEC19 | -10000000 | | |
And the following transfers should happen:
| from | to | from account | to account | market id | amount | asset | type |
| party1 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1008000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| party1 | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 8992000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| aux | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 648000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| aux | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 648000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| aux | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 4145200 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| market | market | ACCOUNT_TYPE_INSURANCE | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 200 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| market | market | ACCOUNT_TYPE_INSURANCE | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 200 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| market | aux2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 4931133 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
| market | party2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 4931133 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
| market | party3 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 4931134 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
Expand Down
7 changes: 7 additions & 0 deletions core/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ func InitializeScenario(s *godog.ScenarioContext) {
s.Step(`^debug network parameter "([^"]*)"$`, func(name string) error {
return steps.DebugNetworkParameter(execsetup.log, execsetup.netParams, name)
})
s.Step(`^debug funding payment events$`, func() error {
steps.DebugFundingPaymentsEvents(execsetup.broker, execsetup.log)
return nil
})

// Event steps
s.Step(`^clear all events$`, func() error {
Expand All @@ -712,6 +716,9 @@ func InitializeScenario(s *godog.ScenarioContext) {
s.Step(`^the following funding period events should be emitted:$`, func(table *godog.Table) error {
return steps.TheFollowingFundingPeriodEventsShouldBeEmitted(execsetup.broker, table)
})
s.Step(`^the following funding payment events should be emitted:$`, func(table *godog.Table) error {
return steps.TheFollowingFundingPaymentEventsShouldBeEmitted(execsetup.broker, table)
})
s.Step(`^the following events should be emitted:$`, func(table *godog.Table) error {
return steps.TheFollowingEventsShouldBeEmitted(execsetup.broker, table)
})
Expand Down
Loading
Loading