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

fix: update query interfaces #339

Merged
merged 2 commits into from
Mar 27, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
Expand Down Expand Up @@ -145,6 +144,7 @@ require (
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
41 changes: 11 additions & 30 deletions x/alliance/bindings/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
)

type QueryPlugin struct {
allianceKeeper keeper.Keeper
allianceKeeper *keeper.Keeper
}

func NewAllianceQueryPlugin(keeper keeper.Keeper) *QueryPlugin {
func NewAllianceQueryPlugin(keeper *keeper.Keeper) *QueryPlugin {
return &QueryPlugin{
allianceKeeper: keeper,
}
Expand Down Expand Up @@ -73,17 +73,17 @@ func (q *QueryPlugin) GetAlliance(ctx sdk.Context, denom string) (res []byte, er
},
IsInitialized: asset.IsInitialized,
})
return
return res, err
}

func (q *QueryPlugin) GetDelegation(ctx sdk.Context, denom string, delegator string, validator string) (res []byte, err error) {
delegatorAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
return
return nil, err
}
validatorAddr, err := sdk.ValAddressFromBech32(validator)
if err != nil {
return
return nil, err
}
delegation, found := q.allianceKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr, denom)
if !found {
Expand All @@ -103,10 +103,7 @@ func (q *QueryPlugin) GetDelegation(ctx sdk.Context, denom string, delegator str
Delegator: delegation.DelegatorAddress,
Validator: delegation.ValidatorAddress,
Denom: delegation.Denom,
Amount: types.Coin{
Denom: balance.Denom,
Amount: balance.Amount.String(),
},
Amount: balance.Amount.String(),
})
return res, err
}
Expand All @@ -118,40 +115,24 @@ func (q *QueryPlugin) GetDelegationRewards(ctx sdk.Context,
) (res []byte, err error) {
delegatorAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
return
return nil, err
}
validatorAddr, err := sdk.ValAddressFromBech32(validator)
if err != nil {
return
}
delegation, found := q.allianceKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr, denom)
if !found {
return nil, alliancetypes.ErrDelegationNotFound
return nil, err
}
allianceValidator, err := q.allianceKeeper.GetAllianceValidator(ctx, validatorAddr)
if err != nil {
return nil, err
}
asset, found := q.allianceKeeper.GetAssetByDenom(ctx, denom)
if !found {
return nil, alliancetypes.ErrUnknownAsset
}

rewards, _, err := q.allianceKeeper.CalculateDelegationRewards(ctx, delegation, allianceValidator, asset)
rewards, err := q.allianceKeeper.ClaimDelegationRewards(ctx, delegatorAddr, allianceValidator, denom)
if err != nil {
return
}

var coins []types.Coin
for _, coin := range rewards {
coins = append(coins, types.Coin{
Denom: coin.Denom,
Amount: coin.Amount.String(),
})
return nil, err
}

res, err = json.Marshal(types.DelegationRewardsResponse{
Rewards: coins,
Rewards: rewards,
})
return res, err
}
17 changes: 7 additions & 10 deletions x/alliance/bindings/tests/query_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAssetQuery(t *testing.T) {
},
})

querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
querier := bindings.CustomQuerier(querierPlugin)

assetQuery := bindingtypes.AllianceQuery{
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestDelegationQuery(t *testing.T) {
_, err = app.AllianceKeeper.Delegate(ctx, delAddr, val, sdk.NewCoin(AllianceDenom, sdk.NewInt(1000_000)))
require.NoError(t, err)

querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
querier := bindings.CustomQuerier(querierPlugin)

delegationQuery := bindingtypes.AllianceQuery{
Expand All @@ -124,10 +124,7 @@ func TestDelegationQuery(t *testing.T) {
Delegator: delAddr.String(),
Validator: val.GetOperator().String(),
Denom: AllianceDenom,
Amount: bindingtypes.Coin{
Denom: AllianceDenom,
Amount: "1000000",
},
Amount: "1000000",
}, delegationResponse)
}

Expand Down Expand Up @@ -175,7 +172,7 @@ func TestDelegationRewardsQuery(t *testing.T) {
err = app.AllianceKeeper.AddAssetsToRewardPool(ctx, mintPoolAddr, val, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(2000_000))))
require.NoError(t, err)

querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
querier := bindings.CustomQuerier(querierPlugin)

delegationQuery := bindingtypes.AllianceQuery{
Expand All @@ -195,10 +192,10 @@ func TestDelegationRewardsQuery(t *testing.T) {
require.NoError(t, err)

require.Equal(t, bindingtypes.DelegationRewardsResponse{
Rewards: []bindingtypes.Coin{
Rewards: []sdk.Coin{
{
Denom: "stake",
Amount: "2000000",
Amount: sdk.NewInt(2000000),
},
},
}, response)
Expand All @@ -214,7 +211,7 @@ func TestCustomQuerier(t *testing.T) {
},
})

querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
querier := bindings.CustomQuerier(querierPlugin)

queryBytes := []byte("{\"random\": \"query\"}")
Expand Down
11 changes: 4 additions & 7 deletions x/alliance/bindings/types/response.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"

type AllianceResponse struct {
Denom string `json:"denom"`
RewardWeight string `json:"reward_weight"`
Expand All @@ -18,18 +20,13 @@ type RewardWeightRange struct {
Max string `json:"max"`
}

type Coin struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}

type DelegationResponse struct {
Delegator string `json:"delegator"`
Validator string `json:"validator"`
Denom string `json:"denom"`
Amount Coin `json:"amount"`
Amount string `json:"amount"`
}

type DelegationRewardsResponse struct {
Rewards []Coin `json:"rewards"`
Rewards sdk.Coins `json:"rewards"`
}
Loading