code.vegaprotocol.io/vega@v0.79.0/datanode/entities/funding_payment.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 entities
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  )
    27  
    28  type FundingPayment struct {
    29  	PartyID                 PartyID
    30  	MarketID                MarketID
    31  	FundingPeriodSeq        uint64
    32  	Amount                  num.Decimal
    33  	VegaTime                time.Time
    34  	TxHash                  TxHash
    35  	LossSocialisationAmount num.Decimal
    36  }
    37  
    38  func NewFundingPaymentsFromProto(
    39  	fps *eventspb.FundingPayments,
    40  	txHash TxHash,
    41  	vegaTime time.Time,
    42  ) ([]*FundingPayment, error) {
    43  	payments := make([]*FundingPayment, 0, len(fps.Payments))
    44  
    45  	marketID := MarketID(fps.MarketId)
    46  	for _, v := range fps.Payments {
    47  		amount, err := num.DecimalFromString(v.Amount)
    48  		if err != nil {
    49  			return nil, err
    50  		}
    51  
    52  		payments = append(payments,
    53  			&FundingPayment{
    54  				PartyID:          PartyID(v.PartyId),
    55  				MarketID:         marketID,
    56  				FundingPeriodSeq: fps.Seq,
    57  				Amount:           amount,
    58  				VegaTime:         vegaTime,
    59  				TxHash:           txHash,
    60  			},
    61  		)
    62  	}
    63  
    64  	return payments, nil
    65  }
    66  
    67  func (fp FundingPayment) ToProto() *v2.FundingPayment {
    68  	return &v2.FundingPayment{
    69  		PartyId:          fp.PartyID.String(),
    70  		MarketId:         fp.MarketID.String(),
    71  		FundingPeriodSeq: fp.FundingPeriodSeq,
    72  		Timestamp:        fp.VegaTime.UnixNano(),
    73  		Amount:           fp.Amount.String(),
    74  		LossAmount:       fp.LossSocialisationAmount.String(),
    75  	}
    76  }
    77  
    78  func (fp FundingPayment) Cursor() *Cursor {
    79  	pc := FundingPaymentCursor{
    80  		VegaTime: fp.VegaTime,
    81  		MarketID: fp.MarketID,
    82  		PartyID:  fp.PartyID,
    83  	}
    84  
    85  	return NewCursor(pc.String())
    86  }
    87  
    88  func (fp FundingPayment) ToProtoEdge(_ ...any) (*v2.FundingPaymentEdge, error) {
    89  	return &v2.FundingPaymentEdge{
    90  		Node:   fp.ToProto(),
    91  		Cursor: fp.Cursor().Encode(),
    92  	}, nil
    93  }
    94  
    95  type FundingPaymentCursor struct {
    96  	VegaTime time.Time `json:"vegaTime"`
    97  	MarketID MarketID  `json:"marketID"`
    98  	PartyID  PartyID   `json:"partyID"`
    99  }
   100  
   101  func (c FundingPaymentCursor) String() string {
   102  	bs, err := json.Marshal(c)
   103  	if err != nil {
   104  		panic(fmt.Errorf("could not marshal funding payment cursor: %w", err))
   105  	}
   106  	return string(bs)
   107  }
   108  
   109  func (c *FundingPaymentCursor) Parse(cursorString string) error {
   110  	if cursorString == "" {
   111  		return nil
   112  	}
   113  	return json.Unmarshal([]byte(cursorString), c)
   114  }