Skip to content

Commit

Permalink
update grpc query
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 committed Nov 3, 2023
1 parent 0b4898c commit 1bc47f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
24 changes: 23 additions & 1 deletion x/protocolpool/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/grpc/status"

"cosmossdk.io/collections"
"cosmossdk.io/math"
"cosmossdk.io/x/protocolpool/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -55,5 +56,26 @@ func (k Querier) UnclaimedBudget(ctx context.Context, req *types.QueryUnclaimedB
} else {
unclaimedBudget = budget.TotalBudget.Sub(*budget.ClaimedAmount)
}
return &types.QueryUnclaimedBudgetResponse{UnclaimedAmount: &unclaimedBudget}, nil

if budget.ClaimedAmount == nil {
zeroCoin := sdk.NewCoin(budget.TotalBudget.Denom, math.ZeroInt())
budget.ClaimedAmount = &zeroCoin
}

if budget.NextClaimFrom == nil {
budget.NextClaimFrom = budget.StartTime
}

if budget.TranchesLeft == 0 {
budget.TranchesLeft = budget.Tranches
}

return &types.QueryUnclaimedBudgetResponse{
TotalBudget: budget.TotalBudget,
ClaimedAmount: budget.ClaimedAmount,
UnclaimedAmount: &unclaimedBudget,
NextClaimFrom: budget.NextClaimFrom,
Period: budget.Period,
TranchesLeft: budget.TranchesLeft,
}, nil
}
22 changes: 21 additions & 1 deletion x/protocolpool/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"time"

"cosmossdk.io/math"
"cosmossdk.io/x/protocolpool/keeper"
"cosmossdk.io/x/protocolpool/types"

Expand All @@ -13,13 +14,16 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
queryServer := keeper.NewQuerier(suite.poolKeeper)
startTime := suite.ctx.BlockTime().Add(-70 * time.Second)
period := time.Duration(60) * time.Second
zeroCoin := sdk.NewCoin("foo", math.ZeroInt())
nextClaimFrom := startTime.Add(period)
testCases := []struct {
name string
preRun func()
req *types.QueryUnclaimedBudgetRequest
expErr bool
expErrMsg string
unclaimedFunds *sdk.Coin
resp *types.QueryUnclaimedBudgetResponse
}{
{
name: "empty recipient address",
Expand Down Expand Up @@ -56,6 +60,14 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
},
expErr: false,
unclaimedFunds: &fooCoin,
resp: &types.QueryUnclaimedBudgetResponse{
TotalBudget: &fooCoin,
ClaimedAmount: &zeroCoin,
UnclaimedAmount: &fooCoin,
NextClaimFrom: &startTime,
Period: &period,
TranchesLeft: 2,
},
},
{
name: "valid case with claim",
Expand Down Expand Up @@ -85,6 +97,14 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
},
expErr: false,
unclaimedFunds: &fooCoin2,
resp: &types.QueryUnclaimedBudgetResponse{
TotalBudget: &fooCoin,
ClaimedAmount: &fooCoin2,
UnclaimedAmount: &fooCoin2,
NextClaimFrom: &nextClaimFrom,
Period: &period,
TranchesLeft: 1,
},
},
}
for _, tc := range testCases {
Expand All @@ -98,7 +118,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {
suite.Require().NoError(err)
suite.Require().Equal(tc.unclaimedFunds, resp.UnclaimedAmount)
suite.Require().Equal(tc.resp, resp)
}
})
}
Expand Down

0 comments on commit 1bc47f7

Please sign in to comment.