From 50459d4a1f50bc9b6004c64ec9cf83c45caefdaa Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 29 Aug 2024 14:44:44 +0300 Subject: [PATCH] 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)