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