Skip to content

Commit

Permalink
Added module Swap (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 authored Mar 16, 2021
1 parent d28deaf commit 5d555ee
Show file tree
Hide file tree
Showing 30 changed files with 1,224 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
tmdb "github.com/tendermint/tm-db"

"github.com/sentinel-official/hub/x/deposit"
"github.com/sentinel-official/hub/x/swap"
"github.com/sentinel-official/hub/x/vpn"
)

Expand Down Expand Up @@ -61,6 +62,7 @@ var (
staking.AppModuleBasic{},
supply.AppModuleBasic{},
upgrade.AppModuleBasic{},
swap.AppModuleBasic{},
vpn.AppModuleBasic{},
)
)
Expand Down Expand Up @@ -102,6 +104,7 @@ type App struct {
stakingKeeper staking.Keeper
supplyKeeper supply.Keeper
upgradeKeeper upgrade.Keeper
swapKeeper swap.Keeper
vpnKeeper vpn.Keeper
}

Expand All @@ -119,7 +122,7 @@ func NewApp(
keys = sdk.NewKVStoreKeys(baseapp.MainStoreKey,
auth.StoreKey, distribution.StoreKey, evidence.StoreKey, gov.StoreKey,
mint.StoreKey, params.StoreKey, slashing.StoreKey, staking.StoreKey,
supply.StoreKey, upgrade.StoreKey, vpn.StoreKey,
supply.StoreKey, upgrade.StoreKey, swap.StoreKey, vpn.StoreKey,
)
tkeys = sdk.NewTransientStoreKeys(params.TStoreKey)
)
Expand Down Expand Up @@ -149,6 +152,7 @@ func NewApp(
app.subspaces[mint.ModuleName] = app.paramsKeeper.Subspace(mint.DefaultParamspace)
app.subspaces[slashing.ModuleName] = app.paramsKeeper.Subspace(slashing.DefaultParamspace)
app.subspaces[staking.ModuleName] = app.paramsKeeper.Subspace(staking.DefaultParamspace)
app.subspaces[swap.ModuleName] = app.paramsKeeper.Subspace(swap.DefaultParamspace)

app.accountKeeper = auth.NewAccountKeeper(app.cdc,
keys[auth.StoreKey],
Expand Down Expand Up @@ -219,6 +223,10 @@ func NewApp(
app.slashingKeeper.Hooks(),
),
)
app.swapKeeper = swap.NewKeeper(cdc,
keys[swap.StoreKey],
app.subspaces[swap.ModuleName],
app.supplyKeeper)
app.vpnKeeper = vpn.NewKeeper(app.cdc,
keys[vpn.StoreKey],
app.paramsKeeper,
Expand All @@ -240,6 +248,7 @@ func NewApp(
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
supply.NewAppModule(app.supplyKeeper, app.accountKeeper),
upgrade.NewAppModule(app.upgradeKeeper),
swap.NewAppModule(app.swapKeeper),
vpn.NewAppModule(app.accountKeeper, app.vpnKeeper),
)

Expand All @@ -252,7 +261,8 @@ func NewApp(
app.mm.SetOrderInitGenesis(
auth.ModuleName, distribution.ModuleName, staking.ModuleName, bank.ModuleName,
slashing.ModuleName, gov.ModuleName, mint.ModuleName, supply.ModuleName,
crisis.ModuleName, genutil.ModuleName, evidence.ModuleName, vpn.ModuleName,
crisis.ModuleName, genutil.ModuleName, evidence.ModuleName,
swap.ModuleName, vpn.ModuleName,
)

app.mm.RegisterInvariants(&app.crisisKeeper)
Expand All @@ -268,6 +278,7 @@ func NewApp(
slashing.NewAppModule(app.slashingKeeper, app.accountKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
supply.NewAppModule(app.supplyKeeper, app.accountKeeper),
swap.NewAppModule(app.swapKeeper),
vpn.NewAppModule(app.accountKeeper, app.vpnKeeper),
)

Expand Down Expand Up @@ -320,6 +331,7 @@ func (a *App) ModuleAccountsPermissions() map[string][]string {
mint.ModuleName: {supply.Minter},
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
swap.ModuleName: {supply.Minter},
deposit.ModuleName: nil,
}
}
Expand Down
1 change: 1 addition & 0 deletions x/swap/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package swap
77 changes: 77 additions & 0 deletions x/swap/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/sentinel-official/hub/x/swap/types
// ALIASGEN: github.com/sentinel-official/hub/x/swap/keeper
// ALIASGEN: github.com/sentinel-official/hub/x/swap/querier
package swap

import (
"github.com/sentinel-official/hub/x/swap/keeper"
"github.com/sentinel-official/hub/x/swap/querier"
"github.com/sentinel-official/hub/x/swap/types"
)

const (
EthereumHashLength = types.EthereumHashLength
AttributeKeyTxHash = types.AttributeKeyTxHash
AttributeKeyAddress = types.AttributeKeyAddress
AttributeKeyAmount = types.AttributeKeyAmount
ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute
DefaultParamspace = types.DefaultParamspace
DefaultSwapEnabled = types.DefaultSwapEnabled
DefaultSwapDenom = types.DefaultSwapDenom
QuerySwap = types.QuerySwap
QuerySwaps = types.QuerySwaps
)

var (
// functions aliases
RegisterCodec = types.RegisterCodec
BytesToHash = types.BytesToHash
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
SwapKey = types.SwapKey
NewMsgSwap = types.NewMsgSwap
NewParams = types.NewParams
DefaultParams = types.DefaultParams
ParamsKeyTable = types.ParamsKeyTable
NewQuerySwapParams = types.NewQuerySwapParams
NewQuerySwapsParams = types.NewQuerySwapsParams
NewKeeper = keeper.NewKeeper
NewQuerier = querier.NewQuerier

// variable aliases
ModuleCdc = types.ModuleCdc
ErrorMarshal = types.ErrorMarshal
ErrorUnmarshal = types.ErrorUnmarshal
ErrorUnknownMsgType = types.ErrorUnknownMsgType
ErrorUnknownQueryType = types.ErrorUnknownQueryType
ErrorInvalidField = types.ErrorInvalidField
ErrorSwapIsDisabled = types.ErrorSwapIsDisabled
ErrorUnauthorized = types.ErrorUnauthorized
ErrorDuplicateSwap = types.ErrorDuplicateSwap
EventTypeSet = types.EventTypeSet
ParamsSubspace = types.ParamsSubspace
RouterKey = types.RouterKey
StoreKey = types.StoreKey
EventModuleName = types.EventModuleName
SwapKeyPrefix = types.SwapKeyPrefix
DefaultApproveBy = types.DefaultApproveBy
KeySwapEnabled = types.KeySwapEnabled
KeySwapDenom = types.KeySwapDenom
KeyApproveBy = types.KeyApproveBy
)

type (
EthereumHash = types.EthereumHash
GenesisState = types.GenesisState
MsgSwap = types.MsgSwap
Params = types.Params
QuerySwapParams = types.QuerySwapParams
QuerySwapsParams = types.QuerySwapsParams
Swap = types.Swap
Swaps = types.Swaps
Keeper = keeper.Keeper
)
38 changes: 38 additions & 0 deletions x/swap/client/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"
)

func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swap",
Short: "Querying commands for the swap module",
}

cmd.AddCommand(
flags.GetCommands(
querySwap(cdc),
querySwaps(cdc),
)...,
)

return cmd
}

func GetTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swap",
Short: "Swap module sub-commands",
}

cmd.AddCommand(
flags.PostCommands(
txSwap(cdc),
)...,
)

return cmd
}
6 changes: 6 additions & 0 deletions x/swap/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cli

const (
flagSkip = "skip"
flagLimit = "limit"
)
73 changes: 73 additions & 0 deletions x/swap/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cli

import (
"encoding/hex"
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"

"github.com/sentinel-official/hub/x/swap/client/common"
"github.com/sentinel-official/hub/x/swap/types"
)

func querySwap(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "swap",
Short: "Query a swap",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)

txHash, err := hex.DecodeString(args[0])
if err != nil {
return err
}

swap, err := common.QuerySwap(ctx, types.BytesToHash(txHash))
if err != nil {
return err
}

fmt.Println(swap)
return nil
},
}
}

func querySwaps(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swaps",
Short: "Query swaps",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)

skip, err := cmd.Flags().GetInt(flagSkip)
if err != nil {
return err
}

limit, err := cmd.Flags().GetInt(flagLimit)
if err != nil {
return err
}

swaps, err := common.QuerySwaps(ctx, skip, limit)
if err != nil {
return err
}

for _, swap := range swaps {
fmt.Printf("%s\n\n", swap)
}

return nil
},
}

cmd.Flags().Int(flagSkip, 0, "skip")
cmd.Flags().Int(flagLimit, 25, "limit")

return cmd
}
53 changes: 53 additions & 0 deletions x/swap/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cli

import (
"bufio"
"encoding/hex"
"strconv"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/spf13/cobra"

"github.com/sentinel-official/hub/x/swap/types"
)

func txSwap(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swap [txHash] [receiver] [amount]",
Short: "Swap from SENT to DVPN",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
buffer := bufio.NewReader(cmd.InOrStdin())
txb := auth.NewTxBuilderFromCLI(buffer).WithTxEncoder(utils.GetTxEncoder(cdc))
ctx := context.NewCLIContextWithInput(buffer).WithCodec(cdc)

txHash, err := hex.DecodeString(args[0])
if err != nil {
return err
}

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

amount, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return err
}

msg := types.NewMsgSwap(ctx.FromAddress, types.BytesToHash(txHash), receiver, sdk.NewInt(amount))
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(ctx, txb, []sdk.Msg{msg})
},
}

return cmd
}
57 changes: 57 additions & 0 deletions x/swap/client/common/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package common

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"

"github.com/sentinel-official/hub/x/swap/types"
)

func QuerySwap(ctx context.CLIContext, txHash types.EthereumHash) (*types.Swap, error) {
params := types.NewQuerySwapParams(txHash)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}

path := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySwap)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no swap found")
}

var swap types.Swap
if err = ctx.Codec.UnmarshalJSON(res, &swap); err != nil {
return nil, err
}

return &swap, nil
}

func QuerySwaps(ctx context.CLIContext, skip, limit int) (types.Swaps, error) {
params := types.NewQuerySwapsParams(skip, limit)
bytes, err := ctx.Codec.MarshalJSON(params)
if err != nil {
return nil, err
}

path := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySwaps)
res, _, err := ctx.QueryWithData(path, bytes)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("no swaps found")
}

var swaps types.Swaps
if err = ctx.Codec.UnmarshalJSON(res, &swaps); err != nil {
return nil, err
}

return swaps, nil
}
Loading

0 comments on commit 5d555ee

Please sign in to comment.