Skip to content

Commit

Permalink
Merge pull request #66 from gateway-fm/update/formatAccountsCore
Browse files Browse the repository at this point in the history
feat: format accounts core
asolovov authored May 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 248e1bf + c677eae commit bf3723b
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/new_lib.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ func main() {
log.Fatal(err)
}

res, err := lib.GetVaultDebtHistorical(big.NewInt(1), common.HexToAddress("0xC74eA762cF06c9151cE074E6a569a5945b6302E7"), big.NewInt(13625311))
res, err := lib.GetAccountCollateralCore(big.NewInt(1), common.HexToAddress("0xC74EA762CF06C9151CE074E6A569A5945B6302E7"))
if err != nil {
log.Fatal(err)
}
8 changes: 8 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
@@ -299,6 +299,10 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
FormatAccountCore(id *big.Int) (*models.Account, error)

// 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)

// Config is used to get current lib config
Config() *config.PerpsvConfig

@@ -627,6 +631,10 @@ func (p *Perpsv3) FormatAccountsLimit(limit uint64) ([]*models.Account, error) {
return p.service.FormatAccountsLimit(limit)
}

func (p *Perpsv3) FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) {
return p.service.FormatAccountsCoreLimit(limit)
}

func (p *Perpsv3) FormatAccountCore(id *big.Int) (*models.Account, error) {
return p.service.FormatAccountCore(id)
}
69 changes: 69 additions & 0 deletions services/accounts.go
Original file line number Diff line number Diff line change
@@ -487,6 +487,32 @@ func (s *Service) formatAccounts(opts *bind.FilterOpts) ([]*models.Account, erro
return accounts, nil
}

func (s *Service) formatAccountsCore(opts *bind.FilterOpts) ([]*models.Account, error) {
iterator, err := s.core.FilterAccountCreated(opts, nil, nil)
if err != nil {
logger.Log().WithField("layer", "Service-formatAccountsCore").Errorf("error get iterator: %v", err.Error())
return nil, errors.GetFilterErr(err, "core")
}

var accounts []*models.Account

for iterator.Next() {
if iterator.Error() != nil {
logger.Log().WithField("layer", "Service-formatAccountsCore").Errorf("iterator error: %v", err.Error())
return nil, errors.GetFilterErr(iterator.Error(), "core")
}

account, err := s.formatAccountCore(iterator.Event.AccountId)
if err != nil {
return nil, err
}

accounts = append(accounts, account)
}

return accounts, nil
}

// formatAccount is used to get models.Account data from given account id
func (s *Service) formatAccount(id *big.Int) (*models.Account, error) {
owner, err := s.perpsMarket.GetAccountOwner(nil, id)
@@ -510,6 +536,49 @@ func (s *Service) formatAccount(id *big.Int) (*models.Account, error) {
return models.FormatAccount(id, owner, time.Uint64(), permissions), nil
}

func (s *Service) FormatAccountsCoreLimit(limit uint64) ([]*models.Account, error) {
iterations, last, err := s.getIterationsForLimitQuery(limit)
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,
)

fromBlock := s.coreFirstBlock
toBlock := fromBlock + limit
for i := uint64(1); i <= iterations; i++ {
if i%10 == 0 || i == iterations {
logger.Log().WithField("layer", "Service-FormatAccountsCoreLimit").Infof("-- iteration %v", i)
}

opts := s.getFilterOptsCore(fromBlock, &toBlock)

res, err := s.formatAccountsCore(opts)
if err != nil {
return nil, err
}

accounts = append(accounts, res...)

fromBlock = toBlock + 1

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

logger.Log().WithField("layer", "Service-FormatAccountsLimit").Infof("task completed successfully")

return accounts, nil
}

func (s *Service) FormatAccountCore(id *big.Int) (*models.Account, error) {
return s.formatAccountCore(id)
}
4 changes: 4 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
@@ -193,6 +193,10 @@ type IService interface {
// FormatAccountCore is used to get all accounts and their additional data from the core contract with given block search
// limit. For most public RPC providers the value for limit is 20 000 blocks
FormatAccountCore(id *big.Int) (*models.Account, error)

// 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)
}

// Service is an implementation of IService interface

0 comments on commit bf3723b

Please sign in to comment.