code.vegaprotocol.io/vega@v0.79.0/core/events/funding_payments.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 events
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  )
    25  
    26  type FundingPayments struct {
    27  	*Base
    28  	p *eventspb.FundingPayments
    29  }
    30  
    31  func NewFundingPaymentsEvent(ctx context.Context, marketID string, seq uint64, transfers []Transfer) *FundingPayments {
    32  	payments := make([]*eventspb.FundingPayment, 0, len(transfers))
    33  	for _, t := range transfers {
    34  		transfer := t.Transfer()
    35  		pos := true
    36  		if transfer.Type == types.TransferTypePerpFundingLoss {
    37  			pos = false
    38  		}
    39  		amt := num.Numeric{}
    40  		amt.SetInt(num.IntFromUint(transfer.Amount.Amount, pos))
    41  		payments = append(payments, &eventspb.FundingPayment{
    42  			PartyId: t.Party(),
    43  			Amount:  amt.String(),
    44  		})
    45  	}
    46  	interval := &FundingPayments{
    47  		Base: newBase(ctx, FundingPaymentsEvent),
    48  		p: &eventspb.FundingPayments{
    49  			MarketId: marketID,
    50  			Seq:      seq,
    51  			Payments: payments,
    52  		},
    53  	}
    54  	return interval
    55  }
    56  
    57  func (p FundingPayments) MarketID() string {
    58  	return p.p.MarketId
    59  }
    60  
    61  func (p FundingPayments) IsParty(id string) bool {
    62  	for _, fp := range p.p.Payments {
    63  		if fp.PartyId == id {
    64  			return true
    65  		}
    66  	}
    67  	return false
    68  }
    69  
    70  func (p *FundingPayments) FundingPayments() *eventspb.FundingPayments {
    71  	return p.p
    72  }
    73  
    74  func (p FundingPayments) Proto() eventspb.FundingPayments {
    75  	return *p.p
    76  }
    77  
    78  func (p FundingPayments) StreamMessage() *eventspb.BusEvent {
    79  	busEvent := newBusEventFromBase(p.Base)
    80  	busEvent.Event = &eventspb.BusEvent_FundingPayments{
    81  		FundingPayments: p.p,
    82  	}
    83  	return busEvent
    84  }
    85  
    86  func FundingPaymentEventFromStream(ctx context.Context, be *eventspb.BusEvent) *FundingPayments {
    87  	return &FundingPayments{
    88  		Base: newBaseFromBusEvent(ctx, FundingPaymentsEvent, be),
    89  		p:    be.GetFundingPayments(),
    90  	}
    91  }