Skip to content

Commit

Permalink
amend velodrome scraper for reading json files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jppade committed Sep 26, 2024
1 parent 844a5c3 commit 42e2288
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/exchange-scrapers/collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/diadata-org/diadata/exchange-scrapers/collector
go 1.19

require (
github.com/diadata-org/diadata v1.4.539
github.com/diadata-org/diadata v1.4.541
github.com/segmentio/kafka-go v0.4.35
github.com/sirupsen/logrus v1.9.3
)
Expand Down
16 changes: 16 additions & 0 deletions config/velodrome/fullPools/AerodromeV1FullPools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Tokens": [
{
"Address": "0x6cDcb1C4A4D1C3C6d054b27AC5B77e89eAFb971d",
"ForeignName": "USDC-AERO"
},
{
"Address": "0x7818B53F423457151407103321d8F961773815D1",
"ForeignName": "USDz-sUSDz"
},
{
"Address": "0x6d0b9C9E92a3De30081563c3657B5258b3fFa38B",
"ForeignName": "USDz-USDC"
}
]
}
8 changes: 8 additions & 0 deletions config/velodrome/reverse_tokens/AerodromeV1Quotetoken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Tokens": [
{
"Address": "0x4200000000000000000000000000000000000006",
"Symbol": "WETH"
}
]
}
28 changes: 28 additions & 0 deletions config/velodrome/subscribe_pools/AerodromeV1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"Pools": [
{
"Address": "0x2C4909355b0C036840819484c3A882A95659aBf3",
"ForeignName": "WETH-DEGEN"
},
{
"Address": "0x7f670f78B17dEC44d5Ef68a48740b6f8849cc2e6",
"ForeignName": "WETH-AERO"
},
{
"Address": "0x6cDcb1C4A4D1C3C6d054b27AC5B77e89eAFb971d",
"ForeignName": "USDC-AERO"
},
{
"Address": "0x2ce63497999F520CC2afaaadbCFC37Afd9deF4b0",
"ForeignName": "USDz-WETH"
},
{
"Address": "0x7818B53F423457151407103321d8F961773815D1",
"ForeignName": "USDz-sUSDz"
},
{
"Address": "0x6d0b9C9E92a3De30081563c3657B5258b3fFa38B",
"ForeignName": "USDz-USDC"
}
]
}
126 changes: 100 additions & 26 deletions pkg/dia/scraper/exchange-scrapers/VelodromeScraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ type VelodromeScraper struct {
relDB *models.RelDB
// error handling; to read error or closed, first acquire read lock
// only cleanup method should hold write lock
errorLock sync.RWMutex
error error
closed bool
pools []dia.Pool
errorLock sync.RWMutex
error error
closed bool
pools []dia.Pool
listenByAddress bool
reverseQuotetokens *[]string
reverseBasetokens *[]string
fullPools *[]string
// used to keep track of trading pairs that we subscribed to
pairScrapers map[string]*VelodromePairScraper
exchangeName string
Expand All @@ -54,7 +58,11 @@ type VelodromeScraper struct {

func NewVelodromeScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB) *VelodromeScraper {
log.Info("NewVelodromeScraper: ", exchange.Name)
var s *VelodromeScraper
var (
s *VelodromeScraper
listenByAddress bool
err error
)

switch exchange.Name {
case dia.VelodromeExchange:
Expand All @@ -77,6 +85,30 @@ func NewVelodromeScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB
log.Warnf("parse liquidity threshold: %v. Set to default %v", err, liquidityThresholdUSD)
}

listenByAddress, err = strconv.ParseBool(utils.Getenv("LISTEN_BY_ADDRESS", ""))
if err != nil {
log.Fatal("parse LISTEN_BY_ADDRESS: ", err)
}
s.listenByAddress = listenByAddress

s.reverseBasetokens, err = getReverseTokensFromConfig("velodrome/reverse_tokens/" + s.exchangeName + "Basetoken")
if err != nil {
log.Error("error getting basetokens for which pairs should be reversed: ", err)
}
log.Infof("reverse the following basetokens on %s: %v", s.exchangeName, s.reverseBasetokens)

s.reverseQuotetokens, err = getReverseTokensFromConfig("velodrome/reverse_tokens/" + s.exchangeName + "Quotetoken")
if err != nil {
log.Error("error getting quotetokens for which pairs should be reversed: ", err)
}
log.Infof("reverse the following quotetokens on %s: %v", s.exchangeName, s.reverseQuotetokens)

s.fullPools, err = getReverseTokensFromConfig("velodrome/fullPools/" + s.exchangeName + "FullPools")
if err != nil {
log.Error("error getting fullPools for which pairs should be reversed: ", err)
}
log.Infof("Take into account both directions of a trade on the following pools: %v", s.fullPools)

err = s.loadPools(liquidityThreshold, liquidityThresholdUSD)
if err != nil {
log.Fatal("load pools: ", err)
Expand Down Expand Up @@ -157,6 +189,30 @@ func (s *VelodromeScraper) WatchSwaps(pool dia.Pool) {
VerifiedPair: true,
}

switch {
case utils.Contains(s.reverseBasetokens, token1.Address):
// If we need quotation of a base token, reverse pair
tSwapped, err := dia.SwapTrade(*t)
if err == nil {
t = &tSwapped
}
case utils.Contains(s.reverseQuotetokens, token0.Address):
// If we need quotation of a base token, reverse pair
tSwapped, err := dia.SwapTrade(*t)
if err == nil {
t = &tSwapped
}
}

if utils.Contains(s.fullPools, pool.Address) {
tSwapped, err := dia.SwapTrade(*t)
if err == nil {
if tSwapped.Price > 0 {
s.chanTrades <- &tSwapped
}
}
}

if price > 0 {
log.Infof("Got trade at time %v - symbol: %s, pair: %s, price: %v, volume:%v", t.Time, t.Symbol, t.Pair, t.Price, t.Volume)
s.chanTrades <- t
Expand Down Expand Up @@ -296,33 +352,51 @@ func (ps *VelodromePairScraper) Pair() dia.ExchangePair {
func (s *VelodromeScraper) loadPools(liquiThreshold float64, liquidityThresholdUSD float64) (err error) {
var pools []dia.Pool

// Load all pools above liqui threshold.
pools, err = s.relDB.GetAllPoolsExchange(s.exchangeName, liquiThreshold)
if err != nil {
return
}
if s.listenByAddress {

log.Info("Found ", len(pools), " pools.")
log.Info("make pool map...")
lowerBoundCount := 0
for _, pool := range pools {
if len(pool.Assetvolumes) != 2 {
log.Warn("not enough assets in pool with address: ", pool.Address)
continue
// Only load pool info for addresses from json file.
poolAddresses, errAddr := getAddressesFromConfig("velodrome/subscribe_pools/" + s.exchangeName)
if errAddr != nil {
log.Error("fetch pool addresses from config file: ", errAddr)
}
for _, address := range poolAddresses {
pool, errPool := s.relDB.GetPoolByAddress(Exchanges[s.exchangeName].BlockChain.Name, address.Hex())
if errPool != nil {
log.Fatalf("Get pool with address %s: %v", address.Hex(), errPool)
}
s.pools = append(s.pools, pool)
}

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

// Load all pools above liqui threshold.
pools, err = s.relDB.GetAllPoolsExchange(s.exchangeName, liquiThreshold)
if err != nil {
return
}
if lowerBound {
lowerBoundCount++

log.Info("Found ", len(pools), " pools.")
log.Info("make pool map...")
lowerBoundCount := 0
for _, pool := range pools {
if len(pool.Assetvolumes) != 2 {
log.Warn("not enough assets in pool with address: ", pool.Address)
continue
}

liquidity, lowerBound := pool.GetPoolLiquidityUSD()
// Discard pool if complete USD liquidity is below threshold.
if !lowerBound && liquidity < liquidityThresholdUSD {
continue
}
if lowerBound {
lowerBoundCount++
}
s.pools = append(s.pools, pool)
}
s.pools = append(s.pools, pool)
log.Infof("found %v subscribable pools.", len(s.pools))
log.Infof("%v pools with lowerBound=true.", lowerBoundCount)
}

log.Infof("found %v subscribable pools.", len(s.pools))
log.Infof("%v pools with lowerBound=true.", lowerBoundCount)
return
}
3 changes: 3 additions & 0 deletions pkg/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func UniqueStrings(s []string) []string {

// Contains takes a slice of strings and a string and checks if it is contained in the slice.
func Contains(s *[]string, str string) bool {
if s == nil {
return false
}
for _, a := range *s {
if a == str {
return true
Expand Down

0 comments on commit 42e2288

Please sign in to comment.