-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/precisebank): Add query service with TotalFractionalBalances (#…
…1970) Add query service to precisebank, mostly for e2e test purposes in #1966 Also fix client grpc codec
- Loading branch information
Showing
9 changed files
with
950 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
syntax = "proto3"; | ||
package kava.precisebank.v1; | ||
|
||
import "cosmos/base/v1beta1/coin.proto"; | ||
import "gogoproto/gogo.proto"; | ||
import "google/api/annotations.proto"; | ||
|
||
option go_package = "github.com/kava-labs/kava/x/precisebank/types"; | ||
option (gogoproto.goproto_getters_all) = false; | ||
|
||
// Query defines the gRPC querier service for precisebank module | ||
service Query { | ||
// TotalFractionalBalances returns the total sum of all fractional balances | ||
// managed by the precisebank module. | ||
rpc TotalFractionalBalances(QueryTotalFractionalBalancesRequest) returns (QueryTotalFractionalBalancesResponse) { | ||
option (google.api.http).get = "/kava/precisebank/v1/total_fractional_balances"; | ||
} | ||
} | ||
|
||
// QueryTotalFractionalBalancesRequest defines the request type for Query/TotalFractionalBalances method. | ||
message QueryTotalFractionalBalancesRequest {} | ||
|
||
// QueryTotalFractionalBalancesResponse defines the response type for Query/TotalFractionalBalances method. | ||
message QueryTotalFractionalBalancesResponse { | ||
// total is the total sum of all fractional balances managed by the precisebank | ||
// module. | ||
cosmos.base.v1beta1.Coin total = 1 [(gogoproto.nullable) = false]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/kava-labs/kava/x/precisebank/types" | ||
) | ||
|
||
type queryServer struct { | ||
keeper Keeper | ||
} | ||
|
||
// NewQueryServerImpl creates a new server for handling gRPC queries. | ||
func NewQueryServerImpl(k Keeper) types.QueryServer { | ||
return &queryServer{keeper: k} | ||
} | ||
|
||
var _ types.QueryServer = queryServer{} | ||
|
||
// TotalFractionalBalances returns the total sum of all fractional balances. | ||
// This is mostly for external verification of the total fractional balances, | ||
// being a multiple of the conversion factor and backed by the reserve. | ||
func (s queryServer) TotalFractionalBalances( | ||
goCtx context.Context, | ||
req *types.QueryTotalFractionalBalancesRequest, | ||
) (*types.QueryTotalFractionalBalancesResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
totalAmount := s.keeper.GetTotalSumFractionalBalances(ctx) | ||
|
||
totalCoin := sdk.NewCoin(types.ExtendedCoinDenom, totalAmount) | ||
|
||
return &types.QueryTotalFractionalBalancesResponse{ | ||
Total: totalCoin, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/kava-labs/kava/x/precisebank/keeper" | ||
"github.com/kava-labs/kava/x/precisebank/testutil" | ||
"github.com/kava-labs/kava/x/precisebank/types" | ||
) | ||
|
||
type grpcQueryTestSuite struct { | ||
testutil.Suite | ||
|
||
queryClient types.QueryClient | ||
} | ||
|
||
func (suite *grpcQueryTestSuite) SetupTest() { | ||
suite.Suite.SetupTest() | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) | ||
types.RegisterQueryServer(queryHelper, keeper.NewQueryServerImpl(suite.Keeper)) | ||
|
||
suite.queryClient = types.NewQueryClient(queryHelper) | ||
} | ||
|
||
func TestGrpcQueryTestSuite(t *testing.T) { | ||
suite.Run(t, new(grpcQueryTestSuite)) | ||
} | ||
|
||
func (suite *grpcQueryTestSuite) TestQueryTotalFractionalBalance() { | ||
testCases := []struct { | ||
name string | ||
giveBalances []sdkmath.Int | ||
}{ | ||
{ | ||
"empty", | ||
[]sdkmath.Int{}, | ||
}, | ||
{ | ||
"min amount", | ||
[]sdkmath.Int{ | ||
types.ConversionFactor().QuoRaw(2), | ||
types.ConversionFactor().QuoRaw(2), | ||
}, | ||
}, | ||
{ | ||
"exceeds conversion factor", | ||
[]sdkmath.Int{ | ||
// 4 accounts * 0.5 == 2 | ||
types.ConversionFactor().QuoRaw(2), | ||
types.ConversionFactor().QuoRaw(2), | ||
types.ConversionFactor().QuoRaw(2), | ||
types.ConversionFactor().QuoRaw(2), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
total := sdk.NewCoin(types.ExtendedCoinDenom, sdkmath.ZeroInt()) | ||
for i, balance := range tc.giveBalances { | ||
addr := sdk.AccAddress([]byte(strconv.Itoa(i))) | ||
suite.Keeper.SetFractionalBalance(suite.Ctx, addr, balance) | ||
|
||
total.Amount = total.Amount.Add(balance) | ||
} | ||
|
||
res, err := suite.queryClient.TotalFractionalBalances( | ||
context.Background(), | ||
&types.QueryTotalFractionalBalancesRequest{}, | ||
) | ||
suite.Require().NoError(err) | ||
|
||
suite.Require().Equal(total, res.Total) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.