code.vegaprotocol.io/vega@v0.79.0/datanode/entities/delegation.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 "strconv" 22 "time" 23 24 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 25 "code.vegaprotocol.io/vega/protos/vega" 26 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 27 28 "github.com/shopspring/decimal" 29 ) 30 31 type Delegation struct { 32 PartyID PartyID `json:"party_id"` 33 NodeID NodeID `json:"node_id"` 34 EpochID int64 `json:"epoch_id"` 35 Amount decimal.Decimal `json:"amount"` 36 TxHash TxHash 37 VegaTime time.Time 38 SeqNum uint64 39 } 40 41 func (d Delegation) String() string { 42 return fmt.Sprintf("{Epoch: %v, Party: %s, Node: %s, Amount: %v}", 43 d.EpochID, d.PartyID, d.NodeID, d.Amount) 44 } 45 46 func (d Delegation) ToProto() *vega.Delegation { 47 protoDelegation := vega.Delegation{ 48 Party: d.PartyID.String(), 49 NodeId: d.NodeID.String(), 50 EpochSeq: fmt.Sprintf("%v", d.EpochID), 51 Amount: d.Amount.String(), 52 } 53 return &protoDelegation 54 } 55 56 func (d Delegation) Cursor() *Cursor { 57 dc := DelegationCursor{ 58 VegaTime: d.VegaTime, 59 PartyID: d.PartyID, 60 NodeID: d.NodeID, 61 EpochID: d.EpochID, 62 } 63 return NewCursor(dc.String()) 64 } 65 66 func (d Delegation) ToProtoEdge(_ ...any) (*v2.DelegationEdge, error) { 67 return &v2.DelegationEdge{ 68 Node: d.ToProto(), 69 Cursor: d.Cursor().Encode(), 70 }, nil 71 } 72 73 func DelegationFromProto(pd *vega.Delegation, txHash TxHash) (Delegation, error) { 74 epochID, err := strconv.ParseInt(pd.EpochSeq, 10, 64) 75 if err != nil { 76 return Delegation{}, fmt.Errorf("parsing epoch '%v': %w", pd.EpochSeq, err) 77 } 78 79 amount, err := decimal.NewFromString(pd.Amount) 80 if err != nil { 81 return Delegation{}, fmt.Errorf("parsing amount of delegation: '%v': %w", 82 pd.Amount, err) 83 } 84 85 delegation := Delegation{ 86 PartyID: PartyID(pd.Party), 87 NodeID: NodeID(pd.NodeId), 88 EpochID: epochID, 89 Amount: amount, 90 TxHash: txHash, 91 } 92 93 return delegation, nil 94 } 95 96 func DelegationFromEventProto(pd *eventspb.DelegationBalanceEvent, txHash TxHash) (Delegation, error) { 97 epochID, err := strconv.ParseInt(pd.EpochSeq, 10, 64) 98 if err != nil { 99 return Delegation{}, fmt.Errorf("parsing epoch '%v': %w", pd.EpochSeq, err) 100 } 101 102 amount, err := decimal.NewFromString(pd.Amount) 103 if err != nil { 104 return Delegation{}, fmt.Errorf("parsing amount of delegation: '%v': %w", 105 pd.Amount, err) 106 } 107 108 delegation := Delegation{ 109 PartyID: PartyID(pd.Party), 110 NodeID: NodeID(pd.NodeId), 111 EpochID: epochID, 112 Amount: amount, 113 TxHash: txHash, 114 } 115 116 return delegation, nil 117 } 118 119 type DelegationCursor struct { 120 VegaTime time.Time `json:"vegaTime"` 121 PartyID PartyID `json:"partyId"` 122 NodeID NodeID `json:"nodeId"` 123 EpochID int64 `json:"epochId"` 124 } 125 126 func (c DelegationCursor) String() string { 127 bs, err := json.Marshal(c) 128 if err != nil { 129 panic(fmt.Errorf("could not marshal delegation cursor: %w", err)) 130 } 131 return string(bs) 132 } 133 134 func (c *DelegationCursor) Parse(cursorString string) error { 135 if cursorString == "" { 136 return nil 137 } 138 return json.Unmarshal([]byte(cursorString), c) 139 }