code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/volume_rebate_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  	VolumeRebateProgramStartedEvent interface {
    29  		events.Event
    30  		GetVolumeRebateProgramStarted() *eventspb.VolumeRebateProgramStarted
    31  	}
    32  
    33  	VolumeRebateProgramUpdatedEvent interface {
    34  		events.Event
    35  		GetVolumeRebateProgramUpdated() *eventspb.VolumeRebateProgramUpdated
    36  	}
    37  
    38  	VolumeRebateProgramEndedEvent interface {
    39  		events.Event
    40  		GetVolumeRebateProgramEnded() *eventspb.VolumeRebateProgramEnded
    41  	}
    42  
    43  	VolumeRebateStore interface {
    44  		AddVolumeRebateProgram(ctx context.Context, referral *entities.VolumeRebateProgram) error
    45  		UpdateVolumeRebateProgram(ctx context.Context, referral *entities.VolumeRebateProgram) error
    46  		EndVolumeRebateProgram(ctx context.Context, version uint64, endedAt time.Time, vegaTime time.Time, seqNum uint64) error
    47  	}
    48  
    49  	VolumeRebateProgram struct {
    50  		subscriber
    51  		store VolumeRebateStore
    52  	}
    53  )
    54  
    55  func NewVolumeRebateProgram(store VolumeRebateStore) *VolumeRebateProgram {
    56  	return &VolumeRebateProgram{
    57  		store: store,
    58  	}
    59  }
    60  
    61  func (rp *VolumeRebateProgram) Types() []events.Type {
    62  	return []events.Type{
    63  		events.VolumeRebateProgramStartedEvent,
    64  		events.VolumeRebateProgramUpdatedEvent,
    65  		events.VolumeRebateProgramEndedEvent,
    66  	}
    67  }
    68  
    69  func (rp *VolumeRebateProgram) Push(ctx context.Context, evt events.Event) error {
    70  	switch e := evt.(type) {
    71  	case VolumeRebateProgramStartedEvent:
    72  		return rp.consumeVolumeRebateProgramStartedEvent(ctx, e)
    73  	case VolumeRebateProgramUpdatedEvent:
    74  		return rp.consumeVolumeRebateProgramUpdatedEvent(ctx, e)
    75  	case VolumeRebateProgramEndedEvent:
    76  		return rp.consumeVolumeRebateProgramEndedEvent(ctx, e)
    77  	default:
    78  		return nil
    79  	}
    80  }
    81  
    82  func (rp *VolumeRebateProgram) consumeVolumeRebateProgramStartedEvent(ctx context.Context, e VolumeRebateProgramStartedEvent) error {
    83  	program := entities.VolumeRebateProgramFromProto(e.GetVolumeRebateProgramStarted().GetProgram(), rp.vegaTime, e.Sequence())
    84  	return rp.store.AddVolumeRebateProgram(ctx, program)
    85  }
    86  
    87  func (rp *VolumeRebateProgram) consumeVolumeRebateProgramUpdatedEvent(ctx context.Context, e VolumeRebateProgramUpdatedEvent) error {
    88  	program := entities.VolumeRebateProgramFromProto(e.GetVolumeRebateProgramUpdated().GetProgram(), rp.vegaTime, e.Sequence())
    89  	return rp.store.UpdateVolumeRebateProgram(ctx, program)
    90  }
    91  
    92  func (rp *VolumeRebateProgram) consumeVolumeRebateProgramEndedEvent(ctx context.Context, e VolumeRebateProgramEndedEvent) error {
    93  	ev := e.GetVolumeRebateProgramEnded()
    94  	return rp.store.EndVolumeRebateProgram(ctx, ev.GetVersion(), time.Unix(0, ev.EndedAt), rp.vegaTime, e.Sequence())
    95  }
    96  
    97  func (rp *VolumeRebateProgram) Name() string {
    98  	return "VolumeRebateProgram"
    99  }