code.vegaprotocol.io/vega@v0.79.0/datanode/entities/game_scores.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  	"code.vegaprotocol.io/vega/libs/num"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  
    27  	"github.com/shopspring/decimal"
    28  )
    29  
    30  type GamePartyScore struct {
    31  	GameID         GameID
    32  	TeamID         *TeamID
    33  	EpochID        int64
    34  	PartyID        PartyID
    35  	Score          decimal.Decimal
    36  	StakingBalance decimal.Decimal
    37  	OpenVolume     decimal.Decimal
    38  	TotalFeesPaid  decimal.Decimal
    39  	IsEligible     bool
    40  	Rank           *uint64
    41  	VegaTime       time.Time
    42  }
    43  
    44  func (pgs GamePartyScore) ToProto() *eventspb.GamePartyScore {
    45  	var teamID *string
    46  	if pgs.TeamID != nil {
    47  		tid := pgs.TeamID.String()
    48  		teamID = &tid
    49  	}
    50  	return &eventspb.GamePartyScore{
    51  		GameId:         pgs.GameID.String(),
    52  		Party:          pgs.PartyID.String(),
    53  		Epoch:          pgs.EpochID,
    54  		TeamId:         teamID,
    55  		Score:          pgs.Score.String(),
    56  		StakingBalance: pgs.StakingBalance.String(),
    57  		OpenVolume:     pgs.OpenVolume.String(),
    58  		TotalFeesPaid:  pgs.TotalFeesPaid.String(),
    59  		IsEligible:     pgs.IsEligible,
    60  		Rank:           pgs.Rank,
    61  		Time:           pgs.VegaTime.UnixNano(),
    62  	}
    63  }
    64  
    65  type GameTeamScore struct {
    66  	GameID   GameID
    67  	TeamID   TeamID
    68  	EpochID  int64
    69  	Score    decimal.Decimal
    70  	VegaTime time.Time
    71  }
    72  
    73  func (pgs GameTeamScore) ToProto() *eventspb.GameTeamScore {
    74  	return &eventspb.GameTeamScore{
    75  		GameId: pgs.GameID.String(),
    76  		Epoch:  pgs.EpochID,
    77  		TeamId: pgs.TeamID.String(),
    78  		Score:  pgs.Score.String(),
    79  		Time:   pgs.VegaTime.UnixNano(),
    80  	}
    81  }
    82  
    83  func GameScoresFromProto(gs *eventspb.GameScores, txHash TxHash, vegaTime time.Time, seqNum uint64) ([]GameTeamScore, []GamePartyScore, error) {
    84  	ts := make([]GameTeamScore, 0, len(gs.TeamScores))
    85  	ps := []GamePartyScore{}
    86  	for _, gsTeam := range gs.TeamScores {
    87  		score, err := num.DecimalFromString(gsTeam.Score)
    88  		if err != nil {
    89  			return nil, nil, err
    90  		}
    91  		ts = append(ts, GameTeamScore{
    92  			GameID:   (ID[_Game])(gsTeam.GameId),
    93  			TeamID:   ID[_Team](gsTeam.TeamId),
    94  			EpochID:  gsTeam.Epoch,
    95  			Score:    score,
    96  			VegaTime: vegaTime,
    97  		})
    98  	}
    99  	for _, gsParty := range gs.PartyScores {
   100  		score, err := num.DecimalFromString(gsParty.Score)
   101  		if err != nil {
   102  			return nil, nil, err
   103  		}
   104  		var stakingBalance num.Decimal
   105  		if len(gsParty.StakingBalance) > 0 {
   106  			stakingBalance, err = num.DecimalFromString(gsParty.StakingBalance)
   107  			if err != nil {
   108  				return nil, nil, err
   109  			}
   110  		}
   111  		var openVolume num.Decimal
   112  		if len(gsParty.OpenVolume) > 0 {
   113  			openVolume, err = num.DecimalFromString(gsParty.OpenVolume)
   114  			if err != nil {
   115  				return nil, nil, err
   116  			}
   117  		}
   118  		var totalFeesPaid num.Decimal
   119  		if len(gsParty.TotalFeesPaid) > 0 {
   120  			totalFeesPaid, err = num.DecimalFromString(gsParty.TotalFeesPaid)
   121  			if err != nil {
   122  				return nil, nil, err
   123  			}
   124  		}
   125  		ps = append(ps, GamePartyScore{
   126  			GameID:         (ID[_Game])(gsParty.GameId),
   127  			EpochID:        gsParty.Epoch,
   128  			PartyID:        ID[_Party](gsParty.Party),
   129  			Score:          score,
   130  			StakingBalance: stakingBalance,
   131  			OpenVolume:     openVolume,
   132  			TotalFeesPaid:  totalFeesPaid,
   133  			IsEligible:     gsParty.IsEligible,
   134  			VegaTime:       vegaTime,
   135  		})
   136  	}
   137  
   138  	return ts, ps, nil
   139  }
   140  
   141  func (pgs GamePartyScore) Cursor() *Cursor {
   142  	cursor := PartyGameScoreCursor{
   143  		GameID:   pgs.GameID.String(),
   144  		PartyID:  pgs.PartyID.String(),
   145  		EpochID:  pgs.EpochID,
   146  		VegaTime: pgs.VegaTime,
   147  	}
   148  	return NewCursor(cursor.String())
   149  }
   150  
   151  func (pgs GamePartyScore) ToProtoEdge(_ ...any) (*v2.GamePartyScoresEdge, error) {
   152  	return &v2.GamePartyScoresEdge{
   153  		Node:   pgs.ToProto(),
   154  		Cursor: pgs.Cursor().Encode(),
   155  	}, nil
   156  }
   157  
   158  func (pgs GameTeamScore) Cursor() *Cursor {
   159  	cursor := TeamGameScoreCursor{
   160  		GameID:   pgs.GameID.String(),
   161  		TeamID:   pgs.TeamID.String(),
   162  		EpochID:  pgs.EpochID,
   163  		VegaTime: pgs.VegaTime,
   164  	}
   165  	return NewCursor(cursor.String())
   166  }
   167  
   168  func (pgs GameTeamScore) ToProtoEdge(_ ...any) (*v2.GameTeamScoresEdge, error) {
   169  	return &v2.GameTeamScoresEdge{
   170  		Node:   pgs.ToProto(),
   171  		Cursor: pgs.Cursor().Encode(),
   172  	}, nil
   173  }
   174  
   175  type PartyGameScoreCursor struct {
   176  	GameID   string    `json:"game_id"`
   177  	PartyID  string    `json:"party_id"`
   178  	EpochID  int64     `json:"epoch_id"`
   179  	VegaTime time.Time `json:"vega_time"`
   180  }
   181  
   182  func (pg PartyGameScoreCursor) String() string {
   183  	bs, err := json.Marshal(pg)
   184  	if err != nil {
   185  		panic(fmt.Errorf("marshalling party game cursor: %w", err))
   186  	}
   187  	return string(bs)
   188  }
   189  
   190  func (pg *PartyGameScoreCursor) Parse(cursorString string) error {
   191  	if cursorString == "" {
   192  		return nil
   193  	}
   194  	return json.Unmarshal([]byte(cursorString), pg)
   195  }
   196  
   197  type TeamGameScoreCursor struct {
   198  	GameID   string    `json:"game_id"`
   199  	TeamID   string    `json:"team_id"`
   200  	EpochID  int64     `json:"epoch_id"`
   201  	VegaTime time.Time `json:"vega_time"`
   202  }
   203  
   204  func (pg TeamGameScoreCursor) String() string {
   205  	bs, err := json.Marshal(pg)
   206  	if err != nil {
   207  		panic(fmt.Errorf("marshalling team game score cursor: %w", err))
   208  	}
   209  	return string(bs)
   210  }
   211  
   212  func (pg *TeamGameScoreCursor) Parse(cursorString string) error {
   213  	if cursorString == "" {
   214  		return nil
   215  	}
   216  	return json.Unmarshal([]byte(cursorString), pg)
   217  }