code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/modelext.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 gql 17 18 import ( 19 "errors" 20 "strconv" 21 22 types "code.vegaprotocol.io/vega/protos/vega" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 ) 25 26 var ( 27 // ErrNilTradingMode ... 28 ErrNilTradingMode = errors.New("nil trading mode") 29 // ErrAmbiguousTradingMode ... 30 ErrAmbiguousTradingMode = errors.New("more than one trading mode selected") 31 // ErrUnimplementedTradingMode ... 32 ErrUnimplementedTradingMode = errors.New("unimplemented trading mode") 33 // ErrNilProduct ... 34 ErrNilProduct = errors.New("nil product") 35 // ErrNilRiskModel ... 36 ErrNilRiskModel = errors.New("nil risk model") 37 // ErrInvalidChange ... 38 ErrInvalidChange = errors.New("nil update market, new market and update network") 39 // ErrNilAssetSource returned when an asset source is not specified at creation. 40 ErrNilAssetSource = errors.New("nil asset source") 41 // ErrUnimplementedAssetSource returned when an asset source specified at creation is not recognised. 42 ErrUnimplementedAssetSource = errors.New("unimplemented asset source") 43 // ErrMultipleProposalChangesSpecified is raised when multiple proposal changes are set 44 // (non-null) for a singe proposal terms. 45 ErrMultipleProposalChangesSpecified = errors.New("multiple proposal changes specified") 46 // ErrMultipleAssetSourcesSpecified is raised when multiple asset source are specified. 47 ErrMultipleAssetSourcesSpecified = errors.New("multiple asset sources specified") 48 // ErrNilPriceMonitoringParameters ... 49 ErrNilPriceMonitoringParameters = errors.New("nil price monitoring parameters") 50 ) 51 52 type MarketLogEvent interface { 53 GetMarketID() string 54 GetPayload() string 55 } 56 57 func PriceMonitoringTriggerFromProto(ppmt *types.PriceMonitoringTrigger) (*PriceMonitoringTrigger, error) { 58 probability, err := strconv.ParseFloat(ppmt.Probability, 64) 59 if err != nil { 60 return nil, err 61 } 62 63 return &PriceMonitoringTrigger{ 64 HorizonSecs: int(ppmt.Horizon), 65 Probability: probability, 66 AuctionExtensionSecs: int(ppmt.AuctionExtension), 67 }, nil 68 } 69 70 func PriceMonitoringParametersFromProto(ppmp *types.PriceMonitoringParameters) (*PriceMonitoringParameters, error) { 71 if ppmp == nil { 72 return nil, ErrNilPriceMonitoringParameters 73 } 74 75 triggers := make([]*PriceMonitoringTrigger, 0, len(ppmp.Triggers)) 76 for _, v := range ppmp.Triggers { 77 trigger, err := PriceMonitoringTriggerFromProto(v) 78 if err != nil { 79 return nil, err 80 } 81 triggers = append(triggers, trigger) 82 } 83 84 return &PriceMonitoringParameters{ 85 Triggers: triggers, 86 }, nil 87 } 88 89 func PriceMonitoringSettingsFromProto(ppmst *types.PriceMonitoringSettings) (*PriceMonitoringSettings, error) { 90 if ppmst == nil { 91 // these are not mandatoryu anyway for now, so if nil we return an empty one 92 return &PriceMonitoringSettings{}, nil 93 } 94 95 params, err := PriceMonitoringParametersFromProto(ppmst.Parameters) 96 if err != nil { 97 return nil, err 98 } 99 return &PriceMonitoringSettings{ 100 Parameters: params, 101 }, nil 102 } 103 104 // ProposalVoteFromProto ... 105 func ProposalVoteFromProto(v *types.Vote) *ProposalVote { 106 return &ProposalVote{ 107 Vote: v, 108 ProposalID: v.ProposalId, 109 } 110 } 111 112 func busEventFromProto(events ...*eventspb.BusEvent) []*BusEvent { 113 r := make([]*BusEvent, 0, len(events)) 114 for _, e := range events { 115 evt := eventFromProto(e) 116 if evt == nil { 117 // @TODO for now just skip unmapped event types, probably better to handle some kind of error 118 // in the future though 119 continue 120 } 121 et, err := eventTypeFromProto(e.Type) 122 if err != nil { 123 // @TODO for now just skip unmapped event types, probably better to handle some kind of error 124 // in the future though 125 continue 126 } 127 be := BusEvent{ 128 ID: e.Id, 129 Type: et, 130 Block: e.Block, 131 Event: evt, 132 } 133 r = append(r, &be) 134 } 135 return r 136 } 137 138 func eventFromProto(e *eventspb.BusEvent) Event { 139 switch e.Type { 140 case eventspb.BusEventType_BUS_EVENT_TYPE_TIME_UPDATE: 141 return &TimeUpdate{ 142 Timestamp: e.GetTimeUpdate().Timestamp, 143 } 144 case eventspb.BusEventType_BUS_EVENT_TYPE_DEPOSIT: 145 return e.GetDeposit() 146 case eventspb.BusEventType_BUS_EVENT_TYPE_WITHDRAWAL: 147 return e.GetWithdrawal() 148 case eventspb.BusEventType_BUS_EVENT_TYPE_TRANSACTION_RESULT: 149 return e.GetTransactionResult() 150 } 151 return nil 152 } 153 154 func eventTypeToProto(btypes ...BusEventType) []eventspb.BusEventType { 155 r := make([]eventspb.BusEventType, 0, len(btypes)) 156 for _, t := range btypes { 157 switch t { 158 case BusEventTypeTimeUpdate: 159 r = append(r, eventspb.BusEventType_BUS_EVENT_TYPE_TIME_UPDATE) 160 r = append(r, eventspb.BusEventType_BUS_EVENT_TYPE_LIQUIDITY_PROVISION) 161 case BusEventTypeDeposit: 162 r = append(r, eventspb.BusEventType_BUS_EVENT_TYPE_DEPOSIT) 163 case BusEventTypeWithdrawal: 164 r = append(r, eventspb.BusEventType_BUS_EVENT_TYPE_WITHDRAWAL) 165 case BusEventTypeTransactionResult: 166 r = append(r, eventspb.BusEventType_BUS_EVENT_TYPE_TRANSACTION_RESULT) 167 } 168 } 169 return r 170 } 171 172 func eventTypeFromProto(t eventspb.BusEventType) (BusEventType, error) { 173 switch t { 174 case eventspb.BusEventType_BUS_EVENT_TYPE_TIME_UPDATE: 175 return BusEventTypeTimeUpdate, nil 176 case eventspb.BusEventType_BUS_EVENT_TYPE_DEPOSIT: 177 return BusEventTypeDeposit, nil 178 case eventspb.BusEventType_BUS_EVENT_TYPE_WITHDRAWAL: 179 return BusEventTypeWithdrawal, nil 180 case eventspb.BusEventType_BUS_EVENT_TYPE_TRANSACTION_RESULT: 181 return BusEventTypeTransactionResult, nil 182 } 183 return "", errors.New("unsupported proto event type") 184 }