Skip to content

Commit

Permalink
feat: remove height usage in action handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
akhlopiachyi committed Feb 1, 2024
1 parent bfd158f commit bc68996
Show file tree
Hide file tree
Showing 26 changed files with 131 additions and 160 deletions.
8 changes: 1 addition & 7 deletions modules/actions/handlers/account_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ import (

func AccountBalanceHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing account balance action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

balance, err := ctx.Sources.BankSource.GetAccountBalance(payload.GetAddress(), height)
balance, err := ctx.Sources.BankSource.GetAccountBalance(payload.GetAddress())
if err != nil {
return nil, fmt.Errorf("error while getting account balance: %s", err)
}
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ import (

func DelegationHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("action", "delegations").
Str("address", payload.GetAddress()).
Msg("executing delegations action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get delegator's total rewards
res, err := ctx.Sources.StakingSource.GetDelegationsWithPagination(height, payload.GetAddress(), payload.GetPagination())
res, err := ctx.Sources.StakingSource.GetDelegationsWithPagination(payload.GetAddress(), payload.GetPagination())
if err != nil {
// For stargate only, returns without throwing error if delegator delegations are not found on the chain
if strings.Contains(err.Error(), codes.NotFound.String()) {
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/delegation_total.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import (

func TotalDelegationAmountHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing total delegation amount action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get all delegations for given delegator address
delegationList, err := ctx.Sources.StakingSource.GetDelegationsWithPagination(height, payload.GetAddress(), nil)
delegationList, err := ctx.Sources.StakingSource.GetDelegationsWithPagination(payload.GetAddress(), nil)
if err != nil {
// For stargate only, returns without throwing error if delegator delegations are not found on the chain
if strings.Contains(err.Error(), codes.NotFound.String()) {
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/delegator_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ import (

func DelegationRewardHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing delegation rewards action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get delegator's total rewards
rewards, err := ctx.Sources.DistrSource.DelegatorTotalRewards(payload.GetAddress(), height)
rewards, err := ctx.Sources.DistrSource.DelegatorTotalRewards(payload.GetAddress())
if err != nil {
return nil, fmt.Errorf("error while getting delegator total rewards: %s", err)
}
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/delegator_withdraw_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ func DelegatorWithdrawAddressHandler(ctx *types.Context, payload *types.Payload)
log.Debug().Str("address", payload.GetAddress()).
Msg("executing delegator withdraw address action")

// Get latest node height
height, err := ctx.GetHeight(nil)
if err != nil {
return nil, err
}

// Get delegator's total rewards
withdrawAddress, err := ctx.Sources.DistrSource.DelegatorWithdrawAddress(payload.GetAddress(), height)
withdrawAddress, err := ctx.Sources.DistrSource.DelegatorWithdrawAddress(payload.GetAddress())
if err != nil {
return nil, fmt.Errorf("error while getting delegator withdraw address: %s", err)
}
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/redelegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ import (

func RedelegationHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing redelegations action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get delegator's redelegations
redelegations, err := ctx.Sources.StakingSource.GetRedelegations(height, &stakingtypes.QueryRedelegationsRequest{
redelegations, err := ctx.Sources.StakingSource.GetRedelegations(&stakingtypes.QueryRedelegationsRequest{
DelegatorAddr: payload.GetAddress(),
Pagination: payload.GetPagination(),
})
Expand Down
10 changes: 2 additions & 8 deletions modules/actions/handlers/unbonding_delegation_total.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ import (

func UnbondingDelegationsTotal(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing unbonding delegation total action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get all unbonding delegations for given delegator address
unbondingDelegations, err := ctx.Sources.StakingSource.GetUnbondingDelegations(height, payload.GetAddress(), nil)
unbondingDelegations, err := ctx.Sources.StakingSource.GetUnbondingDelegations(payload.GetAddress(), nil)
if err != nil {
return nil, fmt.Errorf("error while getting delegator unbonding delegations: %s", err)
}

// Get the bond denom type
params, err := ctx.Sources.StakingSource.GetParams(height)
params, err := ctx.Sources.StakingSource.GetParams()
if err != nil {
return nil, fmt.Errorf("error while getting bond denom type: %s", err)
}
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/unbonding_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ import (

func UnbondingDelegationsHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing unbonding delegations action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get all unbonding delegations for given delegator address
unbondingDelegations, err := ctx.Sources.StakingSource.GetUnbondingDelegations(height, payload.GetAddress(), payload.GetPagination())
unbondingDelegations, err := ctx.Sources.StakingSource.GetUnbondingDelegations(payload.GetAddress(), payload.GetPagination())
if err != nil {
return nil, fmt.Errorf("error while getting delegator unbonding delegations: %s", err)
}
Expand Down
9 changes: 1 addition & 8 deletions modules/actions/handlers/validator_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@ import (

func ValidatorCommissionAmountHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing validator commission action")

// Get latest node height
height, err := ctx.GetHeight(nil)
if err != nil {
return nil, err
}

// Get validator total commission value
commission, err := ctx.Sources.DistrSource.ValidatorCommission(payload.GetAddress(), height)
commission, err := ctx.Sources.DistrSource.ValidatorCommission(payload.GetAddress())
if err != nil {
return nil, fmt.Errorf("error while getting validator commission: %s", err)
}
Expand Down
9 changes: 1 addition & 8 deletions modules/actions/handlers/validator_delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ import (

func ValidatorDelegation(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing validator delegation action")

// Get latest node height
height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get validator's total delegations
res, err := ctx.Sources.StakingSource.GetValidatorDelegationsWithPagination(height, payload.GetAddress(), payload.GetPagination())
res, err := ctx.Sources.StakingSource.GetValidatorDelegationsWithPagination(payload.GetAddress(), payload.GetPagination())
if err != nil {
return nil, fmt.Errorf("error while getting validator delegations: %s", err)
}
Expand Down
8 changes: 1 addition & 7 deletions modules/actions/handlers/validator_redelegations_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ import (

func ValidatorRedelegationsFromHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing validator redelegation action")

height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get redelegations from a source validator address
redelegations, err := ctx.Sources.StakingSource.GetRedelegations(height, &stakingtypes.QueryRedelegationsRequest{
redelegations, err := ctx.Sources.StakingSource.GetRedelegations(&stakingtypes.QueryRedelegationsRequest{
SrcValidatorAddr: payload.GetAddress(),
Pagination: payload.GetPagination(),
})
Expand Down
8 changes: 0 additions & 8 deletions modules/actions/handlers/validator_unbonding_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ import (

func ValidatorUnbondingDelegationsHandler(ctx *types.Context, payload *types.Payload) (interface{}, error) {
log.Debug().Str("address", payload.GetAddress()).
Int64("height", payload.Input.Height).
Msg("executing validator unbonding delegations action")

// Get latest node height
height, err := ctx.GetHeight(payload)
if err != nil {
return nil, err
}

// Get all unbonding delegations from the given validator opr address
unbondingDelegations, err := ctx.Sources.StakingSource.GetUnbondingDelegationsFromValidator(
height,
payload.GetAddress(),
payload.GetPagination(),
)
Expand Down
1 change: 1 addition & 0 deletions modules/actions/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c *Context) GetHeight(payload *Payload) (int64, error) {
if err != nil {
return 0, fmt.Errorf("error while getting chain latest block height: %s", err)
}

return latestHeight, nil
}

Expand Down
11 changes: 8 additions & 3 deletions modules/bank/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/juno/v5/node/local"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/forbole/bdjuno/v4/modules/bank/source"
"github.com/forbole/bdjuno/v4/types"
)
Expand Down Expand Up @@ -83,12 +84,16 @@ func (s Source) GetSupply(height int64) (sdk.Coins, error) {
}

// GetAccountBalances implements bankkeeper.Source
func (s Source) GetAccountBalance(address string, height int64) ([]sdk.Coin, error) {
ctx, err := s.LoadHeight(height)
func (s Source) GetAccountBalance(address string) ([]sdk.Coin, error) {
var err error
var cms sdk.CacheMultiStore
cms, err = s.Cms.CacheMultiStoreWithVersion(s.BlockStore.Height())
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
return nil, fmt.Errorf("error while getting the context: %s", err)
}

ctx := sdk.NewContext(cms, tmproto.Header{}, false, s.Logger)

balRes, err := s.q.AllBalances(sdk.WrapSDKContext(ctx), &banktypes.QueryAllBalancesRequest{Address: address})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
Expand Down
8 changes: 3 additions & 5 deletions modules/bank/source/remote/source_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
)

// GetAccountBalances implements bankkeeper.Source
func (s Source) GetAccountBalance(address string, height int64) ([]sdk.Coin, error) {

func (s Source) GetAccountBalance(address string) ([]sdk.Coin, error) {
// Get account balance at certain height
ctx := utils.GetHeightRequestContext(s.Ctx, height)
ctx := utils.GetHeightRequestContext(s.Ctx)
balRes, err := s.bankClient.AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
Expand All @@ -24,9 +23,8 @@ func (s Source) GetAccountBalance(address string, height int64) ([]sdk.Coin, err

// GetAccountDenomBalance implements bankkeeper.Source
func (s Source) GetAccountDenomBalance(address string, denom string, height int64) (*sdk.Coin, error) {

// Get account balance at certain height
ctx := utils.GetHeightRequestContext(s.Ctx, height)
ctx := utils.GetHeightRequestContext(s.Ctx)
balRes, err := s.bankClient.Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion modules/bank/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ type Source interface {
GetAccountDenomBalance(address string, denom string, height int64) (*sdk.Coin, error)

// -- For hasura action --
GetAccountBalance(address string, height int64) ([]sdk.Coin, error)
GetAccountBalance(address string) ([]sdk.Coin, error)
}
31 changes: 22 additions & 9 deletions modules/distribution/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/forbole/juno/v5/node/local"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
distrsource "github.com/forbole/bdjuno/v4/modules/distribution/source"
)

Expand All @@ -28,12 +29,16 @@ func NewSource(source *local.Source, keeper distrtypes.QueryServer) *Source {
}

// ValidatorCommission implements distrsource.Source
func (s Source) ValidatorCommission(valOperAddr string, height int64) (sdk.DecCoins, error) {
ctx, err := s.LoadHeight(height)
func (s Source) ValidatorCommission(valOperAddr string) (sdk.DecCoins, error) {
var err error
var cms sdk.CacheMultiStore
cms, err = s.Cms.CacheMultiStoreWithVersion(s.BlockStore.Height())
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
return nil, fmt.Errorf("error while getting the context: %s", err)
}

ctx := sdk.NewContext(cms, tmproto.Header{}, false, s.Logger)

res, err := s.q.ValidatorCommission(
sdk.WrapSDKContext(ctx),
&distrtypes.QueryValidatorCommissionRequest{ValidatorAddress: valOperAddr},
Expand All @@ -46,12 +51,16 @@ func (s Source) ValidatorCommission(valOperAddr string, height int64) (sdk.DecCo
}

// DelegatorTotalRewards implements distrsource.Source
func (s Source) DelegatorTotalRewards(delegator string, height int64) ([]distrtypes.DelegationDelegatorReward, error) {
ctx, err := s.LoadHeight(height)
func (s Source) DelegatorTotalRewards(delegator string) ([]distrtypes.DelegationDelegatorReward, error) {
var err error
var cms sdk.CacheMultiStore
cms, err = s.Cms.CacheMultiStoreWithVersion(s.BlockStore.Height())
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
return nil, fmt.Errorf("error while getting the context: %s", err)
}

ctx := sdk.NewContext(cms, tmproto.Header{}, false, s.Logger)

res, err := s.q.DelegationTotalRewards(
sdk.WrapSDKContext(ctx),
&distrtypes.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegator},
Expand All @@ -64,12 +73,16 @@ func (s Source) DelegatorTotalRewards(delegator string, height int64) ([]distrty
}

// DelegatorWithdrawAddress implements distrsource.Source
func (s Source) DelegatorWithdrawAddress(delegator string, height int64) (string, error) {
ctx, err := s.LoadHeight(height)
func (s Source) DelegatorWithdrawAddress(delegator string) (string, error) {
var err error
var cms sdk.CacheMultiStore
cms, err = s.Cms.CacheMultiStoreWithVersion(s.BlockStore.Height())
if err != nil {
return "", fmt.Errorf("error while loading height: %s", err)
return "", fmt.Errorf("error while getting the context: %s", err)
}

ctx := sdk.NewContext(cms, tmproto.Header{}, false, s.Logger)

res, err := s.q.DelegatorWithdrawAddress(
sdk.WrapSDKContext(ctx),
&distrtypes.QueryDelegatorWithdrawAddressRequest{DelegatorAddress: delegator},
Expand Down
Loading

0 comments on commit bc68996

Please sign in to comment.