Skip to content

Commit

Permalink
fix collateral engine transfer requests processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyletang committed Jul 2, 2021
1 parent 4e66299 commit 90c1cbe
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 30 deletions.
7 changes: 5 additions & 2 deletions collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ func (e *Engine) MarkToMarket(ctx context.Context, marketID string, transfers []
// 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)
Expand Down Expand Up @@ -579,8 +582,8 @@ func (e *Engine) MarkToMarket(ctx context.Context, marketID string, transfers []

// here we check if we were able to collect all monies,
// if not send an event to notify the plugins
if totalInAccount := num.Sum(marginAcc.Balance, generalAcc.Balance); totalInAccount.LT(req.Amount) {
delta := req.Amount.Sub(req.Amount, totalInAccount)
if totalInAccount := num.Sum(marginAcc.Balance, generalAcc.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", evt.Party()),
logging.BigUint("amount", delta),
Expand Down
4 changes: 1 addition & 3 deletions integration/features/1847-closeout-long-with-fees.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ Feature: Long close-out test (see ln 293 of system-tests/grpc/trading/tradesTest
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| tt_4 | 4 | -200 | 0 |
| tt_5 | 0 | 0 | -200 |
| tt_5 | 0 | 0 | -102 |
| tt_6 | -4 | 200 | -28 |
| tt_10 | 30 | 0 | 0 |
| tt_11 | -30 | 200 | -63 |
# This is after uint changes...
# | tt_5 | 0 | 0 | -102 |
4 changes: 1 addition & 3 deletions integration/features/1847-closeout-long.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ Feature: Long close-out test (see ln 293 of system-tests/grpc/trading/tradesTest
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| tt_4 | 4 | -200 | 0 |
| tt_5 | 0 | 0 | -200 |
| tt_5 | 0 | 0 | -100 |
| tt_6 | -4 | 200 | -27 |
| tt_10 | 30 | 0 | 0 |
| tt_11 | -30 | 200 | -65 |
# Again: a change after uint stuff
# | tt_5 | 0 | 0 | -100 |
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ Feature: Test loss socialization case 1
Then the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19"
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| trader1 | 0 | 0 | -8000 |
| trader1 | 0 | 0 | -5000 |
| trader2 | 200 | 8000 | -2970 |
| trader3 | -100 | 0 | 0 |
# value before uint stuff
#| trader1 | 0 | 0 | -5000 |
And the insurance pool balance should be "0" for the market "ETH/DEC19"
And the cumulated balance for all accounts should be worth "400105000"
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Feature: Test loss socialization case 2
# check positions
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| trader1 | 0 | 0 | -5000 |
| trader1 | 0 | 0 | -2500 |
| trader2 | 35 | 2000 | -1362 |
| trader3 | 75 | 6000 | -4083 |
| trader4 | -10 | 0 | 0 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ Feature: Test loss socialization case 3
# check positions
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| trader1 | 0 | 0 | -2400 |
| trader1 | 0 | 0 | -2000 |
| trader2 | 100 | 7200 | -2455 |
| trader3 | 0 | 0 | -5600 |
| trader3 | 0 | 0 | -3000 |
| trader4 | 0 | 0 | 528 |
# value before uint stuff
#| trader1 | 0 | 0 | -2000 |
#| trader3 | 0 | 0 | -3000 |
And the insurance pool balance should be "0" for the market "ETH/DEC19"
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ Feature: Test loss socialization case 4
# check positions
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| trader1 | 0 | 0 | -2400 |
| trader1 | 0 | 0 | -2000 |
| trader2 | 100 | 7200 | -90 |
| trader3 | 0 | 0 | -5600 |
| trader3 | 0 | 0 | -3000 |
| trader4 | 0 | 0 | 791 |
# value before uint stuff
#| trader1 | 0 | 0 | -2000 |
#| trader3 | 0 | 0 | -3000 |
And the insurance pool balance should be "0" for the market "ETH/DEC19"
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ Feature: Test loss socialization case 5
# check positions
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| trader1 | 0 | 0 | -2400 |
| trader1 | 0 | 0 | -2000 |
| trader2 | 100 | 7200 | 0 |
| trader3 | 0 | 0 | -5600 |
| trader3 | 0 | 0 | -3000 |
| trader4 | 0 | 0 | 800 |
# value before uint stuff
#| trader1 | 0 | 0 | -2000 |
#| trader3 | 0 | 0 | -3000 |
And the insurance pool balance should be "0" for the market "ETH/DEC19"
4 changes: 1 addition & 3 deletions integration/features/verified-positions-resolution-4.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ Feature: Position resolution case 4
#check positions
Then the traders should have the following profit and loss:
| trader | volume | unrealised pnl | realised pnl |
| designatedLooser | 0 | 0 | -11500 |
| designatedLooser | 0 | 0 | -10000 |
| buySideProvider | 101 | 11500 | -1363 |
# value before uint stuff
#| designatedLooser | 0 | 0 | -10000 |

# checking margins
Then the traders should have the following account balances:
Expand Down

0 comments on commit 90c1cbe

Please sign in to comment.