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

     1  package bitflowhelper
     2  
     3  import (
     4  	"crypto/tls"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"net/http/httputil"
    10  	"time"
    11  
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  const (
    16  	StableSwapDeployer = "SPQC38PW542EQJ5M11CR25P7BS1CA6QT4TBXGB3M"
    17  	XykDeployer        = "SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR"
    18  )
    19  
    20  var SwapContracts = [...]SwapContract{
    21  	{
    22  		DeployerAddress:  XykDeployer,
    23  		ContractRegistry: "stableswap-core-v-1-2",
    24  		ContractType:     1,
    25  	},
    26  	{
    27  		DeployerAddress:  StableSwapDeployer,
    28  		ContractRegistry: "stableswap-stx-ststx-v-1-2",
    29  		ContractType:     0,
    30  	},
    31  	{
    32  		DeployerAddress:  StableSwapDeployer,
    33  		ContractRegistry: "stableswap-usda-susdt-v-1-2",
    34  		ContractType:     0,
    35  	},
    36  	{
    37  		DeployerAddress:  StableSwapDeployer,
    38  		ContractRegistry: "stableswap-aeusdc-susdt-v-1-2",
    39  		ContractType:     0,
    40  	},
    41  	{
    42  		DeployerAddress:  StableSwapDeployer,
    43  		ContractRegistry: "stableswap-usda-aeusdc-v-1-2",
    44  		ContractType:     0,
    45  	},
    46  	{
    47  		DeployerAddress:  StableSwapDeployer,
    48  		ContractRegistry: "stableswap-usda-aeusdc-v-1-4",
    49  		ContractType:     0,
    50  	},
    51  	{
    52  		DeployerAddress:  StableSwapDeployer,
    53  		ContractRegistry: "stableswap-abtc-xbtc-v-1-2",
    54  		ContractType:     0,
    55  	},
    56  	{
    57  		DeployerAddress:  XykDeployer,
    58  		ContractRegistry: "xyk-core-v-1-2",
    59  		ContractType:     1,
    60  	},
    61  }
    62  
    63  type SwapContract struct {
    64  	DeployerAddress  string
    65  	ContractRegistry string
    66  	ContractType     int
    67  }
    68  
    69  type BitflowClient struct {
    70  	debug      bool
    71  	httpClient *http.Client
    72  	logger     *logrus.Entry
    73  	apiHost    string
    74  	apiKey     string
    75  }
    76  
    77  func NewBitflowClient(apiHost, apiKey string, logger *logrus.Entry, isDebug bool) *BitflowClient {
    78  	tr := &http.Transport{
    79  		TLSClientConfig: &tls.Config{
    80  			MinVersion: tls.VersionTLS12,
    81  			MaxVersion: 0,
    82  		},
    83  	}
    84  	httpClient := &http.Client{
    85  		Transport: tr,
    86  		Timeout:   10 * time.Second,
    87  	}
    88  
    89  	return &BitflowClient{
    90  		debug:      isDebug,
    91  		httpClient: httpClient,
    92  		logger:     logger,
    93  		apiHost:    apiHost,
    94  		apiKey:     apiKey,
    95  	}
    96  }
    97  
    98  func (c *BitflowClient) GetAllTokens() ([]TokenMetadata, error) {
    99  	url := fmt.Sprintf("%s/getAllTokensAndPools?key=%s", c.apiHost, c.apiKey)
   100  	resp, err := c.httpClient.Get(url)
   101  
   102  	if err != nil {
   103  		c.logger.WithError(err).Error("failed to fetch tokens")
   104  		return nil, err
   105  	}
   106  	defer resp.Body.Close()
   107  
   108  	if c.debug {
   109  		dump, err := httputil.DumpResponse(resp, true)
   110  		if err != nil {
   111  			c.logger.WithError(err).Error("failed to dump response")
   112  			return nil, err
   113  		}
   114  		c.logger.Debugf("\n%s\n", string(dump))
   115  	}
   116  
   117  	if resp.StatusCode != http.StatusOK {
   118  		err = fmt.Errorf("failed to fetch tokens, status code: %d", resp.StatusCode)
   119  		c.logger.Error(err.Error())
   120  		return nil, err
   121  	}
   122  
   123  	data, err := io.ReadAll(resp.Body)
   124  	if err != nil {
   125  		c.logger.WithError(err).Error("failed to read response body")
   126  		return nil, err
   127  	}
   128  
   129  	var result GetAllTokensResponse
   130  
   131  	err = json.Unmarshal(data, &result)
   132  	if err != nil {
   133  		c.logger.Error("failed to decode tokens response body")
   134  		return nil, err
   135  	}
   136  
   137  	return result.Tokens, nil
   138  }