From 5cc3bcb0ea89a74d826f10fe38947b7effc847a7 Mon Sep 17 00:00:00 2001 From: Dmitry Selivanov Date: Mon, 2 Sep 2024 14:22:01 +0400 Subject: [PATCH] feat: add RetrieveMarketRegisteredOpts --- perpsv3.go | 8 ++++++++ services/marketData.go | 36 ++++++++++++++++++++++++------------ services/service.go | 4 ++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/perpsv3.go b/perpsv3.go index acd7971..59db4f8 100644 --- a/perpsv3.go +++ b/perpsv3.go @@ -136,6 +136,10 @@ type IPerpsv3 interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegistered, error) + // RetrieveMarketRegisteredOpts is used to get all `MarketRegistered` events with given start block, end block and block search + // // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveMarketRegisteredOpts(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.MarketRegistered, error) + // RetrievePoolCreatedLimit is used to get all `PoolCreated` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrievePoolCreatedLimit(limit uint64) ([]*models.PoolCreated, error) @@ -506,6 +510,10 @@ func (p *Perpsv3) RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegist return p.service.RetrieveMarketRegistered(limit) } +func (p *Perpsv3) RetrieveMarketRegisteredOpts(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.MarketRegistered, error) { + return p.service.RetrieveMarketRegisteredOpts(fromBlock, toBlock, limit) +} + func (p *Perpsv3) RetrievePoolCreated(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.PoolCreated, error) { return p.service.RetrievePoolCreated(fromBlock, toBlock, limit) } diff --git a/services/marketData.go b/services/marketData.go index 01bf411..566cdc9 100644 --- a/services/marketData.go +++ b/services/marketData.go @@ -112,25 +112,37 @@ func (s *Service) RetrieveMarketUpdatesBig(fromBlock uint64, toBLock *uint64) ([ } func (s *Service) RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegistered, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.RetrieveMarketRegisteredOpts(0, 0, limit) +} + +func (s *Service) RetrieveMarketRegisteredOpts(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.MarketRegistered, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } var marketRegistrations []*models.MarketRegistered - logger.Log().WithField("layer", "Service-RetrieveMarketRegistered").Infof( - "fetching market registrations with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + + logger.Log().WithField("layer", "Service-RetrieveMarketRegisteredOpts").Infof( + "fetching market registrations with limit: %v from block: %v to block: %v total iterations: %v...", + limit, fromBlock, lastBlock, iterations, ) - fromBlock := s.coreFirstBlock - toBlock := fromBlock + limit + startBlockOfIteration := fromBlock + endBlockOfIteration := startBlockOfIteration + limit + if endBlockOfIteration > toBlock { + endBlockOfIteration = toBlock + } + for i := uint64(1); i <= iterations; i++ { if i%10 == 0 || i == iterations { - logger.Log().WithField("layer", "Service-RetrieveMarketRegistered").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-RetrieveMarketRegisteredOpts").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveMarketRegistrations(opts) if err != nil { @@ -139,16 +151,16 @@ func (s *Service) RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegist marketRegistrations = append(marketRegistrations, res...) - fromBlock = toBlock + 1 + startBlockOfIteration = endBlockOfIteration + 1 if i == iterations-1 { - toBlock = last + endBlockOfIteration = lastBlock } else { - toBlock = fromBlock + limit + endBlockOfIteration = startBlockOfIteration + limit } } - logger.Log().WithField("layer", "Service-RetrieveMarketRegistered").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveMarketRegisteredOpts").Infof("task completed successfully") return marketRegistrations, nil } diff --git a/services/service.go b/services/service.go index 972c0b1..35b10e4 100644 --- a/services/service.go +++ b/services/service.go @@ -131,6 +131,10 @@ type IService interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegistered, error) + // RetrieveMarketRegisteredOpts is used to get all `MarketRegistered` events with given start block, end block and block search + // // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveMarketRegisteredOpts(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.MarketRegistered, error) + // RetrievePoolCreated is used to get all `PoolCreated` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrievePoolCreatedLimit(limit uint64) ([]*models.PoolCreated, error)