code.vegaprotocol.io/vega@v0.79.0/datanode/entities/aggregated_ledgerentries.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/core/types"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	"code.vegaprotocol.io/vega/protos/vega"
    26  
    27  	"github.com/shopspring/decimal"
    28  )
    29  
    30  // AggregatedLedgerEntry represents the the summed amount of ledger entries for a set of accounts within a given time range.
    31  // VegaTime and Quantity will always be set. The others will be nil unless when
    32  // querying grouping by one of the corresponding fields is requested.
    33  type AggregatedLedgerEntry struct {
    34  	VegaTime     time.Time
    35  	Quantity     decimal.Decimal
    36  	TransferType LedgerMovementType
    37  	AssetID      AssetID
    38  
    39  	FromAccountPartyID  PartyID
    40  	ToAccountPartyID    PartyID
    41  	FromAccountMarketID MarketID
    42  	ToAccountMarketID   MarketID
    43  	FromAccountType     types.AccountType
    44  	ToAccountType       types.AccountType
    45  	FromAccountBalance  decimal.Decimal
    46  	ToAccountBalance    decimal.Decimal
    47  	TransferID          TransferID
    48  }
    49  
    50  func (ledgerEntries *AggregatedLedgerEntry) ToProto() *v2.AggregatedLedgerEntry {
    51  	lep := &v2.AggregatedLedgerEntry{}
    52  
    53  	lep.Quantity = ledgerEntries.Quantity.String()
    54  	lep.Timestamp = ledgerEntries.VegaTime.UnixNano()
    55  
    56  	lep.TransferType = vega.TransferType(ledgerEntries.TransferType)
    57  
    58  	assetIDString := ledgerEntries.AssetID.String()
    59  	if assetIDString != "" {
    60  		lep.AssetId = &assetIDString
    61  	}
    62  
    63  	fromPartyIDString := ledgerEntries.FromAccountPartyID.String()
    64  	if fromPartyIDString != "" {
    65  		lep.FromAccountPartyId = &fromPartyIDString
    66  	}
    67  
    68  	toPartyIDString := ledgerEntries.ToAccountPartyID.String()
    69  	if toPartyIDString != "" {
    70  		lep.ToAccountPartyId = &toPartyIDString
    71  	}
    72  
    73  	fromMarketIDString := ledgerEntries.FromAccountMarketID.String()
    74  	if fromMarketIDString != "" {
    75  		lep.FromAccountMarketId = &fromMarketIDString
    76  	}
    77  
    78  	toMarketIDString := ledgerEntries.ToAccountMarketID.String()
    79  	if toMarketIDString != "" {
    80  		lep.ToAccountMarketId = &toMarketIDString
    81  	}
    82  
    83  	lep.FromAccountType = ledgerEntries.FromAccountType
    84  	lep.ToAccountType = ledgerEntries.ToAccountType
    85  	lep.FromAccountBalance = ledgerEntries.FromAccountBalance.String()
    86  	lep.ToAccountBalance = ledgerEntries.ToAccountBalance.String()
    87  	lep.TransferId = ledgerEntries.TransferID.String()
    88  
    89  	return lep
    90  }
    91  
    92  func (ledgerEntries AggregatedLedgerEntry) Cursor() *Cursor {
    93  	return NewCursor(AggregatedLedgerEntriesCursor{
    94  		VegaTime: ledgerEntries.VegaTime,
    95  	}.String())
    96  }
    97  
    98  func (ledgerEntries AggregatedLedgerEntry) ToProtoEdge(_ ...any) (*v2.AggregatedLedgerEntriesEdge, error) {
    99  	return &v2.AggregatedLedgerEntriesEdge{
   100  		Node:   ledgerEntries.ToProto(),
   101  		Cursor: ledgerEntries.Cursor().Encode(),
   102  	}, nil
   103  }
   104  
   105  type AggregatedLedgerEntriesCursor struct {
   106  	VegaTime time.Time `json:"vega_time"`
   107  }
   108  
   109  func (c AggregatedLedgerEntriesCursor) String() string {
   110  	bs, err := json.Marshal(c)
   111  	if err != nil {
   112  		panic(fmt.Errorf("could not marshal aggregate ledger entries cursor: %w", err))
   113  	}
   114  	return string(bs)
   115  }
   116  
   117  func (c *AggregatedLedgerEntriesCursor) Parse(cursorString string) error {
   118  	if cursorString == "" {
   119  		return nil
   120  	}
   121  	return json.Unmarshal([]byte(cursorString), c)
   122  }