Skip to content

Commit

Permalink
feat: Add maker fees stats to fees stats API
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinTrinque committed Oct 16, 2023
1 parent 5cb185d commit ffde128
Show file tree
Hide file tree
Showing 19 changed files with 1,527 additions and 1,192 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
- [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.
- [9785](https://github.com/vegaprotocol/vega/issues/9785) - Support transfers to network treasury.
- [9743](https://github.com/vegaprotocol/system-tests/issues/9743) - Expose maker fees in fees stats API.

### 🐛 Fixes

Expand Down
12 changes: 6 additions & 6 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"sync"
"time"

"golang.org/x/exp/maps"

"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/execution/common"
Expand All @@ -51,6 +49,8 @@ import (
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/logging"
vegapb "code.vegaprotocol.io/vega/protos/vega"

"golang.org/x/exp/maps"
)

// LiquidityMonitor.
Expand Down Expand Up @@ -363,10 +363,10 @@ func (m *Market) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if !m.finalFeesDistributed {
m.liquidity.OnEpochEnd(ctx, m.timeService.GetTimeNow())
}
FeesStats := m.fee.GetFeesStatsOnEpochEnd()
FeesStats.Market = m.GetID()
FeesStats.EpochSeq = epoch.Seq
m.broker.Send(events.NewFeesStatsEvent(ctx, FeesStats))
feesStats := m.fee.GetFeesStatsOnEpochEnd()
feesStats.Market = m.GetID()
feesStats.EpochSeq = epoch.Seq
m.broker.Send(events.NewFeesStatsEvent(ctx, feesStats))
}

m.updateLiquidityFee(ctx)
Expand Down
2 changes: 1 addition & 1 deletion core/fee/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (e *Engine) CalculateForContinuousMode(
}
fee, reward := e.applyDiscountsAndRewards(taker, e.calculateContinuousModeFees(trade), referral, volumeDiscountService)

e.feeStats.RegisterMakerFee(maker, taker, fee.MakerFee)
e.feesStats.RegisterMakerFee(maker, taker, fee.MakerFee)

switch trade.Aggressor {
case types.SideBuy:
Expand Down
10 changes: 5 additions & 5 deletions core/fee/rebate_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ func (f *FeesStats) RegisterMakerFee(makerID, takerID string, amount *num.Uint)

total.Add(total, amount)

makeFeeGenerated, ok := f.MakerFeesGenerated[takerID]
makerFeesGenerated, ok := f.MakerFeesGenerated[takerID]
if !ok {
makeFeeGenerated = map[string]*num.Uint{}
f.MakerFeesGenerated[takerID] = makeFeeGenerated
makerFeesGenerated = map[string]*num.Uint{}
f.MakerFeesGenerated[takerID] = makerFeesGenerated
}

makerTally, ok := makeFeeGenerated[makerID]
makerTally, ok := makerFeesGenerated[makerID]
if !ok {
makerTally = num.NewUint(0)
makeFeeGenerated[makerID] = makerTally
makerFeesGenerated[makerID] = makerTally
}

makerTally.Add(makerTally, amount)
Expand Down
2 changes: 1 addition & 1 deletion datanode/api/trading_data_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4450,7 +4450,7 @@ func (t *TradingDataServiceV2) GetFeesStats(ctx context.Context, req *v2.GetFees
assetID = ptr.From(entities.AssetID(*req.AssetId))
}

stats, err := t.feesStatsService.GetFeesStats(ctx, marketID, assetID, req.EpochSeq, req.Referrer, req.Referee)
stats, err := t.feesStatsService.GetFeesStats(ctx, marketID, assetID, req.EpochSeq, req.PartyId)
if err != nil {
return nil, formatE(ErrGetFeesStats, err)
}
Expand Down
49 changes: 49 additions & 0 deletions datanode/entities/fees_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package entities

import (
"time"

eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)

type FeesStats struct {
MarketID MarketID
AssetID AssetID
EpochSeq uint64
TotalRewardsPaid []*eventspb.PartyAmount
ReferrerRewardsGenerated []*eventspb.ReferrerRewardsGenerated
RefereesDiscountApplied []*eventspb.PartyAmount
VolumeDiscountApplied []*eventspb.PartyAmount
TotalMakerFeesReceived []*eventspb.PartyAmount
MakerFeesGenerated []*eventspb.MakerFeesGenerated
VegaTime time.Time
}

func FeesStatsFromProto(proto *eventspb.FeesStats, vegaTime time.Time) *FeesStats {
return &FeesStats{
MarketID: MarketID(proto.Market),
AssetID: AssetID(proto.Asset),
EpochSeq: proto.EpochSeq,
TotalRewardsPaid: proto.TotalRewardsPaid,
ReferrerRewardsGenerated: proto.ReferrerRewardsGenerated,
RefereesDiscountApplied: proto.RefereesDiscountApplied,
VolumeDiscountApplied: proto.VolumeDiscountApplied,
TotalMakerFeesReceived: proto.TotalMakerFeesReceived,
MakerFeesGenerated: proto.MakerFeesGenerated,
VegaTime: vegaTime,
}
}

func (stats *FeesStats) ToProto() *eventspb.FeesStats {
return &eventspb.FeesStats{
Market: stats.MarketID.String(),
Asset: stats.AssetID.String(),
EpochSeq: stats.EpochSeq,
TotalRewardsPaid: stats.TotalRewardsPaid,
ReferrerRewardsGenerated: stats.ReferrerRewardsGenerated,
RefereesDiscountApplied: stats.RefereesDiscountApplied,
VolumeDiscountApplied: stats.VolumeDiscountApplied,
TotalMakerFeesReceived: stats.TotalMakerFeesReceived,
MakerFeesGenerated: stats.MakerFeesGenerated,
}
}
36 changes: 0 additions & 36 deletions datanode/entities/referral_set_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ type (
SetID string
PartyID string
}

FeesStats struct {
MarketID MarketID
AssetID AssetID
EpochSeq uint64
TotalRewardsPaid []*eventspb.PartyAmount
ReferrerRewardsGenerated []*eventspb.ReferrerRewardsGenerated
RefereesDiscountApplied []*eventspb.PartyAmount
VolumeDiscountApplied []*eventspb.PartyAmount
VegaTime time.Time
}
)

func (s FlattenReferralSetStats) Cursor() *Cursor {
Expand Down Expand Up @@ -120,28 +109,3 @@ func ReferralSetStatsFromProto(proto *eventspb.ReferralSetStatsUpdated, vegaTime
RewardsFactorMultiplier: proto.RewardsFactorMultiplier,
}, nil
}

func FeesStatsFromProto(proto *eventspb.FeesStats, vegaTime time.Time) *FeesStats {
return &FeesStats{
MarketID: MarketID(proto.Market),
AssetID: AssetID(proto.Asset),
EpochSeq: proto.EpochSeq,
TotalRewardsPaid: proto.TotalRewardsPaid,
ReferrerRewardsGenerated: proto.ReferrerRewardsGenerated,
RefereesDiscountApplied: proto.RefereesDiscountApplied,
VolumeDiscountApplied: proto.VolumeDiscountApplied,
VegaTime: vegaTime,
}
}

func (stats *FeesStats) ToProto() *eventspb.FeesStats {
return &eventspb.FeesStats{
Market: stats.MarketID.String(),
Asset: stats.AssetID.String(),
EpochSeq: stats.EpochSeq,
TotalRewardsPaid: stats.TotalRewardsPaid,
ReferrerRewardsGenerated: stats.ReferrerRewardsGenerated,
RefereesDiscountApplied: stats.RefereesDiscountApplied,
VolumeDiscountApplied: stats.VolumeDiscountApplied,
}
}
4 changes: 4 additions & 0 deletions datanode/gateway/graphql/fees_stats_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (r *feesStatsResolver) Epoch(_ context.Context, obj *eventspb.FeesStats) (i
return int(obj.EpochSeq), nil
}

func (r *feesStatsResolver) TotalMakeFeesReceived(_ context.Context, obj *eventspb.FeesStats) ([]*eventspb.PartyAmount, error) {
return obj.TotalMakerFeesReceived, nil
}

func (r *referrerRewardsGeneratedResolver) ReferrerID(_ context.Context, obj *eventspb.ReferrerRewardsGenerated) (string, error) {
return obj.Referrer, nil
}
Loading

0 comments on commit ffde128

Please sign in to comment.