code.vegaprotocol.io/vega@v0.79.0/datanode/entities/withdrawal.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  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	"code.vegaprotocol.io/vega/protos/vega"
    25  
    26  	"github.com/shopspring/decimal"
    27  	"google.golang.org/protobuf/encoding/protojson"
    28  )
    29  
    30  type _Withdrawal struct{}
    31  
    32  type WithdrawalID = ID[_Withdrawal]
    33  
    34  type Withdrawal struct {
    35  	ID                 WithdrawalID
    36  	PartyID            PartyID
    37  	Amount             decimal.Decimal
    38  	Asset              AssetID
    39  	Status             WithdrawalStatus
    40  	Ref                string
    41  	ForeignTxHash      string
    42  	CreatedTimestamp   time.Time
    43  	WithdrawnTimestamp time.Time
    44  	Ext                WithdrawExt
    45  	TxHash             TxHash
    46  	VegaTime           time.Time
    47  }
    48  
    49  func WithdrawalFromProto(withdrawal *vega.Withdrawal, txHash TxHash, vegaTime time.Time) (*Withdrawal, error) {
    50  	var err error
    51  	var amount decimal.Decimal
    52  
    53  	if amount, err = decimal.NewFromString(withdrawal.Amount); err != nil {
    54  		return nil, fmt.Errorf("invalid amount: %w", err)
    55  	}
    56  
    57  	return &Withdrawal{
    58  		ID:                 WithdrawalID(withdrawal.Id),
    59  		PartyID:            PartyID(withdrawal.PartyId),
    60  		Amount:             amount,
    61  		Asset:              AssetID(withdrawal.Asset),
    62  		Status:             WithdrawalStatus(withdrawal.Status),
    63  		Ref:                withdrawal.Ref,
    64  		ForeignTxHash:      withdrawal.TxHash,
    65  		CreatedTimestamp:   NanosToPostgresTimestamp(withdrawal.CreatedTimestamp),
    66  		WithdrawnTimestamp: NanosToPostgresTimestamp(withdrawal.WithdrawnTimestamp),
    67  		Ext:                WithdrawExt{withdrawal.Ext},
    68  		TxHash:             txHash,
    69  		VegaTime:           vegaTime,
    70  	}, nil
    71  }
    72  
    73  func (w Withdrawal) ToProto() *vega.Withdrawal {
    74  	return &vega.Withdrawal{
    75  		Id:                 w.ID.String(),
    76  		PartyId:            w.PartyID.String(),
    77  		Amount:             w.Amount.String(),
    78  		Asset:              w.Asset.String(),
    79  		Status:             vega.Withdrawal_Status(w.Status),
    80  		Ref:                w.Ref,
    81  		TxHash:             w.ForeignTxHash,
    82  		CreatedTimestamp:   w.CreatedTimestamp.UnixNano(),
    83  		WithdrawnTimestamp: w.WithdrawnTimestamp.UnixNano(),
    84  		Ext:                w.Ext.WithdrawExt,
    85  	}
    86  }
    87  
    88  func (w Withdrawal) Cursor() *Cursor {
    89  	wc := WithdrawalCursor{
    90  		VegaTime: w.VegaTime,
    91  		ID:       w.ID,
    92  	}
    93  	return NewCursor(wc.String())
    94  }
    95  
    96  func (w Withdrawal) ToProtoEdge(_ ...any) (*v2.WithdrawalEdge, error) {
    97  	return &v2.WithdrawalEdge{
    98  		Node:   w.ToProto(),
    99  		Cursor: w.Cursor().Encode(),
   100  	}, nil
   101  }
   102  
   103  type WithdrawExt struct {
   104  	*vega.WithdrawExt
   105  }
   106  
   107  func (we WithdrawExt) MarshalJSON() ([]byte, error) {
   108  	return protojson.Marshal(we)
   109  }
   110  
   111  func (we *WithdrawExt) UnmarshalJSON(b []byte) error {
   112  	we.WithdrawExt = &vega.WithdrawExt{}
   113  	return protojson.Unmarshal(b, we)
   114  }
   115  
   116  type WithdrawalCursor struct {
   117  	VegaTime time.Time    `json:"vegaTime"`
   118  	ID       WithdrawalID `json:"id"`
   119  }
   120  
   121  func (wc WithdrawalCursor) String() string {
   122  	bs, err := json.Marshal(wc)
   123  	if err != nil {
   124  		// This should never happen
   125  		panic(fmt.Errorf("failed to marshal withdrawal cursor: %w", err))
   126  	}
   127  	return string(bs)
   128  }
   129  
   130  func (wc *WithdrawalCursor) Parse(cursorString string) error {
   131  	if cursorString == "" {
   132  		return nil
   133  	}
   134  	return json.Unmarshal([]byte(cursorString), wc)
   135  }