code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/all_resolver.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 gql
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/libs/ptr"
    22  	"code.vegaprotocol.io/vega/logging"
    23  	apipb "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  )
    27  
    28  type allResolver struct {
    29  	log  *logging.Logger
    30  	clt2 TradingDataServiceClientV2
    31  }
    32  
    33  func (r *allResolver) getEpochByID(ctx context.Context, id uint64) (*vegapb.Epoch, error) {
    34  	req := &apipb.GetEpochRequest{
    35  		Id: &id,
    36  	}
    37  	resp, err := r.clt2.GetEpoch(ctx, req)
    38  	return resp.Epoch, err
    39  }
    40  
    41  func (r *allResolver) getOrderByID(ctx context.Context, id string, version *int) (*vegapb.Order, error) {
    42  	v, err := convertVersion(version)
    43  	if err != nil {
    44  		r.log.Error("tradingCore client", logging.Error(err))
    45  		return nil, err
    46  	}
    47  	orderReq := &apipb.GetOrderRequest{
    48  		OrderId: id,
    49  		Version: v,
    50  	}
    51  	order, err := r.clt2.GetOrder(ctx, orderReq)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return order.Order, nil
    57  }
    58  
    59  func (r *allResolver) getAssetByID(ctx context.Context, id string) (*vegapb.Asset, error) {
    60  	if len(id) <= 0 {
    61  		return nil, ErrMissingIDOrReference
    62  	}
    63  	req := &apipb.GetAssetRequest{
    64  		AssetId: id,
    65  	}
    66  	res, err := r.clt2.GetAsset(ctx, req)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return res.Asset, nil
    71  }
    72  
    73  func (r *allResolver) getNodeByID(ctx context.Context, id string) (*vegapb.Node, error) {
    74  	if len(id) <= 0 {
    75  		return nil, ErrMissingNodeID
    76  	}
    77  	resp, err := r.clt2.GetNode(
    78  		ctx, &apipb.GetNodeRequest{Id: id})
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return resp.Node, nil
    84  }
    85  
    86  func (r *allResolver) getMarketByID(ctx context.Context, id string) (*vegapb.Market, error) {
    87  	req := apipb.GetMarketRequest{MarketId: id}
    88  	res, err := r.clt2.GetMarket(ctx, &req)
    89  	if err != nil {
    90  		r.log.Error("tradingData client", logging.Error(err))
    91  		return nil, err
    92  	}
    93  	// no error / no market = we did not find it
    94  	if res.Market == nil {
    95  		return nil, nil
    96  	}
    97  	return res.Market, nil
    98  }
    99  
   100  func (r *allResolver) transfersConnection(ctx context.Context, partyID *string, direction *TransferDirection, pagination *apipb.Pagination, isReward *bool, fromEpoch *int, toEpoch *int,
   101  	status *eventspb.Transfer_Status, scope *apipb.ListTransfersRequest_Scope, gameID *string, fromAccountType, toAccountType *vegapb.AccountType,
   102  ) (*apipb.TransferConnection, error) {
   103  	// if direction is nil just default to ToOrFrom, except when isReward is not nil and true, and partyID is not nil, in which case the API requires the direction to be FROM
   104  	if direction == nil && (isReward != nil && *isReward && partyID != nil) {
   105  		d := TransferDirectionFrom
   106  		direction = &d
   107  	} else if direction == nil {
   108  		d := TransferDirectionToOrFrom
   109  		direction = &d
   110  	}
   111  
   112  	var transferDirection apipb.TransferDirection
   113  	switch *direction {
   114  	case TransferDirectionFrom:
   115  		transferDirection = apipb.TransferDirection_TRANSFER_DIRECTION_TRANSFER_FROM
   116  	case TransferDirectionTo:
   117  		transferDirection = apipb.TransferDirection_TRANSFER_DIRECTION_TRANSFER_TO
   118  	case TransferDirectionToOrFrom:
   119  		transferDirection = apipb.TransferDirection_TRANSFER_DIRECTION_TRANSFER_TO_OR_FROM
   120  	}
   121  
   122  	var fromEpochU, toEpochU *uint64
   123  	if fromEpoch != nil {
   124  		fromEpochU = ptr.From(uint64(*fromEpoch))
   125  	}
   126  	if toEpoch != nil {
   127  		toEpochU = ptr.From(uint64(*toEpoch))
   128  	}
   129  
   130  	res, err := r.clt2.ListTransfers(ctx, &apipb.ListTransfersRequest{
   131  		Pubkey:          partyID,
   132  		Direction:       transferDirection,
   133  		Pagination:      pagination,
   134  		IsReward:        isReward,
   135  		FromEpoch:       fromEpochU,
   136  		ToEpoch:         toEpochU,
   137  		Status:          status,
   138  		Scope:           scope,
   139  		GameId:          gameID,
   140  		FromAccountType: fromAccountType,
   141  		ToAccountType:   toAccountType,
   142  	})
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return res.Transfers, nil
   148  }