github.com/diadata-org/diadata@v1.4.593/pkg/dia/service/assetservice/source/bifrost.go (about) 1 package source 2 3 import ( 4 "context" 5 "strings" 6 "time" 7 8 "github.com/diadata-org/diadata/pkg/dia" 9 bifrosthelper "github.com/diadata-org/diadata/pkg/dia/helpers/bifrost-helper" 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 const ( 16 Blockchain = "Bifrost" 17 ) 18 19 // BifrostAssetSource asset collector object - which serves assetCollector command 20 type BifrostAssetSource struct { 21 // client - interaction with bifrost REST API services 22 bifrostClient *bifrosthelper.BifrostClient 23 // channel to store received asset info 24 assetChannel chan dia.Asset 25 // channel which informs about work is finished 26 doneChannel chan bool 27 // blockchain name 28 blockchain string 29 // DB connector to interact with databases 30 relDB *models.RelDB 31 // logs all events here 32 logger *logrus.Entry 33 // swap contracts count limitation in bifrost REST API 34 swapContractsLimit int 35 36 sleepTimeout time.Duration 37 exchangeName string 38 targetSwapContract string 39 } 40 41 // NewBifrostAssetSource creates object to get bifrost assets 42 // ENV values: 43 // 44 // BIFROST_ASSETS_SLEEP_TIMEOUT - (optional,millisecond), make timeout between API calls, default "bifrosthelper.DefaultSleepBetweenContractCalls" value 45 // BIFROST_SWAP_CONTRACTS_LIMIT - (optional, int), limit to get swap contact addresses, default "bifrosthelper.DefaultSwapContractsLimit" value 46 // BIFROST_TARGET_SWAP_CONTRACT - (optional, string), useful for debug, default = "" 47 // BIFROST_DEBUG - (optional, bool), make stdout output with bifrost client http call, default = false 48 func NewBifrostAssetSource(exchange dia.Exchange, relDB *models.RelDB) *BifrostAssetSource { 49 sleepBetweenContractCalls := utils.GetTimeDurationFromIntAsMilliseconds( 50 utils.GetenvInt(strings.ToUpper(exchange.Name)+"_SLEEP_TIMEOUT", bifrosthelper.DefaultSleepBetweenContractCalls), 51 ) 52 swapContractsLimit := utils.GetenvInt( 53 strings.ToUpper(exchange.Name)+"_SWAP_CONTRACTS_LIMIT", 54 bifrosthelper.DefaultSwapContractsLimit, 55 ) 56 targetSwapContract := utils.Getenv(strings.ToUpper(exchange.Name)+"_TARGET_SWAP_CONTRACT", "") 57 isDebug := utils.GetenvBool(strings.ToUpper(exchange.Name)+"_DEBUG", false) 58 59 var ( 60 assetChannel = make(chan dia.Asset) 61 doneChannel = make(chan bool) 62 ) 63 bifrostClient := bifrosthelper.NewBifrostClient( 64 log.WithContext(context.Background()).WithField("context", "BifrostClient"), 65 sleepBetweenContractCalls, 66 isDebug, 67 ) 68 69 logger := log. 70 WithContext(context.Background()). 71 WithField("service", "assetCollector"). 72 WithField("network", "Bifrost") 73 74 scraper := &BifrostAssetSource{ 75 bifrostClient: bifrostClient, 76 assetChannel: assetChannel, 77 doneChannel: doneChannel, 78 blockchain: Blockchain, 79 relDB: relDB, 80 logger: logger, 81 swapContractsLimit: swapContractsLimit, 82 exchangeName: "Bifrost", 83 sleepTimeout: sleepBetweenContractCalls, 84 targetSwapContract: targetSwapContract, 85 } 86 87 go scraper.fetchAssets() 88 89 return scraper 90 } 91 92 func (s *BifrostAssetSource) fetchAssets() { 93 s.logger.Info("Scraping assets...") 94 95 assets, err := s.bifrostClient.ScrapAssets() 96 if err != nil { 97 s.logger.Error("Error when scraping assets: ", err) 98 return 99 } 100 101 for _, asset := range assets { 102 s.assetChannel <- *asset 103 } 104 s.doneChannel <- true 105 } 106 107 func (s *BifrostAssetSource) Asset() chan dia.Asset { 108 return s.assetChannel 109 } 110 111 func (s *BifrostAssetSource) Done() chan bool { 112 return s.doneChannel 113 }