code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/connection_handlers.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  	"fmt"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/vegatime"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  	"code.vegaprotocol.io/vega/protos/vega"
    26  )
    27  
    28  func handleCandleConnectionRequest(ctx context.Context, client TradingDataServiceClientV2, market *vega.Market, sinceRaw string, toRaw *string,
    29  	interval vega.Interval, pagination *v2.Pagination,
    30  ) (*v2.CandleDataConnection, error) {
    31  	since, err := vegatime.Parse(sinceRaw)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	to := time.Unix(0, 0)
    37  	if toRaw != nil {
    38  		to, err = vegatime.Parse(*toRaw)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  	}
    43  
    44  	var mkt string
    45  	if market != nil {
    46  		mkt = market.Id
    47  	}
    48  
    49  	candlesForMktReq := v2.ListCandleIntervalsRequest{MarketId: mkt}
    50  	candlesForMktResp, err := client.ListCandleIntervals(ctx, &candlesForMktReq)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("could not retrieve candles for market %s: %w", mkt, err)
    53  	}
    54  
    55  	requestInterval, err := toV2IntervalString(interval)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("could not retrieve candles for market %s: %w", mkt, err)
    58  	}
    59  
    60  	candleID := ""
    61  
    62  	for _, c4m := range candlesForMktResp.IntervalToCandleId {
    63  		if c4m.Interval == requestInterval {
    64  			candleID = c4m.CandleId
    65  			break
    66  		}
    67  	}
    68  
    69  	if candleID == "" {
    70  		return nil, fmt.Errorf("could not find candle for market %s and interval %s", mkt, interval)
    71  	}
    72  
    73  	newestFirst := false
    74  	if pagination == nil {
    75  		pagination = &v2.Pagination{}
    76  	}
    77  
    78  	pagination.NewestFirst = &newestFirst
    79  
    80  	req := v2.ListCandleDataRequest{
    81  		CandleId:      candleID,
    82  		FromTimestamp: since.UnixNano(),
    83  		ToTimestamp:   to.UnixNano(),
    84  		Pagination:    pagination,
    85  	}
    86  	resp, err := client.ListCandleData(ctx, &req)
    87  	if err != nil {
    88  		return nil, fmt.Errorf("could not retrieve candles for market %s: %w", mkt, err)
    89  	}
    90  
    91  	return resp.Candles, nil
    92  }
    93  
    94  func toV2IntervalString(interval vega.Interval) (string, error) {
    95  	switch interval {
    96  	case vega.Interval_INTERVAL_BLOCK:
    97  		return "block", nil
    98  	case vega.Interval_INTERVAL_I1M:
    99  		return "1 minute", nil
   100  	case vega.Interval_INTERVAL_I5M:
   101  		return "5 minutes", nil
   102  	case vega.Interval_INTERVAL_I15M:
   103  		return "15 minutes", nil
   104  	case vega.Interval_INTERVAL_I30M:
   105  		return "30 minutes", nil
   106  	case vega.Interval_INTERVAL_I1H:
   107  		return "1 hour", nil
   108  	case vega.Interval_INTERVAL_I4H:
   109  		return "4 hours", nil
   110  	case vega.Interval_INTERVAL_I6H:
   111  		return "6 hours", nil
   112  	case vega.Interval_INTERVAL_I8H:
   113  		return "8 hours", nil
   114  	case vega.Interval_INTERVAL_I12H:
   115  		return "12 hours", nil
   116  	case vega.Interval_INTERVAL_I1D:
   117  		return "1 day", nil
   118  	case vega.Interval_INTERVAL_I7D:
   119  		return "7 days", nil
   120  	default:
   121  		return "", fmt.Errorf("interval not support:%s", interval)
   122  	}
   123  }
   124  
   125  func handleWithdrawalsConnectionRequest(ctx context.Context, client TradingDataServiceClientV2, party *vega.Party,
   126  	dateRange *v2.DateRange, pagination *v2.Pagination,
   127  ) (*v2.WithdrawalsConnection, error) {
   128  	req := v2.ListWithdrawalsRequest{PartyId: party.Id, Pagination: pagination, DateRange: dateRange}
   129  	resp, err := client.ListWithdrawals(ctx, &req)
   130  	if err != nil {
   131  		return nil, fmt.Errorf("could not retrieve withdrawals for party %s: %w", party.Id, err)
   132  	}
   133  	return resp.Withdrawals, nil
   134  }
   135  
   136  func handleDepositsConnectionRequest(ctx context.Context, client TradingDataServiceClientV2, party *vega.Party,
   137  	dateRange *v2.DateRange, pagination *v2.Pagination,
   138  ) (*v2.DepositsConnection, error) {
   139  	req := v2.ListDepositsRequest{PartyId: party.Id, Pagination: pagination, DateRange: dateRange}
   140  	resp, err := client.ListDeposits(ctx, &req)
   141  	if err != nil {
   142  		return nil, fmt.Errorf("could not retrieve deposits for party %s: %w", party.Id, err)
   143  	}
   144  	return resp.Deposits, nil
   145  }
   146  
   147  func handleProposalsRequest(ctx context.Context, client TradingDataServiceClientV2, party *vega.Party, ref *string, inType *v2.ListGovernanceDataRequest_Type,
   148  	inState *vega.Proposal_State, pagination *v2.Pagination,
   149  ) (*v2.GovernanceDataConnection, error) {
   150  	var partyID *string
   151  
   152  	if party != nil {
   153  		partyID = &party.Id
   154  	}
   155  
   156  	req := v2.ListGovernanceDataRequest{
   157  		ProposerPartyId:   partyID,
   158  		ProposalReference: ref,
   159  		ProposalType:      inType,
   160  		ProposalState:     inState,
   161  		Pagination:        pagination,
   162  	}
   163  	resp, err := client.ListGovernanceData(ctx, &req)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  	return resp.Connection, nil
   168  }
   169  
   170  func handleDelegationConnectionRequest(ctx context.Context, client TradingDataServiceClientV2,
   171  	partyID, nodeID, epochID *string, pagination *v2.Pagination,
   172  ) (*v2.DelegationsConnection, error) {
   173  	req := v2.ListDelegationsRequest{
   174  		PartyId:    partyID,
   175  		NodeId:     nodeID,
   176  		EpochId:    epochID,
   177  		Pagination: pagination,
   178  	}
   179  
   180  	resp, err := client.ListDelegations(ctx, &req)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("could not retrieve requested delegations: %w", err)
   183  	}
   184  	return resp.Delegations, nil
   185  }