-
Notifications
You must be signed in to change notification settings - Fork 19
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
added loss socialisation to final settlement #3745
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -442,7 +442,16 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers | |
} | ||
responses := make([]*types.TransferResponse, 0, len(transfers)) | ||
asset := transfers[0].Amount.Asset | ||
// This is where we'll implement everything | ||
|
||
var ( | ||
winidx int | ||
expectCollected num.Decimal | ||
expCollected = num.Zero() | ||
totalAmountCollected = num.Zero() | ||
) | ||
|
||
brokerEvts := make([]events.Event, 0, len(transfers)) | ||
|
||
settle, insurance, err := e.getSystemAccounts(marketID, asset) | ||
if err != nil { | ||
e.log.Error( | ||
|
@@ -451,8 +460,123 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers | |
) | ||
return nil, err | ||
} | ||
// get the component that calculates the loss socialisation etc... if needed | ||
for _, transfer := range transfers { | ||
|
||
// process loses first | ||
for i, transfer := range transfers { | ||
if transfer == nil { | ||
continue | ||
} | ||
if transfer.Type == types.TransferType_TRANSFER_TYPE_WIN { | ||
// we processed all losses break then | ||
winidx = i | ||
break | ||
} | ||
|
||
req, err := e.getTransferRequest(ctx, transfer, settle, insurance, &marginUpdate{}) | ||
if err != nil { | ||
e.log.Error( | ||
"Failed to build transfer request for event", | ||
logging.Error(err), | ||
) | ||
return nil, err | ||
} | ||
|
||
// accumulate the expected transfer size | ||
expCollected.AddSum(req.Amount) | ||
expectCollected = expectCollected.Add(num.DecimalFromUint(req.Amount)) | ||
// doing a copy of the amount here, as the request is send to getLedgerEntries, which actually | ||
// modifies it | ||
requestAmount := req.Amount.Clone() | ||
|
||
// set the amount (this can change the req.Amount value if we entered loss socialisation | ||
res, err := e.getLedgerEntries(ctx, req) | ||
if err != nil { | ||
e.log.Error( | ||
"Failed to transfer funds", | ||
logging.Error(err), | ||
) | ||
return nil, err | ||
} | ||
amountCollected := num.Zero() | ||
|
||
for _, bal := range res.Balances { | ||
amountCollected.AddSum(bal.Balance) | ||
if err := e.UpdateBalance(ctx, bal.Account.Id, bal.Balance); err != nil { | ||
e.log.Error( | ||
"Could not update the target account in transfer", | ||
logging.String("account-id", bal.Account.Id), | ||
logging.Error(err), | ||
) | ||
return nil, err | ||
} | ||
} | ||
totalAmountCollected.AddSum(amountCollected) | ||
responses = append(responses, res) | ||
|
||
// Update to see how much we still need | ||
requestAmount = requestAmount.Sub(requestAmount, amountCollected) | ||
if transfer.Owner != types.NetworkParty { | ||
// no error possible here, we're just reloading the accounts to ensure the correct balance | ||
general, margin, _ := e.getMTMPartyAccounts(transfer.Owner, marketID, asset) | ||
if totalInAccount := num.Sum(general.Balance, margin.Balance); totalInAccount.LT(requestAmount) { | ||
delta := req.Amount.Sub(requestAmount, totalInAccount) | ||
e.log.Warn("loss socialization missing amount to be collected or used from insurance pool", | ||
logging.String("party-id", transfer.Owner), | ||
logging.BigUint("amount", delta), | ||
logging.String("market-id", settle.MarketId)) | ||
|
||
brokerEvts = append(brokerEvts, | ||
events.NewLossSocializationEvent(ctx, transfer.Owner, marketID, delta, false, e.currentTime)) | ||
} | ||
} | ||
} | ||
|
||
if len(brokerEvts) > 0 { | ||
e.broker.SendBatch(brokerEvts) | ||
} | ||
|
||
// if winidx is 0, this means we had now win and loss, but may have some event which | ||
// needs to be propagated forward so we return now. | ||
if winidx == 0 { | ||
if !settle.Balance.IsZero() { | ||
return nil, ErrSettlementBalanceNotZero | ||
jeremyletang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return responses, nil | ||
} | ||
|
||
// now compare what's in the settlement account what we expect initially to redistribute. | ||
// if there's not enough we enter loss socialization | ||
distr := simpleDistributor{ | ||
log: e.log, | ||
marketID: marketID, | ||
expectCollected: expCollected, | ||
collected: totalAmountCollected, | ||
requests: make([]request, 0, len(transfers)-winidx), | ||
ts: e.currentTime, | ||
} | ||
|
||
if distr.LossSocializationEnabled() { | ||
e.log.Warn("Entering loss socialization on final settlement", | ||
logging.String("market-id", marketID), | ||
logging.String("asset", asset), | ||
logging.BigUint("expect-collected", expCollected), | ||
logging.BigUint("collected", settle.Balance)) | ||
for _, transfer := range transfers[winidx:] { | ||
if transfer != nil && transfer.Type == types.TransferType_TRANSFER_TYPE_WIN { | ||
distr.Add(transfer) | ||
} | ||
} | ||
if evts := distr.Run(ctx); len(evts) != 0 { | ||
e.broker.SendBatch(evts) | ||
} | ||
} | ||
|
||
// then we process all the wins | ||
for _, transfer := range transfers[winidx:] { | ||
if transfer == nil { | ||
continue | ||
} | ||
|
||
req, err := e.getTransferRequest(ctx, transfer, settle, insurance, &marginUpdate{}) | ||
if err != nil { | ||
e.log.Error( | ||
|
@@ -461,6 +585,7 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers | |
) | ||
return nil, err | ||
} | ||
|
||
// set the amount (this can change the req.Amount value if we entered loss socialisation | ||
res, err := e.getLedgerEntries(ctx, req) | ||
if err != nil { | ||
|
@@ -470,6 +595,8 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers | |
) | ||
return nil, err | ||
} | ||
|
||
// update the to accounts now | ||
for _, bal := range res.Balances { | ||
if err := e.UpdateBalance(ctx, bal.Account.Id, bal.Balance); err != nil { | ||
e.log.Error( | ||
|
@@ -482,6 +609,11 @@ func (e *Engine) FinalSettlement(ctx context.Context, marketID string, transfers | |
} | ||
responses = append(responses, res) | ||
} | ||
|
||
if !settle.Balance.IsZero() { | ||
return nil, ErrSettlementBalanceNotZero | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, this should probably be a panic thinking of it? |
||
} | ||
|
||
return responses, nil | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this is a decimal