github.com/diadata-org/diadata@v1.4.593/pkg/dia/helpers/gqlclient/client.go (about)

     1  package gqlclient
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/diadata-org/diadata/pkg/dia"
    12  	gql "github.com/machinebox/graphql"
    13  )
    14  
    15  func GetGraphqlAssetQuotationFromDia(blockchain, address string, blockDuration int, assetl dia.AssetList) (price float64, tradesCount float64, tradeTime time.Time, source []string, err error) {
    16  	currentTime := time.Now()
    17  	starttime := currentTime.Add(time.Duration(-blockDuration*2) * time.Second)
    18  	type Response struct {
    19  		GetFeed []struct {
    20  			Name        string    `json:"Name"`
    21  			Time        time.Time `json:"Time"`
    22  			Value       float64   `json:"Value"`
    23  			Pools       []string  `json:"Pools"`
    24  			Pairs       []string  `json:"Pairs"`
    25  			TradesCount float64   `json:"TradesCount"`
    26  		} `json:"GetFeed"`
    27  	}
    28  	client := gql.NewClient("https://api.diadata.org" + "/graphql/query")
    29  	query := `
    30      query  {
    31  		 GetFeed(
    32  		 	Filter: "vwap", 
    33  			BlockSizeSeconds: ` + strconv.Itoa(blockDuration) + `, 
    34  			BlockShiftSeconds: ` + strconv.Itoa(blockDuration) + `,
    35  			StartTime: ` + strconv.FormatInt(starttime.Unix(), 10) + `, 
    36  			EndTime: ` + strconv.FormatInt(currentTime.Unix(), 10) + `, 
    37  			FeedSelection: ` + generateFeedSelectionQuery(assetl) + `,
    38  		  ) {
    39  				Name
    40  				Time
    41  				Value
    42  				TradesCount
    43  				Pools
    44  				Pairs
    45  				TradesCount
    46  
    47  	  	}
    48  		}`
    49  
    50  	req := gql.NewRequest(query)
    51  
    52  	ctx := context.Background()
    53  	var r Response
    54  	if err = client.Run(ctx, req, &r); err != nil {
    55  		err = errors.New("no results")
    56  		return
    57  	}
    58  	if len(r.GetFeed) == 0 {
    59  		err = errors.New("no results")
    60  		return
    61  	}
    62  	source = append(source, r.GetFeed[len(r.GetFeed)-1].Pools...)
    63  	source = append(source, r.GetFeed[len(r.GetFeed)-1].Pairs...)
    64  
    65  	tradesCount = r.GetFeed[len(r.GetFeed)-1].TradesCount
    66  	price = r.GetFeed[len(r.GetFeed)-1].Value
    67  	tradeTime = r.GetFeed[len(r.GetFeed)-1].Time
    68  
    69  	return
    70  }
    71  
    72  func generateFeedSelectionQuery(assetList dia.AssetList) string {
    73  	var jsonObjects []string
    74  
    75  	splitted := strings.Split(assetList.AssetName, "-")
    76  
    77  	exchangepair := convertExchangePairs(assetList.Exchanges)
    78  	jsonObject := fmt.Sprintf(`{
    79  			Address: %q,
    80  			Blockchain: %q,
    81  			Exchangepairs: %s
    82  		}`, splitted[1], splitted[0], exchangepair)
    83  
    84  	jsonObjects = append(jsonObjects, jsonObject)
    85  
    86  	jsonArray := "[" + strings.Join(jsonObjects, ",") + "]"
    87  
    88  	return jsonArray
    89  }
    90  
    91  func convertExchangePairs(exchangePairs []dia.ExchangeList) string {
    92  	// Create a slice to hold JSON objects
    93  	var jsonObjects []string
    94  
    95  	// Iterate through the exchangePairs
    96  	for _, exchangePair := range exchangePairs {
    97  		jsonObject := fmt.Sprintf(`{Exchange: "%s"`, exchangePair.Name)
    98  
    99  		var quotedArr []string
   100  
   101  		for _, item := range exchangePair.Pairs {
   102  			quotedArr = append(quotedArr, fmt.Sprintf("\"%s\"", strings.Replace(strings.TrimSpace(item), "/", "-", -1)))
   103  		}
   104  
   105  		pairs := `[` + strings.Join(quotedArr, ",") + `]`
   106  
   107  		jsonObject += `,Pairs:` + pairs
   108  
   109  		jsonObject += "}"
   110  
   111  		jsonObjects = append(jsonObjects, jsonObject)
   112  	}
   113  
   114  	jsonArray := "[" + strings.Join(jsonObjects, ",") + "]"
   115  
   116  	return jsonArray
   117  }