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

Onion module #73

Open
wants to merge 16 commits into
base: next
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ import (
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

onion "github.com/Team-Kujira/core/x/onion"
onionkeeper "github.com/Team-Kujira/core/x/onion/keeper"
oniontypes "github.com/Team-Kujira/core/x/onion/types"
ibcwasm "github.com/cosmos/ibc-go/modules/light-clients/08-wasm"
ibcwasmkeeper "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper"
ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
Expand Down Expand Up @@ -226,6 +229,7 @@ var (
oracle.AppModuleBasic{},
alliancemodule.AppModuleBasic{},
cwica.AppModuleBasic{},
onion.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -295,6 +299,7 @@ type App struct {
ConsensusParamsKeeper consensusparamkeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
OnionKeeper *onionkeeper.Keeper
IBCFeeKeeper ibcfeekeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
Expand Down Expand Up @@ -383,6 +388,7 @@ func New(
batchtypes.StoreKey,
AllianceStoreKey,
cwicatypes.StoreKey,
oniontypes.StoreKey,
)

tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -546,6 +552,14 @@ func New(
scopedIBCKeeper,
)

app.OnionKeeper = onionkeeper.NewKeeper(
keys[oniontypes.StoreKey],
app.GetSubspace(oniontypes.ModuleName),
app.MsgServiceRouter(),
app.AccountKeeper,
txConfig.SignModeHandler(),
)

// IBC Fee Module keeper

app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
Expand Down Expand Up @@ -762,6 +776,7 @@ func New(
transferStack = transfer.NewIBCModule(app.TransferKeeper)
transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper)
transferStack = cwica.NewIBCMiddleware(transferStack, app.CwICAKeeper, app.IBCKeeper.ChannelKeeper)
transferStack = onion.NewIBCModule(transferStack, app.OnionKeeper, encodingConfig.TxConfig)

// Create Interchain Accounts Stack
// SendPacket, since it is originating from the application to core IBC:
Expand Down Expand Up @@ -965,6 +980,8 @@ func New(
),

ibcwasm.NewAppModule(app.WasmClientKeeper),

onion.NewAppModule(*app.OnionKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -1002,6 +1019,7 @@ func New(
alliancemoduletypes.ModuleName,
cwicatypes.ModuleName,
ibcwasmtypes.ModuleName,
oniontypes.ModuleName,
)

app.ModuleManager.SetOrderEndBlockers(
Expand Down Expand Up @@ -1035,6 +1053,7 @@ func New(
alliancemoduletypes.ModuleName,
cwicatypes.ModuleName,
ibcwasmtypes.ModuleName,
oniontypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -1075,6 +1094,7 @@ func New(
alliancemoduletypes.ModuleName,
wasmtypes.ModuleName,
cwicatypes.ModuleName,
oniontypes.ModuleName,
)

app.ModuleManager.RegisterInvariants(app.CrisisKeeper)
Expand Down Expand Up @@ -1328,6 +1348,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(oracletypes.ModuleName)
paramsKeeper.Subspace(batchtypes.ModuleName)
paramsKeeper.Subspace(alliancemoduletypes.ModuleName)
paramsKeeper.Subspace(oniontypes.ModuleName)

return paramsKeeper
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ require (
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/terra-money/alliance v0.3.5
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -156,7 +158,6 @@ require (
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand Down Expand Up @@ -187,7 +188,6 @@ require (
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
Expand Down
18 changes: 18 additions & 0 deletions proto/kujira/onion/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
package kujira.onion;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "kujira/onion/params.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

message GenesisState {
Params params = 1 [ (gogoproto.nullable) = false ];
repeated OnionSequence sequences = 2 [ (gogoproto.nullable) = false ];
}

message OnionSequence {
string address = 1;
uint64 sequence = 2;
}
11 changes: 11 additions & 0 deletions proto/kujira/onion/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package kujira.onion;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

message Params {
}
20 changes: 20 additions & 0 deletions proto/kujira/onion/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package kujira.onion;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "kujira/onion/genesis.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

// Query defines the gRPC querier service.
service Query {
rpc Sequence(QuerySequenceRequest) returns (QuerySequenceResponse) {
option (google.api.http).get = "/kujira/onion/sequence/{address}";
}
}

message QuerySequenceRequest { string address = 1; }
message QuerySequenceResponse {
OnionSequence seq = 1 [ (gogoproto.nullable) = false ];
}
10 changes: 10 additions & 0 deletions proto/kujira/onion/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package kujira.onion;

import "gogoproto/gogo.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

// Msg defines the Msg service.
service Msg {
}
68 changes: 68 additions & 0 deletions x/onion/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cli

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"github.com/Team-Kujira/core/x/onion/types"
)

func indexRunCmd(cmd *cobra.Command, _ []string) error {
usageTemplate := `Usage:{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}

{{if .HasAvailableSubCommands}}Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
cmd.SetUsageTemplate(usageTemplate)
return cmd.Help()
}

// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: indexRunCmd,
}

cmd.AddCommand(
GetCmdQuerySequence(),
)
return cmd
}

// GetCmdQuerySequence implements the query sequence command.
func GetCmdQuerySequence() *cobra.Command {
cmd := &cobra.Command{
Use: "sequence [address]",
Args: cobra.ExactArgs(1),
Short: "Query the onion sequence of an address",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Sequence(context.Background(), &types.QuerySequenceRequest{
Address: args[0],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
138 changes: 138 additions & 0 deletions x/onion/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package cli

import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/Team-Kujira/core/x/onion/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdktx "github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// NewTxCmd returns a root CLI command handler for all x/bank transaction commands.
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Onion transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(
NewSendTxCmd(),
)

return txCmd
}

// NewSendTxCmd returns a CLI command handler for creating a MsgSend transaction.
func NewSendTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [to_address] [amount]",
Short: "Send funds from one account to another.",
Long: `Send funds from one account to another.
Note, the '--from' flag is ignored as it is implied from [from_key_or_address].
When using '--dry-run' a key name cannot be used, only a bech32 address.
`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

toAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

coins, err := sdk.ParseCoinsNormalized(args[2])
if err != nil {
return err
}

msg := banktypes.NewMsgSend(clientCtx.GetFromAddress(), toAddr, coins)

return WriteBase64Tx(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func WriteBase64Tx(clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk.Msg) error {
txf, err := sdktx.NewFactoryCLI(clientCtx, flagSet)
if err != nil {
return err
}

txf, err = txf.Prepare(clientCtx)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
newSeq := uint64(0)
res, err := queryClient.Sequence(context.Background(), &types.QuerySequenceRequest{
Address: clientCtx.GetFromAddress().String(),
})
if err == nil {
newSeq = res.Seq.Sequence
}
txf = txf.WithSequence(newSeq)
txf = txf.WithAccountNumber(types.AccountNumber)

if txf.SimulateAndExecute() || clientCtx.Simulate {
if clientCtx.Offline {
return errors.New("cannot estimate gas in offline mode")
}

_, adjusted, err := sdktx.CalculateGas(clientCtx, txf, msgs...)
if err != nil {
return err
}

txf = txf.WithGas(adjusted)
_, _ = fmt.Fprintf(os.Stderr, "%s\n", sdktx.GasEstimateResponse{GasEstimate: txf.Gas()})
}

if clientCtx.Simulate {
return nil
}

tx, err := txf.BuildUnsignedTx(msgs...)
if err != nil {
return err
}

err = sdktx.Sign(txf, clientCtx.GetFromName(), tx, true)
if err != nil {
return err
}

txBytes, err := clientCtx.TxConfig.TxEncoder()(tx.GetTx())
if err != nil {
return err
}

txBase64Encoding := base64.StdEncoding.EncodeToString(txBytes)
fmt.Println(txBase64Encoding)
return nil
}
Loading
Loading