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

Feat/v0.4/update binding error #345

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
31 changes: 6 additions & 25 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,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) {
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: math.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"`
}
Loading