Skip to content

Commit

Permalink
feat: add market community tags
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Letang <me@jeremyletang.com>
  • Loading branch information
jeremyletang committed May 3, 2024
1 parent 346e527 commit 8fc54de
Show file tree
Hide file tree
Showing 16 changed files with 5,320 additions and 4,742 deletions.
4 changes: 4 additions & 0 deletions core/events/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const (
TeamsStatsUpdatedEvent
TimeWeightedNotionalPositionUpdatedEvent
CancelledOrdersEvent
MarketCommunityTagsEvent
)

var (
Expand Down Expand Up @@ -273,6 +274,7 @@ var (
eventspb.BusEventType_BUS_EVENT_TYPE_TEAMS_STATS_UPDATED: TeamsStatsUpdatedEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_TIME_WEIGHTED_NOTIONAL_POSITION_UPDATED: TimeWeightedNotionalPositionUpdatedEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_CANCELLED_ORDERS: CancelledOrdersEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_MARKET_COMMUNITY_TAG: MarketCommunityTagsEvent,
// If adding a type here, please also add it to datanode/broker/convert.go
}

Expand Down Expand Up @@ -365,6 +367,7 @@ var (
TeamsStatsUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TEAMS_STATS_UPDATED,
TimeWeightedNotionalPositionUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TIME_WEIGHTED_NOTIONAL_POSITION_UPDATED,
CancelledOrdersEvent: eventspb.BusEventType_BUS_EVENT_TYPE_CANCELLED_ORDERS,
MarketCommunityTagsEvent: eventspb.BusEventType_BUS_EVENT_TYPE_MARKET_COMMUNITY_TAG,
// If adding a type here, please also add it to datanode/broker/convert.go
}

Expand Down Expand Up @@ -456,6 +459,7 @@ var (
TeamsStatsUpdatedEvent: "TeamsStatsUpdatedEvent",
TimeWeightedNotionalPositionUpdatedEvent: "TimeWeightedNotionalPositionUpdatedEvent",
CancelledOrdersEvent: "CancelledOrdersEvent",
MarketCommunityTagsEvent: "MarketCommunityTagsEvent",
}
)

Expand Down
57 changes: 57 additions & 0 deletions core/events/market_community_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package events

import (
"context"

eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)

type MarketCommunityTags struct {
*Base
a eventspb.MarketCommunityTags
}

func NewMarketCommunityTagsEvent(ctx context.Context, e eventspb.MarketCommunityTags) *MarketCommunityTags {
return &MarketCommunityTags{
Base: newBase(ctx, MarketCommunityTagsEvent),
a: e,
}
}

func (a *MarketCommunityTags) MarketCommunityTags() eventspb.MarketCommunityTags {
return a.a
}

func (a MarketCommunityTags) Proto() eventspb.MarketCommunityTags {
return a.a
}

func (a MarketCommunityTags) StreamMessage() *eventspb.BusEvent {
busEvent := newBusEventFromBase(a.Base)
busEvent.Event = &eventspb.BusEvent_MarketCommunityTags{
MarketCommunityTags: &a.a,
}
return busEvent
}

func MarketCommunityTagsEventFromStream(ctx context.Context, be *eventspb.BusEvent) *MarketCommunityTags {
return &MarketCommunityTags{
Base: newBaseFromBusEvent(ctx, MarketCommunityTagsEvent, be),
a: *be.GetMarketCommunityTags(),
}
}
2 changes: 2 additions & 0 deletions core/execution/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Engine struct {
allMarkets map[string]common.CommonMarket
allMarketsCpy []common.CommonMarket

communityTags *MarketCommunityTags
collateral common.Collateral
assets common.Assets
referralDiscountRewardService fee.ReferralDiscountRewardService
Expand Down Expand Up @@ -216,6 +217,7 @@ func NewEngine(
referralDiscountRewardService: referralDiscountRewardService,
volumeDiscountService: volumeDiscountService,
banking: banking,
communityTags: NewMarketCommunityTags(broker),
}

// set the eligibility for proposer bonus checker
Expand Down
16 changes: 11 additions & 5 deletions core/execution/engine_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,17 @@ func (e *Engine) serialise() (snapshot []byte, providers []types.StateProvider,
allMarketIDs = append(allMarketIDs, cm.GetID())
}

tags := e.communityTags.serialize()

pl := types.Payload{
Data: &types.PayloadExecutionMarkets{
ExecutionMarkets: &types.ExecutionMarkets{
Markets: mkts,
SpotMarkets: spotMkts,
SettledMarkets: cpStates,
Successors: successors,
AllMarketIDs: allMarketIDs,
Markets: mkts,
SpotMarkets: spotMkts,
SettledMarkets: cpStates,
Successors: successors,
AllMarketIDs: allMarketIDs,
MarketCommunityTags: tags,
},
},
}
Expand Down Expand Up @@ -382,6 +385,9 @@ func (e *Engine) LoadState(ctx context.Context, payload *types.Payload) ([]types
e.allMarketsCpy = append(e.allMarketsCpy, mkt)
}
}

e.communityTags = NewMarketCommunityTagFromProto(e.broker, pl.ExecutionMarkets.MarketCommunityTags)

return append(providers, spotProviders...), err
default:
return nil, types.ErrUnknownSnapshotType
Expand Down
106 changes: 106 additions & 0 deletions core/execution/market_community_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package execution

import (
"context"
"sort"

"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/execution/common"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"

Check failure on line 24 in core/execution/market_community_tags.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s prefix(code.vegaprotocol.io/vega) -s default -s blank -s dot --custom-order (gci)
"golang.org/x/exp/maps"
)

type MarketCommunityTags struct {
// market id -> set of tags
tags map[string]map[string]struct{}
broker common.Broker
}

func NewMarketCommunityTags(broker common.Broker) *MarketCommunityTags {
return &MarketCommunityTags{
tags: map[string]map[string]struct{}{},
broker: broker,
}
}

func NewMarketCommunityTagFromProto(
broker common.Broker,
state []*eventspb.MarketCommunityTags,
) *MarketCommunityTags {
m := NewMarketCommunityTags(broker)

for _, v := range state {
m.tags[v.MarketId] = map[string]struct{}{}
for _, t := range v.Tags {
m.tags[v.MarketId][t] = struct{}{}
}
}

return m
}

func (m *MarketCommunityTags) serialize() []*eventspb.MarketCommunityTags {
out := make([]*eventspb.MarketCommunityTags, 0, len(m.tags))

for mkt, tags := range m.tags {
mct := &eventspb.MarketCommunityTags{
MarketId: mkt,
Tags: make([]string, 0, len(tags)),
}

for tag, _ := range tags {

Check failure on line 66 in core/execution/market_community_tags.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
mct.Tags = append(mct.Tags, tag)
}

sort.Strings(mct.Tags)
}

sort.Slice(out, func(i, j int) bool { return out[i].MarketId < out[j].MarketId })

return out
}

// UpdateTags by that point the tags have been validated in lenght,

Check failure on line 78 in core/execution/market_community_tags.go

View workflow job for this annotation

GitHub Actions / lint

`lenght` is a misspelling of `length` (misspell)
// so no need to do that again.
func (m *MarketCommunityTags) UpdateTags(
ctx context.Context,
market string,
addTags []string,
removeTags []string,
) {
tags, ok := m.tags[market]
if !ok {
tags = map[string]struct{}{}
}

for _, t := range addTags {
tags[t] = struct{}{}
}

for _, t := range removeTags {
delete(tags, t)
}

evt := eventspb.MarketCommunityTags{
MarketId: market,
Tags: maps.Keys(tags),
}

sort.Strings(evt.Tags)
m.broker.Send(events.NewMarketCommunityTagsEvent(ctx, evt))
}
1 change: 1 addition & 0 deletions core/types/governance_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const (
ProposalTermsTypeUpdateMarketState
ProposalTermsTypeUpdateReferralProgram
ProposalTermsTypeUpdateVolumeDiscountProgram
ProposalTermsTypeUpdateMarketCommunityTags
)

type ProposalSubmission struct {
Expand Down
4 changes: 4 additions & 0 deletions core/types/governance_proposal_terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func (p ProposalTerms) IntoProto() *vegapb.ProposalTerms {
terms.Change = ch
case *vegapb.ProposalTerms_UpdateVolumeDiscountProgram:
terms.Change = ch
case *vegapb.ProposalTerms_UpdateCommunityTags:
terms.Change = ch
}

return terms
Expand Down Expand Up @@ -341,6 +343,8 @@ func ProposalTermsFromProto(p *vegapb.ProposalTerms) (*ProposalTerms, error) {
change, err = NewUpdateReferralProgramProposalFromProto(ch.UpdateReferralProgram)
case *vegapb.ProposalTerms_UpdateVolumeDiscountProgram:
change, err = NewUpdateVolumeDiscountProgramProposalFromProto(ch.UpdateVolumeDiscountProgram)
case *vegapb.ProposalTerms_UpdateCommunityTags:
change = NewUpdateMarketCommunityTagsFromProto(ch.UpdateCommunityTags)
}
if err != nil {
return nil, err
Expand Down
111 changes: 111 additions & 0 deletions core/types/governance_update_community_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package types

import (
"fmt"
"slices"

"code.vegaprotocol.io/vega/libs/stringer"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)

type ProposalTermsUpdateMarketCommunityTags struct {
UpdateMarketCommunityTags *UpdateMarketCommunityTags
}

func (f ProposalTermsUpdateMarketCommunityTags) String() string {
return fmt.Sprintf(
"updateCommunityTags(%s)",
stringer.PtrToString(f.UpdateMarketCommunityTags),
)
}

func (f ProposalTermsUpdateMarketCommunityTags) IntoProto() *vegapb.UpdateCommunityTags {
var updateCommunityTags *vegapb.UpdateCommunityTags
if f.UpdateMarketCommunityTags != nil {
updateCommunityTags = f.UpdateMarketCommunityTags.IntoProto()
}
return updateCommunityTags
}

func (f ProposalTermsUpdateMarketCommunityTags) isPTerm() {}

func (a ProposalTermsUpdateMarketCommunityTags) oneOfSingleProto() vegapb.ProposalOneOffTermChangeType {
return &vegapb.ProposalTerms_UpdateCommunityTags{
UpdateCommunityTags: a.IntoProto(),
}
}

func (a ProposalTermsUpdateMarketCommunityTags) oneOfBatchProto() vegapb.ProposalOneOffTermBatchChangeType {
return &vegapb.BatchProposalTermsChange_UpdateCommunityTags{
UpdateCommunityTags: a.IntoProto(),
}
}

func (f ProposalTermsUpdateMarketCommunityTags) GetTermType() ProposalTermsType {
return ProposalTermsTypeUpdateMarketCommunityTags
}

func (f ProposalTermsUpdateMarketCommunityTags) DeepClone() ProposalTerm {
if f.UpdateMarketCommunityTags == nil {
return &ProposalTermsUpdateMarketCommunityTags{}
}
return &ProposalTermsUpdateMarketCommunityTags{
UpdateMarketCommunityTags: f.UpdateMarketCommunityTags.DeepClone(),
}
}

func NewUpdateMarketCommunityTagsFromProto(p *vegapb.UpdateCommunityTags) *ProposalTermsUpdateMarketCommunityTags {
return &ProposalTermsUpdateMarketCommunityTags{
UpdateMarketCommunityTags: &UpdateMarketCommunityTags{
MarketID: p.MarketId,
AddTags: slices.Clone(p.AddTags),
RemoveTags: slices.Clone(p.RemoveTags),
},
}
}

type UpdateMarketCommunityTags struct {
MarketID string
AddTags []string
RemoveTags []string
}

func (u UpdateMarketCommunityTags) IntoProto() *vegapb.UpdateCommunityTags {
return &vegapb.UpdateCommunityTags{
MarketId: u.MarketID,
AddTags: slices.Clone(u.AddTags),
RemoveTags: slices.Clone(u.RemoveTags),
}
}

func (u UpdateMarketCommunityTags) String() string {
return fmt.Sprintf(
"marketId(%v) addTags(%v) removeTags(%v)",
u.MarketID,
u.AddTags,
u.RemoveTags,
)
}

func (u UpdateMarketCommunityTags) DeepClone() *UpdateMarketCommunityTags {
return &UpdateMarketCommunityTags{
MarketID: u.MarketID,
AddTags: slices.Clone(u.AddTags),
RemoveTags: slices.Clone(u.RemoveTags),
}
}
Loading

0 comments on commit 8fc54de

Please sign in to comment.