Skip to content

Commit

Permalink
Merge pull request #71 from gateway-fm/feat/delegation-updated-start
Browse files Browse the repository at this point in the history
add RetrieveDelegationUpdated(fromBlock uint64, toBlock uint64, limit uint64)
  • Loading branch information
Pashteto authored Aug 28, 2024
2 parents d7b86a3 + b204a66 commit 1589e02
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 38 deletions.
8 changes: 8 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error)

// RetrieveDelegationUpdated is used to get all `DelegationUpdated` events with given start block, end block and block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveDelegationUpdated(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.DelegationUpdated, error)

// RetrieveCollateralWithdrawnLimit is used to get all `Withdrawn` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error)
Expand Down Expand Up @@ -420,6 +424,10 @@ func (p *Perpsv3) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.Delega
return p.service.RetrieveDelegationUpdatedLimit(limit)
}

func (p *Perpsv3) RetrieveDelegationUpdated(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.DelegationUpdated, error) {
return p.service.RetrieveDelegationUpdated(fromBlock, toBlock, limit)
}

func (p *Perpsv3) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) {
return p.service.RetrieveCollateralWithdrawnLimit(limit)
}
Expand Down
6 changes: 3 additions & 3 deletions services/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *Service) FormatAccount(id *big.Int) (*models.Account, error) {
}

func (s *Service) FormatAccountsLimit(limit uint64) ([]*models.Account, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *Service) FormatAccountsLimit(limit uint64) ([]*models.Account, error) {
}

func (s *Service) RetrieveAccountLiquidationsLimit(limit uint64) ([]*models.AccountLiquidated, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -537,7 +537,7 @@ func (s *Service) formatAccount(id *big.Int) (*models.Account, error) {
}

func (s *Service) FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *Service) GetCollateralPrice(blockNumber *big.Int, collateralType common
}

func (s *Service) RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *Service) getCollateralWithdrawn(event *core.CoreWithdrawn, blockN uint6
}

func (s *Service) RetrieveCollateralDepositedLimit(limit uint64) ([]*models.CollateralDeposited, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/liquidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *Service) RetrieveLiquidations(fromBlock uint64, toBLock *uint64) ([]*mo
}

func (s *Service) RetrieveLiquidationsLimit(limit uint64) ([]*models.Liquidation, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/liquidationsCore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (s *Service) RetrieveLiquidationsCore(limit uint64) ([]*models.CoreLiquidation, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *Service) getLiquidationCore(event *core.CoreLiquidation, blockN uint64)
}

func (s *Service) RetrieveVaultLiquidationsCore(limit uint64) ([]*models.CoreVaultLiquidation, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions services/marketData.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func (s *Service) RetrieveMarketUpdatesLimit(limit uint64) ([]*models.MarketUpdate, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *Service) RetrieveMarketUpdatesLimit(limit uint64) ([]*models.MarketUpda
}

func (s *Service) RetrieveMarketUpdatesBigLimit(limit uint64) ([]*models.MarketUpdateBig, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *Service) RetrieveMarketUpdatesBig(fromBlock uint64, toBLock *uint64) ([
}

func (s *Service) RetrieveMarketRegistered(limit uint64) ([]*models.MarketRegistered, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/marketUSD.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (s *Service) RetrieveMarketUSDDepositedLimit(limit uint64) ([]*models.MarketUSDDeposited, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *Service) getMarketUSDDeposited(event *core.CoreMarketUsdDeposited, bloc
}

func (s *Service) RetrieveMarketUSDWithdrawnLimit(limit uint64) ([]*models.MarketUSDWithdrawn, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *Service) RetrieveOrders(fromBlock uint64, toBLock *uint64) ([]*models.O
}

func (s *Service) RetrieveOrdersLimit(limit uint64) ([]*models.Order, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down
38 changes: 25 additions & 13 deletions services/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func (s *Service) RetrieveUSDMintedLimit(limit uint64) ([]*models.USDMinted, err
}

func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -100,25 +100,37 @@ func (s *Service) RetrieveUSDBurnedLimit(limit uint64) ([]*models.USDBurned, err
}

func (s *Service) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
return s.RetrieveDelegationUpdated(0, 0, limit)
}

func (s *Service) RetrieveDelegationUpdated(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.DelegationUpdated, error) {
iterations, lastBlock, err := s.getIterationsForQuery(fromBlock, toBlock, limit)
if err != nil {
return nil, err
}

var delegations []*models.DelegationUpdated

logger.Log().WithField("layer", "Service-RetrieveDelegationUpdatedLimit").Infof(
"fetching DelegationUpdated with limit: %v to block: %v total iterations: %v...",
limit, last, iterations,
if fromBlock == 0 {
fromBlock = s.coreFirstBlock
}

logger.Log().WithField("layer", "Service-RetrieveDelegationUpdated").Infof(
"fetching DelegationUpdated 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-RetrieveUSDMBurnedLimit").Infof("-- iteration %v", i)
}
opts := s.getFilterOptsCore(fromBlock, &toBlock)
opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration)

res, err := s.retrieveDelegationUpdated(opts)
if err != nil {
Expand All @@ -127,12 +139,12 @@ func (s *Service) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.Delega

delegations = append(delegations, res...)

fromBlock = toBlock + 1
startBlockOfIteration = endBlockOfIteration + 1

if i == iterations-1 {
toBlock = last
endBlockOfIteration = lastBlock
} else {
toBlock = fromBlock + limit
endBlockOfIteration = startBlockOfIteration + limit
}
}

Expand All @@ -142,7 +154,7 @@ func (s *Service) RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.Delega
}

func (s *Service) RetrievePoolCreated(limit uint64) ([]*models.PoolCreated, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (s *Service) RetrieveRewardClaimedLimit(limit uint64) ([]*models.RewardClaimed, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (s *Service) getRewardClaimed(event *core.CoreRewardsClaimed, blockN uint64
}

func (s *Service) RetrieveRewardDistributedLimit(limit uint64) ([]*models.RewardDistributed, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryCore(limit)
if err != nil {
return nil, err
}
Expand Down
57 changes: 49 additions & 8 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -74,6 +75,10 @@ type IService interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveDelegationUpdatedLimit(limit uint64) ([]*models.DelegationUpdated, error)

// RetrieveDelegationUpdated is used to get all `DelegationUpdated` events with given start block, end block and block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveDelegationUpdated(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.DelegationUpdated, error)

// RetrieveCollateralWithdrawnLimit is used to get all `Withdrawn` events from the Core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
RetrieveCollateralWithdrawnLimit(limit uint64) ([]*models.CollateralWithdrawn, error)
Expand Down Expand Up @@ -199,6 +204,14 @@ type IService interface {
FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error)
}

type ContractType int

const (
ContractPerpsMarket ContractType = iota

ContractCore
)

// Service is an implementation of IService interface
type Service struct {
chainID config.ChainID
Expand Down Expand Up @@ -271,22 +284,50 @@ func NewService(
return s, nil
}

// getIterationsForLimitQuery is used to get iterations of querying data from the contract with given rpc limit for blocks
// getIterationsForLimitQueryPerpsMarket is used to get iterations of querying data from the contract with given rpc limit for blocks
// and latest block number. Limit by default (if given limit is 0) is set to 20 000 blocks
func (s *Service) getIterationsForLimitQuery(limit uint64) (iterations uint64, lastBlock uint64, err error) {
lastBlock, err = s.rpcClient.BlockNumber(context.Background())
if err != nil {
logger.Log().WithField("layer", "Service-getIterationsForLimitQuery").Errorf("get latest block rpc error: %v", err.Error())
return 0, 0, errors.GetRPCProviderErr(err, "BlockNumber")
func (s *Service) getIterationsForLimitQueryPerpsMarket(limit uint64) (iterations uint64, lastBlock uint64, err error) {
return s.getIterationsForQuery(0, 0, limit, ContractPerpsMarket)
}

// getIterationsForLimitQueryCore is used to get iterations of querying data from the contract with given rpc limit for blocks
// and latest block number. Limit by default (if given limit is 0) is set to 20 000 blocks
func (s *Service) getIterationsForLimitQueryCore(limit uint64) (iterations uint64, lastBlock uint64, err error) {
return s.getIterationsForQuery(0, 0, limit, ContractCore)
}

func (s *Service) getIterationsForQuery(fromBlock uint64, toBlock uint64, limit uint64, contractType ContractType) (iterations uint64, lastBlock uint64, err error) {
if fromBlock == 0 {
switch contractType {
case ContractPerpsMarket:
fromBlock = s.perpsMarketFirstBlock
case ContractCore:
fromBlock = s.coreFirstBlock
}
}

if toBlock == 0 {
lastBlock, err = s.rpcClient.BlockNumber(context.Background())
if err != nil {
logger.Log().WithField("layer", "Service-getIterationsForQuery").Errorf("get latest block rpc error: %v", err.Error())
return 0, 0, errors.GetRPCProviderErr(err, "BlockNumber")
}

toBlock = lastBlock
}

if limit == 0 {
limit = 20000
}

iterations = (lastBlock-s.perpsMarketFirstBlock)/limit + 1
if fromBlock > toBlock {
logger.Log().WithField("layer", "Service-getIterationsForQuery").Error("fromBlock > toBlock")
return 0, 0, fmt.Errorf("fromBlock > toBlock")
}

iterations = (toBlock-fromBlock)/limit + 1

return iterations, lastBlock, nil
return iterations, toBlock, nil
}

// getFilterOptsPerpsMarket is used to get options for event filtering on perps market contract
Expand Down
2 changes: 1 addition & 1 deletion services/trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *Service) RetrieveTrades(fromBlock uint64, toBLock *uint64) ([]*models.T
}

func (s *Service) RetrieveTradesLimit(limit uint64) ([]*models.Trade, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
iterations, last, err := s.getIterationsForLimitQueryPerpsMarket(limit)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1589e02

Please sign in to comment.