From e333acb8d511fa250a81be065464382374231b20 Mon Sep 17 00:00:00 2001 From: wwestgarth Date: Wed, 14 Aug 2024 10:57:04 +0100 Subject: [PATCH] fix: referral set stats API unmarshalls discount factors properly --- datanode/sqlstore/referral_sets.go | 47 ++++++++++++++++++++++--- datanode/sqlstore/referral_sets_test.go | 17 ++++++--- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/datanode/sqlstore/referral_sets.go b/datanode/sqlstore/referral_sets.go index 4525021fd6..d3fa15a47d 100644 --- a/datanode/sqlstore/referral_sets.go +++ b/datanode/sqlstore/referral_sets.go @@ -17,12 +17,15 @@ package sqlstore import ( "context" + "encoding/json" "fmt" "strings" + "time" "code.vegaprotocol.io/vega/datanode/entities" "code.vegaprotocol.io/vega/datanode/metrics" v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" + "code.vegaprotocol.io/vega/protos/vega" eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" "github.com/georgysavva/scany/pgxscan" @@ -181,6 +184,21 @@ func (rs *ReferralSets) GetReferralSetStats(ctx context.Context, setID *entities pageInfo entities.PageInfo ) + stats := []struct { + SetID entities.ReferralSetID + AtEpoch uint64 + WasEligible bool + ReferralSetRunningNotionalTakerVolume string + ReferrerTakerVolume string + VegaTime time.Time + PartyID string + DiscountFactors string + EpochNotionalTakerVolume string + RewardFactors *vega.RewardFactors + RewardsMultiplier string + RewardsFactorsMultiplier *vega.RewardFactors + }{} + query = `SELECT set_id, at_epoch, was_eligible, @@ -220,8 +238,6 @@ func (rs *ReferralSets) GetReferralSetStats(ctx context.Context, setID *entities query = fmt.Sprintf("%s %s", query, whereStr) - stats := []entities.FlattenReferralSetStats{} - query, args, err := PaginateQuery[entities.ReferralSetStatsCursor](query, args, referralSetStatsOrdering, pagination) if err != nil { return nil, pageInfo, err @@ -231,9 +247,32 @@ func (rs *ReferralSets) GetReferralSetStats(ctx context.Context, setID *entities return nil, pageInfo, err } - stats, pageInfo = entities.PageEntities[*v2.ReferralSetStatsEdge](stats, pagination) + flattenStats := []entities.FlattenReferralSetStats{} + for _, stat := range stats { + discountFactors := &vega.DiscountFactors{} + if err := json.Unmarshal([]byte(stat.DiscountFactors), discountFactors); err != nil { + return nil, pageInfo, err + } + + flattenStats = append(flattenStats, entities.FlattenReferralSetStats{ + SetID: stat.SetID, + AtEpoch: stat.AtEpoch, + WasEligible: stat.WasEligible, + ReferralSetRunningNotionalTakerVolume: stat.ReferralSetRunningNotionalTakerVolume, + ReferrerTakerVolume: stat.ReferrerTakerVolume, + VegaTime: stat.VegaTime, + PartyID: stat.PartyID, + DiscountFactors: discountFactors, + EpochNotionalTakerVolume: stat.EpochNotionalTakerVolume, + RewardFactors: stat.RewardFactors, + RewardsMultiplier: stat.RewardsMultiplier, + RewardsFactorsMultiplier: stat.RewardsFactorsMultiplier, + }) + } + + flattenStats, pageInfo = entities.PageEntities[*v2.ReferralSetStatsEdge](flattenStats, pagination) - return stats, pageInfo, nil + return flattenStats, pageInfo, nil } func (rs *ReferralSets) ListReferralSetReferees(ctx context.Context, referralSetID *entities.ReferralSetID, referrer, referee *entities.PartyID, diff --git a/datanode/sqlstore/referral_sets_test.go b/datanode/sqlstore/referral_sets_test.go index aae2e69ae5..78566bcfa1 100644 --- a/datanode/sqlstore/referral_sets_test.go +++ b/datanode/sqlstore/referral_sets_test.go @@ -29,6 +29,7 @@ import ( "code.vegaprotocol.io/vega/datanode/sqlstore" vgcrypto "code.vegaprotocol.io/vega/libs/crypto" "code.vegaprotocol.io/vega/libs/num" + "code.vegaprotocol.io/vega/protos/vega" vegapb "code.vegaprotocol.io/vega/protos/vega" eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" @@ -618,8 +619,12 @@ func getRefereeStats(t *testing.T, refs []entities.ReferralSetRefereeStats, disc stats := make([]*eventspb.RefereeStats, len(refs)) for i, r := range refs { stats[i] = &eventspb.RefereeStats{ - PartyId: r.Referee.String(), - DiscountFactor: discountFactor, + PartyId: r.Referee.String(), + DiscountFactors: &vega.DiscountFactors{ + InfrastructureDiscountFactor: discountFactor, + LiquidityDiscountFactor: discountFactor, + MakerDiscountFactor: discountFactor, + }, } } return stats @@ -656,8 +661,12 @@ func TestReferralSets_GetReferralSetStats(t *testing.T) { ReferralSetRunningNotionalTakerVolume: fmt.Sprintf("%d000000", i+1), RefereesStats: setupPartyReferralSetStatsMod(t, parties, func(j int, party entities.Party) *eventspb.RefereeStats { return &eventspb.RefereeStats{ - PartyId: party.ID.String(), - DiscountFactor: fmt.Sprintf("0.1%d%d", i+1, j+1), + PartyId: party.ID.String(), + DiscountFactors: &vega.DiscountFactors{ + InfrastructureDiscountFactor: "0.1", + LiquidityDiscountFactor: "0.1", + MakerDiscountFactor: "0.1", + }, EpochNotionalTakerVolume: strconv.Itoa((i+1)*100 + (j+1)*10), } }),