Skip to content

Commit

Permalink
refactor: Enhance the basic validation for MsgDeployFungibleCoinZRC20 (
Browse files Browse the repository at this point in the history
…#1310)

* add the basic validation for MsgDeployFungibleCoinZRC20

* modify erc20 decimals limit
  • Loading branch information
lukema95 authored Oct 26, 2023
1 parent 0c3d90f commit f9d9987
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func (k msgServer) DeployFungibleCoinZRC20(goCtx context.Context, msg *types.Msg
var address common.Address
var err error

if err = msg.ValidateBasic(); err != nil {
return nil, err
}

if msg.Creator != k.observerKeeper.GetParams(ctx).GetAdminPolicyAccount(zetaObserverTypes.Policy_Type_group2) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Deploy can only be executed by the correct policy account")
}
if msg.Decimals > 255 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "decimals must be less than 256")
}

if msg.CoinType == zetacommon.CoinType_Gas {
// #nosec G701 always in range
address, err = k.SetupChainGasCoinAndPool(ctx, msg.ForeignChainId, msg.Name, msg.Symbol, uint8(msg.Decimals), big.NewInt(msg.GasLimit))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestMsgServer_DeployFungibleCoinZRC20(t *testing.T) {
admin,
sample.EthAddress().Hex(),
chainID,
256,
78,
"foo",
"foo",
common.CoinType_Gas,
Expand Down
6 changes: 5 additions & 1 deletion x/fungible/types/message_deploy_fungible_coin_zrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func (msg *MsgDeployFungibleCoinZRC20) ValidateBasic() error {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if msg.GasLimit < 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidGasLimit, "invalid gas limit (%s)", err)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidGasLimit, "invalid gas limit")
}

if msg.Decimals > 77 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "decimals must be less than 78")
}
return nil
}
8 changes: 8 additions & 0 deletions x/fungible/types/message_deploy_fungible_coin_zrc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func TestMsgDeployFungibleCoinZRC4_ValidateBasic(t *testing.T) {
},
err: sdkerrors.ErrInvalidGasLimit,
},
{
name: "invalid decimals",
msg: types.MsgDeployFungibleCoinZRC20{
Creator: sample.AccAddress(),
Decimals: 78,
},
err: sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "decimals must be less than 78"),
},
{
name: "valid message",
msg: types.MsgDeployFungibleCoinZRC20{
Expand Down

0 comments on commit f9d9987

Please sign in to comment.