github.com/diadata-org/diadata@v1.4.593/pkg/dia/service/assetservice/source/bitflow.go (about)

     1  package source
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/diadata-org/diadata/pkg/dia"
     8  	"github.com/diadata-org/diadata/pkg/dia/helpers/bitflowhelper"
     9  	models "github.com/diadata-org/diadata/pkg/model"
    10  	"github.com/diadata-org/diadata/pkg/utils"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // BitflowAssetSource asset collector object - which serves assetCollector command
    15  type BitflowAssetSource struct {
    16  	bitflowClient *bitflowhelper.BitflowClient
    17  	assetChannel  chan dia.Asset
    18  	doneChannel   chan bool
    19  	blockchain    string
    20  	relDB         *models.RelDB
    21  	logger        *logrus.Entry
    22  }
    23  
    24  // NewBitflowAssetSource creates object to fetch bitflow assets
    25  // ENV values:
    26  //
    27  //	BITFLOW_API_HOST - (required, string), bitflow private REST API host, required to fetch assets
    28  //	BITFLOW_API_KEY - (required, string), bitflow private API key, required to acess the REST API
    29  //	BIFROST_DEBUG - (optional, bool), make stdout output with bitflow client http call, default = false
    30  func NewBitflowAssetSource(exchange dia.Exchange, relDB *models.RelDB) *BitflowAssetSource {
    31  	envPrefix := strings.ToUpper(exchange.Name)
    32  
    33  	apiHost := utils.Getenv(envPrefix+"_API_HOST", "")
    34  	apiKey := utils.Getenv(envPrefix+"_API_KEY", "")
    35  	isDebug := utils.GetenvBool(envPrefix+"_DEBUG", false)
    36  
    37  	bitflowClient := bitflowhelper.NewBitflowClient(
    38  		apiHost,
    39  		apiKey,
    40  		log.WithContext(context.Background()).WithField("context", "BitflowClient"),
    41  		isDebug,
    42  	)
    43  
    44  	logger := log.
    45  		WithContext(context.Background()).
    46  		WithField("service", "assetCollector").
    47  		WithField("network", exchange.BlockChain.Name)
    48  
    49  	scraper := &BitflowAssetSource{
    50  		bitflowClient: bitflowClient,
    51  		assetChannel:  make(chan dia.Asset),
    52  		doneChannel:   make(chan bool),
    53  		blockchain:    exchange.BlockChain.Name,
    54  		relDB:         relDB,
    55  		logger:        logger,
    56  	}
    57  
    58  	go scraper.fetchAssets()
    59  
    60  	return scraper
    61  }
    62  
    63  func (s *BitflowAssetSource) fetchAssets() {
    64  	s.logger.Info("scraping assets...")
    65  
    66  	tokens, err := s.bitflowClient.GetAllTokens()
    67  	if err != nil {
    68  		s.logger.WithError(err).Error("error when scraping assets")
    69  		return
    70  	}
    71  
    72  	for _, token := range tokens {
    73  		if token.Symbol == "STX" && token.TokenContract == "null" {
    74  			token.TokenContract = "0x0000000000000000000000000000000000000000"
    75  		}
    76  		s.assetChannel <- dia.Asset{
    77  			Address:    token.TokenContract,
    78  			Name:       token.Name,
    79  			Symbol:     token.Symbol,
    80  			Decimals:   uint8(token.TokenDecimals),
    81  			Blockchain: s.blockchain,
    82  		}
    83  		for _, wrappedToken := range token.WrapTokens {
    84  			s.assetChannel <- dia.Asset{
    85  				Address:    wrappedToken.TokenContract,
    86  				Symbol:     "w" + token.Symbol,
    87  				Name:       wrappedToken.Name,
    88  				Decimals:   uint8(wrappedToken.TokenDecimals),
    89  				Blockchain: s.blockchain,
    90  			}
    91  		}
    92  	}
    93  
    94  	s.doneChannel <- true
    95  }
    96  
    97  func (s *BitflowAssetSource) Asset() chan dia.Asset {
    98  	return s.assetChannel
    99  }
   100  
   101  func (s *BitflowAssetSource) Done() chan bool {
   102  	return s.doneChannel
   103  }