Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/deposits start #74

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

40 changes: 40 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
46 changes: 30 additions & 16 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 @@ -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")
}

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: %v", iterator.Error())
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: %v", iterator.Error())
return nil, errors.GetFilterErr(iterator.Error(), "core")
}

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