diff --git a/mocks/service/mockService.go b/mocks/service/mockService.go index 9d1b556..490a40a 100644 --- a/mocks/service/mockService.go +++ b/mocks/service/mockService.go @@ -458,6 +458,21 @@ func (mr *MockIServiceMockRecorder) RetrieveAccountLiquidationsLimit(limit inter return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveAccountLiquidationsLimit", reflect.TypeOf((*MockIService)(nil).RetrieveAccountLiquidationsLimit), limit) } +// RetrieveCollateralDeposited mocks base method. +func (m *MockIService) RetrieveCollateralDeposited(fromBlock, toBlock, limit uint64) ([]*models.CollateralDeposited, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RetrieveCollateralDeposited", fromBlock, toBlock, limit) + ret0, _ := ret[0].([]*models.CollateralDeposited) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RetrieveCollateralDeposited indicates an expected call of RetrieveCollateralDeposited. +func (mr *MockIServiceMockRecorder) RetrieveCollateralDeposited(fromBlock, toBlock, limit interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveCollateralDeposited", reflect.TypeOf((*MockIService)(nil).RetrieveCollateralDeposited), fromBlock, toBlock, limit) +} + // RetrieveCollateralDepositedLimit mocks base method. func (m *MockIService) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) { m.ctrl.T.Helper() @@ -473,6 +488,21 @@ func (mr *MockIServiceMockRecorder) RetrieveCollateralDepositedLimit(limit inter return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveCollateralDepositedLimit", reflect.TypeOf((*MockIService)(nil).RetrieveCollateralDepositedLimit), limit) } +// RetrieveCollateralWithdrawn mocks base method. +func (m *MockIService) RetrieveCollateralWithdrawn(fromBlock, toBlock, limit uint64) ([]*models.CollateralWithdrawn, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RetrieveCollateralWithdrawn", fromBlock, toBlock, limit) + ret0, _ := ret[0].([]*models.CollateralWithdrawn) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RetrieveCollateralWithdrawn indicates an expected call of RetrieveCollateralWithdrawn. +func (mr *MockIServiceMockRecorder) RetrieveCollateralWithdrawn(fromBlock, toBlock, limit interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveCollateralWithdrawn", reflect.TypeOf((*MockIService)(nil).RetrieveCollateralWithdrawn), fromBlock, toBlock, limit) +} + // RetrieveCollateralWithdrawnLimit mocks base method. func (m *MockIService) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) { m.ctrl.T.Helper() @@ -488,6 +518,21 @@ func (mr *MockIServiceMockRecorder) RetrieveCollateralWithdrawnLimit(limit inter return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveCollateralWithdrawnLimit", reflect.TypeOf((*MockIService)(nil).RetrieveCollateralWithdrawnLimit), limit) } +// RetrieveDelegationUpdated mocks base method. +func (m *MockIService) RetrieveDelegationUpdated(fromBlock, toBlock, limit uint64) ([]*models.DelegationUpdated, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RetrieveDelegationUpdated", fromBlock, toBlock, limit) + ret0, _ := ret[0].([]*models.DelegationUpdated) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RetrieveDelegationUpdated indicates an expected call of RetrieveDelegationUpdated. +func (mr *MockIServiceMockRecorder) RetrieveDelegationUpdated(fromBlock, toBlock, limit interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RetrieveDelegationUpdated", reflect.TypeOf((*MockIService)(nil).RetrieveDelegationUpdated), fromBlock, toBlock, limit) +} + // RetrieveDelegationUpdatedLimit mocks base method. func (m *MockIService) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error) { m.ctrl.T.Helper() diff --git a/perpsv3.go b/perpsv3.go index 08d7338..c9498c5 100644 --- a/perpsv3.go +++ b/perpsv3.go @@ -78,10 +78,18 @@ type IPerpsv3 interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, error) + // RetrieveUSDMintedLimit is used to get all `usdMinted` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDMinted, error) + // RetrieveUSDBurnedLimit is used to get all `usdBurned` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, error) + // RetrieveUSDBurnedLimit is used to get all `usdBurned` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDBurned, error) + // RetrieveDelegationUpdatedLimit is used to get all `DelegationUpdated` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error) @@ -94,10 +102,18 @@ type IPerpsv3 interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) + // RetrieveCollateralWithdrawnLimit is used to get all `Withdrawn` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralWithdrawn, error) + // RetrieveCollateralDepositedLimit is used to get all `Deposited` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) + // RetrieveCollateralDepositedLimit is used to get all `Deposited` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralDeposited, error) + // RetrieveRewardClaimedLimit is used to get all `RewardClaimed` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveRewardClaimedLimit(limit uint64) ([]*models.RewardClaimed, error) @@ -328,6 +344,10 @@ type IPerpsv3 interface { // limit. For most public RPC providers the value for limit is 20 000 blocks FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) + // FormatAccountsCoreLimit is used to get all accounts and their additional data from the contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + FormatAccountsCore(fromBlock, toBlock, limit uint64) ([]*models.Account, error) + // Config is used to get current lib config Config() *config.PerpsvConfig @@ -420,10 +440,18 @@ func (p *Perpsv3) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err return p.service.RetrieveUSDMintedLimit(limit) } +func (p *Perpsv3) RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDMinted, error) { + return p.service.RetrieveUSDMinted(fromBlock, toBlock, limit) +} + func (p *Perpsv3) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, error) { return p.service.RetrieveUSDBurnedLimit(limit) } +func (p *Perpsv3) RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDBurned, error) { + return p.service.RetrieveUSDBurned(fromBlock, toBlock, limit) +} + func (p *Perpsv3) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error) { return p.service.RetrieveDelegationUpdatedLimit(limit) } @@ -436,10 +464,18 @@ func (p *Perpsv3) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.Coll return p.service.RetrieveCollateralWithdrawnLimit(limit) } +func (p *Perpsv3) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralWithdrawn, error) { + return p.service.RetrieveCollateralWithdrawn(fromBlock, toBlock, limit) +} + func (p *Perpsv3) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) { return p.service.RetrieveCollateralDepositedLimit(limit) } +func (p *Perpsv3) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralDeposited, error) { + return p.service.RetrieveCollateralDeposited(fromBlock, toBlock, limit) +} + func (p *Perpsv3) RetrieveRewardClaimedLimit(limit uint64) ([]*models.RewardClaimed, error) { return p.service.RetrieveRewardClaimedLimit(limit) } @@ -695,6 +731,10 @@ func (p *Perpsv3) FormatAccountCore(id *big.Int) (*models.Account, error) { return p.service.FormatAccountCore(id) } +func (p *Perpsv3) FormatAccountsCore(fromBlock, toBlock, limit uint64) ([]*models.Account, error) { + return p.service.FormatAccountsCore(fromBlock, toBlock, limit) +} + func (p *Perpsv3) Config() *config.PerpsvConfig { return p.config } diff --git a/services/accounts.go b/services/accounts.go index 7eaf673..c5fd23f 100644 --- a/services/accounts.go +++ b/services/accounts.go @@ -2,10 +2,11 @@ package services import ( "fmt" - "github.com/ethereum/go-ethereum/common" "math/big" "time" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/gateway-fm/perpsv3-Go/config" @@ -92,7 +93,7 @@ func (s *Service) RetrieveAccountLiquidationsLimit(limit uint64) ([]*models.Acco for iterator.Next() { if iterator.Error() != nil { - logger.Log().WithField("layer", "Service-QueryAccountLiquidatedLimit").Errorf("iterator error: %v", err.Error()) + logger.Log().WithField("layer", "Service-QueryAccountLiquidatedLimit").Errorf("iterator error: %v", iterator.Error()) return nil, errors.GetFilterErr(iterator.Error(), "perps market") } @@ -472,7 +473,7 @@ func (s *Service) formatAccounts(opts *bind.FilterOpts) ([]*models.Account, erro for iterator.Next() { if iterator.Error() != nil { - logger.Log().WithField("layer", "Service-formatAccounts").Errorf("iterator error: %v", err.Error()) + logger.Log().WithField("layer", "Service-formatAccounts").Errorf("iterator error: %v", iterator.Error()) return nil, errors.GetFilterErr(iterator.Error(), "perps market") } @@ -498,7 +499,7 @@ func (s *Service) formatAccountsCore(opts *bind.FilterOpts) ([]*models.Account, for iterator.Next() { if iterator.Error() != nil { - logger.Log().WithField("layer", "Service-formatAccountsCore").Errorf("iterator error: %v", err.Error()) + logger.Log().WithField("layer", "Service-formatAccountsCore").Errorf("iterator error: %v", iterator.Error()) return nil, errors.GetFilterErr(iterator.Error(), "core") } @@ -537,26 +538,39 @@ func (s *Service) formatAccount(id *big.Int) (*models.Account, error) { } func (s *Service) FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.FormatAccountsCore(0, 0, limit) +} + +func (s *Service) FormatAccountsCore(fromBlock, toBlock, limit uint64) ([]*models.Account, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } var accounts []*models.Account - logger.Log().WithField("layer", "Service-FormatAccountsCoreLimit").Infof( - "fetching accounts with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + + logger.Log().WithField("layer", "Service-FormatAccountsCore").Infof( + "fetching accounts 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-FormatAccountsCoreLimit").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-FormatAccountsCore").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.formatAccountsCore(opts) if err != nil { @@ -565,16 +579,16 @@ func (s *Service) FormatAccountsCoreLimit(limit uint64) ([]*models.Account, erro accounts = append(accounts, 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-FormatAccountsLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-FormatAccounts").Infof("task completed successfully") return accounts, nil } diff --git a/services/collateral.go b/services/collateral.go index 5eeb323..9ee884a 100644 --- a/services/collateral.go +++ b/services/collateral.go @@ -2,9 +2,10 @@ package services import ( "context" - "github.com/ethereum/go-ethereum/common" "math/big" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/gateway-fm/perpsv3-Go/contracts/core" @@ -28,25 +29,39 @@ func (s *Service) GetCollateralPrice(blockNumber *big.Int, collateralType common } func (s *Service) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.RetrieveCollateralWithdrawn(0, 0, limit) +} + +func (s *Service) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralWithdrawn, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + var withdraws []*models.CollateralWithdrawn - logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawnLimit").Infof( - "fetching CollateralWithdrawn with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawn").Infof( + "fetching CollateralWithdrawn 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-RetrieveCollateralWithdrawnLimit").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawn").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveCollateralWithdrawn(opts) if err != nil { @@ -55,16 +70,16 @@ func (s *Service) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.Coll withdraws = append(withdraws, 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-RetrieveCollateralWithdrawnLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawn").Infof("task completed successfully") return withdraws, nil } @@ -109,25 +124,38 @@ func (s *Service) getCollateralWithdrawn(event *core.CoreWithdrawn, blockN uint6 } func (s *Service) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.RetrieveCollateralDeposited(0, 0, limit) +} + +func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralDeposited, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + var deposits []*models.CollateralDeposited - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Infof( - "fetching CollateralDeposited with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Infof( + "fetching CollateralDeposited 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-RetrieveCollateralDepositedLimit").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveCollateralDeposited(opts) if err != nil { @@ -136,16 +164,16 @@ func (s *Service) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.Coll deposits = append(deposits, 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-RetrieveCollateralDepositedLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Infof("task completed successfully") return deposits, nil } @@ -153,7 +181,7 @@ func (s *Service) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.Coll func (s *Service) retrieveCollateralDeposited(opts *bind.FilterOpts) ([]*models.CollateralDeposited, error) { iterator, err := s.core.FilterDeposited(opts, nil, nil, nil) if err != nil { - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Errorf("error get iterator: %v", err.Error()) + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Errorf("error get iterator: %v", err.Error()) return nil, errors.GetFilterErr(err, "core") } @@ -161,7 +189,7 @@ func (s *Service) retrieveCollateralDeposited(opts *bind.FilterOpts) ([]*models. for iterator.Next() { if iterator.Error() != nil { - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Errorf("iterator error: %v", iterator.Error().Error()) + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Errorf("iterator error: %v", iterator.Error().Error()) return nil, errors.GetFilterErr(iterator.Error(), "core") } @@ -180,7 +208,7 @@ func (s *Service) retrieveCollateralDeposited(opts *bind.FilterOpts) ([]*models. func (s *Service) getCollateralDeposited(event *core.CoreDeposited, blockN uint64) (*models.CollateralDeposited, error) { block, err := s.rpcClient.HeaderByNumber(context.Background(), big.NewInt(int64(blockN))) if err != nil { - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Errorf( + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Errorf( "get block:%v by number error: %v", blockN, err.Error(), ) return nil, errors.GetRPCProviderErr(err, "HeaderByNumber") diff --git a/services/pools.go b/services/pools.go index 0970cce..1b7efdd 100644 --- a/services/pools.go +++ b/services/pools.go @@ -18,25 +18,37 @@ import ( ) func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.RetrieveUSDMinted(0, 0, limit) +} +func (s *Service) RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDMinted, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } var mints []*models.USDMinted - logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Infof( - "fetching USDMinted with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + logger.Log().WithField("layer", "Service-RetrieveUSDMinted").Infof( + "fetching USDMinted with limit: %v from block %v to block: %v total iterations: %v...", + limit, fromBlock, lastBlock, iterations, ) - fromBlock := s.coreFirstBlock - toBlock := fromBlock + limit + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + + 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-RetrieveUSDMintedLimit").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-RetrieveUSDMinted").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveUSDMinted(opts) if err != nil { @@ -45,40 +57,52 @@ func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err mints = append(mints, 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-RetrieveLiquidationsLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveUSDMinted").Infof("task completed successfully") return mints, nil } func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, error) { - iterations, last, err := s.getIterationsForLimitQueryCore(limit) + return s.RetrieveUSDBurned(0, 0, limit) +} + +func (s *Service) RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDBurned, error) { + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } var burns []*models.USDBurned - logger.Log().WithField("layer", "Service-RetrieveUSDBurnedLimit").Infof( - "fetching USDMBurned with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + logger.Log().WithField("layer", "Service-RetrieveUSDBurned").Infof( + "fetching USDMBurned with limit: %v from block %v to block: %v total iterations: %v...", + limit, fromBlock, lastBlock, iterations, ) - fromBlock := s.coreFirstBlock - toBlock := fromBlock + limit + if fromBlock == 0 { + fromBlock = s.coreFirstBlock + } + + 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-RetrieveUSDMBurnedLimit").Infof("-- iteration %v", i) + logger.Log().WithField("layer", "Service-RetrieveUSDMBurned").Infof("-- iteration %v", i) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveUSDBurned(opts) if err != nil { @@ -87,16 +111,16 @@ func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, err burns = append(burns, 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-RetrieveLiquidationsLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveUSDBurned").Infof("task completed successfully") return burns, nil } diff --git a/services/service.go b/services/service.go index 16a3576..972c0b1 100644 --- a/services/service.go +++ b/services/service.go @@ -67,10 +67,18 @@ type IService interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, error) + // RetrieveUSDMintedLimit is used to get all `usdMinted` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDMinted, error) + // RetrieveUSDBurnedLimit is used to get all `usdBurned` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, error) + // RetrieveUSDBurnedLimit is used to get all `usdBurned` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDBurned, error) + // RetrieveDelegationUpdatedLimit is used to get all `DelegationUpdated` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error) @@ -83,10 +91,18 @@ type IService interface { // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) + // RetrieveCollateralWithdrawn is used to get all `Withdrawn` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralWithdrawn, error) + // RetrieveCollateralDepositedLimit is used to get all `Deposited` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) + // RetrieveCollateralDepositedLimit is used to get all `Deposited` events from the Core contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralDeposited, error) + // RetrieveRewardClaimedLimit is used to get all `RewardClaimed` events from the Core contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks RetrieveRewardClaimedLimit(limit uint64) ([]*models.RewardClaimed, error) @@ -222,6 +238,10 @@ type IService interface { // FormatAccountsCoreLimit is used to get all accounts and their additional data from the contract with given block search // limit. For most public RPC providers the value for limit is 20 000 blocks FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) + + // FormatAccountsCoreLimit is used to get all accounts and their additional data from the contract with given start block, end block and block search + // limit. For most public RPC providers the value for limit is 20 000 blocks + FormatAccountsCore(fromBlock, toBlock, limit uint64) ([]*models.Account, error) } type ContractType int