From 50459d4a1f50bc9b6004c64ec9cf83c45caefdaa Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 29 Aug 2024 14:44:44 +0300 Subject: [PATCH 1/4] feat: RetrieveCollateralWithdrawn & RetrieveCollateralDeposited --- mocks/service/mockService.go | 45 ++++++++++++++++++++++++++ services/accounts.go | 7 ++-- services/collateral.go | 62 ++++++++++++++++++++++++++---------- services/service.go | 8 +++++ 4 files changed, 102 insertions(+), 20 deletions(-) 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/services/accounts.go b/services/accounts.go index 7eaf673..531e3df 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" @@ -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: %s", err) 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: %s", err) return nil, errors.GetFilterErr(iterator.Error(), "core") } diff --git a/services/collateral.go b/services/collateral.go index 5eeb323..9490d5c 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.getIterationsForLimitQueryCore(limit) 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, + limit, 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) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveCollateralWithdrawn(opts) if err != nil { @@ -55,12 +70,12 @@ 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 } } @@ -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.getIterationsForLimitQueryCore(limit) 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, + limit, 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) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveCollateralDeposited(opts) if err != nil { @@ -136,12 +164,12 @@ 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 } } diff --git a/services/service.go b/services/service.go index 16a3576..dfb8e3e 100644 --- a/services/service.go +++ b/services/service.go @@ -83,10 +83,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) From f6bd39177a71c2f079e07b93bda50fbff36fd4b6 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 29 Aug 2024 18:34:14 +0300 Subject: [PATCH 2/4] feat: added RetrieveUSDBurned and RetrieveUSDMinted --- services/pools.go | 56 ++++++++++++++++++++++++++++++++------------- services/service.go | 8 +++++++ 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/services/pools.go b/services/pools.go index 0970cce..39a70a8 100644 --- a/services/pools.go +++ b/services/pools.go @@ -18,7 +18,10 @@ 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.getIterationsForLimitQueryCore(limit) if err != nil { return nil, err } @@ -27,16 +30,25 @@ func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err logger.Log().WithField("layer", "Service-RetrieveUSDMintedLimit").Infof( "fetching USDMinted with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + limit, 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) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveUSDMinted(opts) if err != nil { @@ -45,12 +57,12 @@ 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 } } @@ -60,7 +72,11 @@ func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err } 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.getIterationsForLimitQueryCore(limit) if err != nil { return nil, err } @@ -69,16 +85,24 @@ func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, err logger.Log().WithField("layer", "Service-RetrieveUSDBurnedLimit").Infof( "fetching USDMBurned with limit: %v to block: %v total iterations: %v...", - limit, last, iterations, + limit, 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) } - opts := s.getFilterOptsCore(fromBlock, &toBlock) + opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration) res, err := s.retrieveUSDBurned(opts) if err != nil { @@ -87,12 +111,12 @@ 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 } } diff --git a/services/service.go b/services/service.go index dfb8e3e..f4eda98 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) From c8422063597c546f1cd07c58a99d48102814043d Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Fri, 30 Aug 2024 09:36:58 +0300 Subject: [PATCH 3/4] fix: review --- services/accounts.go | 2 +- services/collateral.go | 30 +++++++++++++++--------------- services/pools.go | 24 ++++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/services/accounts.go b/services/accounts.go index 531e3df..2663ff9 100644 --- a/services/accounts.go +++ b/services/accounts.go @@ -499,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: %s", err) + logger.Log().WithField("layer", "Service-formatAccountsCore").Errorf("iterator error: %v", iterator.Error()) return nil, errors.GetFilterErr(iterator.Error(), "core") } diff --git a/services/collateral.go b/services/collateral.go index 9490d5c..9ee884a 100644 --- a/services/collateral.go +++ b/services/collateral.go @@ -33,7 +33,7 @@ func (s *Service) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.Coll } func (s *Service) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralWithdrawn, error) { - iterations, lastBlock, err := s.getIterationsForLimitQueryCore(limit) + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } @@ -44,9 +44,9 @@ func (s *Service) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, var withdraws []*models.CollateralWithdrawn - logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawnLimit").Infof( - "fetching CollateralWithdrawn with limit: %v to block: %v total iterations: %v...", - limit, lastBlock, 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, ) startBlockOfIteration := fromBlock @@ -58,7 +58,7 @@ func (s *Service) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, 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(startBlockOfIteration, &endBlockOfIteration) @@ -79,7 +79,7 @@ func (s *Service) RetrieveCollateralWithdrawn(fromBlock uint64, toBlock uint64, } } - logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawnLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveCollateralWithdrawn").Infof("task completed successfully") return withdraws, nil } @@ -128,7 +128,7 @@ func (s *Service) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.Coll } func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CollateralDeposited, error) { - iterations, lastBlock, err := s.getIterationsForLimitQueryCore(limit) + iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit, ContractCore) if err != nil { return nil, err } @@ -139,9 +139,9 @@ func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, var deposits []*models.CollateralDeposited - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Infof( - "fetching CollateralDeposited with limit: %v to block: %v total iterations: %v...", - limit, lastBlock, 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, ) startBlockOfIteration := fromBlock @@ -153,7 +153,7 @@ func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, 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(startBlockOfIteration, &endBlockOfIteration) @@ -173,7 +173,7 @@ func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, } } - logger.Log().WithField("layer", "Service-RetrieveCollateralDepositedLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveCollateralDeposited").Infof("task completed successfully") return deposits, nil } @@ -181,7 +181,7 @@ func (s *Service) RetrieveCollateralDeposited(fromBlock uint64, toBlock uint64, 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") } @@ -189,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") } @@ -208,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 39a70a8..1b7efdd 100644 --- a/services/pools.go +++ b/services/pools.go @@ -21,16 +21,16 @@ func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err return s.RetrieveUSDMinted(0, 0, limit) } func (s *Service) RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDMinted, error) { - iterations, lastBlock, err := s.getIterationsForLimitQueryCore(limit) + 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, lastBlock, 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, ) if fromBlock == 0 { @@ -46,7 +46,7 @@ func (s *Service) RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint 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(startBlockOfIteration, &endBlockOfIteration) @@ -66,7 +66,7 @@ func (s *Service) RetrieveUSDMinted(fromBlock uint64, toBlock uint64, limit uint } } - logger.Log().WithField("layer", "Service-RetrieveLiquidationsLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveUSDMinted").Infof("task completed successfully") return mints, nil } @@ -76,16 +76,16 @@ func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, err } func (s *Service) RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.USDBurned, error) { - iterations, lastBlock, err := s.getIterationsForLimitQueryCore(limit) + 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, lastBlock, 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, ) if fromBlock == 0 { @@ -100,7 +100,7 @@ func (s *Service) RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint } 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(startBlockOfIteration, &endBlockOfIteration) @@ -120,7 +120,7 @@ func (s *Service) RetrieveUSDBurned(fromBlock uint64, toBlock uint64, limit uint } } - logger.Log().WithField("layer", "Service-RetrieveLiquidationsLimit").Infof("task completed successfully") + logger.Log().WithField("layer", "Service-RetrieveUSDBurned").Infof("task completed successfully") return burns, nil } From e4d3f3f138a3b457ae572faa57f0c673b8c109d1 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Fri, 30 Aug 2024 10:08:49 +0300 Subject: [PATCH 4/4] feat: interaface level and FormatAccountsCore --- perpsv3.go | 40 ++++++++++++++++++++++++++++++++++++++++ services/accounts.go | 41 +++++++++++++++++++++++++++-------------- services/service.go | 4 ++++ 3 files changed, 71 insertions(+), 14 deletions(-) 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 2663ff9..c5fd23f 100644 --- a/services/accounts.go +++ b/services/accounts.go @@ -93,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") } @@ -473,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: %s", err) + logger.Log().WithField("layer", "Service-formatAccounts").Errorf("iterator error: %v", iterator.Error()) return nil, errors.GetFilterErr(iterator.Error(), "perps market") } @@ -538,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 { @@ -566,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/service.go b/services/service.go index f4eda98..972c0b1 100644 --- a/services/service.go +++ b/services/service.go @@ -238,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