Skip to content

Commit

Permalink
Merge pull request #497 from bandprotocol/add-query-total-fees
Browse files Browse the repository at this point in the history
[Tunnel] Add query total fees
  • Loading branch information
RogerKSI authored Nov 28, 2024
2 parents c608cc8 + ad15b80 commit 913d8ff
Show file tree
Hide file tree
Showing 8 changed files with 1,675 additions and 277 deletions.
1,201 changes: 1,051 additions & 150 deletions api/band/tunnel/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

53 changes: 46 additions & 7 deletions api/band/tunnel/v1beta1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/band/tunnel/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ service Query {
option (google.api.http).get = "/tunnel/v1beta1/tunnels/{tunnel_id}/packets/{sequence}";
}

// TotalFees is a RPC method that returns the total fees collected by the tunnel
rpc TotalFees(QueryTotalFeesRequest) returns (QueryTotalFeesResponse) {
option (google.api.http).get = "/tunnel/v1beta1/total_fees";
}

// Params is a RPC method that returns all parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/tunnel/v1beta1/params";
Expand Down Expand Up @@ -148,6 +153,15 @@ message QueryPacketResponse {
Packet packet = 1;
}

// QueryTotalFeesRequest is the request type for the Query/TotalFees RPC method.
message QueryTotalFeesRequest {}

// QueryTotalFeesResponse is the response type for the Query/TotalFees RPC method.
message QueryTotalFeesResponse {
// total_fees is the total fees collected by the tunnel.
TotalFees total_fees = 1 [(gogoproto.nullable) = false];
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryParamsRequest {}

Expand Down
5 changes: 5 additions & 0 deletions x/tunnel/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
{ProtoField: "sequence"},
},
},
{
RpcMethod: "TotalFees",
Use: "total-fees",
Short: "Query total fees",
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Expand Down
10 changes: 10 additions & 0 deletions x/tunnel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func (q queryServer) Packet(c context.Context, req *types.QueryPacketRequest) (*
return &types.QueryPacketResponse{Packet: &packet}, nil
}

func (q queryServer) TotalFees(
c context.Context,
req *types.QueryTotalFeesRequest,
) (*types.QueryTotalFeesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

totalFees := q.k.GetTotalFees(ctx)
return &types.QueryTotalFeesResponse{TotalFees: totalFees}, nil
}

// Params queries all params of the module.
func (q queryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
Expand Down
152 changes: 90 additions & 62 deletions x/tunnel/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,82 @@ func (s *KeeperTestSuite) TestGRPCQueryTunnel() {
s.Require().Equal(tunnel, resp.Tunnel)
}

func (s *KeeperTestSuite) TestGRPCQueryDeposits() {
ctx, k, q := s.ctx, s.keeper, s.queryServer

tunnel := types.Tunnel{
ID: 1,
Sequence: 2,
}
r := types.TSSRoute{
DestinationChainID: "1",
DestinationContractAddress: "0x123",
}
err := tunnel.SetRoute(&r)
s.Require().NoError(err)
k.SetTunnel(ctx, tunnel)

deposit1 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor1")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
deposit2 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor2")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(110))),
}
k.SetDeposit(ctx, deposit1)
k.SetDeposit(ctx, deposit2)

resp, err := q.Deposits(ctx, &types.QueryDepositsRequest{
TunnelId: 1,
})
s.Require().NoError(err)
s.Require().NotNil(resp)
s.Require().Len(resp.Deposits, 2)
s.Require().Equal(deposit1, *resp.Deposits[0])
s.Require().Equal(deposit2, *resp.Deposits[1])
}

func (s *KeeperTestSuite) TestGRPCQueryDeposit() {
ctx, k, q := s.ctx, s.keeper, s.queryServer

tunnel := types.Tunnel{
ID: 1,
Sequence: 2,
}
r := types.TSSRoute{
DestinationChainID: "1",
DestinationContractAddress: "0x123",
}
err := tunnel.SetRoute(&r)
s.Require().NoError(err)
k.SetTunnel(ctx, tunnel)

deposit1 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
k.SetDeposit(ctx, deposit1)

deposit2 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
k.SetDeposit(ctx, deposit2)

resp, err := q.Deposit(ctx, &types.QueryDepositRequest{
TunnelId: 1,
Depositor: deposit1.Depositor,
})
s.Require().NoError(err)
s.Require().NotNil(resp)
s.Require().Equal(deposit1, resp.Deposit)
}

func (s *KeeperTestSuite) TestGRPCQueryPackets() {
ctx, k, q := s.ctx, s.keeper, s.queryServer

Expand Down Expand Up @@ -142,78 +218,30 @@ func (s *KeeperTestSuite) TestGRPCQueryPacket() {
s.Require().Equal(packet1, *res.Packet)
}

func (s *KeeperTestSuite) TestGRPCQueryDeposits() {
func (s *KeeperTestSuite) TestGRPCQueryTotalFees() {
ctx, k, q := s.ctx, s.keeper, s.queryServer

tunnel := types.Tunnel{
ID: 1,
Sequence: 2,
// Set total fees
totalFees := types.TotalFees{
TotalBasePacketFee: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
r := types.TSSRoute{
DestinationChainID: "1",
DestinationContractAddress: "0x123",
}
err := tunnel.SetRoute(&r)
s.Require().NoError(err)
k.SetTunnel(ctx, tunnel)
k.SetTotalFees(ctx, totalFees)

deposit1 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor1")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
deposit2 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor2")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(110))),
}
k.SetDeposit(ctx, deposit1)
k.SetDeposit(ctx, deposit2)

resp, err := q.Deposits(ctx, &types.QueryDepositsRequest{
TunnelId: 1,
})
// Query total fees
res, err := q.TotalFees(ctx, &types.QueryTotalFeesRequest{})
s.Require().NoError(err)
s.Require().NotNil(resp)
s.Require().Len(resp.Deposits, 2)
s.Require().Equal(deposit1, *resp.Deposits[0])
s.Require().Equal(deposit2, *resp.Deposits[1])
s.Require().Equal(totalFees, res.TotalFees)
}

func (s *KeeperTestSuite) TestGRPCQueryDeposit() {
func (s *KeeperTestSuite) TestGRPCQueryParams() {
ctx, k, q := s.ctx, s.keeper, s.queryServer

tunnel := types.Tunnel{
ID: 1,
Sequence: 2,
}
r := types.TSSRoute{
DestinationChainID: "1",
DestinationContractAddress: "0x123",
}
err := tunnel.SetRoute(&r)
// Set params
err := k.SetParams(ctx, types.DefaultParams())
s.Require().NoError(err)
k.SetTunnel(ctx, tunnel)

deposit1 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
k.SetDeposit(ctx, deposit1)

deposit2 := types.Deposit{
TunnelID: 1,
Depositor: sdk.AccAddress([]byte("depositor")).String(),
Amount: sdk.NewCoins(sdk.NewCoin("band", sdkmath.NewInt(100))),
}
k.SetDeposit(ctx, deposit2)

resp, err := q.Deposit(ctx, &types.QueryDepositRequest{
TunnelId: 1,
Depositor: deposit1.Depositor,
})
// Query params
res, err := q.Params(ctx, &types.QueryParamsRequest{})
s.Require().NoError(err)
s.Require().NotNil(resp)
s.Require().Equal(deposit1, resp.Deposit)
s.Require().Equal(types.DefaultParams(), res.Params)
}
Loading

0 comments on commit 913d8ff

Please sign in to comment.