code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/volume_discount_programs.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 "time" 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 VolumeDiscountProgramStartedEvent interface { 29 events.Event 30 GetVolumeDiscountProgramStarted() *eventspb.VolumeDiscountProgramStarted 31 } 32 33 VolumeDiscountProgramUpdatedEvent interface { 34 events.Event 35 GetVolumeDiscountProgramUpdated() *eventspb.VolumeDiscountProgramUpdated 36 } 37 38 VolumeDiscountProgramEndedEvent interface { 39 events.Event 40 GetVolumeDiscountProgramEnded() *eventspb.VolumeDiscountProgramEnded 41 } 42 43 VolumeDiscountStore interface { 44 AddVolumeDiscountProgram(ctx context.Context, referral *entities.VolumeDiscountProgram) error 45 UpdateVolumeDiscountProgram(ctx context.Context, referral *entities.VolumeDiscountProgram) error 46 EndVolumeDiscountProgram(ctx context.Context, version uint64, endedAt time.Time, vegaTime time.Time, seqNum uint64) error 47 } 48 49 VolumeDiscountProgram struct { 50 subscriber 51 store VolumeDiscountStore 52 } 53 ) 54 55 func NewVolumeDiscountProgram(store VolumeDiscountStore) *VolumeDiscountProgram { 56 return &VolumeDiscountProgram{ 57 store: store, 58 } 59 } 60 61 func (rp *VolumeDiscountProgram) Types() []events.Type { 62 return []events.Type{ 63 events.VolumeDiscountProgramStartedEvent, 64 events.VolumeDiscountProgramUpdatedEvent, 65 events.VolumeDiscountProgramEndedEvent, 66 } 67 } 68 69 func (rp *VolumeDiscountProgram) Push(ctx context.Context, evt events.Event) error { 70 switch e := evt.(type) { 71 case VolumeDiscountProgramStartedEvent: 72 return rp.consumeVolumeDiscountProgramStartedEvent(ctx, e) 73 case VolumeDiscountProgramUpdatedEvent: 74 return rp.consumeVolumeDiscountProgramUpdatedEvent(ctx, e) 75 case VolumeDiscountProgramEndedEvent: 76 return rp.consumeVolumeDiscountProgramEndedEvent(ctx, e) 77 default: 78 return nil 79 } 80 } 81 82 func (rp *VolumeDiscountProgram) consumeVolumeDiscountProgramStartedEvent(ctx context.Context, e VolumeDiscountProgramStartedEvent) error { 83 program := entities.VolumeDiscountProgramFromProto(e.GetVolumeDiscountProgramStarted().GetProgram(), rp.vegaTime, e.Sequence()) 84 return rp.store.AddVolumeDiscountProgram(ctx, program) 85 } 86 87 func (rp *VolumeDiscountProgram) consumeVolumeDiscountProgramUpdatedEvent(ctx context.Context, e VolumeDiscountProgramUpdatedEvent) error { 88 program := entities.VolumeDiscountProgramFromProto(e.GetVolumeDiscountProgramUpdated().GetProgram(), rp.vegaTime, e.Sequence()) 89 return rp.store.UpdateVolumeDiscountProgram(ctx, program) 90 } 91 92 func (rp *VolumeDiscountProgram) consumeVolumeDiscountProgramEndedEvent(ctx context.Context, e VolumeDiscountProgramEndedEvent) error { 93 ev := e.GetVolumeDiscountProgramEnded() 94 return rp.store.EndVolumeDiscountProgram(ctx, ev.GetVersion(), time.Unix(0, ev.EndedAt), rp.vegaTime, e.Sequence()) 95 } 96 97 func (rp *VolumeDiscountProgram) Name() string { 98 return "VolumeDiscountProgram" 99 }