Skip to content

Commit

Permalink
refactor: rename UpdateBridgeToken to RegisterBridgeToken (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 8, 2025
1 parent fda5a29 commit ff94709
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 497 deletions.
550 changes: 275 additions & 275 deletions api/fx/erc20/v1/tx.pulsar.go

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions api/fx/erc20/v1/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/interface_registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@
"/fx.erc20.v1.MsgConvertCoinResponse",
"/fx.erc20.v1.MsgConvertDenom",
"/fx.erc20.v1.MsgConvertERC20",
"/fx.erc20.v1.MsgRegisterBridgeToken",
"/fx.erc20.v1.MsgRegisterBridgeTokenResponse",
"/fx.erc20.v1.MsgRegisterCoin",
"/fx.erc20.v1.MsgRegisterERC20",
"/fx.erc20.v1.MsgRegisterNativeCoin",
Expand All @@ -186,8 +188,6 @@
"/fx.erc20.v1.MsgRegisterNativeERC20Response",
"/fx.erc20.v1.MsgToggleTokenConversion",
"/fx.erc20.v1.MsgToggleTokenConversionResponse",
"/fx.erc20.v1.MsgUpdateBridgeToken",
"/fx.erc20.v1.MsgUpdateBridgeTokenResponse",
"/fx.erc20.v1.MsgUpdateDenomAlias",
"/fx.erc20.v1.MsgUpdateParams",
"/fx.erc20.v1.MsgUpdateParamsResponse",
Expand Down Expand Up @@ -406,12 +406,12 @@
"/fx.erc20.v1.MsgConvertCoin",
"/fx.erc20.v1.MsgConvertDenom",
"/fx.erc20.v1.MsgConvertERC20",
"/fx.erc20.v1.MsgRegisterBridgeToken",
"/fx.erc20.v1.MsgRegisterCoin",
"/fx.erc20.v1.MsgRegisterERC20",
"/fx.erc20.v1.MsgRegisterNativeCoin",
"/fx.erc20.v1.MsgRegisterNativeERC20",
"/fx.erc20.v1.MsgToggleTokenConversion",
"/fx.erc20.v1.MsgUpdateBridgeToken",
"/fx.erc20.v1.MsgUpdateDenomAlias",
"/fx.erc20.v1.MsgUpdateParams",
"/fx.evm.v1.MsgCallContract",
Expand Down Expand Up @@ -498,12 +498,12 @@
"/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade",
"/ethermint.evm.v1.MsgUpdateParams",
"/ethermint.feemarket.v1.MsgUpdateParams",
"/fx.erc20.v1.MsgRegisterBridgeToken",
"/fx.erc20.v1.MsgRegisterCoin",
"/fx.erc20.v1.MsgRegisterERC20",
"/fx.erc20.v1.MsgRegisterNativeCoin",
"/fx.erc20.v1.MsgRegisterNativeERC20",
"/fx.erc20.v1.MsgToggleTokenConversion",
"/fx.erc20.v1.MsgUpdateBridgeToken",
"/fx.erc20.v1.MsgUpdateDenomAlias",
"/fx.erc20.v1.MsgUpdateParams",
"/fx.evm.v1.MsgCallContract",
Expand Down
10 changes: 5 additions & 5 deletions proto/fx/erc20/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ service Msg {
rpc RegisterNativeERC20(MsgRegisterNativeERC20)
returns (MsgRegisterNativeERC20Response);

rpc UpdateBridgeToken(MsgUpdateBridgeToken)
returns (MsgUpdateBridgeTokenResponse);
rpc RegisterBridgeToken(MsgRegisterBridgeToken)
returns (MsgRegisterBridgeTokenResponse);
}

// MsgConvertCoin defines a Msg to convert a Cosmos Coin to a ERC20 token
Expand Down Expand Up @@ -116,9 +116,9 @@ message MsgRegisterNativeERC20Response {
ERC20Token erc20_token = 1 [ (gogoproto.nullable) = false ];
}

message MsgUpdateBridgeToken {
message MsgRegisterBridgeToken {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "erc20/MsgUpdateBridgeToken";
option (amino.name) = "erc20/MsgRegisterBridgeToken";

// authority is the address of the governance account.
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
Expand All @@ -131,4 +131,4 @@ message MsgUpdateBridgeToken {
bool native_token = 7;
}

message MsgUpdateBridgeTokenResponse {}
message MsgRegisterBridgeTokenResponse {}
70 changes: 0 additions & 70 deletions x/erc20/keeper/erc20_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,16 @@ package keeper

import (
"context"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"

fxtypes "github.com/pundiai/fx-core/v8/types"
"github.com/pundiai/fx-core/v8/x/erc20/types"
)

func (k Keeper) RegisterNativeCoin(ctx context.Context, name, symbol string, decimals uint8) (types.ERC20Token, error) {
if err := k.CheckEnableErc20(ctx); err != nil {
return types.ERC20Token{}, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
erc20Addr, err := k.DeployUpgradableToken(sdkCtx, k.contractOwner, name, symbol, decimals)
if err != nil {
return types.ERC20Token{}, err
}

erc20Token, err := k.AddERC20Token(ctx, name, symbol, decimals, erc20Addr, types.OWNER_MODULE)
if err != nil {
return types.ERC20Token{}, err
}

sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterCoin,
sdk.NewAttribute(types.AttributeKeyDenom, erc20Token.Denom),
sdk.NewAttribute(types.AttributeKeyTokenAddress, erc20Token.Erc20Address),
))
return erc20Token, nil
}

func (k Keeper) RegisterNativeERC20(ctx context.Context, erc20Addr common.Address) (types.ERC20Token, error) {
if err := k.CheckEnableErc20(ctx); err != nil {
return types.ERC20Token{}, err
}

name, symbol, decimals, err := k.ERC20BaseInfo(ctx, erc20Addr)
if err != nil {
return types.ERC20Token{}, err
}

erc20Token, err := k.AddERC20Token(ctx, name, symbol, decimals, erc20Addr, types.OWNER_EXTERNAL)
if err != nil {
return types.ERC20Token{}, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterERC20,
sdk.NewAttribute(types.AttributeKeyDenom, erc20Token.Denom),
sdk.NewAttribute(types.AttributeKeyTokenAddress, erc20Token.Erc20Address),
))

return erc20Token, nil
}

func (k Keeper) AddERC20Token(ctx context.Context, name, symbol string, decimals uint8, erc20Addr common.Address, contractOwner types.Owner) (types.ERC20Token, error) {
var metadata banktypes.Metadata
if symbol == fxtypes.DefaultDenom {
Expand Down Expand Up @@ -124,22 +73,3 @@ func (k Keeper) ToggleTokenConvert(ctx context.Context, token string) (types.ERC
))
return erc20Token, nil
}

func (k Keeper) UpdateBridgeToken(ctx context.Context, baseDenom, channel, ibcDenom, chainName, contractAddr string, isNative bool) (types.ERC20Token, error) {
erc20Token, err := k.ERC20Token.Get(ctx, baseDenom)
if err != nil {
return types.ERC20Token{}, sdkerrors.ErrNotFound.Wrapf("token %s not found", baseDenom)
}

// add ibc token
isIBCDenom := strings.HasPrefix(ibcDenom, ibctransfertypes.DenomPrefix+"/")
if isIBCDenom {
return erc20Token, k.AddIBCToken(ctx, channel, baseDenom, ibcDenom)
}

// add bridge token
if !fxtypes.IsSupportChain(chainName) {
return types.ERC20Token{}, sdkerrors.ErrKeyNotFound.Wrapf("chain name %s not found", chainName)
}
return erc20Token, k.AddBridgeToken(ctx, baseDenom, chainName, contractAddr, isNative)
}
6 changes: 3 additions & 3 deletions x/erc20/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (s msgServer) RegisterNativeERC20(c context.Context, req *types.MsgRegister
return &types.MsgRegisterNativeERC20Response{Erc20Token: erc20Token}, nil
}

func (s msgServer) UpdateBridgeToken(c context.Context, req *types.MsgUpdateBridgeToken) (*types.MsgUpdateBridgeTokenResponse, error) {
func (s msgServer) RegisterBridgeToken(c context.Context, req *types.MsgRegisterBridgeToken) (*types.MsgRegisterBridgeTokenResponse, error) {
if s.k.authority != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", s.k.authority, req.Authority)
}

_, err := s.k.UpdateBridgeToken(c, req.BaseDenom, req.Channel, req.IbcDenom,
_, err := s.k.RegisterBridgeToken(c, req.BaseDenom, req.Channel, req.IbcDenom,
req.ChainName, req.ContractAddress, req.NativeToken)
return &types.MsgUpdateBridgeTokenResponse{}, err
return &types.MsgRegisterBridgeTokenResponse{}, err
}
82 changes: 82 additions & 0 deletions x/erc20/keeper/proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package keeper

import (
"context"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"

fxtypes "github.com/pundiai/fx-core/v8/types"
"github.com/pundiai/fx-core/v8/x/erc20/types"
)

func (k Keeper) RegisterNativeCoin(ctx context.Context, name, symbol string, decimals uint8) (types.ERC20Token, error) {
if err := k.CheckEnableErc20(ctx); err != nil {
return types.ERC20Token{}, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
erc20Addr, err := k.DeployUpgradableToken(sdkCtx, k.contractOwner, name, symbol, decimals)
if err != nil {
return types.ERC20Token{}, err
}

erc20Token, err := k.AddERC20Token(ctx, name, symbol, decimals, erc20Addr, types.OWNER_MODULE)
if err != nil {
return types.ERC20Token{}, err
}

sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterCoin,
sdk.NewAttribute(types.AttributeKeyDenom, erc20Token.Denom),
sdk.NewAttribute(types.AttributeKeyTokenAddress, erc20Token.Erc20Address),
))
return erc20Token, nil
}

func (k Keeper) RegisterNativeERC20(ctx context.Context, erc20Addr common.Address) (types.ERC20Token, error) {
if err := k.CheckEnableErc20(ctx); err != nil {
return types.ERC20Token{}, err
}

name, symbol, decimals, err := k.ERC20BaseInfo(ctx, erc20Addr)
if err != nil {
return types.ERC20Token{}, err
}

erc20Token, err := k.AddERC20Token(ctx, name, symbol, decimals, erc20Addr, types.OWNER_EXTERNAL)
if err != nil {
return types.ERC20Token{}, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterERC20,
sdk.NewAttribute(types.AttributeKeyDenom, erc20Token.Denom),
sdk.NewAttribute(types.AttributeKeyTokenAddress, erc20Token.Erc20Address),
))

return erc20Token, nil
}

func (k Keeper) RegisterBridgeToken(ctx context.Context, baseDenom, channel, ibcDenom, chainName, contractAddr string, isNative bool) (types.ERC20Token, error) {
erc20Token, err := k.ERC20Token.Get(ctx, baseDenom)
if err != nil {
return types.ERC20Token{}, sdkerrors.ErrNotFound.Wrapf("token %s not found", baseDenom)
}

// add ibc token
isIBCDenom := strings.HasPrefix(ibcDenom, ibctransfertypes.DenomPrefix+"/")
if isIBCDenom {
return erc20Token, k.AddIBCToken(ctx, channel, baseDenom, ibcDenom)
}

// add bridge token
if !fxtypes.IsSupportChain(chainName) {
return types.ERC20Token{}, sdkerrors.ErrKeyNotFound.Wrapf("chain name %s not found", chainName)
}
return erc20Token, k.AddBridgeToken(ctx, baseDenom, chainName, contractAddr, isNative)
}
4 changes: 2 additions & 2 deletions x/erc20/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&MsgUpdateDenomAlias{},
&MsgRegisterNativeCoin{},
&MsgRegisterNativeERC20{},
&MsgUpdateBridgeToken{},
&MsgRegisterBridgeToken{},
)
registry.RegisterImplementations(
(*govv1betal.Content)(nil),
Expand All @@ -49,7 +49,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgUpdateDenomAlias{}, fmt.Sprintf("%s/%s", ModuleName, "MsgUpdateDenomAlias"), nil)
cdc.RegisterConcrete(&MsgRegisterNativeCoin{}, fmt.Sprintf("%s/%s", ModuleName, "MsgRegisterNativeCoin"), nil)
cdc.RegisterConcrete(&MsgRegisterNativeERC20{}, fmt.Sprintf("%s/%s", ModuleName, "MsgRegisterNativeERC20"), nil)
cdc.RegisterConcrete(&MsgUpdateBridgeToken{}, fmt.Sprintf("%s/%s", ModuleName, "MsgUpdateBridgeToken"), nil)
cdc.RegisterConcrete(&MsgRegisterBridgeToken{}, fmt.Sprintf("%s/%s", ModuleName, "MsgRegisterBridgeToken"), nil)

cdc.RegisterConcrete(&RegisterCoinProposal{}, fmt.Sprintf("%s/%s", ModuleName, "RegisterCoinProposal"), nil)
cdc.RegisterConcrete(&RegisterERC20Proposal{}, fmt.Sprintf("%s/%s", ModuleName, "RegisterERC20Proposal"), nil)
Expand Down
4 changes: 2 additions & 2 deletions x/erc20/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
_ sdk.Msg = &MsgUpdateDenomAlias{}
_ sdk.Msg = &MsgRegisterNativeCoin{}
_ sdk.Msg = &MsgRegisterNativeERC20{}
_ sdk.Msg = &MsgUpdateBridgeToken{}
_ sdk.Msg = &MsgRegisterBridgeToken{}
)

func NewMsgConvertCoin(coin sdk.Coin, receiver common.Address, sender sdk.AccAddress) *MsgConvertCoin {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (m *MsgRegisterNativeERC20) ValidateBasic() error {
return nil
}

func (m *MsgUpdateBridgeToken) ValidateBasic() error {
func (m *MsgRegisterBridgeToken) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("authority address: %s", err.Error())
}
Expand Down
Loading

0 comments on commit ff94709

Please sign in to comment.