code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/time_weighted_notional_position.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 sqlsubscribers 17 18 import ( 19 "context" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/core/events" 23 "code.vegaprotocol.io/vega/datanode/entities" 24 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 25 ) 26 27 type ( 28 TimeWeightedNotionalPositionUpdatedEvent interface { 29 events.Event 30 TimeWeightedNotionalPositionUpdated() *eventspb.TimeWeightedNotionalPositionUpdated 31 } 32 33 TimeWeightedNotionalPositionStore interface { 34 Upsert(ctx context.Context, twNotionalPos entities.TimeWeightedNotionalPosition) error 35 } 36 37 TimeWeightedNotionalPosition struct { 38 subscriber 39 store TimeWeightedNotionalPositionStore 40 } 41 ) 42 43 func NewTimeWeightedNotionalPosition(store TimeWeightedNotionalPositionStore) *TimeWeightedNotionalPosition { 44 return &TimeWeightedNotionalPosition{ 45 store: store, 46 } 47 } 48 49 func (tw *TimeWeightedNotionalPosition) Types() []events.Type { 50 return []events.Type{ 51 events.TimeWeightedNotionalPositionUpdatedEvent, 52 } 53 } 54 55 func (tw *TimeWeightedNotionalPosition) Push(ctx context.Context, e events.Event) error { 56 switch t := e.(type) { 57 case TimeWeightedNotionalPositionUpdatedEvent: 58 pos, err := entities.TimeWeightedNotionalPositionFromProto(t.TimeWeightedNotionalPositionUpdated(), tw.vegaTime) 59 if err != nil { 60 return fmt.Errorf("error converting TimeWeightedNotionalPositionUpdatedEvent to TimeWeightedNotionalPosition: %w", err) 61 } 62 return tw.store.Upsert(ctx, *pos) 63 default: 64 return fmt.Errorf("unexpected event type: %T", e) 65 } 66 } 67 68 func (tw *TimeWeightedNotionalPosition) Name() string { 69 return "TimeWeightedNotionalPosition" 70 }