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: grpc query total recv packet fees #1015

Merged
merged 4 commits into from
Mar 1, 2022
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
33 changes: 33 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
- [QueryIncentivizedPacketResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketResponse)
- [QueryIncentivizedPacketsRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsRequest)
- [QueryIncentivizedPacketsResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsResponse)
- [QueryTotalRecvFeesRequest](#ibc.applications.fee.v1.QueryTotalRecvFeesRequest)
- [QueryTotalRecvFeesResponse](#ibc.applications.fee.v1.QueryTotalRecvFeesResponse)

- [Query](#ibc.applications.fee.v1.Query)

Expand Down Expand Up @@ -983,6 +985,36 @@ QueryIncentivizedPacketsResponse is the response type for the incentivized packe




<a name="ibc.applications.fee.v1.QueryTotalRecvFeesRequest"></a>

### QueryTotalRecvFeesRequest
QueryTotalRecvFeesRequest defines the request type for the TotalRecvFees rpc


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `packet_id` | [ibc.core.channel.v1.PacketId](#ibc.core.channel.v1.PacketId) | | the packet identifier for the associated fees |






<a name="ibc.applications.fee.v1.QueryTotalRecvFeesResponse"></a>

### QueryTotalRecvFeesResponse
QueryTotalRecvFeesResponse defines the response type for the TotalRecvFees rpc


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `recv_fees` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | the total packet receive fees |





<!-- end messages -->

<!-- end enums -->
Expand All @@ -999,6 +1031,7 @@ Query provides defines the gRPC querier service.
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `IncentivizedPackets` | [QueryIncentivizedPacketsRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketsRequest) | [QueryIncentivizedPacketsResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketsResponse) | Gets all incentivized packets | GET|/ibc/apps/fee/v1/incentivized_packets|
| `IncentivizedPacket` | [QueryIncentivizedPacketRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketRequest) | [QueryIncentivizedPacketResponse](#ibc.applications.fee.v1.QueryIncentivizedPacketResponse) | Gets the fees expected for submitting the ReceivePacket, AcknowledgementPacket, and TimeoutPacket messages for the given packet | GET|/ibc/apps/fee/v1/incentivized_packet/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|
| `TotalRecvFees` | [QueryTotalRecvFeesRequest](#ibc.applications.fee.v1.QueryTotalRecvFeesRequest) | [QueryTotalRecvFeesResponse](#ibc.applications.fee.v1.QueryTotalRecvFeesResponse) | TotalRecvFees returns the total receive fees for a packet given its identifier | GET|/ibc/apps/fee/v1/total_recv_fees/port/{packet_id.port_id}/channel/{packet_id.channel_id}/sequence/{packet_id.sequence}|

<!-- end services -->

Expand Down
26 changes: 26 additions & 0 deletions modules/apps/29-fee/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,29 @@ func (k Keeper) IncentivizedPacket(c context.Context, req *types.QueryIncentiviz
IncentivizedPacket: types.NewIdentifiedPacketFees(req.PacketId, feesInEscrow.PacketFees),
}, nil
}

// TotalRecvFees implements the Query/TotalRecvFees gRPC method
func (k Keeper) TotalRecvFees(goCtx context.Context, req *types.QueryTotalRecvFeesRequest) (*types.QueryTotalRecvFeesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

feesInEscrow, found := k.GetFeesInEscrow(ctx, req.PacketId)
if !found {
return nil, status.Errorf(
codes.NotFound,
sdkerrors.Wrapf(types.ErrFeeNotFound, "channel: %s, port: %s, sequence: %d", req.PacketId.ChannelId, req.PacketId.PortId, req.PacketId.Sequence).Error(),
)
}

var recvFees sdk.Coins
for _, packetFee := range feesInEscrow.PacketFees {
recvFees = recvFees.Add(packetFee.Fee.RecvFee...)
}

return &types.QueryTotalRecvFeesResponse{
RecvFees: recvFees,
}, nil
}
64 changes: 64 additions & 0 deletions modules/apps/29-fee/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,67 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPacket() {
})
}
}

func (suite *KeeperTestSuite) TestQueryTotalRecvFees() {
var (
req *types.QueryTotalRecvFeesRequest
)

testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
{
"packet not found",
func() {
req.PacketId = channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 100)
},
false,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest() // reset

suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID)

packetID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1)

fee := types.NewFee(defaultReceiveFee, defaultAckFee, defaultTimeoutFee)
packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount.GetAddress().String(), []string(nil))

for i := 0; i < 3; i++ {
// escrow three packet fees for the same packet
err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), packetID, packetFee)
suite.Require().NoError(err)
}

req = &types.QueryTotalRecvFeesRequest{
PacketId: packetID,
}

tc.malleate()

ctx := sdk.WrapSDKContext(suite.chainA.GetContext())
res, err := suite.queryClient.TotalRecvFees(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)

// expected total is three times the default recv fee
expectedFees := defaultReceiveFee.Add(defaultReceiveFee...).Add(defaultReceiveFee...)
suite.Require().Equal(expectedFees, res.RecvFees)
} else {
suite.Require().Error(err)
}
})
}
}
Loading