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

IBC Router Middleware #373

Closed
wants to merge 7 commits into from
Closed
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
107 changes: 107 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

## Table of Contents

- [ibc/applications/router/v1/genesis.proto](#ibc/applications/router/v1/genesis.proto)
- [GenesisState](#ibc.applications.router.v1.GenesisState)
- [Params](#ibc.applications.router.v1.Params)

- [ibc/applications/router/v1/query.proto](#ibc/applications/router/v1/query.proto)
- [QueryParamsRequest](#ibc.applications.router.v1.QueryParamsRequest)
- [QueryParamsResponse](#ibc.applications.router.v1.QueryParamsResponse)

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

- [ibc/applications/transfer/v1/transfer.proto](#ibc/applications/transfer/v1/transfer.proto)
- [DenomTrace](#ibc.applications.transfer.v1.DenomTrace)
- [FungibleTokenPacketData](#ibc.applications.transfer.v1.FungibleTokenPacketData)
Expand Down Expand Up @@ -246,6 +256,103 @@



<a name="ibc/applications/router/v1/genesis.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## ibc/applications/router/v1/genesis.proto



<a name="ibc.applications.router.v1.GenesisState"></a>

### GenesisState
GenesisState defines the router genesis state


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#ibc.applications.router.v1.Params) | | |






<a name="ibc.applications.router.v1.Params"></a>

### Params
Params defines the set of IBC router parameters.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `fee_percentage` | [string](#string) | | |





<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->

<!-- end services -->



<a name="ibc/applications/router/v1/query.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## ibc/applications/router/v1/query.proto



<a name="ibc.applications.router.v1.QueryParamsRequest"></a>

### QueryParamsRequest
QueryParamsRequest is the request type for the Query/Params RPC method.






<a name="ibc.applications.router.v1.QueryParamsResponse"></a>

### QueryParamsResponse
QueryParamsResponse is the response type for the Query/Params RPC method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#ibc.applications.router.v1.Params) | | params defines the parameters of the module. |





<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->


<a name="ibc.applications.router.v1.Query"></a>

### Query
Query provides defines the gRPC querier service.

| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `Params` | [QueryParamsRequest](#ibc.applications.router.v1.QueryParamsRequest) | [QueryParamsResponse](#ibc.applications.router.v1.QueryParamsResponse) | Params queries all parameters of the router module. | GET|/ibc/apps/router/v1/params|

<!-- end services -->



<a name="ibc/applications/transfer/v1/transfer.proto"></a>
<p align="right"><a href="#top">Top</a></p>

Expand Down
59 changes: 59 additions & 0 deletions modules/apps/router/client/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/ibc-go/modules/apps/router/types"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the query commands for router
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: "ibc-router",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
}

queryCmd.AddCommand(
GetCmdParams(),
)

return queryCmd
}

// GetCmdParams returns the command handler for ibc-router parameter querying.
func GetCmdParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current ibc-router parameters",
Long: "Query the current ibc-router parameters",
Args: cobra.NoArgs,
Example: fmt.Sprintf("%s query ibc-router params", version.AppName),
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res.Params)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// NewTxCmd returns the transaction commands for router
func NewTxCmd() *cobra.Command {
return nil
}
16 changes: 16 additions & 0 deletions modules/apps/router/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/router/types"
)

// InitGenesis
func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
k.SetParams(ctx, state.Params)
}

// ExportGenesis
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{Params: k.GetParams(ctx)}
}
19 changes: 19 additions & 0 deletions modules/apps/router/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/router/types"
)

var _ types.QueryServer = Keeper{}

func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)

return &types.QueryParamsResponse{
Params: &params,
}, nil
}
82 changes: 82 additions & 0 deletions modules/apps/router/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keeper

import (
"time"

"github.com/armon/go-metrics"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ibc-go/modules/apps/router/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
coretypes "github.com/cosmos/ibc-go/modules/core/types"
)

// Keeper defines the IBC fungible transfer keeper
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace

transferKeeper types.TransferKeeper
distrKeeper types.DistributionKeeper
}

// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
transferKeeper types.TransferKeeper, distrKeeper types.DistributionKeeper,
) Keeper {

return Keeper{
cdc: cdc,
storeKey: key,
transferKeeper: transferKeeper,
paramSpace: paramSpace,
distrKeeper: distrKeeper,
}
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName)
}

func (k Keeper) ForwardTransferPacket(ctx sdk.Context, receiver sdk.AccAddress, token sdk.Coin, port, channel, finalDest string, labels []metrics.Label) error {
feeAmount := token.Amount.ToDec().Mul(k.GetFeePercentage(ctx)).RoundInt()
packetAmount := token.Amount.Sub(feeAmount)
feeCoins := sdk.Coins{sdk.NewCoin(token.Denom, feeAmount)}
packetCoin := sdk.NewCoin(token.Denom, packetAmount)

// pay fees
if feeAmount.IsPositive() {
if err := k.distrKeeper.FundCommunityPool(ctx, feeCoins, receiver); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
}

// send tokens to destination
if err := k.transferKeeper.SendTransfer(ctx, port, channel, packetCoin, receiver, finalDest, clienttypes.Height{0, 0}, uint64(ctx.BlockTime().Add(30*time.Minute).UnixNano())); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}

defer func() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "ibc", "transfer"},
float32(token.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, token.Denom)},
)

telemetry.IncrCounterWithLabels(
[]string{"ibc", types.ModuleName, "send"},
1,
labels,
)
}()
return nil
}
23 changes: 23 additions & 0 deletions modules/apps/router/keeper/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/router/types"
)

// GetSendEnabled retrieves the send enabled boolean from the paramstore
func (k Keeper) GetFeePercentage(ctx sdk.Context) sdk.Dec {
var res sdk.Dec
k.paramSpace.Get(ctx, types.KeyFeePercentage, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetFeePercentage(ctx))
}

// SetParams sets the total set of ibc-transfer parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
Loading