code.vegaprotocol.io/vega@v0.79.0/datanode/entities/epoch_reward_summary.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 22 "code.vegaprotocol.io/vega/libs/num" 23 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 24 "code.vegaprotocol.io/vega/protos/vega" 25 ) 26 27 type EpochRewardSummary struct { 28 AssetID AssetID 29 MarketID MarketID 30 RewardType string 31 EpochID uint64 32 Amount num.Decimal 33 } 34 35 func (r *EpochRewardSummary) ToProto() *vega.EpochRewardSummary { 36 protoRewardSummary := vega.EpochRewardSummary{ 37 AssetId: r.AssetID.String(), 38 MarketId: r.MarketID.String(), 39 RewardType: r.RewardType, 40 Epoch: r.EpochID, 41 Amount: r.Amount.String(), 42 } 43 return &protoRewardSummary 44 } 45 46 func (r EpochRewardSummary) Cursor() *Cursor { 47 cursor := EpochRewardSummaryCursor{ 48 AssetID: r.AssetID.String(), 49 MarketID: r.MarketID.String(), 50 RewardType: r.RewardType, 51 EpochID: r.EpochID, 52 Amount: r.Amount.String(), 53 } 54 return NewCursor(cursor.String()) 55 } 56 57 func (r EpochRewardSummary) ToProtoEdge(_ ...any) (*v2.EpochRewardSummaryEdge, error) { 58 return &v2.EpochRewardSummaryEdge{ 59 Node: r.ToProto(), 60 Cursor: r.Cursor().Encode(), 61 }, nil 62 } 63 64 type EpochRewardSummaryCursor struct { 65 EpochID uint64 `json:"epoch_id"` 66 AssetID string `json:"asset_id"` 67 MarketID string `json:"market_id"` 68 RewardType string `json:"reward_type"` 69 Amount string `json:"amount"` 70 } 71 72 func (rc EpochRewardSummaryCursor) String() string { 73 bs, err := json.Marshal(rc) 74 if err != nil { 75 // This should never happen. 76 panic(fmt.Errorf("marshalling epoch reward summary cursor: %w", err)) 77 } 78 return string(bs) 79 } 80 81 func (rc *EpochRewardSummaryCursor) Parse(cursorString string) error { 82 if cursorString == "" { 83 return nil 84 } 85 return json.Unmarshal([]byte(cursorString), rc) 86 }