code.vegaprotocol.io/vega@v0.79.0/datanode/entities/margin_mode.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 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 26 ) 27 28 type PartyMarginMode struct { 29 MarketID MarketID 30 PartyID PartyID 31 MarginMode vega.MarginMode 32 MarginFactor *num.Decimal 33 MinTheoreticalMarginFactor *num.Decimal 34 MaxTheoreticalLeverage *num.Decimal 35 AtEpoch uint64 36 } 37 38 func (t PartyMarginMode) Cursor() *Cursor { 39 tc := PartyMarginModeCursor{ 40 MarketID: t.MarketID, 41 PartyID: t.PartyID, 42 } 43 return NewCursor(tc.String()) 44 } 45 46 func (t PartyMarginMode) ToProto() *v2.PartyMarginMode { 47 var marginFactor, minTheoreticalMarginFactor, maxTheoreticalLeverage *string 48 49 if t.MarginFactor != nil { 50 factor := t.MarginFactor.String() 51 marginFactor = &factor 52 } 53 54 if t.MinTheoreticalMarginFactor != nil { 55 factor := t.MinTheoreticalMarginFactor.String() 56 minTheoreticalMarginFactor = &factor 57 } 58 59 if t.MaxTheoreticalLeverage != nil { 60 leverage := t.MaxTheoreticalLeverage.String() 61 maxTheoreticalLeverage = &leverage 62 } 63 64 return &v2.PartyMarginMode{ 65 MarketId: string(t.MarketID), 66 PartyId: string(t.PartyID), 67 MarginMode: t.MarginMode, 68 MarginFactor: marginFactor, 69 MinTheoreticalMarginFactor: minTheoreticalMarginFactor, 70 MaxTheoreticalLeverage: maxTheoreticalLeverage, 71 AtEpoch: t.AtEpoch, 72 } 73 } 74 75 func (t PartyMarginMode) ToProtoEdge(_ ...any) (*v2.PartyMarginModeEdge, error) { 76 return &v2.PartyMarginModeEdge{ 77 Node: t.ToProto(), 78 Cursor: t.Cursor().Encode(), 79 }, nil 80 } 81 82 func PartyMarginModeFromProto(update *eventspb.PartyMarginModeUpdated) PartyMarginMode { 83 var marginFactor, minTheoreticalMarginFactor, maxTheoreticalLeverage *num.Decimal 84 85 if update.MarginFactor != nil { 86 factor, _ := num.DecimalFromString(*update.MarginFactor) 87 marginFactor = &factor 88 } 89 90 if update.MinTheoreticalMarginFactor != nil { 91 factor, _ := num.DecimalFromString(*update.MinTheoreticalMarginFactor) 92 minTheoreticalMarginFactor = &factor 93 } 94 95 if update.MaxTheoreticalLeverage != nil { 96 factor, _ := num.DecimalFromString(*update.MaxTheoreticalLeverage) 97 maxTheoreticalLeverage = &factor 98 } 99 100 return PartyMarginMode{ 101 MarketID: MarketID(update.MarketId), 102 PartyID: PartyID(update.PartyId), 103 MarginMode: update.MarginMode, 104 MarginFactor: marginFactor, 105 MinTheoreticalMarginFactor: minTheoreticalMarginFactor, 106 MaxTheoreticalLeverage: maxTheoreticalLeverage, 107 AtEpoch: update.AtEpoch, 108 } 109 } 110 111 type PartyMarginModeCursor struct { 112 MarketID MarketID 113 PartyID PartyID 114 } 115 116 func (tc PartyMarginModeCursor) String() string { 117 bs, err := json.Marshal(tc) 118 if err != nil { 119 panic(fmt.Errorf("could not marshal party margin mode cursor: %v", err)) 120 } 121 return string(bs) 122 } 123 124 func (tc *PartyMarginModeCursor) Parse(cursorString string) error { 125 if cursorString == "" { 126 return nil 127 } 128 return json.Unmarshal([]byte(cursorString), tc) 129 }