code.vegaprotocol.io/vega@v0.79.0/datanode/entities/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 entities
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"time"
    22  
    23  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  )
    26  
    27  type _Party struct{}
    28  
    29  type PartyID = ID[_Party]
    30  
    31  func NewPartyIDSlice(ids ...string) []PartyID {
    32  	res := make([]PartyID, 0, len(ids))
    33  	for _, v := range ids {
    34  		res = append(res, PartyID(v))
    35  	}
    36  	return res
    37  }
    38  
    39  type Party struct {
    40  	ID       PartyID
    41  	TxHash   TxHash
    42  	VegaTime *time.Time // Can be NULL for built-in party 'network'
    43  }
    44  
    45  func PartyFromProto(pp *vegapb.Party, txHash TxHash) Party {
    46  	return Party{
    47  		ID:     PartyID(pp.Id),
    48  		TxHash: txHash,
    49  	}
    50  }
    51  
    52  func (p Party) ToProto() *vegapb.Party {
    53  	return &vegapb.Party{
    54  		Id: p.ID.String(),
    55  	}
    56  }
    57  
    58  func (p Party) Cursor() *Cursor {
    59  	return NewCursor(p.String())
    60  }
    61  
    62  func (p Party) ToProtoEdge(_ ...any) (*v2.PartyEdge, error) {
    63  	return &v2.PartyEdge{
    64  		Node:   p.ToProto(),
    65  		Cursor: p.Cursor().Encode(),
    66  	}, nil
    67  }
    68  
    69  func (p *Party) Parse(cursorString string) error {
    70  	if cursorString == "" {
    71  		return nil
    72  	}
    73  
    74  	return json.Unmarshal([]byte(cursorString), p)
    75  }
    76  
    77  func (p Party) String() string {
    78  	bs, err := json.Marshal(p)
    79  	if err != nil {
    80  		panic(fmt.Errorf("failed to marshal party: %w", err))
    81  	}
    82  	return string(bs)
    83  }
    84  
    85  type PartyProfile struct {
    86  	PartyID  PartyID
    87  	Alias    string
    88  	Metadata []*vegapb.Metadata
    89  }
    90  
    91  func (p PartyProfile) ToProto() *vegapb.PartyProfile {
    92  	return &vegapb.PartyProfile{
    93  		PartyId:  p.PartyID.String(),
    94  		Alias:    p.Alias,
    95  		Metadata: p.Metadata,
    96  	}
    97  }
    98  
    99  func (p PartyProfile) Cursor() *Cursor {
   100  	return NewCursor(p.String())
   101  }
   102  
   103  func (p PartyProfile) ToProtoEdge(_ ...any) (*v2.PartyProfileEdge, error) {
   104  	return &v2.PartyProfileEdge{
   105  		Node:   p.ToProto(),
   106  		Cursor: p.Cursor().Encode(),
   107  	}, nil
   108  }
   109  
   110  func (p *PartyProfile) Parse(cursorString string) error {
   111  	if cursorString == "" {
   112  		return nil
   113  	}
   114  
   115  	return json.Unmarshal([]byte(cursorString), p)
   116  }
   117  
   118  func (p PartyProfile) String() string {
   119  	bs, err := json.Marshal(p)
   120  	if err != nil {
   121  		panic(fmt.Errorf("failed to marshal party profile: %w", err))
   122  	}
   123  	return string(bs)
   124  }
   125  
   126  func PartyProfileFromProto(t *vegapb.PartyProfile) *PartyProfile {
   127  	return &PartyProfile{
   128  		PartyID:  PartyID(t.PartyId),
   129  		Alias:    t.Alias,
   130  		Metadata: t.Metadata,
   131  	}
   132  }
   133  
   134  type PartyProfileCursor struct {
   135  	ID PartyID
   136  }
   137  
   138  func (tc PartyProfileCursor) String() string {
   139  	bs, err := json.Marshal(tc)
   140  	if err != nil {
   141  		panic(fmt.Errorf("could not marshal party profile cursor: %v", err))
   142  	}
   143  	return string(bs)
   144  }
   145  
   146  func (tc *PartyProfileCursor) Parse(cursorString string) error {
   147  	if cursorString == "" {
   148  		return nil
   149  	}
   150  	return json.Unmarshal([]byte(cursorString), tc)
   151  }