code.vegaprotocol.io/vega@v0.79.0/core/events/party.go (about)

     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  	"strings"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    24  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    25  
    26  	"golang.org/x/exp/maps"
    27  	"golang.org/x/exp/slices"
    28  )
    29  
    30  type Party struct {
    31  	*Base
    32  	p types.Party
    33  }
    34  
    35  func NewPartyEvent(ctx context.Context, party types.Party) *Party {
    36  	return &Party{
    37  		Base: newBase(ctx, PartyEvent),
    38  		p:    party,
    39  	}
    40  }
    41  
    42  func (p Party) IsParty(id string) bool {
    43  	return p.p.Id == id
    44  }
    45  
    46  func (p *Party) Party() types.Party {
    47  	return p.p
    48  }
    49  
    50  func (p Party) Proto() *vegapb.Party {
    51  	return p.p.IntoProto()
    52  }
    53  
    54  func (p Party) StreamMessage() *eventspb.BusEvent {
    55  	busEvent := newBusEventFromBase(p.Base)
    56  	busEvent.Event = &eventspb.BusEvent_Party{
    57  		Party: p.Proto(),
    58  	}
    59  
    60  	return busEvent
    61  }
    62  
    63  func PartyEventFromStream(ctx context.Context, be *eventspb.BusEvent) *Party {
    64  	return &Party{
    65  		Base: newBaseFromBusEvent(ctx, PartyEvent, be),
    66  		p:    types.Party{Id: be.GetParty().Id},
    67  	}
    68  }
    69  
    70  type PartyProfileUpdated struct {
    71  	*Base
    72  	e eventspb.PartyProfileUpdated
    73  }
    74  
    75  func (t PartyProfileUpdated) StreamMessage() *eventspb.BusEvent {
    76  	busEvent := newBusEventFromBase(t.Base)
    77  	busEvent.Event = &eventspb.BusEvent_PartyProfileUpdated{
    78  		PartyProfileUpdated: &t.e,
    79  	}
    80  
    81  	return busEvent
    82  }
    83  
    84  func (t PartyProfileUpdated) PartyProfileUpdated() *eventspb.PartyProfileUpdated {
    85  	return &t.e
    86  }
    87  
    88  func NewPartyProfileUpdatedEvent(ctx context.Context, p *types.PartyProfile) *PartyProfileUpdated {
    89  	metadata := make([]*vegapb.Metadata, 0, len(p.Metadata))
    90  	for k, v := range p.Metadata {
    91  		metadata = append(metadata, &vegapb.Metadata{
    92  			Key:   k,
    93  			Value: v,
    94  		})
    95  	}
    96  
    97  	// Ensure deterministic order in event.
    98  	slices.SortStableFunc(metadata, func(a, b *vegapb.Metadata) int {
    99  		return strings.Compare(a.Key, b.Key)
   100  	})
   101  
   102  	// Ensure deterministic order in event.
   103  	slices.SortStableFunc(metadata, func(a, b *vegapb.Metadata) int {
   104  		return strings.Compare(a.Key, b.Key)
   105  	})
   106  
   107  	derivedKeys := maps.Keys(p.DerivedKeys)
   108  
   109  	slices.SortStableFunc(derivedKeys, func(a, b string) int {
   110  		return strings.Compare(a, b)
   111  	})
   112  
   113  	return &PartyProfileUpdated{
   114  		Base: newBase(ctx, PartyProfileUpdatedEvent),
   115  		e: eventspb.PartyProfileUpdated{
   116  			UpdatedProfile: &vegapb.PartyProfile{
   117  				PartyId:     p.PartyID.String(),
   118  				Alias:       p.Alias,
   119  				Metadata:    metadata,
   120  				DerivedKeys: derivedKeys,
   121  			},
   122  		},
   123  	}
   124  }
   125  
   126  func PartyProfileUpdatedEventFromStream(ctx context.Context, be *eventspb.BusEvent) *PartyProfileUpdated {
   127  	return &PartyProfileUpdated{
   128  		Base: newBaseFromBusEvent(ctx, PartyProfileUpdatedEvent, be),
   129  		e:    *be.GetPartyProfileUpdated(),
   130  	}
   131  }