Skip to content

Commit

Permalink
feat: RetrieveCollateralWithdrawn & RetrieveCollateralDeposited
Browse files Browse the repository at this point in the history
  • Loading branch information
xfiendx4life committed Aug 29, 2024
1 parent 22d8a2f commit 50459d4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 20 deletions.
45 changes: 45 additions & 0 deletions mocks/service/mockService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions services/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand All @@ -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")
}

Expand Down
62 changes: 45 additions & 17 deletions services/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
8 changes: 8 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 50459d4

Please sign in to comment.