diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1f17ede3..815752291b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -158,6 +158,7 @@ - [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add filtering for party to the referral fees API. - [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add X day aggregate totals for referral set referees. - [2985](https://github.com/vegaprotocol/system-tests/issues/2985) - Coverage for insurance pool transfers, fix deadlock when terminating pending market through governance. +- [9770](https://github.com/vegaprotocol/vega/issues/9770) - Fix `PnL` flickering bug. ### 🐛 Fixes diff --git a/datanode/entities/position.go b/datanode/entities/position.go index e5014907e1..557a59ad39 100644 --- a/datanode/entities/position.go +++ b/datanode/entities/position.go @@ -115,18 +115,16 @@ func (p *Position) UpdateWithTrade(trade vega.Trade, seller bool, pf num.Decimal } marketPrice, _ := num.DecimalFromString(trade.Price) // this is market price // Scale the trade to the correct size - positionPrice := marketPrice.Mul(pf) opened, closed := CalculateOpenClosedVolume(p.PendingOpenVolume, size) - realisedPnlDelta := positionPrice.Sub(p.PendingAverageEntryPrice).Mul(num.DecimalFromInt64(closed)).Div(pf) + realisedPnlDelta := marketPrice.Sub(p.PendingAverageEntryPrice).Mul(num.DecimalFromInt64(closed)).Div(pf) p.PendingRealisedPnl = p.PendingRealisedPnl.Add(realisedPnlDelta) p.PendingOpenVolume -= closed - positionPriceUint, _ := num.UintFromDecimal(positionPrice) marketPriceUint, _ := num.UintFromDecimal(marketPrice) - p.PendingAverageEntryPrice = updateVWAP(p.PendingAverageEntryPrice, p.PendingOpenVolume, opened, positionPriceUint) + p.PendingAverageEntryPrice = updateVWAP(p.PendingAverageEntryPrice, p.PendingOpenVolume, opened, marketPriceUint) p.PendingAverageEntryMarketPrice = updateVWAP(p.PendingAverageEntryMarketPrice, p.PendingOpenVolume, opened, marketPriceUint) p.PendingOpenVolume += opened - p.pendingMTM(positionPrice, pf) + p.pendingMTM(marketPrice, pf) } func (p *Position) ApplyFundingPayment(amount *num.Int) { diff --git a/datanode/entities/position_test.go b/datanode/entities/position_test.go index 8fd3d9559e..0360da41cd 100644 --- a/datanode/entities/position_test.go +++ b/datanode/entities/position_test.go @@ -180,7 +180,7 @@ func TestPnLWithPositionDecimals(t *testing.T) { position.UpdateWithTrade(trade, false, dp) pp := position.ToProto() assert.Equal(t, "0", pp.RealisedPnl) - assert.Equal(t, "400", pp.UnrealisedPnl) + assert.Equal(t, "0", pp.UnrealisedPnl) // now MTM settlement event, contains the same trades, mark price is 1k ps := events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1000), []events.TradeSettlement{ tradeStub{ @@ -211,7 +211,7 @@ func TestPnLWithPositionDecimals(t *testing.T) { position.UpdateWithTrade(trade, false, dp) pp = position.ToProto() assert.Equal(t, "0", pp.RealisedPnl) - assert.Equal(t, "5744", pp.UnrealisedPnl) + assert.Equal(t, "0", pp.UnrealisedPnl) assert.EqualValues(t, 6, pp.OpenVolume) // now assume this last trade was the only trade that occurred before MTM ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1150), []events.TradeSettlement{ @@ -236,8 +236,8 @@ func TestPnLWithPositionDecimals(t *testing.T) { } position.UpdateWithTrade(trade, true, dp) pp = position.ToProto() - assert.Equal(t, "1249", pp.RealisedPnl) - assert.Equal(t, "6244", pp.UnrealisedPnl) + assert.Equal(t, "0", pp.RealisedPnl) + assert.Equal(t, "1", pp.UnrealisedPnl) assert.EqualValues(t, 5, pp.OpenVolume) ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1250), []events.TradeSettlement{ tradeStub{ @@ -261,7 +261,7 @@ func TestPnLWithPositionDecimals(t *testing.T) { } position.UpdateWithTrade(trade, true, dp) pp = position.ToProto() - assert.Equal(t, "6495", pp.RealisedPnl) + assert.Equal(t, "1", pp.RealisedPnl) assert.Equal(t, "0", pp.UnrealisedPnl) assert.EqualValues(t, 0, pp.OpenVolume) ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1250), []events.TradeSettlement{ @@ -318,7 +318,7 @@ func TestPnLWithTradeDecimals(t *testing.T) { position.UpdateWithTrade(trade, false, dp) pp = position.ToProto() assert.Equal(t, "-300", pp.RealisedPnl) - assert.Equal(t, "3883", pp.UnrealisedPnl) + assert.Equal(t, "50", pp.UnrealisedPnl) } type tradeStub struct { diff --git a/datanode/networkhistory/service_test.go b/datanode/networkhistory/service_test.go index b21fef32a8..3a432ae31c 100644 --- a/datanode/networkhistory/service_test.go +++ b/datanode/networkhistory/service_test.go @@ -378,12 +378,12 @@ func TestMain(t *testing.M) { log.Infof("%s", goldenSourceHistorySegment[4000].HistorySegmentID) log.Infof("%s", goldenSourceHistorySegment[5000].HistorySegmentID) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmQYY51bDtR745ZhiiU2SMfYtnnGMVgXVXVCez5jacftto", snapshots) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmcPW35HdsBUgbQNiJZms56E5TUYvKyeWY8hGbFAqADn7r", snapshots) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmYxNwnS7CgzLwBTJXCjvYeFFQWQtUzEERgUSfr74stsen", snapshots) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmZ1BDgrU3qHgT1fnfZoaJCnSmghcerZg2rMp6xw8G2TcR", snapshots) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmTHvtzTVCRbMvNFpX68SAy2sQBgfccwnwy3Hu1jycy9T6", snapshots) - panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmW83by4cR7WULEVirZqPgbKA9xZbGsEEMhsYMEKf3BPnq", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmadLDsehNEvYzhdBrGKQf3Y4nPXTvKTEEuvEnWVvt9m9x", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmVwvKA843CyqjT63JFMJhT5QamRjL6q4qzFayRQNYWVbb", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmVEWvqdAUQ8xmAdBKKYtyGoqd3T3hmFGikhEUzed6HRSF", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmZ88jdbhYatm9wKD4gEUkqsd5Wnjnw1PLBWbLFWDMGkGt", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmVy9nvUAXAjFazm4wpCcQJVudzmdwLipHuu4RTnS4i1kR", snapshots) + panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmTKSv5ofDsVLnzgy4FCRLHABdknskVo3kinadeYqXBwWj", snapshots) }, postgresRuntimePath, sqlFs) if exitCode != 0 {