Skip to content

Commit 8fc54de

Browse files
committed
feat: add market community tags
Signed-off-by: Jeremy Letang <[email protected]>
1 parent 346e527 commit 8fc54de

File tree

16 files changed

+5320
-4742
lines changed

16 files changed

+5320
-4742
lines changed

core/events/bus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ const (
172172
TeamsStatsUpdatedEvent
173173
TimeWeightedNotionalPositionUpdatedEvent
174174
CancelledOrdersEvent
175+
MarketCommunityTagsEvent
175176
)
176177

177178
var (
@@ -273,6 +274,7 @@ var (
273274
eventspb.BusEventType_BUS_EVENT_TYPE_TEAMS_STATS_UPDATED: TeamsStatsUpdatedEvent,
274275
eventspb.BusEventType_BUS_EVENT_TYPE_TIME_WEIGHTED_NOTIONAL_POSITION_UPDATED: TimeWeightedNotionalPositionUpdatedEvent,
275276
eventspb.BusEventType_BUS_EVENT_TYPE_CANCELLED_ORDERS: CancelledOrdersEvent,
277+
eventspb.BusEventType_BUS_EVENT_TYPE_MARKET_COMMUNITY_TAG: MarketCommunityTagsEvent,
276278
// If adding a type here, please also add it to datanode/broker/convert.go
277279
}
278280

@@ -365,6 +367,7 @@ var (
365367
TeamsStatsUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TEAMS_STATS_UPDATED,
366368
TimeWeightedNotionalPositionUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TIME_WEIGHTED_NOTIONAL_POSITION_UPDATED,
367369
CancelledOrdersEvent: eventspb.BusEventType_BUS_EVENT_TYPE_CANCELLED_ORDERS,
370+
MarketCommunityTagsEvent: eventspb.BusEventType_BUS_EVENT_TYPE_MARKET_COMMUNITY_TAG,
368371
// If adding a type here, please also add it to datanode/broker/convert.go
369372
}
370373

@@ -456,6 +459,7 @@ var (
456459
TeamsStatsUpdatedEvent: "TeamsStatsUpdatedEvent",
457460
TimeWeightedNotionalPositionUpdatedEvent: "TimeWeightedNotionalPositionUpdatedEvent",
458461
CancelledOrdersEvent: "CancelledOrdersEvent",
462+
MarketCommunityTagsEvent: "MarketCommunityTagsEvent",
459463
}
460464
)
461465

core/events/market_community_tags.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2023 Gobalsky Labs Limited
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as
5+
// published by the Free Software Foundation, either version 3 of the
6+
// License, or (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
package events
17+
18+
import (
19+
"context"
20+
21+
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
22+
)
23+
24+
type MarketCommunityTags struct {
25+
*Base
26+
a eventspb.MarketCommunityTags
27+
}
28+
29+
func NewMarketCommunityTagsEvent(ctx context.Context, e eventspb.MarketCommunityTags) *MarketCommunityTags {
30+
return &MarketCommunityTags{
31+
Base: newBase(ctx, MarketCommunityTagsEvent),
32+
a: e,
33+
}
34+
}
35+
36+
func (a *MarketCommunityTags) MarketCommunityTags() eventspb.MarketCommunityTags {
37+
return a.a
38+
}
39+
40+
func (a MarketCommunityTags) Proto() eventspb.MarketCommunityTags {
41+
return a.a
42+
}
43+
44+
func (a MarketCommunityTags) StreamMessage() *eventspb.BusEvent {
45+
busEvent := newBusEventFromBase(a.Base)
46+
busEvent.Event = &eventspb.BusEvent_MarketCommunityTags{
47+
MarketCommunityTags: &a.a,
48+
}
49+
return busEvent
50+
}
51+
52+
func MarketCommunityTagsEventFromStream(ctx context.Context, be *eventspb.BusEvent) *MarketCommunityTags {
53+
return &MarketCommunityTags{
54+
Base: newBaseFromBusEvent(ctx, MarketCommunityTagsEvent, be),
55+
a: *be.GetMarketCommunityTags(),
56+
}
57+
}

core/execution/engine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type Engine struct {
7878
allMarkets map[string]common.CommonMarket
7979
allMarketsCpy []common.CommonMarket
8080

81+
communityTags *MarketCommunityTags
8182
collateral common.Collateral
8283
assets common.Assets
8384
referralDiscountRewardService fee.ReferralDiscountRewardService
@@ -216,6 +217,7 @@ func NewEngine(
216217
referralDiscountRewardService: referralDiscountRewardService,
217218
volumeDiscountService: volumeDiscountService,
218219
banking: banking,
220+
communityTags: NewMarketCommunityTags(broker),
219221
}
220222

221223
// set the eligibility for proposer bonus checker

core/execution/engine_snapshot.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,17 @@ func (e *Engine) serialise() (snapshot []byte, providers []types.StateProvider,
316316
allMarketIDs = append(allMarketIDs, cm.GetID())
317317
}
318318

319+
tags := e.communityTags.serialize()
320+
319321
pl := types.Payload{
320322
Data: &types.PayloadExecutionMarkets{
321323
ExecutionMarkets: &types.ExecutionMarkets{
322-
Markets: mkts,
323-
SpotMarkets: spotMkts,
324-
SettledMarkets: cpStates,
325-
Successors: successors,
326-
AllMarketIDs: allMarketIDs,
324+
Markets: mkts,
325+
SpotMarkets: spotMkts,
326+
SettledMarkets: cpStates,
327+
Successors: successors,
328+
AllMarketIDs: allMarketIDs,
329+
MarketCommunityTags: tags,
327330
},
328331
},
329332
}
@@ -382,6 +385,9 @@ func (e *Engine) LoadState(ctx context.Context, payload *types.Payload) ([]types
382385
e.allMarketsCpy = append(e.allMarketsCpy, mkt)
383386
}
384387
}
388+
389+
e.communityTags = NewMarketCommunityTagFromProto(e.broker, pl.ExecutionMarkets.MarketCommunityTags)
390+
385391
return append(providers, spotProviders...), err
386392
default:
387393
return nil, types.ErrUnknownSnapshotType
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (C) 2023 Gobalsky Labs Limited
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as
5+
// published by the Free Software Foundation, either version 3 of the
6+
// License, or (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
package execution
17+
18+
import (
19+
"context"
20+
"sort"
21+
22+
"code.vegaprotocol.io/vega/core/events"
23+
"code.vegaprotocol.io/vega/core/execution/common"
24+
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
25+
"golang.org/x/exp/maps"
26+
)
27+
28+
type MarketCommunityTags struct {
29+
// market id -> set of tags
30+
tags map[string]map[string]struct{}
31+
broker common.Broker
32+
}
33+
34+
func NewMarketCommunityTags(broker common.Broker) *MarketCommunityTags {
35+
return &MarketCommunityTags{
36+
tags: map[string]map[string]struct{}{},
37+
broker: broker,
38+
}
39+
}
40+
41+
func NewMarketCommunityTagFromProto(
42+
broker common.Broker,
43+
state []*eventspb.MarketCommunityTags,
44+
) *MarketCommunityTags {
45+
m := NewMarketCommunityTags(broker)
46+
47+
for _, v := range state {
48+
m.tags[v.MarketId] = map[string]struct{}{}
49+
for _, t := range v.Tags {
50+
m.tags[v.MarketId][t] = struct{}{}
51+
}
52+
}
53+
54+
return m
55+
}
56+
57+
func (m *MarketCommunityTags) serialize() []*eventspb.MarketCommunityTags {
58+
out := make([]*eventspb.MarketCommunityTags, 0, len(m.tags))
59+
60+
for mkt, tags := range m.tags {
61+
mct := &eventspb.MarketCommunityTags{
62+
MarketId: mkt,
63+
Tags: make([]string, 0, len(tags)),
64+
}
65+
66+
for tag, _ := range tags {
67+
mct.Tags = append(mct.Tags, tag)
68+
}
69+
70+
sort.Strings(mct.Tags)
71+
}
72+
73+
sort.Slice(out, func(i, j int) bool { return out[i].MarketId < out[j].MarketId })
74+
75+
return out
76+
}
77+
78+
// UpdateTags by that point the tags have been validated in lenght,
79+
// so no need to do that again.
80+
func (m *MarketCommunityTags) UpdateTags(
81+
ctx context.Context,
82+
market string,
83+
addTags []string,
84+
removeTags []string,
85+
) {
86+
tags, ok := m.tags[market]
87+
if !ok {
88+
tags = map[string]struct{}{}
89+
}
90+
91+
for _, t := range addTags {
92+
tags[t] = struct{}{}
93+
}
94+
95+
for _, t := range removeTags {
96+
delete(tags, t)
97+
}
98+
99+
evt := eventspb.MarketCommunityTags{
100+
MarketId: market,
101+
Tags: maps.Keys(tags),
102+
}
103+
104+
sort.Strings(evt.Tags)
105+
m.broker.Send(events.NewMarketCommunityTagsEvent(ctx, evt))
106+
}

core/types/governance_proposal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ const (
170170
ProposalTermsTypeUpdateMarketState
171171
ProposalTermsTypeUpdateReferralProgram
172172
ProposalTermsTypeUpdateVolumeDiscountProgram
173+
ProposalTermsTypeUpdateMarketCommunityTags
173174
)
174175

175176
type ProposalSubmission struct {

core/types/governance_proposal_terms.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func (p ProposalTerms) IntoProto() *vegapb.ProposalTerms {
171171
terms.Change = ch
172172
case *vegapb.ProposalTerms_UpdateVolumeDiscountProgram:
173173
terms.Change = ch
174+
case *vegapb.ProposalTerms_UpdateCommunityTags:
175+
terms.Change = ch
174176
}
175177

176178
return terms
@@ -341,6 +343,8 @@ func ProposalTermsFromProto(p *vegapb.ProposalTerms) (*ProposalTerms, error) {
341343
change, err = NewUpdateReferralProgramProposalFromProto(ch.UpdateReferralProgram)
342344
case *vegapb.ProposalTerms_UpdateVolumeDiscountProgram:
343345
change, err = NewUpdateVolumeDiscountProgramProposalFromProto(ch.UpdateVolumeDiscountProgram)
346+
case *vegapb.ProposalTerms_UpdateCommunityTags:
347+
change = NewUpdateMarketCommunityTagsFromProto(ch.UpdateCommunityTags)
344348
}
345349
if err != nil {
346350
return nil, err
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (C) 2023 Gobalsky Labs Limited
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as
5+
// published by the Free Software Foundation, either version 3 of the
6+
// License, or (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
package types
17+
18+
import (
19+
"fmt"
20+
"slices"
21+
22+
"code.vegaprotocol.io/vega/libs/stringer"
23+
vegapb "code.vegaprotocol.io/vega/protos/vega"
24+
)
25+
26+
type ProposalTermsUpdateMarketCommunityTags struct {
27+
UpdateMarketCommunityTags *UpdateMarketCommunityTags
28+
}
29+
30+
func (f ProposalTermsUpdateMarketCommunityTags) String() string {
31+
return fmt.Sprintf(
32+
"updateCommunityTags(%s)",
33+
stringer.PtrToString(f.UpdateMarketCommunityTags),
34+
)
35+
}
36+
37+
func (f ProposalTermsUpdateMarketCommunityTags) IntoProto() *vegapb.UpdateCommunityTags {
38+
var updateCommunityTags *vegapb.UpdateCommunityTags
39+
if f.UpdateMarketCommunityTags != nil {
40+
updateCommunityTags = f.UpdateMarketCommunityTags.IntoProto()
41+
}
42+
return updateCommunityTags
43+
}
44+
45+
func (f ProposalTermsUpdateMarketCommunityTags) isPTerm() {}
46+
47+
func (a ProposalTermsUpdateMarketCommunityTags) oneOfSingleProto() vegapb.ProposalOneOffTermChangeType {
48+
return &vegapb.ProposalTerms_UpdateCommunityTags{
49+
UpdateCommunityTags: a.IntoProto(),
50+
}
51+
}
52+
53+
func (a ProposalTermsUpdateMarketCommunityTags) oneOfBatchProto() vegapb.ProposalOneOffTermBatchChangeType {
54+
return &vegapb.BatchProposalTermsChange_UpdateCommunityTags{
55+
UpdateCommunityTags: a.IntoProto(),
56+
}
57+
}
58+
59+
func (f ProposalTermsUpdateMarketCommunityTags) GetTermType() ProposalTermsType {
60+
return ProposalTermsTypeUpdateMarketCommunityTags
61+
}
62+
63+
func (f ProposalTermsUpdateMarketCommunityTags) DeepClone() ProposalTerm {
64+
if f.UpdateMarketCommunityTags == nil {
65+
return &ProposalTermsUpdateMarketCommunityTags{}
66+
}
67+
return &ProposalTermsUpdateMarketCommunityTags{
68+
UpdateMarketCommunityTags: f.UpdateMarketCommunityTags.DeepClone(),
69+
}
70+
}
71+
72+
func NewUpdateMarketCommunityTagsFromProto(p *vegapb.UpdateCommunityTags) *ProposalTermsUpdateMarketCommunityTags {
73+
return &ProposalTermsUpdateMarketCommunityTags{
74+
UpdateMarketCommunityTags: &UpdateMarketCommunityTags{
75+
MarketID: p.MarketId,
76+
AddTags: slices.Clone(p.AddTags),
77+
RemoveTags: slices.Clone(p.RemoveTags),
78+
},
79+
}
80+
}
81+
82+
type UpdateMarketCommunityTags struct {
83+
MarketID string
84+
AddTags []string
85+
RemoveTags []string
86+
}
87+
88+
func (u UpdateMarketCommunityTags) IntoProto() *vegapb.UpdateCommunityTags {
89+
return &vegapb.UpdateCommunityTags{
90+
MarketId: u.MarketID,
91+
AddTags: slices.Clone(u.AddTags),
92+
RemoveTags: slices.Clone(u.RemoveTags),
93+
}
94+
}
95+
96+
func (u UpdateMarketCommunityTags) String() string {
97+
return fmt.Sprintf(
98+
"marketId(%v) addTags(%v) removeTags(%v)",
99+
u.MarketID,
100+
u.AddTags,
101+
u.RemoveTags,
102+
)
103+
}
104+
105+
func (u UpdateMarketCommunityTags) DeepClone() *UpdateMarketCommunityTags {
106+
return &UpdateMarketCommunityTags{
107+
MarketID: u.MarketID,
108+
AddTags: slices.Clone(u.AddTags),
109+
RemoveTags: slices.Clone(u.RemoveTags),
110+
}
111+
}

0 commit comments

Comments
 (0)