Skip to content

Commit

Permalink
addressing DeepSource issues and code refactoring and update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
koteld committed Jan 19, 2024
1 parent 091efc0 commit 1b9c8e2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 38 deletions.
5 changes: 4 additions & 1 deletion pkg/dia/helpers/horizonhelper/stellarAssetInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func GetStellarAssetInfo(client *horizonclient.Client, assetCode, assetIssuer, b
log = log.WithField("tomlURL", tomlURL)

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: 0,
},
}
tomlClient := &http.Client{
Transport: tr,
Expand Down
8 changes: 2 additions & 6 deletions pkg/dia/scraper/exchange-scrapers/APIScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,14 @@ func NewAPIScraper(exchange string, scrape bool, key string, secret string, relD
return NewVelodromeScraper(Exchanges[dia.VelodromeExchange], scrape, relDB)
case dia.MaverickExchange:
return NewMaverickScraper(Exchanges[dia.MaverickExchange], scrape, relDB)
// case dia.FinageForex:
// return NewFinageForexScraper(Exchanges[dia.FinageForex], scrape, relDB, key, secret)

//case dia.FinageForex:
// return NewFinageForexScraper(Exchanges[dia.FinageForex], scrape, relDB, key, secret)
case dia.MultiChain:
return NewBridgeSwapScraper(Exchanges[dia.MultiChain], scrape, relDB)

case "Influx":
return NewInfluxScraper(scrape)

case "UniswapHistory":
return NewUniswapHistoryScraper(Exchanges[dia.UniswapExchange], scrape, relDB)

case dia.StellarExchange:
return NewStellarScraper(Exchanges[dia.StellarExchange], scrape, relDB)
default:
Expand Down
16 changes: 7 additions & 9 deletions pkg/dia/scraper/exchange-scrapers/StellarScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -81,8 +80,8 @@ func NewStellarScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB)
shutdownDone = make(chan nothing)
pairScrapers = make(map[string]*StellarPairScraper)
chanTrades = make(chan *dia.Trade)
logger = logrus.WithFields(logrus.Fields{
"context": "StellarScraper",
logger = logrus.New().WithFields(logrus.Fields{
"context": "StellarExchangeScraper",
"cursor": cursor,
})
)
Expand Down Expand Up @@ -126,7 +125,7 @@ func (s *StellarScraper) mainLoop() {

func (s *StellarScraper) tradeHandler(stellarTrade hProtocol.Trade) {
log := s.logger.WithFields(logrus.Fields{
"context": "StellarScraper.tradeHandler",
"context": "StellarExchangeScraper.tradeHandler",
"tradeID": stellarTrade.ID,
})

Expand Down Expand Up @@ -175,15 +174,14 @@ func (s *StellarScraper) tradeHandler(stellarTrade hProtocol.Trade) {
QuoteToken: quoteToken,
}

s.logger.Infof("StellarScraper.tradeHandler.stellarTrade %# v", pretty.Formatter(stellarTrade))
s.logger.Infof("StellarScraper.tradeHandler.diaTrade %# v", pretty.Formatter(diaTrade))
log.Infof("stellarTrade %# v", pretty.Formatter(stellarTrade))
log.Infof("diaTrade %# v", pretty.Formatter(diaTrade))

s.chanTrades <- diaTrade
}

func (s *StellarScraper) getDIAAsset(assetType, assetCode, assetIssuer string) (asset dia.Asset, err error) {
s.logger.
WithError(err).
WithFields(logrus.Fields{
"assetType": assetType,
"assetCode": assetCode,
Expand Down Expand Up @@ -231,7 +229,7 @@ func (s *StellarScraper) ScrapePair(pair dia.ExchangePair) (PairScraper, error)
return nil, s.error
}
if s.closed {
return nil, errors.New("StellarScraper: Call ScrapePair on closed scraper")
return nil, errors.New("StellarExchangeScraper: Call ScrapePair on closed scraper")
}
ps := &StellarPairScraper{
parent: s,
Expand Down Expand Up @@ -270,7 +268,7 @@ func (s *StellarScraper) FetchAvailablePairs() ([]dia.ExchangePair, error) {
}
}(response.Body)

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)

if err != nil {
s.logger.WithError(err).Error("failed to read symbols")
Expand Down
10 changes: 7 additions & 3 deletions pkg/dia/scraper/liquidity-scrapers/StellarScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func NewStellarScraper(exchange dia.Exchange, relDB *models.RelDB, datastore *mo
poolChannel = make(chan dia.Pool)
doneChannel = make(chan bool)
scraper *StellarScraper
logger = log.WithFields(logrus.Fields{
"context": "StellarScraper",
logger = logrus.New().WithFields(logrus.Fields{
"context": "StellarLiquidityScraper",
"cursor": cursor,
})
)
Expand Down Expand Up @@ -112,6 +112,11 @@ func (scraper *StellarScraper) fetchPools() {
}

func (scraper *StellarScraper) getDIAPool(stellarPool hProtocol.LiquidityPool) (dia.Pool, error) {
scraper.logger.
WithFields(logrus.Fields{
"poolId": stellarPool.ID,
}).Info("fetching pool info")

assetvolumes := make([]dia.AssetVolume, len(stellarPool.Reserves))
for i, stellarAsset := range stellarPool.Reserves {
asset, err := scraper.getDIAAsset(stellarAsset.Asset)
Expand Down Expand Up @@ -142,7 +147,6 @@ func (scraper *StellarScraper) getDIAPool(stellarPool hProtocol.LiquidityPool) (

func (scraper *StellarScraper) getDIAAsset(stellarAsset string) (asset dia.Asset, err error) {
scraper.logger.
WithError(err).
WithFields(logrus.Fields{
"asset": stellarAsset,
}).Info("fetching asset info")
Expand Down
22 changes: 5 additions & 17 deletions pkg/dia/service/assetservice/source/stellar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/diadata-org/diadata/pkg/dia"
"github.com/diadata-org/diadata/pkg/dia/helpers/horizonhelper"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"
"github.com/stellar/go/clients/horizonclient"
)
Expand All @@ -20,37 +19,26 @@ type StellarAssetSource struct {
assetChannel chan dia.Asset
doneChannel chan bool
blockchain string
relDB *models.RelDB
cursor *string
limit uint
}

func NewStellarAssetSource(exchange dia.Exchange) *StellarAssetSource {
var (
horizonClient *horizonclient.Client
assetChannel = make(chan dia.Asset)
doneChannel = make(chan bool)
)

cursor := utils.Getenv(strings.ToUpper(exchange.Name)+"_ASSETS_CURSOR", defaultAssetRequestCursor)
limit := utils.GetenvUint(strings.ToUpper(exchange.Name)+"_ASSETS_LIMIT", defaultAssetRequestLimit)

switch exchange.Name {
case dia.StellarExchange:
horizonClient = horizonclient.DefaultPublicNetClient
}
var (
assetChannel = make(chan dia.Asset)
doneChannel = make(chan bool)
)

relDB, err := models.NewRelDataStore()
if err != nil {
log.Fatal("make new relational datastore: ", err)
}
horizonClient := horizonclient.DefaultPublicNetClient

scraper := &StellarAssetSource{
horizonClient: horizonClient,
assetChannel: assetChannel,
doneChannel: doneChannel,
blockchain: exchange.BlockChain.Name,
relDB: relDB,
cursor: &cursor,
limit: limit,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

func Getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
if value == "" {
return fallback
}
return value
}

func GetenvUint(key string, fallback uint) uint {
value := os.Getenv(key)
if len(value) == 0 {
if value == "" {
return fallback
}

Expand Down

0 comments on commit 1b9c8e2

Please sign in to comment.