Skip to content

Commit

Permalink
Merge pull request #4 from InjectiveLabs/bridge
Browse files Browse the repository at this point in the history
erc20bridge modifications
  • Loading branch information
albertchon authored Jan 28, 2021
2 parents 0715b8b + 59b5114 commit c18dc29
Show file tree
Hide file tree
Showing 25 changed files with 5,194 additions and 53 deletions.
89 changes: 89 additions & 0 deletions chain/erc20bridge/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cli

import (
"context"

"github.com/InjectiveLabs/sdk-go/chain/erc20bridge/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the parent command for all modules/bank CLi query commands.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the erc20bridge module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
GetBridgesCmd(),
GetHubParamsCmd(),
)
return cmd
}

// GetBridgesCmd queries a bridges registered
func GetBridgesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bridges",
Short: "Gets bridges registered",
Long: "Gets bridges registered.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryBridgesRequest{}

res, err := queryClient.Bridges(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetHubParamsCmd queries hub info
func GetHubParamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "hubparams",
Short: "Gets hub info.",
Long: "Gets hub info.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryHubParamsRequest{}

res, err := queryClient.HubParams(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
38 changes: 38 additions & 0 deletions chain/erc20bridge/types/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

var (
// ModuleCdc references the global erc20bridge module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding.
//
// The actual codec used for serialization should be provided to modules/erc20bridge and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
)

// RegisterLegacyAminoCodec registers concrete types on the LegacyAmino codec
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(TokenMapping{}, "cosmos-sdk/TokenMapping", nil)
cdc.RegisterConcrete(&RegisterTokenMappingProposal{}, "cosmos-sdk/RegisterTokenMappingProposal", nil)
cdc.RegisterConcrete(&ResetHubProposal{}, "cosmos-sdk/ResetHubProposal", nil)
}

// RegisterInterfaces register implementations
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgERC20BridgeMint{},
&MsgInitHub{},
)
registry.RegisterImplementations(
(*govtypes.Content)(nil),
&RegisterTokenMappingProposal{},
&ResetHubProposal{},
)
}
16 changes: 16 additions & 0 deletions chain/erc20bridge/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// errors
var (
ErrInvalidErc20Address = sdkerrors.Register(ModuleName, 1, "invalid erc20 address")
ErrUnmatchingCosmosDenom = sdkerrors.Register(ModuleName, 2, "unmatching cosmos denom")
ErrNotAllowedBridge = sdkerrors.Register(ModuleName, 3, "not allowed bridge")
ErrInternalEthMinting = sdkerrors.Register(ModuleName, 4, "internal ethereum minting error")
ErrInitHubABI = sdkerrors.Register(ModuleName, 5, "init hub abi error")
ErrWritingEthTxPayload = sdkerrors.Register(ModuleName, 6, "writing ethereum tx payload error")
ErrHubAlreadySet = sdkerrors.Register(ModuleName, 7, "hub already set")
)
26 changes: 26 additions & 0 deletions chain/erc20bridge/types/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

// constants
const (
// module name
ModuleName = "erc20bridge"

// StoreKey to be used when creating the KVStore
StoreKey = ModuleName

// RouterKey to be used for message routing
RouterKey = ModuleName

// events
EventTypeTemplateCreation = "template_creation"
EventTypeBurn = "burn"

AttributeKeyAddress = "address"
AttributeKeyProposedCosmosCoin = "propsed_cosmos_coin"
AttributeKeyContractAddress = "burn_contract_address"
AttributeKeyBurnEthAddress = "burn_eth_address"
AttributeKeyRecipientCosmosAddress = "recipient_cosmos_address"
AttributeKeyAmount = "amount"

TypeMsgMint = "mint_erc20"
)
38 changes: 38 additions & 0 deletions chain/erc20bridge/types/msg_inithub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
_ sdk.Msg = &MsgInitHub{}
)

// NewMsgInitHub returns init hub msg instance
func NewMsgInitHub(hub string, proposer sdk.AccAddress) *MsgInitHub {
return &MsgInitHub{
HubAddress: hub,
Proposer: proposer,
}
}

// Route should return the name of the module
func (msg MsgInitHub) Route() string { return RouterKey }

// Type should return the action
func (msg MsgInitHub) Type() string { return "inithub" }

// ValidateBasic runs stateless checks on the message
func (msg MsgInitHub) ValidateBasic() error {
return nil
}

// GetSignBytes encodes the message for signing
func (msg *MsgInitHub) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}

// GetSigners defines whose signature is required
func (msg MsgInitHub) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Proposer}
}
60 changes: 60 additions & 0 deletions chain/erc20bridge/types/msg_mint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package types

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)

var (
_ sdk.Msg = &MsgERC20BridgeMint{}
)

// NewMsgERC20BridgeMint returns init hub msg instance
func NewMsgERC20BridgeMint(mapping string, amount string, address string, proposer sdk.AccAddress) *MsgERC20BridgeMint {
return &MsgERC20BridgeMint{
MappingId: mapping,
Amount: amount,
Address: address,
Proposer: proposer,
}
}

// Route should return the name of the module
func (msg MsgERC20BridgeMint) Route() string { return RouterKey }

// Type should return the action
func (msg MsgERC20BridgeMint) Type() string { return TypeMsgMint }

// ValidateBasic runs stateless checks on the message
func (msg MsgERC20BridgeMint) ValidateBasic() error {
coins, err := sdk.ParseCoinsNormalized(msg.Amount)
if err != nil {
return err
}
if len(coins) > 1 || coins.Empty() {
return fmt.Errorf("invalid coins field: %s", coins.String())
}
if msg.Proposer.Empty() {
return errors.New("empty proposer")
}
if msg.MappingId == "" {
return errors.New("empty mapping id")
}
if !common.IsHexAddress(msg.Address) {
return errors.New("invalid eth address")
}
return nil
}

// GetSignBytes encodes the message for signing
func (msg *MsgERC20BridgeMint) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}

// GetSigners defines whose signature is required
func (msg MsgERC20BridgeMint) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Proposer}
}
49 changes: 49 additions & 0 deletions chain/erc20bridge/types/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package types

import (
fmt "fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/ethereum/go-ethereum/common"
)

// Parameter store key
var (
ParamStoreKeyHubParams = []byte("hubparams")
// DefaultGasLimit is gas limit we use for erc20bridge internal ethereum transactions
DefaultGasLimit = uint64(100_000_000) // 100M
)

// ParamKeyTable - Key declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable(
paramtypes.NewParamSetPair(ParamStoreKeyHubParams, HubParams{}, validateHubParams),
)
}

// NewParams creates a new Params object
func NewParams(hp HubParams) Params {
return Params{
HubParams: hp,
}
}

func validateHubParams(i interface{}) error {
v, ok := i.(HubParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if len(v.HubAddress) != common.AddressLength {
return fmt.Errorf("invalid hub address: %s", v.HubAddress)
}

return nil
}

// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyHubParams, &p.HubParams, validateHubParams),
}
}
Loading

0 comments on commit c18dc29

Please sign in to comment.