From d76cd0162ce573ce8174d037113406d85728e58a Mon Sep 17 00:00:00 2001 From: javiersuweijie Date: Wed, 27 Mar 2024 11:52:08 +0800 Subject: [PATCH 1/2] fix: update query interfaces --- x/alliance/bindings/query_plugin.go | 29 ++++--------------- .../bindings/tests/query_plugin_test.go | 17 +++++------ x/alliance/bindings/types/response.go | 11 +++---- 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/x/alliance/bindings/query_plugin.go b/x/alliance/bindings/query_plugin.go index 1cda6725..2f9010f9 100644 --- a/x/alliance/bindings/query_plugin.go +++ b/x/alliance/bindings/query_plugin.go @@ -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, } @@ -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 } @@ -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 } diff --git a/x/alliance/bindings/tests/query_plugin_test.go b/x/alliance/bindings/tests/query_plugin_test.go index 625b892f..abaa9a26 100644 --- a/x/alliance/bindings/tests/query_plugin_test.go +++ b/x/alliance/bindings/tests/query_plugin_test.go @@ -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{ @@ -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{ @@ -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) } @@ -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{ @@ -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) @@ -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\"}") diff --git a/x/alliance/bindings/types/response.go b/x/alliance/bindings/types/response.go index 50a32b69..f97f4555 100644 --- a/x/alliance/bindings/types/response.go +++ b/x/alliance/bindings/types/response.go @@ -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"` @@ -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"` } From cef3a84a9a02a0ee3b59ae590f3fbab70cdf06ff Mon Sep 17 00:00:00 2001 From: javiersuweijie Date: Wed, 27 Mar 2024 14:23:22 +0800 Subject: [PATCH 2/2] fix: lint --- x/alliance/bindings/query_plugin.go | 2 +- x/alliance/bindings/tests/query_plugin_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/alliance/bindings/query_plugin.go b/x/alliance/bindings/query_plugin.go index 2f9010f9..ed3fa6a2 100644 --- a/x/alliance/bindings/query_plugin.go +++ b/x/alliance/bindings/query_plugin.go @@ -73,7 +73,7 @@ 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) { diff --git a/x/alliance/bindings/tests/query_plugin_test.go b/x/alliance/bindings/tests/query_plugin_test.go index abaa9a26..abd021f9 100644 --- a/x/alliance/bindings/tests/query_plugin_test.go +++ b/x/alliance/bindings/tests/query_plugin_test.go @@ -200,7 +200,7 @@ func TestDelegationRewardsQuery(t *testing.T) { Rewards: []sdk.Coin{ { Denom: "stake", - Amount: sdk.NewInt(2000000), + Amount: math.NewInt(2000000), }, }, }, response)