github.com/diadata-org/diadata@v1.4.593/pkg/dia/helpers/queryHelper/blockgenerator.go (about) 1 package queryhelper 2 3 import ( 4 "github.com/diadata-org/diadata/pkg/dia" 5 scrapers "github.com/diadata-org/diadata/pkg/dia/scraper/exchange-scrapers" 6 "github.com/diadata-org/diadata/pkg/utils" 7 ) 8 9 var ( 10 EXCHANGES = scrapers.Exchanges 11 ) 12 13 const ( 14 PAIR_SPLITTER = "-" 15 ) 16 17 type Block struct { 18 Trades []dia.Trade 19 TimeStamp int64 20 } 21 22 type Blockgenerator struct { 23 trades []dia.Trade 24 } 25 26 // GetBlockSources returns a unique list of all pairs and pools contained in the block's @b trades. 27 func (b *Block) GetBlockSources() (pairs []dia.ExchangePair, pools []dia.Pool) { 28 29 pairsCheckMap := make(map[string]struct{}) 30 poolsCheckMap := make(map[string]struct{}) 31 32 for _, t := range b.Trades { 33 if EXCHANGES[t.Source].Centralized { 34 err := t.NormalizeSymbols(true, PAIR_SPLITTER) 35 if err != nil { 36 log.Errorf("NormalizeSymbols on pair %s and exchange %s: %v", t.Pair, t.Source, err) 37 } 38 if _, ok := pairsCheckMap[t.Source+t.Pair]; !ok { 39 pairs = append(pairs, dia.ExchangePair{Exchange: t.Source, ForeignName: t.Pair}) 40 pairsCheckMap[t.Source+t.Pair] = struct{}{} 41 } 42 } else { 43 if _, ok := poolsCheckMap[t.Source+t.PoolAddress]; !ok { 44 pools = append(pools, dia.Pool{Exchange: EXCHANGES[t.Source], Address: t.PoolAddress}) 45 poolsCheckMap[t.Source+t.PoolAddress] = struct{}{} 46 } 47 } 48 } 49 return 50 } 51 52 func NewBlockGenerator(trades []dia.Trade) *Blockgenerator { 53 return &Blockgenerator{trades: trades} 54 } 55 56 func (bg *Blockgenerator) GenerateBlocks(blockSizeSeconds int64, blockShiftSeconds int64, bins []utils.TimeBin) (tradeBlocks []Block) { 57 58 if len(bg.trades) == 0 { 59 return 60 } 61 62 tradeIndex := 0 63 for _, bin := range bins { 64 65 tradeBlock := Block{TimeStamp: bin.Endtime.UnixNano()} 66 // Fill bin as long as trade's timestamp is smaller than or equal to the right border of the bin. 67 for tradeIndex < len(bg.trades) && (bg.trades[tradeIndex].Time.Before(bin.Endtime) || bg.trades[tradeIndex].Time == bin.Endtime) { 68 tradeBlock.Trades = append(tradeBlock.Trades, bg.trades[tradeIndex]) 69 tradeIndex++ 70 } 71 72 tradeBlocks = append(tradeBlocks, tradeBlock) 73 } 74 75 return 76 77 }