github.com/diadata-org/diadata@v1.4.593/pkg/dia/service/assetservice/source/velar.go (about) 1 package source 2 3 import ( 4 "context" 5 "strconv" 6 "strings" 7 8 "github.com/diadata-org/diadata/pkg/dia" 9 "github.com/diadata-org/diadata/pkg/dia/helpers/velarhelper" 10 models "github.com/diadata-org/diadata/pkg/model" 11 "github.com/diadata-org/diadata/pkg/utils" 12 "github.com/sirupsen/logrus" 13 ) 14 15 // VelarAssetSource asset collector object - which serves assetCollector command 16 type VelarAssetSource struct { 17 velarClient *velarhelper.VelarClient 18 assetChannel chan dia.Asset 19 doneChannel chan bool 20 blockchain string 21 relDB *models.RelDB 22 logger *logrus.Entry 23 } 24 25 // NewVelarAssetSource creates object to fetch velar assets 26 // ENV values: 27 // 28 // VELAR_DEBUG - (optional, bool), make stdout output with velar client http call, default = false 29 func NewVelarAssetSource(exchange dia.Exchange, relDB *models.RelDB) *VelarAssetSource { 30 envPrefix := strings.ToUpper(exchange.Name) 31 isDebug := utils.GetenvBool(envPrefix+"_DEBUG", false) 32 33 velarClient := velarhelper.NewVelarClient( 34 log.WithContext(context.Background()).WithField("context", "VelarClient"), 35 isDebug, 36 ) 37 38 logger := log. 39 WithContext(context.Background()). 40 WithField("service", "assetCollector"). 41 WithField("network", exchange.BlockChain.Name) 42 43 scraper := &VelarAssetSource{ 44 velarClient: velarClient, 45 assetChannel: make(chan dia.Asset), 46 doneChannel: make(chan bool), 47 blockchain: exchange.BlockChain.Name, 48 relDB: relDB, 49 logger: logger, 50 } 51 52 go scraper.fetchAssets() 53 54 return scraper 55 } 56 57 func (s *VelarAssetSource) fetchAssets() { 58 s.logger.Info("scraping assets...") 59 60 tokens, err := s.velarClient.GetAllTokens() 61 if err != nil { 62 s.logger.WithError(err).Error("error when scraping assets") 63 return 64 } 65 66 for _, token := range tokens { 67 decimals := len(strconv.Itoa(token.DecimalNum)) - 1 68 69 s.assetChannel <- dia.Asset{ 70 Address: token.ContractAddress, 71 Name: token.Name, 72 Symbol: token.Symbol, 73 Decimals: uint8(decimals), 74 Blockchain: s.blockchain, 75 } 76 } 77 78 s.doneChannel <- true 79 } 80 81 func (s *VelarAssetSource) Asset() chan dia.Asset { 82 return s.assetChannel 83 } 84 85 func (s *VelarAssetSource) Done() chan bool { 86 return s.doneChannel 87 }