code.vegaprotocol.io/vega@v0.79.0/datanode/entities/volume_discount_stats.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  	"code.vegaprotocol.io/vega/protos/vega"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  )
    27  
    28  type (
    29  	VolumeDiscountStats struct {
    30  		AtEpoch                    uint64
    31  		PartiesVolumeDiscountStats []*eventspb.PartyVolumeDiscountStats
    32  		VegaTime                   time.Time
    33  	}
    34  
    35  	FlattenVolumeDiscountStats struct {
    36  		AtEpoch         uint64
    37  		PartyID         string
    38  		DiscountFactors *vega.DiscountFactors
    39  		RunningVolume   string
    40  		VegaTime        time.Time
    41  	}
    42  
    43  	VolumeDiscountStatsCursor struct {
    44  		VegaTime time.Time
    45  		AtEpoch  uint64
    46  		PartyID  string
    47  	}
    48  )
    49  
    50  func (s FlattenVolumeDiscountStats) Cursor() *Cursor {
    51  	c := VolumeDiscountStatsCursor{
    52  		VegaTime: s.VegaTime,
    53  		AtEpoch:  s.AtEpoch,
    54  		PartyID:  s.PartyID,
    55  	}
    56  	return NewCursor(c.ToString())
    57  }
    58  
    59  func (s FlattenVolumeDiscountStats) ToProtoEdge(_ ...any) (*v2.VolumeDiscountStatsEdge, error) {
    60  	return &v2.VolumeDiscountStatsEdge{
    61  		Node:   s.ToProto(),
    62  		Cursor: s.Cursor().Encode(),
    63  	}, nil
    64  }
    65  
    66  func (s FlattenVolumeDiscountStats) ToProto() *v2.VolumeDiscountStats {
    67  	return &v2.VolumeDiscountStats{
    68  		AtEpoch:         s.AtEpoch,
    69  		PartyId:         s.PartyID,
    70  		DiscountFactors: s.DiscountFactors,
    71  		RunningVolume:   s.RunningVolume,
    72  	}
    73  }
    74  
    75  func NewVolumeDiscountStatsFromProto(vestingStatsProto *eventspb.VolumeDiscountStatsUpdated, vegaTime time.Time) (*VolumeDiscountStats, error) {
    76  	return &VolumeDiscountStats{
    77  		AtEpoch:                    vestingStatsProto.AtEpoch,
    78  		PartiesVolumeDiscountStats: vestingStatsProto.Stats,
    79  		VegaTime:                   vegaTime,
    80  	}, nil
    81  }
    82  
    83  func (c *VolumeDiscountStatsCursor) ToString() string {
    84  	bs, err := json.Marshal(c)
    85  	if err != nil {
    86  		panic(fmt.Errorf("could not marshal volume discount stats cursor: %v", err))
    87  	}
    88  	return string(bs)
    89  }
    90  
    91  func (c *VolumeDiscountStatsCursor) Parse(cursorString string) error {
    92  	if cursorString == "" {
    93  		return nil
    94  	}
    95  	return json.Unmarshal([]byte(cursorString), c)
    96  }