Skip to content

Commit

Permalink
fix: referral set stats API unmarshalls discount factors properly
Browse files Browse the repository at this point in the history
  • Loading branch information
wwestgarth committed Aug 14, 2024
1 parent 028e0a7 commit e333acb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
47 changes: 43 additions & 4 deletions datanode/sqlstore/referral_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions datanode/sqlstore/referral_sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
}),
Expand Down

0 comments on commit e333acb

Please sign in to comment.