diff --git a/go.mod b/go.mod index cbba8c97..9eb9c406 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/x/alliance/bindings/query_plugin.go b/x/alliance/bindings/query_plugin.go index df00cc5f..ed3fa6a2 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, } @@ -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 { @@ -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 } @@ -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 } diff --git a/x/alliance/bindings/tests/query_plugin_test.go b/x/alliance/bindings/tests/query_plugin_test.go index 444a9153..6f0f5080 100644 --- a/x/alliance/bindings/tests/query_plugin_test.go +++ b/x/alliance/bindings/tests/query_plugin_test.go @@ -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{ @@ -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{ @@ -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) } @@ -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{ @@ -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) @@ -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\"}") 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"` }