Skip to content

Commit

Permalink
Merge pull request #9748 from vegaprotocol/9746
Browse files Browse the repository at this point in the history
fix: LP fees reward handling + destination type
  • Loading branch information
ze97286 authored Oct 11, 2023
2 parents 20b860f + ddf22f7 commit a62308b
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 73 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@
- [9734](https://github.com/vegaprotocol/vega/issues/9734) - Fix creation of new account types for existing assets during migration
- [9731](https://github.com/vegaprotocol/vega/issues/9731) - Allow team rewards to apply to all teams.
- [9727](https://github.com/vegaprotocol/vega/issues/9727) - Initialize teams earlier to avoid panic.
- [9746](https://github.com/vegaprotocol/vega/issues/9746) - Fix handling of LP fees reward
- [9747](https://github.com/vegaprotocol/vega/issues/9747) - Return correct destination type
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add filtering for party to the referral fees API.
- [9751](https://github.com/vegaprotocol/vega/issues/9751) - Make sure that LP fee party accounts exists.

Expand Down
4 changes: 3 additions & 1 deletion core/execution/common/liquidity_provision_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (m *MarketLiquidity) AllocateFees(ctx context.Context) error {
return nil
}

m.marketActivityTracker.UpdateFeesFromTransfers(m.asset, m.marketID, feeTransfer.Transfers())
ledgerMovements, err := m.transferFees(ctx, feeTransfer)
if err != nil {
return fmt.Errorf("failed to transfer fees: %w", err)
Expand Down Expand Up @@ -212,6 +211,8 @@ func (m *MarketLiquidity) distributeFeesAndCalculateBonuses(
bonusPerParty[partyID] = bonus
}

m.marketActivityTracker.UpdateFeesFromTransfers(m.asset, m.marketID, allTransfers.transfers)

// transfer all the fees.
ledgerMovements, err := m.transferFees(ctx, allTransfers)
if err != nil {
Expand Down Expand Up @@ -279,6 +280,7 @@ func (m *MarketLiquidity) distributePerformanceBonuses(
bonusTransfers.transfers = append(bonusTransfers.transfers, transfer)
}

m.marketActivityTracker.UpdateFeesFromTransfers(m.asset, m.marketID, bonusTransfers.transfers)
ledgerMovements, err := m.transferFees(ctx, bonusTransfers)
if err != nil {
m.log.Panic("failed to distribute SLA bonuses", logging.Error(err))
Expand Down
3 changes: 2 additions & 1 deletion core/execution/common/liquidity_provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func newMarketLiquidity(t *testing.T) *marketLiquidityTest {

teams := mocks.NewMockTeams(ctrl)
bc := mocks.NewMockAccountBalanceChecker(ctrl)
marketTracker := common.NewMarketActivityTracker(logging.NewTestLogger(), epochEngine, teams, bc)
marketTracker := common.NewMarketActivityTracker(logging.NewTestLogger(), teams, bc)
epochEngine.NotifyOnEpoch(marketTracker.OnEpochEvent, marketTracker.OnEpochRestore)

marketLiquidity := common.NewMarketLiquidity(
log,
Expand Down
7 changes: 3 additions & 4 deletions core/execution/common/market_activity_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type MarketActivityTracker struct {
}

// NewMarketActivityTracker instantiates the fees tracker.
func NewMarketActivityTracker(log *logging.Logger, epochEngine EpochEngine, teams Teams, balanceChecker AccountBalanceChecker) *MarketActivityTracker {
func NewMarketActivityTracker(log *logging.Logger, teams Teams, balanceChecker AccountBalanceChecker) *MarketActivityTracker {
mat := &MarketActivityTracker{
assetToMarketTrackers: map[string]map[string]*marketTracker{},
ss: &snapshotState{},
Expand All @@ -118,7 +118,6 @@ func NewMarketActivityTracker(log *logging.Logger, epochEngine EpochEngine, team
partyTakerNotionalVolume: map[string]*num.Uint{},
}

epochEngine.NotifyOnEpoch(mat.onEpochEvent, mat.onEpochRestore)
return mat
}

Expand Down Expand Up @@ -365,7 +364,7 @@ func (mat *MarketActivityTracker) RemoveMarket(asset, marketID string) {
}

// onEpochEvent is called when the state of the epoch changes, we only care about new epochs starting.
func (mat *MarketActivityTracker) onEpochEvent(_ context.Context, epoch types.Epoch) {
func (mat *MarketActivityTracker) OnEpochEvent(_ context.Context, epoch types.Epoch) {
if epoch.Action == proto.EpochAction_EPOCH_ACTION_START {
mat.epochStartTime = epoch.StartTime
mat.partyContributionCache = map[string][]*types.PartyContributionScore{}
Expand Down Expand Up @@ -434,7 +433,7 @@ func (mat *MarketActivityTracker) UpdateFeesFromTransfers(asset, market string,
mat.addFees(mt.makerFeesPaid, t.Owner, t.Amount.Amount, mt.totalMakerFeesPaid)
case types.TransferTypeMakerFeeReceive:
mat.addFees(mt.makerFeesReceived, t.Owner, t.Amount.Amount, mt.totalMakerFeesReceived)
case types.TransferTypeLiquidityFeeDistribute:
case types.TransferTypeLiquidityFeeNetDistribute, types.TransferTypeSlaPerformanceBonusDistribute:
mat.addFees(mt.lpFees, t.Owner, t.Amount.Amount, mt.totalLpFees)
default:
}
Expand Down
18 changes: 12 additions & 6 deletions core/execution/common/market_activity_tracker_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ func TestCalculateMetricForIndividualsAvePosition(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down Expand Up @@ -434,7 +435,8 @@ func TestCalculateMetricForPartyAvePosition(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down Expand Up @@ -617,7 +619,8 @@ func TestCalculateMetricForIndividualReturnVolatility(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down Expand Up @@ -753,7 +756,8 @@ func TestCalculateMetricForIndividualsRelativeReturn(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down Expand Up @@ -912,7 +916,8 @@ func TestCalculateMetricForPartyRelativeReturn(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down Expand Up @@ -1123,7 +1128,8 @@ func TestCalculateMetricForParty(t *testing.T) {
ctrl := gomock.NewController(t)
teams := mocks.NewMockTeams(ctrl)
balanceChecker := mocks.NewMockAccountBalanceChecker(ctrl)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), epochService, teams, balanceChecker)
tracker := NewMarketActivityTracker(logging.NewTestLogger(), teams, balanceChecker)
epochService.NotifyOnEpoch(tracker.OnEpochEvent, tracker.OnEpochRestore)
tracker.SetEligibilityChecker(&DummyEligibilityChecker{})
epochService.target(context.Background(), types.Epoch{Seq: 1, Action: vgproto.EpochAction_EPOCH_ACTION_START, StartTime: time.Time{}})

Expand Down
2 changes: 1 addition & 1 deletion core/execution/common/market_activity_tracker_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (mat *MarketActivityTracker) restore(tracker *snapshot.MarketTracker) {
}

// onEpochRestore is called when the state of the epoch changes, we only care about new epochs starting.
func (mat *MarketActivityTracker) onEpochRestore(_ context.Context, epoch types.Epoch) {
func (mat *MarketActivityTracker) OnEpochRestore(_ context.Context, epoch types.Epoch) {
mat.currentEpoch = epoch.Seq
mat.epochStartTime = epoch.StartTime
}
Loading

0 comments on commit a62308b

Please sign in to comment.