Skip to content

Commit

Permalink
feat: add RetrieveMarketRegisteredOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Selivanov committed Sep 2, 2024
1 parent a946290 commit 5cc3bcb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
8 changes: 8 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
36 changes: 24 additions & 12 deletions services/marketData.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5cc3bcb

Please sign in to comment.