Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support party stats with no markets to retrive for all markets #11658

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [11583](https://github.com/vegaprotocol/vega/issues/11583) - Rough bound on price interval when matching with `AMMs` is now looser and calculated in the `AMM` engine.
- [11624](https://github.com/vegaprotocol/vega/issues/11624) - prevent creation of rewards with no payout, but with high computational cost.
- [11619](https://github.com/vegaprotocol/vega/issues/11619) - Fix `EstimatePositions` API for capped futures.
- [11645](https://github.com/vegaprotocol/vega/issues/11645) - Support party stats with no markets to retrieve for all markets.


## 0.78.0
Expand Down
5 changes: 5 additions & 0 deletions datanode/service/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type MarketStore interface {
GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Market, error)
GetAllPaged(ctx context.Context, marketID string, pagination entities.CursorPagination, includeSettled bool) ([]entities.Market, entities.PageInfo, error)
ListSuccessorMarkets(ctx context.Context, marketID string, fullHistory bool, pagination entities.CursorPagination) ([]entities.SuccessorMarket, entities.PageInfo, error)
GetAllFees(ctx context.Context) ([]entities.Market, error)
}

type Markets struct {
Expand All @@ -51,6 +52,10 @@ func (m *Markets) Initialise(ctx context.Context) error {
return nil
}

func (m *Markets) GetAllFees(ctx context.Context) ([]entities.Market, error) {
return m.store.GetAllFees(ctx)
}

func (m *Markets) Upsert(ctx context.Context, market *entities.Market) error {
if err := m.store.Upsert(ctx, market); err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions datanode/service/mocks/mocks.go

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

12 changes: 8 additions & 4 deletions datanode/service/party_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type VRSStore interface {
// MktStore is a duplicate interface at this point, but again: custom method fetching list of markets would be handy.
type MktStore interface {
GetByIDs(ctx context.Context, marketID []string) ([]entities.Market, error)
// NB: although it returns Market entity, all it has is id and fees. Trying to access anything else on it will get NPE.
GetAllFees(ctx context.Context) ([]entities.Market, error)
}

type VRStore interface {
Expand Down Expand Up @@ -109,13 +111,15 @@ func (s *PSvc) GetPartyStats(ctx context.Context, partyID string, markets []stri
return nil, err
}
// then get the markets:
mkts, err := s.mkt.GetByIDs(ctx, markets)
var mkts []entities.Market
if len(markets) > 0 {
mkts, err = s.mkt.GetByIDs(ctx, markets)
} else {
mkts, err = s.mkt.GetAllFees(ctx)
}
if err != nil {
return nil, err
}
if len(mkts) == 0 {
return nil, fmt.Errorf("no valid markets provided")
}
lastE := uint64(epoch.ID - 1)

data := &v2.GetPartyDiscountStatsResponse{}
Expand Down
11 changes: 11 additions & 0 deletions datanode/sqlstore/markets.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ func (m *Markets) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]en
return markets, m.wrapE(err)
}

// GetAllFees returns fee information for all markets.
// it returns a market entity with only id and fee.
// NB: it's not cached nor paged.
func (m *Markets) GetAllFees(ctx context.Context) ([]entities.Market, error) {
markets := make([]entities.Market, 0)
args := make([]interface{}, 0)
query := `select mc.id, mc.fees from markets_current mc where state != 'STATE_REJECTED' AND state != 'STATE_SETTLED' AND state != 'STATE_CLOSED' order by mc.id`
err := pgxscan.Select(ctx, m.ConnectionSource, &markets, query, args...)
return markets, err
}

func (m *Markets) GetAllPaged(ctx context.Context, marketID string, pagination entities.CursorPagination, includeSettled bool) ([]entities.Market, entities.PageInfo, error) {
key := newCacheKey(marketID, pagination, includeSettled)
m.allCacheLock.Lock()
Expand Down
61 changes: 61 additions & 0 deletions datanode/sqlstore/markets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,67 @@ func TestMarkets_Get(t *testing.T) {
t.Run("GetByID should return a perpetual market if it exists", getByIDShouldReturnAPerpetualMarketIfItExists)
}

func TestGetAllFees(t *testing.T) {
bs, md := setupMarketsTest(t)

ctx := tempTransaction(t)

block := addTestBlock(t, ctx, bs)

market := entities.Market{
ID: "deadbeef",
TxHash: generateTxHash(),
VegaTime: block.VegaTime,
State: entities.MarketStateActive,
Fees: entities.Fees{
Factors: &entities.FeeFactors{
MakerFee: "0.1",
InfrastructureFee: "0.2",
LiquidityFee: "0.3",
BuyBackFee: "0.4",
TreasuryFee: "0.5",
},
},
}
err := md.Upsert(ctx, &market)
require.NoError(t, err)

market2 := entities.Market{
ID: "beefdead",
TxHash: generateTxHash(),
VegaTime: block.VegaTime,
State: entities.MarketStateActive,
Fees: entities.Fees{
Factors: &entities.FeeFactors{
MakerFee: "0.5",
InfrastructureFee: "0.4",
LiquidityFee: "0.3",
BuyBackFee: "0.2",
TreasuryFee: "0.1",
},
},
}
err = md.Upsert(ctx, &market2)
require.NoError(t, err, "Saving market entity to database")

mkts, err := md.GetAllFees(ctx)
require.NoError(t, err)
require.Equal(t, 2, len(mkts))

require.Equal(t, "beefdead", mkts[0].ID.String())
require.Equal(t, "0.5", mkts[0].Fees.Factors.MakerFee)
require.Equal(t, "0.4", mkts[0].Fees.Factors.InfrastructureFee)
require.Equal(t, "0.3", mkts[0].Fees.Factors.LiquidityFee)
require.Equal(t, "0.2", mkts[0].Fees.Factors.BuyBackFee)
require.Equal(t, "0.1", mkts[0].Fees.Factors.TreasuryFee)
require.Equal(t, "deadbeef", mkts[1].ID.String())
require.Equal(t, "0.1", mkts[1].Fees.Factors.MakerFee)
require.Equal(t, "0.2", mkts[1].Fees.Factors.InfrastructureFee)
require.Equal(t, "0.3", mkts[1].Fees.Factors.LiquidityFee)
require.Equal(t, "0.4", mkts[1].Fees.Factors.BuyBackFee)
require.Equal(t, "0.5", mkts[1].Fees.Factors.TreasuryFee)
}

func getByIDShouldReturnTheRequestedMarketIfItExists(t *testing.T) {
bs, md := setupMarketsTest(t)

Expand Down
Loading