Skip to content

Commit

Permalink
fix: update query interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Mar 27, 2024
1 parent 3d5b45d commit d76cd01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
29 changes: 5 additions & 24 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 @@ -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 @@ -124,34 +121,18 @@ func (q *QueryPlugin) GetDelegationRewards(ctx sdk.Context,
if err != nil {
return nil, err
}
delegation, found := q.allianceKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr, denom)
if !found {
return nil, alliancetypes.ErrDelegationNotFound
}
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 nil, err
}

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

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 @@ -35,7 +35,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 @@ -104,7 +104,7 @@ func TestDelegationQuery(t *testing.T) {
_, err = app.AllianceKeeper.Delegate(ctx, delAddr, val, sdk.NewCoin(AllianceDenom, math.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 @@ -127,10 +127,7 @@ func TestDelegationQuery(t *testing.T) {
Delegator: delAddr.String(),
Validator: val.GetOperator(),
Denom: AllianceDenom,
Amount: bindingtypes.Coin{
Denom: AllianceDenom,
Amount: "1000000",
},
Amount: "1000000",
}, delegationResponse)
}

Expand Down Expand Up @@ -180,7 +177,7 @@ func TestDelegationRewardsQuery(t *testing.T) {
err = app.AllianceKeeper.AddAssetsToRewardPool(ctx, mintPoolAddr, val, sdk.NewCoins(sdk.NewCoin("stake", math.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 @@ -200,10 +197,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 @@ -219,7 +216,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"`
}

0 comments on commit d76cd01

Please sign in to comment.