Skip to content

Commit b1c3c5c

Browse files
Merge pull request #339 from terra-money/feat/v0.3/update-binding-error
fix: update query interfaces
2 parents 528af3d + 3cfc47a commit b1c3c5c

File tree

4 files changed

+23
-48
lines changed

4 files changed

+23
-48
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/grpc-ecosystem/grpc-gateway v1.16.0
1818
github.com/spf13/cast v1.5.1
1919
github.com/spf13/cobra v1.7.0
20-
github.com/spf13/viper v1.16.0
2120
github.com/stretchr/testify v1.8.4
2221
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
2322
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
@@ -145,6 +144,7 @@ require (
145144
github.com/spf13/afero v1.9.5 // indirect
146145
github.com/spf13/jwalterweatherman v1.1.0 // indirect
147146
github.com/spf13/pflag v1.0.5 // indirect
147+
github.com/spf13/viper v1.16.0 // indirect
148148
github.com/subosito/gotenv v1.4.2 // indirect
149149
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
150150
github.com/tendermint/go-amino v0.16.0 // indirect

x/alliance/bindings/query_plugin.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
)
1313

1414
type QueryPlugin struct {
15-
allianceKeeper keeper.Keeper
15+
allianceKeeper *keeper.Keeper
1616
}
1717

18-
func NewAllianceQueryPlugin(keeper keeper.Keeper) *QueryPlugin {
18+
func NewAllianceQueryPlugin(keeper *keeper.Keeper) *QueryPlugin {
1919
return &QueryPlugin{
2020
allianceKeeper: keeper,
2121
}
@@ -73,17 +73,17 @@ func (q *QueryPlugin) GetAlliance(ctx sdk.Context, denom string) (res []byte, er
7373
},
7474
IsInitialized: asset.IsInitialized,
7575
})
76-
return
76+
return res, err
7777
}
7878

7979
func (q *QueryPlugin) GetDelegation(ctx sdk.Context, denom string, delegator string, validator string) (res []byte, err error) {
8080
delegatorAddr, err := sdk.AccAddressFromBech32(delegator)
8181
if err != nil {
82-
return
82+
return nil, err
8383
}
8484
validatorAddr, err := sdk.ValAddressFromBech32(validator)
8585
if err != nil {
86-
return
86+
return nil, err
8787
}
8888
delegation, found := q.allianceKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr, denom)
8989
if !found {
@@ -103,10 +103,7 @@ func (q *QueryPlugin) GetDelegation(ctx sdk.Context, denom string, delegator str
103103
Delegator: delegation.DelegatorAddress,
104104
Validator: delegation.ValidatorAddress,
105105
Denom: delegation.Denom,
106-
Amount: types.Coin{
107-
Denom: balance.Denom,
108-
Amount: balance.Amount.String(),
109-
},
106+
Amount: balance.Amount.String(),
110107
})
111108
return res, err
112109
}
@@ -118,40 +115,24 @@ func (q *QueryPlugin) GetDelegationRewards(ctx sdk.Context,
118115
) (res []byte, err error) {
119116
delegatorAddr, err := sdk.AccAddressFromBech32(delegator)
120117
if err != nil {
121-
return
118+
return nil, err
122119
}
123120
validatorAddr, err := sdk.ValAddressFromBech32(validator)
124121
if err != nil {
125-
return
126-
}
127-
delegation, found := q.allianceKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr, denom)
128-
if !found {
129-
return nil, alliancetypes.ErrDelegationNotFound
122+
return nil, err
130123
}
131124
allianceValidator, err := q.allianceKeeper.GetAllianceValidator(ctx, validatorAddr)
132125
if err != nil {
133126
return nil, err
134127
}
135-
asset, found := q.allianceKeeper.GetAssetByDenom(ctx, denom)
136-
if !found {
137-
return nil, alliancetypes.ErrUnknownAsset
138-
}
139128

140-
rewards, _, err := q.allianceKeeper.CalculateDelegationRewards(ctx, delegation, allianceValidator, asset)
129+
rewards, err := q.allianceKeeper.ClaimDelegationRewards(ctx, delegatorAddr, allianceValidator, denom)
141130
if err != nil {
142-
return
143-
}
144-
145-
var coins []types.Coin
146-
for _, coin := range rewards {
147-
coins = append(coins, types.Coin{
148-
Denom: coin.Denom,
149-
Amount: coin.Amount.String(),
150-
})
131+
return nil, err
151132
}
152133

153134
res, err = json.Marshal(types.DelegationRewardsResponse{
154-
Rewards: coins,
135+
Rewards: rewards,
155136
})
156137
return res, err
157138
}

x/alliance/bindings/tests/query_plugin_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAssetQuery(t *testing.T) {
3434
},
3535
})
3636

37-
querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
37+
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
3838
querier := bindings.CustomQuerier(querierPlugin)
3939

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

104-
querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
104+
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
105105
querier := bindings.CustomQuerier(querierPlugin)
106106

107107
delegationQuery := bindingtypes.AllianceQuery{
@@ -124,10 +124,7 @@ func TestDelegationQuery(t *testing.T) {
124124
Delegator: delAddr.String(),
125125
Validator: val.GetOperator().String(),
126126
Denom: AllianceDenom,
127-
Amount: bindingtypes.Coin{
128-
Denom: AllianceDenom,
129-
Amount: "1000000",
130-
},
127+
Amount: "1000000",
131128
}, delegationResponse)
132129
}
133130

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

178-
querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
175+
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
179176
querier := bindings.CustomQuerier(querierPlugin)
180177

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

197194
require.Equal(t, bindingtypes.DelegationRewardsResponse{
198-
Rewards: []bindingtypes.Coin{
195+
Rewards: []sdk.Coin{
199196
{
200197
Denom: "stake",
201-
Amount: "2000000",
198+
Amount: sdk.NewInt(2000000),
202199
},
203200
},
204201
}, response)
@@ -214,7 +211,7 @@ func TestCustomQuerier(t *testing.T) {
214211
},
215212
})
216213

217-
querierPlugin := bindings.NewAllianceQueryPlugin(app.AllianceKeeper)
214+
querierPlugin := bindings.NewAllianceQueryPlugin(&app.AllianceKeeper)
218215
querier := bindings.CustomQuerier(querierPlugin)
219216

220217
queryBytes := []byte("{\"random\": \"query\"}")

x/alliance/bindings/types/response.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package types
22

3+
import sdk "github.com/cosmos/cosmos-sdk/types"
4+
35
type AllianceResponse struct {
46
Denom string `json:"denom"`
57
RewardWeight string `json:"reward_weight"`
@@ -18,18 +20,13 @@ type RewardWeightRange struct {
1820
Max string `json:"max"`
1921
}
2022

21-
type Coin struct {
22-
Denom string `json:"denom"`
23-
Amount string `json:"amount"`
24-
}
25-
2623
type DelegationResponse struct {
2724
Delegator string `json:"delegator"`
2825
Validator string `json:"validator"`
2926
Denom string `json:"denom"`
30-
Amount Coin `json:"amount"`
27+
Amount string `json:"amount"`
3128
}
3229

3330
type DelegationRewardsResponse struct {
34-
Rewards []Coin `json:"rewards"`
31+
Rewards sdk.Coins `json:"rewards"`
3532
}

0 commit comments

Comments
 (0)