Skip to content

Commit

Permalink
feat: services to recieve data for account
Browse files Browse the repository at this point in the history
  • Loading branch information
xfiendx4life committed Sep 4, 2024
1 parent 8cbdb5b commit 0fcff90
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 1 deletion.
3 changes: 2 additions & 1 deletion events/events.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package events

import (
"github.com/gateway-fm/perpsv3-Go/contracts/Account"
"math/big"

"github.com/gateway-fm/perpsv3-Go/contracts/Account"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"

Expand Down
4 changes: 4 additions & 0 deletions models/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ func decodePermissionsCore(perm core.IAccountModuleAccountPermissions) (res []Pe

return res
}

func DecodePermissionCore(perm [32]byte) (Permission, error) {
return PermissionFromString(strings.TrimRight(string(perm[:]), string(rune(0))))
}
24 changes: 24 additions & 0 deletions perpsv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ type IPerpsv3 interface {
// limit. For most public RPC providers the value for limit is 20 000 blocks
FormatAccountsCore(fromBlock, toBlock, limit uint64) ([]*models.Account, error)

// RetrieveChangeOwner is used to get all owner changes and 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
RetrieveChangeOwner(fromBlock, toBlock, limit uint64) ([]*models.AccountTransfer, error)

// RetrievePermissionRevoked is used to get all the revoked permission and 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
RetrievePermissionRevoked(fromBlock, toBlock, limit uint64) ([]*models.PermissionChanged, error)

// RetrievePermissionGranted is used to get all the granted permission and 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
RetrievePermissionGranted(fromBlock, toBlock, limit uint64) ([]*models.PermissionChanged, error)

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

Expand Down Expand Up @@ -537,6 +549,18 @@ func (p *Perpsv3) RetrieveLiquidationsCore(fromBlock uint64, toBlock uint64, lim
func (p *Perpsv3) RetrieveVaultLiquidationsCore(fromBlock uint64, toBlock uint64, limit uint64) ([]*models.CoreVaultLiquidation, error) {
return p.service.RetrieveVaultLiquidationsCore(fromBlock, toBlock, limit)
}

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

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

func (p *Perpsv3) RetrievePermissionGranted(fromBlock, toBlock, limit uint64) ([]*models.PermissionChanged, error) {
return p.service.RetrievePermissionGranted(fromBlock, toBlock, limit)
}
func (p *Perpsv3) ListenTrades() (*events.TradeSubscription, error) {
return p.events.ListenTrades()
}
Expand Down
230 changes: 230 additions & 0 deletions services/accounts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"context"
"fmt"
"math/big"
"time"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"

"github.com/gateway-fm/perpsv3-Go/config"
"github.com/gateway-fm/perpsv3-Go/contracts/core"
"github.com/gateway-fm/perpsv3-Go/contracts/forwarder"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
Expand Down Expand Up @@ -638,3 +640,231 @@ func (s *Service) GetAccountAvailableCollateral(accountId *big.Int, collateralTy

return res, nil
}

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

var permissions []*models.PermissionChanged

if fromBlock == 0 {
fromBlock = s.coreFirstBlock
}

logger.Log().WithField("layer", "Service-GetPermissionRevoked").Infof(
"fetching permission revoke with limit: %v from block: %v to block: %v total iterations: %v...",
limit, fromBlock, lastBlock, iterations,
)

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-GetPermissionRevoked").Infof("-- iteration %v", i)
}

opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration)

iterator, err := s.core.FilterPermissionRevoked(opts, nil, nil, nil)
if err != nil {
return nil, err
}
for iterator.Next() {
if iterator.Error() != nil {
logger.Log().WithField("layer", "Service-GetPermissionRevoked").Errorf("iterator error: %v", iterator.Error())
return nil, errors.GetFilterErr(iterator.Error(), "core contract")
}
parsedPermission, err := permissionRevokedToPermissionChanged(iterator.Event)
if err != nil {
logger.Log().WithField("layer", "GetPermissionRevoked").Errorf("error decoding permission %s", err)
continue
}
permissions = append(permissions, parsedPermission)

}

startBlockOfIteration = endBlockOfIteration + 1

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

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

return permissions, nil
}

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

var permissions []*models.PermissionChanged

if fromBlock == 0 {
fromBlock = s.coreFirstBlock
}

logger.Log().WithField("layer", "Service-GetPermissionGranted").Infof(
"fetching permission revoke with limit: %v from block: %v to block: %v total iterations: %v...",
limit, fromBlock, lastBlock, iterations,
)

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-GetPermissionGranted").Infof("-- iteration %v", i)
}

opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration)

iterator, err := s.core.FilterPermissionGranted(opts, nil, nil, nil)
if err != nil {
return nil, err
}
for iterator.Next() {
if iterator.Error() != nil {
logger.Log().WithField("layer", "Service-GetPermissionGranted").Errorf("iterator error: %v", iterator.Error())
return nil, errors.GetFilterErr(iterator.Error(), "core contract")
}
parsedPermission, err := permissionGrantedToPermissionChanged(iterator.Event)
if err != nil {
logger.Log().WithField("layer", "GetPermissionGranted").Errorf("error decoding permission %s", err)
continue
}
permissions = append(permissions, parsedPermission)

}

startBlockOfIteration = endBlockOfIteration + 1

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

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

return permissions, nil
}

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

var transfers []*models.AccountTransfer

if fromBlock == 0 {
fromBlock = s.coreFirstBlock
}

logger.Log().WithField("layer", "Service-RetrieveChangeOwner").Infof(
"fetching permission revoke with limit: %v from block: %v to block: %v total iterations: %v...",
limit, fromBlock, lastBlock, iterations,
)

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-RetrieveChangeOwner").Infof("-- iteration %v", i)
}

opts := s.getFilterOptsCore(startBlockOfIteration, &endBlockOfIteration)

iterator, err := s.core.FilterOwnerChanged(opts)
if err != nil {
return nil, err
}
for iterator.Next() {
if iterator.Error() != nil {
logger.Log().WithField("layer", "Service-RetrieveChangeOwner").Errorf("iterator error: %v", iterator.Error())
return nil, errors.GetFilterErr(iterator.Error(), "core contract")
}
transfer, err := s.toOwnerTransfered(iterator.Event)
if err != nil {
logger.Log().WithField("layer", "RetrieveChangeOwner").Errorf("error decoding CoreOwnerChanged %s", err)
continue
}
transfers = append(transfers, transfer)

}

startBlockOfIteration = endBlockOfIteration + 1

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

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

return transfers, nil
}

func permissionRevokedToPermissionChanged(revoked *core.CorePermissionRevoked) (*models.PermissionChanged, error) {
permission, err := models.DecodePermissionCore(revoked.Permission)
if err != nil {
return nil, err
}
return &models.PermissionChanged{
AccountID: revoked.AccountId,
User: revoked.User,
Permission: permission,
}, nil
}

func permissionGrantedToPermissionChanged(revoked *core.CorePermissionGranted) (*models.PermissionChanged, error) {
permission, err := models.DecodePermissionCore(revoked.Permission)
if err != nil {
return nil, err
}
return &models.PermissionChanged{
AccountID: revoked.AccountId,
User: revoked.User,
Permission: permission,
}, nil
}

func (s *Service) toOwnerTransfered(changed *core.CoreOwnerChanged) (*models.AccountTransfer, error) {
block, err := s.rpcClient.BlockByHash(context.Background(), changed.Raw.BlockHash)
if err != nil {
return nil, err
}
return &models.AccountTransfer{
From: changed.OldOwner,
To: changed.NewOwner,
BlockNumber: changed.Raw.BlockNumber,
BlockTimestamp: block.Time(),
//! We don't have account address in here
//! So we know from and to but do not know what
}, nil
}
12 changes: 12 additions & 0 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ type IService interface {
// 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)

// RetrieveChangeOwner is used to get all owner changes and 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
RetrieveChangeOwner(fromBlock, toBlock, limit uint64) ([]*models.AccountTransfer, error)

// RetrievePermissionRevoked is used to get all the revoked permission and 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
RetrievePermissionRevoked(fromBlock, toBlock, limit uint64) ([]*models.PermissionChanged, error)

// RetrievePermissionGranted is used to get all the granted permission and 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
RetrievePermissionGranted(fromBlock, toBlock, limit uint64) ([]*models.PermissionChanged, error)
}

type ContractType int
Expand Down

0 comments on commit 0fcff90

Please sign in to comment.