code.vegaprotocol.io/vega@v0.79.0/datanode/entities/account_balance.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  
    25  	"github.com/shopspring/decimal"
    26  )
    27  
    28  type AccountBalance struct {
    29  	*Account
    30  	Balance  decimal.Decimal
    31  	TxHash   TxHash
    32  	VegaTime time.Time
    33  }
    34  
    35  func (ab AccountBalance) ToProto() *v2.AccountBalance {
    36  	return &v2.AccountBalance{
    37  		Owner:    ab.PartyID.String(),
    38  		Balance:  ab.Balance.String(),
    39  		Asset:    ab.AssetID.String(),
    40  		MarketId: ab.MarketID.String(),
    41  		Type:     ab.Account.Type,
    42  	}
    43  }
    44  
    45  func (ab AccountBalance) ToProtoWithParent(parentPartyID *string) *v2.AccountBalance {
    46  	proto := ab.ToProto()
    47  	proto.ParentPartyId = parentPartyID
    48  	return proto
    49  }
    50  
    51  func (ab AccountBalance) ToProtoEdge(args ...any) (*v2.AccountEdge, error) {
    52  	var parentPartyID *string
    53  	if len(args) > 0 {
    54  		perPartyDerivedKey, ok := args[0].(map[string]string)
    55  		if !ok {
    56  			return nil, fmt.Errorf("expected argument of type map[string]string, got %T", args[0])
    57  		}
    58  
    59  		if party, isDerivedKey := perPartyDerivedKey[ab.PartyID.String()]; isDerivedKey {
    60  			parentPartyID = &party
    61  		}
    62  	}
    63  
    64  	return &v2.AccountEdge{
    65  		Node:   ab.ToProtoWithParent(parentPartyID),
    66  		Cursor: ab.Cursor().Encode(),
    67  	}, nil
    68  }
    69  
    70  type AccountBalanceKey struct {
    71  	AccountID AccountID
    72  	VegaTime  time.Time
    73  }
    74  
    75  func (ab AccountBalance) Key() AccountBalanceKey {
    76  	return AccountBalanceKey{ab.Account.ID, ab.VegaTime}
    77  }
    78  
    79  func (ab AccountBalance) ToRow() []interface{} {
    80  	return []interface{}{ab.Account.ID, ab.TxHash, ab.VegaTime, ab.Balance}
    81  }
    82  
    83  func (ab AccountBalance) Cursor() *Cursor {
    84  	cursor := AccountCursor{
    85  		AccountID: ab.Account.ID,
    86  	}
    87  
    88  	return NewCursor(cursor.String())
    89  }
    90  
    91  func (ab AccountBalance) Equal(other AccountBalance) bool {
    92  	return ab.AssetID == other.AssetID &&
    93  		ab.PartyID == other.PartyID &&
    94  		ab.MarketID == other.MarketID &&
    95  		ab.Type == other.Type &&
    96  		ab.Balance.Equal(other.Balance)
    97  }
    98  
    99  type AccountCursor struct {
   100  	AccountID AccountID `json:"account_id"`
   101  }
   102  
   103  func (ac AccountCursor) String() string {
   104  	bs, err := json.Marshal(ac)
   105  	if err != nil {
   106  		// This should never happen.
   107  		panic(fmt.Errorf("could not marshal account cursor: %w", err))
   108  	}
   109  	return string(bs)
   110  }
   111  
   112  func (ac *AccountCursor) Parse(cursorString string) error {
   113  	if cursorString == "" {
   114  		return nil
   115  	}
   116  	return json.Unmarshal([]byte(cursorString), ac)
   117  }