Skip to content

Commit

Permalink
Merge pull request #11601 from vegaprotocol/11577-party-stats-api
Browse files Browse the repository at this point in the history
11577 party stats api
  • Loading branch information
jeremyletang authored Aug 22, 2024
2 parents db4e035 + 6556f96 commit 428b119
Show file tree
Hide file tree
Showing 33 changed files with 4,507 additions and 2,301 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [11570](https://github.com/vegaprotocol/vega/issues/11570) - Include the required set of parties for evaluation for eligible entities reward.
- [11576](https://github.com/vegaprotocol/vega/issues/11576) - Replace additional rebate validation with a cap.
- [11533](https://github.com/vegaprotocol/vega/issues/11533) - Suppose per party fee discounts in fee estimation.
- [11577](https://github.com/vegaprotocol/vega/issues/11577) - Add API for party discounts and rewards.

### 🐛 Fixes

Expand Down
4 changes: 3 additions & 1 deletion core/types/rebate_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)

Expand All @@ -42,10 +43,11 @@ type VolumeRebateBenefitTier struct {

func (v VolumeRebateProgram) IntoProto() *vegapb.VolumeRebateProgram {
benefitTiers := make([]*vegapb.VolumeRebateBenefitTier, 0, len(v.VolumeRebateBenefitTiers))
for _, tier := range v.VolumeRebateBenefitTiers {
for i, tier := range v.VolumeRebateBenefitTiers {
benefitTiers = append(benefitTiers, &vegapb.VolumeRebateBenefitTier{
MinimumPartyMakerVolumeFraction: tier.MinimumPartyMakerVolumeFraction.String(),
AdditionalMakerRebate: tier.AdditionalMakerRebate.String(),
TierNumber: ptr.From(uint64(i + 1)),
})
}

Expand Down
4 changes: 3 additions & 1 deletion core/types/referral_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)

Expand Down Expand Up @@ -172,12 +173,13 @@ func (c ReferralProgram) String() string {

func (c ReferralProgram) IntoProto() *vegapb.ReferralProgram {
benefitTiers := make([]*vegapb.BenefitTier, 0, len(c.BenefitTiers))
for _, tier := range c.BenefitTiers {
for i, tier := range c.BenefitTiers {
benefitTiers = append(benefitTiers, &vegapb.BenefitTier{
MinimumEpochs: tier.MinimumEpochs.String(),
MinimumRunningNotionalTakerVolume: tier.MinimumRunningNotionalTakerVolume.String(),
ReferralRewardFactors: tier.ReferralRewardFactors.IntoRewardFactorsProto(),
ReferralDiscountFactors: tier.ReferralDiscountFactors.IntoDiscountFactorsProto(),
TierNumber: ptr.From(uint64(i + 1)),
})
}

Expand Down
4 changes: 3 additions & 1 deletion core/types/volume_discount_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)

Expand All @@ -42,10 +43,11 @@ type VolumeBenefitTier struct {

func (v VolumeDiscountProgram) IntoProto() *vegapb.VolumeDiscountProgram {
benefitTiers := make([]*vegapb.VolumeBenefitTier, 0, len(v.VolumeBenefitTiers))
for _, tier := range v.VolumeBenefitTiers {
for i, tier := range v.VolumeBenefitTiers {
benefitTiers = append(benefitTiers, &vegapb.VolumeBenefitTier{
MinimumRunningNotionalTakerVolume: tier.MinimumRunningNotionalTakerVolume.String(),
VolumeDiscountFactors: tier.VolumeDiscountFactors.IntoDiscountFactorsProto(),
TierNumber: ptr.From(uint64(i + 1)),
})
}

Expand Down
15 changes: 15 additions & 0 deletions datanode/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"code.vegaprotocol.io/vega/libs/subscribers"
"code.vegaprotocol.io/vega/logging"
protoapi "code.vegaprotocol.io/vega/protos/data-node/api/v2"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
vegaprotoapi "code.vegaprotocol.io/vega/protos/vega/api/v1"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"

Expand Down Expand Up @@ -152,6 +153,10 @@ type AMMService interface {
ListByPartyMarketStatus(ctx context.Context, party, market *string, status *entities.AMMStatus, pagination entities.CursorPagination) ([]entities.AMMPool, entities.PageInfo, error)
}

type PartyStatsSvc interface {
GetPartyStats(ctx context.Context, partyID string, marketIDs []string) (*v2.GetPartyDiscountStatsResponse, error)
}

// GRPCServer represent the grpc api provided by the vega node.
type GRPCServer struct {
Config
Expand Down Expand Up @@ -534,6 +539,15 @@ func (g *GRPCServer) Start(ctx context.Context, lis net.Listener) error {
)

streamIntercept := grpc.StreamInterceptor(subscriptionRateLimiter.WithGrpcInterceptor(g.ipFromContext))
partyDiscountStats := service.NewPartyStatsService(g.epochService,
g.referralSetsService.ReferralSets,
g.volumeDiscountStatsService,
g.volumeRebateStatsService,
g.marketsService,
g.referralProgramService.ReferralPrograms,
g.volumeDiscountProgramService.VolumeDiscountPrograms,
g.volumeRebateProgramService.VolumeRebatePrograms,
)

g.srv = grpc.NewServer(intercept, streamIntercept)

Expand Down Expand Up @@ -608,6 +622,7 @@ func (g *GRPCServer) Start(ctx context.Context, lis net.Listener) error {
AMMPoolService: g.ammPoolService,
volumeRebateStatsService: g.volumeRebateStatsService,
volumeRebateProgramService: g.volumeRebateProgramService,
partyDiscountStats: partyDiscountStats,
}

protoapi.RegisterTradingDataServiceServer(g.srv, tradingDataSvcV2)
Expand Down
6 changes: 6 additions & 0 deletions datanode/api/trading_data_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type TradingDataServiceV2 struct {
twNotionalPositionService *service.TimeWeightedNotionalPosition
gameScoreService *service.GameScore
AMMPoolService AMMService
partyDiscountStats PartyStatsSvc
}

func (t *TradingDataServiceV2) SetLogger(l *logging.Logger) {
Expand Down Expand Up @@ -4220,6 +4221,11 @@ func (t *TradingDataServiceV2) ObserveGovernance(req *v2.ObserveGovernanceReques
})
}

// GetPartyDiscountStats this is just passing the call through to the service.
func (t *TradingDataServiceV2) GetPartyDiscountStats(ctx context.Context, req *v2.GetPartyDiscountStatsRequest) (*v2.GetPartyDiscountStatsResponse, error) {
return t.partyDiscountStats.GetPartyStats(ctx, req.PartyId, req.MarketIds)
}

func (t *TradingDataServiceV2) proposalToGovernanceData(ctx context.Context, proposal entities.Proposal) (*vega.GovernanceData, error) {
yesVotes, err := t.governanceService.GetYesVotesForProposal(ctx, proposal.ID.String())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions datanode/entities/referral_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type (
)

func ReferralProgramFromProto(proto *vega.ReferralProgram, vegaTime time.Time, seqNum uint64) *ReferralProgram {
for i := range proto.BenefitTiers {
proto.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1))
}
return &ReferralProgram{
ID: ReferralProgramID(proto.Id),
Version: proto.Version,
Expand Down
3 changes: 3 additions & 0 deletions datanode/entities/volume_discount_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type (
)

func VolumeDiscountProgramFromProto(proto *vega.VolumeDiscountProgram, vegaTime time.Time, seqNum uint64) *VolumeDiscountProgram {
for i := range proto.BenefitTiers {
proto.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1))
}
return &VolumeDiscountProgram{
ID: VolumeDiscountProgramID(proto.Id),
Version: proto.Version,
Expand Down
4 changes: 4 additions & 0 deletions datanode/entities/volume_rebate_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type (
)

func VolumeRebateProgramFromProto(proto *vega.VolumeRebateProgram, vegaTime time.Time, seqNum uint64) *VolumeRebateProgram {
// set the tier numbers accordingly
for i := range proto.BenefitTiers {
proto.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1))
}
return &VolumeRebateProgram{
ID: VolumeRebateProgramID(proto.Id),
Version: proto.Version,
Expand Down
16 changes: 16 additions & 0 deletions datanode/gateway/graphql/benefit_tier_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func (br *benefitTierResolver) MinimumEpochs(_ context.Context, obj *vega.Benefi
return int(minEpochs), nil
}

func (v *benefitTierResolver) TierNumber(_ context.Context, obj *vega.BenefitTier) (*int, error) {
if obj.TierNumber == nil {
return nil, nil
}
i := int(*obj.TierNumber)
return &i, nil
}

type volumeBenefitTierResolver VegaResolverRoot

// VolumeDiscountFactors implements VolumeBenefitTierResolver.
Expand All @@ -99,3 +107,11 @@ func (v *volumeBenefitTierResolver) VolumeDiscountFactors(ctx context.Context, o
LiquidityFactor: liq.String(),
}, nil
}

func (v *volumeBenefitTierResolver) TierNumber(_ context.Context, obj *vega.VolumeBenefitTier) (*int, error) {
if obj.TierNumber == nil {
return nil, nil
}
i := int(*obj.TierNumber)
return &i, nil
}
Loading

0 comments on commit 428b119

Please sign in to comment.