Skip to content

Commit

Permalink
Merge branch 'develop' into 9746
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 authored Oct 11, 2023
2 parents 91d66ba + 3bdef94 commit e6289c7
Show file tree
Hide file tree
Showing 16 changed files with 1,430 additions and 1,233 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [9258](https://github.com/vegaprotocol/vega/issues/9258) - Change HTTP endpoint from `/volume-discount-stats` to `/volume-discount-program/stats`.
- [9258](https://github.com/vegaprotocol/vega/issues/9258) - Change HTTP endpoint from `/referral-sets/stats/{id}` to `/referral-sets/{id}/stats`.
- [9719](https://github.com/vegaprotocol/vega/issues/9719) - Remove unnecessary fields from referral and volume discount program proposals.
- [9733](https://github.com/vegaprotocol/vega/issues/9733) - Making `set_id` optional in `referral set stats` endpoint

### 🗑️ Deprecation

Expand Down Expand Up @@ -296,6 +297,7 @@
- [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.

## 0.72.1

Expand Down
7 changes: 5 additions & 2 deletions datanode/api/trading_data_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4256,7 +4256,10 @@ func (t *TradingDataServiceV2) GetReferralSetStats(ctx context.Context, req *v2.
return nil, formatE(ErrInvalidPagination, err)
}

setID := entities.ReferralSetID(req.ReferralSetId)
var setID *entities.ReferralSetID
if req.ReferralSetId != nil {
setID = ptr.From(entities.ReferralSetID(*req.ReferralSetId))
}

var referee *entities.PartyID
if req.Referee != nil {
Expand Down Expand Up @@ -4442,7 +4445,7 @@ func (t *TradingDataServiceV2) GetReferralFeeStats(ctx context.Context, req *v2.
assetID = ptr.From(entities.AssetID(*req.AssetId))
}

stats, err := t.referralFeeStatsService.GetFeeStats(ctx, marketID, assetID, req.EpochSeq)
stats, err := t.referralFeeStatsService.GetFeeStats(ctx, marketID, assetID, req.EpochSeq, req.Referrer, req.Referee)
if err != nil {
return nil, formatE(ErrGetReferralFeeStats, err)
}
Expand Down
2 changes: 2 additions & 0 deletions datanode/entities/referral_set_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type (
}

FlattenReferralSetStats struct {
SetID ReferralSetID
AtEpoch uint64
ReferralSetRunningNotionalTakerVolume string
VegaTime time.Time
Expand All @@ -50,6 +51,7 @@ type (
ReferralSetStatsCursor struct {
VegaTime time.Time
AtEpoch uint64
SetID string
PartyID string
}

Expand Down
44 changes: 31 additions & 13 deletions datanode/gateway/graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions datanode/gateway/graphql/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ func (r *myQueryResolver) ReferralSetReferees(ctx context.Context, id, referrer,
return resp.ReferralSetReferees, nil
}

func (r *myQueryResolver) ReferralSetStats(ctx context.Context, setID string, epoch *int, partyID *string, pagination *v2.Pagination) (*v2.ReferralSetStatsConnection, error) {
func (r *myQueryResolver) ReferralSetStats(ctx context.Context, setID *string, epoch *int, partyID *string, pagination *v2.Pagination) (*v2.ReferralSetStatsConnection, error) {
var epochU64Ptr *uint64
if epoch != nil {
epochU64 := uint64(*epoch)
Expand Down Expand Up @@ -1730,7 +1730,9 @@ func (r *myQueryResolver) TeamRefereeHistory(ctx context.Context, referee string
return history.TeamRefereeHistory, nil
}

func (r *myQueryResolver) ReferralFeeStats(ctx context.Context, marketID *string, assetID *string, epoch *int) (*v1.FeeStats, error) {
func (r *myQueryResolver) ReferralFeeStats(ctx context.Context, marketID *string, assetID *string, epoch *int,
referrer, referee *string,
) (*v1.FeeStats, error) {
var epochSeq *uint64

if epoch != nil {
Expand All @@ -1741,6 +1743,8 @@ func (r *myQueryResolver) ReferralFeeStats(ctx context.Context, marketID *string
MarketId: marketID,
AssetId: assetID,
EpochSeq: epochSeq,
Referrer: referrer,
Referee: referee,
}

resp, err := r.tradingDataClientV2.GetReferralFeeStats(ctx, req)
Expand Down
9 changes: 6 additions & 3 deletions datanode/gateway/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,10 @@ type Query {
assetId: ID
"Optional epoch to filter for. If omitted, the most recent epoch's data is returned."
epoch: Int
"Optional referrer party ID to filter for"
referrer: ID
"Optional referee party ID to filter for"
referee: ID
): ReferralSetFeeStats

referralSetReferees(
Expand All @@ -862,8 +866,8 @@ type Query {

"Get referral set statistics"
referralSetStats(
"Referral set ID to fetch stats for"
id: ID!
"Optional referral set ID to fetch stats for"
setId: ID
"Optional epoch to get statistics from. If not provided, the latest statistics are returned"
epoch: Int
"Optional party ID. If not provided all parties for the epoch are returned"
Expand Down Expand Up @@ -6271,4 +6275,3 @@ type VolumeDiscountStats {
"Party's running volume."
runningVolume: String!
}

12 changes: 6 additions & 6 deletions datanode/networkhistory/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "QmYDRBne4Zm448jJpkjdhfM7631NSHpDmRju18AievkihV", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmbeVesfQwdULqSHrxwRrNSwBRa4Djw124AN2vYvtZwk12", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmYW6bUS6d5qx1L2yJTRYUYjsVVsPCbxrnmCuymb6nbJhM", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmSWqUrypRjhPtzqDtJygvPnE8JtAFz3AiUTWoh41YNpxJ", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "Qmate2TDHuFoEQZt2LGZ9JPrQ5NMqh8Yk9KwBPzHCyVf42", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmcYzt4avPei3jhfNqFP1kbuBYX9rHE15M8KquuZbJMWdt", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "Qmf8eRBLC6qrDRWSq4Hw3UYoFBX9AkdXQVN33mkPhaWPEP", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmZJC4jeiFDkzZ536C2Mvmf3skGckMbGAamfoBLJERuP1H", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmZD9VCqXyCikbwv2bxfd7Ejp2v1GvPDvExXNdSNgRnoaY", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmaLKLnUyiQmSgVVVTi1GEdw7zUxdy6ipZZvSnkfEsnxmp", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmWpSgBj2D2u46jjDG52FXhDBq298to7noPkfYZNDneCdy", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmcEkqcYQUALBd4xjrC1nJttRWxD9NZbdPEpX634CQGvoW", snapshots)
}, postgresRuntimePath, sqlFs)

if exitCode != 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- +goose Up

-- index the json columns to make them faster to query
create index if not exists idx_referral_fee_stats_total_rewards_paid on referral_fee_stats using gin(total_rewards_paid);
create index if not exists idx_referral_fee_stats_referrer_rewards_generated on referral_fee_stats using gin(referrer_rewards_generated);
create index if not exists idx_referral_fee_stats_referees_discount_applied on referral_fee_stats using gin(referees_discount_applied);
create index if not exists idx_referral_fee_stats_volume_discount_applied on referral_fee_stats using gin(volume_discount_applied);

-- +goose Down

drop index if exists idx_referral_fee_stats_total_rewards_paid;
drop index if exists idx_referral_fee_stats_referrer_rewards_generated;
drop index if exists idx_referral_fee_stats_referees_discount_applied;
drop index if exists idx_referral_fee_stats_volume_discount_applied;
54 changes: 50 additions & 4 deletions datanode/sqlstore/referral_fee_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/georgysavva/scany/pgxscan"

Expand Down Expand Up @@ -63,7 +64,8 @@ func (rfs *ReferralFeeStats) AddFeeStats(ctx context.Context, stats *entities.Re
return err
}

func (rfs *ReferralFeeStats) GetFeeStats(ctx context.Context, marketID *entities.MarketID, assetID *entities.AssetID, epochSeq *uint64) (
func (rfs *ReferralFeeStats) GetFeeStats(ctx context.Context, marketID *entities.MarketID, assetID *entities.AssetID,
epochSeq *uint64, referrerID, refereeID *string) (
*entities.ReferralFeeStats, error,
) {
defer metrics.StartSQLQuery("ReferralFeeStats", "GetFeeStats")()
Expand All @@ -73,12 +75,12 @@ func (rfs *ReferralFeeStats) GetFeeStats(ctx context.Context, marketID *entities
args []interface{}
)

if marketID == nil && assetID == nil {
return nil, errors.New("marketID or assetID must be provided")
if marketID != nil && assetID != nil {
return nil, errors.New("only a marketID or assetID should be provided")
}

query := `SELECT * FROM referral_fee_stats`
where := make([]string, 0, 3)
where := make([]string, 0)

if epochSeq != nil {
where = append(where, fmt.Sprintf("epoch_seq = %s", nextBindVar(&args, *epochSeq)))
Expand All @@ -92,6 +94,10 @@ func (rfs *ReferralFeeStats) GetFeeStats(ctx context.Context, marketID *entities
where = append(where, fmt.Sprintf("market_id = %s", nextBindVar(&args, *marketID)))
}

if partyFilter := getPartyFilter(referrerID, refereeID); partyFilter != "" {
where = append(where, partyFilter)
}

if len(where) > 0 {
for i, w := range where {
if i == 0 {
Expand All @@ -116,3 +122,43 @@ func (rfs *ReferralFeeStats) GetFeeStats(ctx context.Context, marketID *entities

return &stats[0], err
}

func getPartyFilter(referrerID, refereeID *string) string {
builder := strings.Builder{}
if referrerID == nil && refereeID == nil {
return ""
}

builder.WriteString("(")

if referrerID != nil {
builder.WriteString(fmt.Sprintf(
`total_rewards_paid @> '[{"party_id":"%s"}]'`, *referrerID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`referrer_rewards_generated @> '[{"referrer":"%s"}]'`, *referrerID,
))
}

if refereeID != nil {
if referrerID != nil {
builder.WriteString(" OR ")
}
builder.WriteString(fmt.Sprintf(
`referrer_rewards_generated @> '[{"generated_reward":[{"party":"%s"}]}]'`, *refereeID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`referees_discount_applied @> '[{"party_id":"%s"}]'`, *refereeID,
))
builder.WriteString(" OR ")
builder.WriteString(fmt.Sprintf(
`volume_discount_applied @> '[{"party_id":"%s"}]'`, *refereeID,
))
}

builder.WriteString(")")

return builder.String()
}
Loading

0 comments on commit e6289c7

Please sign in to comment.