code.vegaprotocol.io/vega@v0.79.0/datanode/entities/stake_linking.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  	"math"
    22  	"time"
    23  
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  
    27  	"github.com/shopspring/decimal"
    28  )
    29  
    30  type _StakeLinking struct{}
    31  
    32  type StakeLinkingID = ID[_StakeLinking]
    33  
    34  type StakeLinking struct {
    35  	ID                 StakeLinkingID
    36  	StakeLinkingType   StakeLinkingType
    37  	EthereumTimestamp  time.Time
    38  	PartyID            PartyID
    39  	Amount             decimal.Decimal
    40  	StakeLinkingStatus StakeLinkingStatus
    41  	FinalizedAt        time.Time
    42  	ForeignTxHash      string
    43  	ForeignBlockHeight int64
    44  	ForeignBlockTime   int64
    45  	LogIndex           int64
    46  	EthereumAddress    string
    47  	TxHash             TxHash
    48  	VegaTime           time.Time
    49  }
    50  
    51  func StakeLinkingFromProto(stake *eventspb.StakeLinking, txHash TxHash, vegaTime time.Time) (*StakeLinking, error) {
    52  	id := StakeLinkingID(stake.Id)
    53  	partyID := PartyID(stake.Party)
    54  	amount, err := decimal.NewFromString(stake.Amount)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("received invalid staking amount: %s - %w", stake.Amount, err)
    57  	}
    58  	if stake.BlockHeight > math.MaxInt64 {
    59  		return nil, fmt.Errorf("block height is too high: %d", stake.BlockHeight)
    60  	}
    61  	if stake.LogIndex > math.MaxInt64 {
    62  		return nil, fmt.Errorf("log index is too hight: %d", stake.LogIndex)
    63  	}
    64  
    65  	logIndex := int64(stake.LogIndex)
    66  
    67  	return &StakeLinking{
    68  		ID:                 id,
    69  		StakeLinkingType:   StakeLinkingType(stake.Type),
    70  		EthereumTimestamp:  time.Unix(stake.Ts, 0),
    71  		PartyID:            partyID,
    72  		Amount:             amount,
    73  		StakeLinkingStatus: StakeLinkingStatus(stake.Status),
    74  		FinalizedAt:        time.Unix(0, stake.FinalizedAt),
    75  		ForeignTxHash:      stake.TxHash,
    76  		ForeignBlockHeight: int64(stake.BlockHeight),
    77  		ForeignBlockTime:   stake.BlockTime,
    78  		LogIndex:           logIndex,
    79  		EthereumAddress:    stake.EthereumAddress,
    80  		TxHash:             txHash,
    81  		VegaTime:           vegaTime,
    82  	}, nil
    83  }
    84  
    85  func (s *StakeLinking) ToProto() *eventspb.StakeLinking {
    86  	return &eventspb.StakeLinking{
    87  		Id:              s.ID.String(),
    88  		Type:            eventspb.StakeLinking_Type(s.StakeLinkingType),
    89  		Ts:              s.EthereumTimestamp.Unix(),
    90  		Party:           s.PartyID.String(),
    91  		Amount:          s.Amount.String(),
    92  		Status:          eventspb.StakeLinking_Status(s.StakeLinkingStatus),
    93  		FinalizedAt:     s.FinalizedAt.UnixNano(),
    94  		TxHash:          s.ForeignTxHash,
    95  		BlockHeight:     uint64(s.ForeignBlockHeight),
    96  		BlockTime:       s.ForeignBlockTime,
    97  		LogIndex:        uint64(s.LogIndex),
    98  		EthereumAddress: s.EthereumAddress,
    99  	}
   100  }
   101  
   102  func (s StakeLinking) Cursor() *Cursor {
   103  	cursor := StakeLinkingCursor{
   104  		VegaTime: s.VegaTime,
   105  		ID:       s.ID,
   106  	}
   107  	return NewCursor(cursor.String())
   108  }
   109  
   110  func (s StakeLinking) ToProtoEdge(_ ...any) (*v2.StakeLinkingEdge, error) {
   111  	return &v2.StakeLinkingEdge{
   112  		Node:   s.ToProto(),
   113  		Cursor: s.Cursor().Encode(),
   114  	}, nil
   115  }
   116  
   117  type StakeLinkingCursor struct {
   118  	VegaTime time.Time      `json:"vegaTime"`
   119  	ID       StakeLinkingID `json:"id"`
   120  }
   121  
   122  func (s StakeLinkingCursor) String() string {
   123  	bs, err := json.Marshal(s)
   124  	if err != nil {
   125  		panic(fmt.Errorf("could not serialize StakeLinkingCursor: %w", err))
   126  	}
   127  	return string(bs)
   128  }
   129  
   130  func (s *StakeLinkingCursor) Parse(cursorString string) error {
   131  	if cursorString == "" {
   132  		return nil
   133  	}
   134  
   135  	return json.Unmarshal([]byte(cursorString), s)
   136  }