code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/vesting_summary.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  	VestingBalancesSummaryEvent interface {
    29  		events.Event
    30  		VestingBalancesSummary() *eventspb.VestingBalancesSummary
    31  	}
    32  
    33  	VestingBalancesStore interface {
    34  		Add(ctx context.Context, balance entities.PartyVestingBalance) error
    35  	}
    36  
    37  	LockedBalancesStore interface {
    38  		Add(ctx context.Context, balance entities.PartyLockedBalance) error
    39  		Prune(ctx context.Context, currentEpoch uint64) error
    40  	}
    41  
    42  	VestingBalancesSummary struct {
    43  		subscriber
    44  		vestingStore VestingBalancesStore
    45  		lockedStore  LockedBalancesStore
    46  	}
    47  )
    48  
    49  func NewVestingBalancesSummary(
    50  	vestingStore VestingBalancesStore,
    51  	lockedStore LockedBalancesStore,
    52  ) *VestingBalancesSummary {
    53  	return &VestingBalancesSummary{
    54  		vestingStore: vestingStore,
    55  		lockedStore:  lockedStore,
    56  	}
    57  }
    58  
    59  func (v *VestingBalancesSummary) Types() []events.Type {
    60  	return []events.Type{
    61  		events.VestingBalancesSummaryEvent,
    62  	}
    63  }
    64  
    65  func (v *VestingBalancesSummary) Push(ctx context.Context, evt events.Event) error {
    66  	switch e := evt.(type) {
    67  	case VestingBalancesSummaryEvent:
    68  		return v.consumeVestingBalancesSummaryEvent(ctx, e, v.vegaTime)
    69  	default:
    70  		return nil
    71  	}
    72  }
    73  
    74  func (v *VestingBalancesSummary) consumeVestingBalancesSummaryEvent(ctx context.Context, e VestingBalancesSummaryEvent, t time.Time) error {
    75  	evt := e.VestingBalancesSummary()
    76  
    77  	for _, pvs := range evt.PartiesVestingSummary {
    78  		for _, ppvb := range pvs.PartyVestingBalances {
    79  			pvb, err := entities.PartyVestingBalanceFromProto(
    80  				pvs.Party, evt.EpochSeq, ppvb, t)
    81  			if err != nil {
    82  				return err
    83  			}
    84  
    85  			if err := v.vestingStore.Add(ctx, *pvb); err != nil {
    86  				return err
    87  			}
    88  		}
    89  
    90  		for _, pplb := range pvs.PartyLockedBalances {
    91  			plb, err := entities.PartyLockedBalanceFromProto(
    92  				pvs.Party, evt.EpochSeq, pplb, t)
    93  			if err != nil {
    94  				return err
    95  			}
    96  
    97  			if err := v.lockedStore.Add(ctx, *plb); err != nil {
    98  				return err
    99  			}
   100  		}
   101  	}
   102  
   103  	return v.lockedStore.Prune(ctx, evt.EpochSeq)
   104  }
   105  
   106  func (v *VestingBalancesSummary) Name() string {
   107  	return "VestingBalancesSummary"
   108  }