code.vegaprotocol.io/vega@v0.79.0/datanode/entities/account.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  	"fmt"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/libs/ptr"
    23  	"code.vegaprotocol.io/vega/protos/vega"
    24  )
    25  
    26  type (
    27  	_Account  struct{}
    28  	AccountID = ID[_Account]
    29  )
    30  
    31  type Account struct {
    32  	ID       AccountID
    33  	PartyID  PartyID
    34  	AssetID  AssetID
    35  	MarketID MarketID
    36  	Type     vega.AccountType
    37  	TxHash   TxHash
    38  	VegaTime time.Time
    39  }
    40  
    41  func (a Account) ToProto() *vega.Account {
    42  	return &vega.Account{
    43  		Id:       a.ID.String(),
    44  		Owner:    a.PartyID.String(),
    45  		Asset:    a.AssetID.String(),
    46  		MarketId: a.MarketID.String(),
    47  		Type:     a.Type,
    48  	}
    49  }
    50  
    51  func (a Account) ToAccountDetailsProto() *vega.AccountDetails {
    52  	return &vega.AccountDetails{
    53  		Owner:    ptr.From(a.PartyID.String()),
    54  		AssetId:  a.AssetID.String(),
    55  		MarketId: ptr.From(a.MarketID.String()),
    56  		Type:     a.Type,
    57  	}
    58  }
    59  
    60  func (a Account) String() string {
    61  	return fmt.Sprintf("{ID: %s}", a.ID)
    62  }
    63  
    64  func AccountFromProto(va *vega.Account, txHash TxHash) (Account, error) {
    65  	// In account proto messages, network party is '*' and no market is '!'
    66  	partyID := va.Owner
    67  	if partyID == "*" {
    68  		partyID = "network"
    69  	}
    70  
    71  	marketID := va.MarketId
    72  	if marketID == "!" {
    73  		marketID = ""
    74  	}
    75  
    76  	account := Account{
    77  		PartyID:  PartyID(partyID),
    78  		AssetID:  AssetID(va.Asset),
    79  		MarketID: MarketID(marketID),
    80  		Type:     va.Type,
    81  		TxHash:   txHash,
    82  	}
    83  	return account, nil
    84  }
    85  
    86  func AccountProtoFromDetails(ad *vega.AccountDetails, txHash TxHash) (Account, error) {
    87  	marketID, partyID := "", "network"
    88  	if ad.MarketId != nil {
    89  		marketID = *ad.MarketId
    90  	}
    91  	if ad.Owner != nil {
    92  		partyID = *ad.Owner
    93  	}
    94  	return Account{
    95  		TxHash:   txHash,
    96  		PartyID:  ID[_Party](partyID),
    97  		MarketID: ID[_Market](marketID),
    98  		Type:     ad.Type,
    99  		AssetID:  ID[_Asset](ad.AssetId),
   100  	}, nil
   101  }