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

Feature/erc20zone/client #12

Open
wants to merge 3 commits into
base: develop
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
47 changes: 47 additions & 0 deletions erc20zone/x/erc20service/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cli

import (
"fmt"

"github.com/DE-labtory/erc20peggy/erc20zone/x/erc20service"

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

func GetCmdToken(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "token [address]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
addr := args[0]

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/token/%s", queryRoute, addr), nil)
if err != nil {
return err
}

out := erc20service.QueryResToken{}
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
177 changes: 177 additions & 0 deletions erc20zone/x/erc20service/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cli

import (
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/spf13/cobra"

"github.com/DE-labtory/erc20peggy/erc20zone/x/erc20service"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
)

func GetCmdMint(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "mint [address] [balance]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

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

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

msg := erc20service.NewMsgMint(addr, balance)
if err := msg.ValidateBasic(); err != nil {
return err
}

cliCtx.PrintResponse = true

return utils.GenerateOrBroadcastMsgs(
cliCtx,
authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)),
[]sdk.Msg{msg},
false,
)
},
}
}

func GetCmdBurn(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "burn [address] [balance]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

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

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

msg := erc20service.NewMsgBurn(addr, balance)
if err := msg.ValidateBasic(); err != nil {
return err
}

cliCtx.PrintResponse = true

return utils.GenerateOrBroadcastMsgs(
cliCtx,
authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)),
[]sdk.Msg{msg},
false,
)
},
}
}

func GetCmdLock(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "lock [address] [balance]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

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

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

msg := erc20service.NewMsgLock(addr, balance)
if err := msg.ValidateBasic(); err != nil {
return err
}

cliCtx.PrintResponse = true

return utils.GenerateOrBroadcastMsgs(
cliCtx,
authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)),
[]sdk.Msg{msg},
false,
)
},
}
}

func GetCmdUnlock(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "unlock [address] [balance]",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc)
if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

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

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

msg := erc20service.NewMsgUnlock(addr, balance)
if err := msg.ValidateBasic(); err != nil {
return err
}

cliCtx.PrintResponse = true

return utils.GenerateOrBroadcastMsgs(
cliCtx,
authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)),
[]sdk.Msg{msg},
false,
)
},
}
}
65 changes: 65 additions & 0 deletions erc20zone/x/erc20service/client/module_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 DE-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package client

import (
erc20servicecmd "github.com/DE-labtory/erc20peggy/erc20zone/x/erc20service/client/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
amino "github.com/tendermint/go-amino"
)

type ModuleClient struct {
accStoreKey string
lockStoreKey string
cdc *amino.Codec
}

func NewModuleClient(accStoreKey string, lockStoreKey string, cdc *amino.Codec) ModuleClient {
return ModuleClient{
accStoreKey: accStoreKey,
lockStoreKey: lockStoreKey,
cdc: cdc,
}
}

func (c ModuleClient) GetQueryCmd() *cobra.Command {
erc20svcQueryCmd := &cobra.Command{
Use: "erc20service",
}

erc20svcQueryCmd.AddCommand(client.GetCommands(
erc20servicecmd.GetCmdToken(c.accStoreKey, c.cdc),
)...)

return erc20svcQueryCmd
}

func (c ModuleClient) GetTxCmd() *cobra.Command {
erc20svcTxCmd := &cobra.Command{
Use: "erc20service",
}

erc20svcTxCmd.AddCommand(client.PostCommands(
erc20servicecmd.GetCmdMint(c.cdc),
erc20servicecmd.GetCmdBurn(c.cdc),
erc20servicecmd.GetCmdLock(c.cdc),
erc20servicecmd.GetCmdUnlock(c.cdc),
)...)

return erc20svcTxCmd
}
Loading