Skip to content

Commit

Permalink
add liquidity filter for pools in platypus dex scraper.
Browse files Browse the repository at this point in the history
  • Loading branch information
jppade committed Jul 5, 2023
1 parent 40e3da3 commit e917338
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/dia/scraper/exchange-scrapers/APIScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func NewAPIScraper(exchange string, scrape bool, key string, secret string, relD
case dia.PangolinExchange:
return NewUniswapScraper(Exchanges[dia.PangolinExchange], scrape, relDB)
case dia.PlatypusExchange:
return NewPlatypusScraper(Exchanges[dia.PlatypusExchange], scrape)
return NewPlatypusScraper(Exchanges[dia.PlatypusExchange], scrape, relDB)
case dia.SpookyswapExchange:
return NewUniswapScraper(Exchanges[dia.SpookyswapExchange], scrape, relDB)
case dia.QuickswapExchange:
Expand Down
50 changes: 47 additions & 3 deletions pkg/dia/scraper/exchange-scrapers/PlatypusScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -14,6 +15,7 @@ import (
platypusAssetABI "github.com/diadata-org/diadata/pkg/dia/scraper/exchange-scrapers/platypusfinance/asset"
platypusPoolABI "github.com/diadata-org/diadata/pkg/dia/scraper/exchange-scrapers/platypusfinance/pool"
platypusTokenABI "github.com/diadata-org/diadata/pkg/dia/scraper/exchange-scrapers/platypusfinance/token"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"

"github.com/diadata-org/diadata/pkg/dia"
Expand Down Expand Up @@ -91,14 +93,15 @@ type PlatypusScraper struct {

WsClient *ethclient.Client
RestClient *ethclient.Client
relDB *models.RelDB
platypusCoins map[string]*PlatypusCoin
pools *PlatypusPools
screenPools bool
basePoolRegistry platypusRegistry
}

// NewPlatypusScraper Returns a new exchange scraper
func NewPlatypusScraper(exchange dia.Exchange, scrape bool) *PlatypusScraper {
func NewPlatypusScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB) *PlatypusScraper {

registries := []platypusRegistry{
{Version: 3, Address: common.HexToAddress(platypusMasterRegV3Addr)},
Expand All @@ -116,6 +119,19 @@ func NewPlatypusScraper(exchange dia.Exchange, scrape bool) *PlatypusScraper {
log.Fatal("init ws client: ", err)
}

// Only include pools with (minimum) liquidity bigger than given env var.
liquidityThreshold, err := strconv.ParseFloat(utils.Getenv("LIQUIDITY_THRESHOLD", "0"), 64)
if err != nil {
liquidityThreshold = float64(0)
log.Warnf("parse liquidity threshold: %v. Set to default %v", err, liquidityThreshold)
}
// Only include pools with (minimum) liquidity USD value bigger than given env var.
liquidityThresholdUSD, err := strconv.ParseFloat(utils.Getenv("LIQUIDITY_THRESHOLD_USD", "0"), 64)
if err != nil {
liquidityThresholdUSD = float64(0)
log.Warnf("parse liquidity threshold: %v. Set to default %v", err, liquidityThresholdUSD)
}

scraper := &PlatypusScraper{
exchangeName: exchange.Name,
RestClient: restClient,
Expand All @@ -132,9 +148,10 @@ func NewPlatypusScraper(exchange dia.Exchange, scrape bool) *PlatypusScraper {
},
}

scraper.relDB = relDB
// Load metadata from master registries
for _, registry := range registries {
err := scraper.loadPoolsAndCoins(registry)
err := scraper.loadPoolsAndCoins(registry, liquidityThreshold, liquidityThresholdUSD)
if err != nil {
log.Errorf("loadPoolsAndCoins error w %s registry (v%d): %s", registry.Address.Hex(), registry.Version, err)
}
Expand Down Expand Up @@ -218,7 +235,7 @@ func (ps *PlatypusPairScraper) Pair() dia.ExchangePair {
}

// Load pools and coins metadata from master registry
func (s *PlatypusScraper) loadPoolsAndCoins(registry platypusRegistry) (err error) {
func (s *PlatypusScraper) loadPoolsAndCoins(registry platypusRegistry, liquidityThreshold float64, liquidityThresholdUSD float64) (err error) {
log.Infof("loading master contract %s version %d and querying registry", registry.Address.Hex(), registry.Version)
contractMaster, err := platypusfinance.NewBaseMasterPlatypusCaller(registry.Address, s.RestClient)
if err != nil {
Expand All @@ -232,19 +249,46 @@ func (s *PlatypusScraper) loadPoolsAndCoins(registry platypusRegistry) (err erro
return err
}

lowerBoundCount := 0
for i := 0; i < int(poolCount.Int64()); i++ {
asset, errPoolInfo := contractMaster.PoolInfo(&bind.CallOpts{}, big.NewInt(int64(i)))
if errPoolInfo != nil {
log.Error("PoolInfo: ", errPoolInfo)
return err
}
pool, errPool := s.relDB.GetPoolByAddress(dia.ETHEREUM, asset.LpToken.Hex())
if errPool != nil {
log.Errorf("Get pool %s by address: %v", asset.LpToken.Hex(), errPool)
}

lowLiqui := false
for _, av := range pool.Assetvolumes {
if av.Volume < liquidityThreshold {
log.Warnf("low liquidity on %s: %v", pool.Address, av.Volume)
lowLiqui = true
break
}
}
if lowLiqui {
continue
}

liquidity, lowerBound := pool.GetPoolLiquidityUSD()
// Discard pool if complete USD liquidity is below threshold.
if !lowerBound && liquidity < liquidityThresholdUSD {
continue
}
if lowerBound {
lowerBoundCount++
}

errPoolData := s.loadPoolData(asset.LpToken.Hex())
if errPoolData != nil {
log.Errorf("loadPoolData error at asset %s: %s", asset.LpToken.Hex(), errPoolData)
return errPoolData
}
}
log.Info("lowerBound: ", lowerBoundCount)

return err
}
Expand Down

0 comments on commit e917338

Please sign in to comment.