code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/erc20_multisig_event.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  
    21  	"code.vegaprotocol.io/vega/core/events"
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  type ERC20MultiSigSignerAddedEvent interface {
    29  	events.Event
    30  	Proto() eventspb.ERC20MultiSigSignerAdded
    31  }
    32  
    33  type ERC20MultiSigSignerRemovedEvent interface {
    34  	events.Event
    35  	Proto() eventspb.ERC20MultiSigSignerRemoved
    36  }
    37  
    38  type ERC20MultiSigSignerEventStore interface {
    39  	Add(ctx context.Context, e *entities.ERC20MultiSigSignerEvent) error
    40  }
    41  
    42  type ERC20MultiSigSignerEvent struct {
    43  	subscriber
    44  	store ERC20MultiSigSignerEventStore
    45  }
    46  
    47  func NewERC20MultiSigSignerEvent(store ERC20MultiSigSignerEventStore) *ERC20MultiSigSignerEvent {
    48  	return &ERC20MultiSigSignerEvent{
    49  		store: store,
    50  	}
    51  }
    52  
    53  func (m *ERC20MultiSigSignerEvent) Types() []events.Type {
    54  	return []events.Type{
    55  		events.ERC20MultiSigSignerAddedEvent,
    56  		events.ERC20MultiSigSignerRemovedEvent,
    57  	}
    58  }
    59  
    60  func (m *ERC20MultiSigSignerEvent) Push(ctx context.Context, evt events.Event) error {
    61  	switch e := evt.(type) {
    62  	case ERC20MultiSigSignerAddedEvent:
    63  		return m.consumeAddedEvent(ctx, e)
    64  	case ERC20MultiSigSignerRemovedEvent:
    65  		return m.consumeRemovedEvent(ctx, e)
    66  	default:
    67  		return errors.Errorf("unknown event type %s", e.Type().String())
    68  	}
    69  }
    70  
    71  func (m *ERC20MultiSigSignerEvent) consumeAddedEvent(ctx context.Context, event ERC20MultiSigSignerAddedEvent) error {
    72  	e := event.Proto()
    73  	record, err := entities.ERC20MultiSigSignerEventFromAddedProto(&e, entities.TxHash(event.TxHash()))
    74  	if err != nil {
    75  		return errors.Wrap(err, "converting signer-added proto to database entity failed")
    76  	}
    77  	return m.store.Add(ctx, record)
    78  }
    79  
    80  func (m *ERC20MultiSigSignerEvent) consumeRemovedEvent(ctx context.Context, event ERC20MultiSigSignerRemovedEvent) error {
    81  	e := event.Proto()
    82  	records, err := entities.ERC20MultiSigSignerEventFromRemovedProto(&e, entities.TxHash(event.TxHash()))
    83  	if err != nil {
    84  		return errors.Wrap(err, "converting signer-added proto to database entity failed")
    85  	}
    86  	for _, r := range records {
    87  		m.store.Add(ctx, r)
    88  	}
    89  	return nil
    90  }
    91  
    92  func (m *ERC20MultiSigSignerEvent) Name() string {
    93  	return "ERC20MultiSigSignerEvent"
    94  }