Skip to content

Commit

Permalink
harbor burn tx
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsingh33 committed Mar 14, 2024
1 parent a965d81 commit e85f946
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 25 deletions.
14 changes: 12 additions & 2 deletions proto/comdex/tokenmint/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax = "proto3";
package comdex.tokenmint.v1beta1;
import "gogoproto/gogo.proto";

import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/comdex-official/comdex/x/tokenmint/types";
option (gogoproto.equal_all) = false;
Expand All @@ -10,6 +10,7 @@ option (gogoproto.goproto_getters_all) = false;
service Msg {

rpc MsgMintNewTokens(MsgMintNewTokensRequest) returns (MsgMintNewTokensResponse);
rpc MsgBurnHarborTokens(MsgBurnHarborTokensRequest) returns (MsgBurnHarborTokensResponse);
}
//Will become governance proposal- will trigger token minting & sending
message MsgMintNewTokensRequest {
Expand All @@ -20,4 +21,13 @@ message MsgMintNewTokensRequest {
(gogoproto.moretags) = "yaml:\"asset_id\""
];
}
message MsgMintNewTokensResponse{}
message MsgMintNewTokensResponse{}

message MsgBurnHarborTokensRequest {
string from = 1 [ (gogoproto.moretags) = "yaml:\"from\"" ];
uint64 app_id = 2 [(gogoproto.moretags) = "yaml:\"app_id\"",
(gogoproto.customname) = "AppId"];
cosmos.base.v1beta1.Coin burn_coins = 3
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
}
message MsgBurnHarborTokensResponse{}
30 changes: 30 additions & 0 deletions x/tokenmint/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"

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

"github.com/comdex-official/comdex/x/tokenmint/types"
)
Expand All @@ -25,6 +26,7 @@ func GetTxCmd() *cobra.Command {

cmd.AddCommand(
txMint(),
txBurn(),
)

return cmd
Expand Down Expand Up @@ -57,3 +59,31 @@ func txMint() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// Token burn txs cmd.
func txBurn() *cobra.Command {
cmd := &cobra.Command{
Use: "tokenburn [app_id] [coins]",
Short: "burn harbor token",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
appID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
burnCoins, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return fmt.Errorf("invalid burn coins: %w", err)
}
msg := types.NewMsgBurnHarborTokensRequest(ctx.GetFromAddress().String(), appID, burnCoins)

return tx.GenerateOrBroadcastTxCLI(ctx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
3 changes: 3 additions & 0 deletions x/tokenmint/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgMintNewTokensRequest:
res, err := server.MsgMintNewTokens(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgBurnHarborTokensRequest:
res, err := server.MsgBurnHarborTokens(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
24 changes: 24 additions & 0 deletions x/tokenmint/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,27 @@ func (k msgServer) MsgMintNewTokens(c context.Context, msg *types.MsgMintNewToke

return &types.MsgMintNewTokensResponse{}, nil
}

func (k msgServer) MsgBurnHarborTokens(c context.Context, msg *types.MsgBurnHarborTokensRequest) (*types.MsgBurnHarborTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
burner, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
if msg.BurnCoins.Denom != "uharbor" {
return &types.MsgBurnHarborTokensResponse{}, types.ErrorInvalidDenom
}
appData, found := k.asset.GetApp(ctx, msg.AppId)
if !found || appData.Name != "harbor" {
return &types.MsgBurnHarborTokensResponse{}, types.ErrorAppMappingDoesNotExists
}
if appData.GenesisToken[0].Recipient != msg.From {
return &types.MsgBurnHarborTokensResponse{}, types.ErrorInvalidFrom
}
err = k.BurnGovTokensForApp(ctx, msg.AppId, burner, msg.BurnCoins)
if err != nil {
return &types.MsgBurnHarborTokensResponse{}, err
}

return &types.MsgBurnHarborTokensResponse{}, nil
}
2 changes: 2 additions & 0 deletions x/tokenmint/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgMintNewTokensRequest{}, "comdex/tokenmint/MsgMintNewTokensRequest", nil)
cdc.RegisterConcrete(&MsgBurnHarborTokensRequest{}, "comdex/tokenmint/MsgBurnHarborTokensRequest", nil)
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgMintNewTokensRequest{},
&MsgBurnHarborTokensRequest{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
1 change: 1 addition & 0 deletions x/tokenmint/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ var (
ErrorInvalidAppID = errors.Register(ModuleName, 1207, "app id can not be zero")
ErrorInvalidAssetID = errors.Register(ModuleName, 1208, "asset id can not be zero")
ErrorInvalidFrom = errors.Register(ModuleName, 1209, "invalid from")
ErrorInvalidDenom = errors.Register(ModuleName, 1210, "invalid denom, should be uharbor")
)
1 change: 1 addition & 0 deletions x/tokenmint/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func KeyPrefix(p string) []byte {
}

var TypeMsgMintNewTokensRequest = ModuleName + ":mintnewtokens"
var TypeMsgBurnHarborTokensRequest = ModuleName + ":burntokens"

var TokenMintKeyPrefix = []byte{0x10}

Expand Down
51 changes: 50 additions & 1 deletion x/tokenmint/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"github.com/cosmos/cosmos-sdk/types/errors"
)

var _ sdk.Msg = (*MsgMintNewTokensRequest)(nil)
var (
_ sdk.Msg = (*MsgMintNewTokensRequest)(nil)
_ sdk.Msg = (*MsgBurnHarborTokensRequest)(nil)
)

func NewMsgMintNewTokensRequest(from string, appID uint64, assetID uint64) *MsgMintNewTokensRequest {
return &MsgMintNewTokensRequest{
Expand Down Expand Up @@ -52,3 +55,49 @@ func (m *MsgMintNewTokensRequest) GetSigners() []sdk.AccAddress {

return []sdk.AccAddress{from}
}

func NewMsgBurnHarborTokensRequest(from string, appID uint64, burnCoin sdk.Coin) *MsgBurnHarborTokensRequest {
return &MsgBurnHarborTokensRequest{
From: from,
AppId: appID,
BurnCoins: burnCoin,
}
}

func (m *MsgBurnHarborTokensRequest) Route() string {
return RouterKey
}

func (m *MsgBurnHarborTokensRequest) Type() string {
return TypeMsgBurnHarborTokensRequest
}

func (m *MsgBurnHarborTokensRequest) ValidateBasic() error {
if m.From == "" {
return errors.Wrap(ErrorInvalidFrom, "from cannot be empty")
}
if m.AppId == 0 || m.AppId < 0 {
return errors.Wrap(ErrorInvalidAppID, "app id can not be zero or negative")
}
if !m.BurnCoins.IsValid() {
return errors.Wrapf(errors.ErrInvalidCoins, "bid amount %s", m.BurnCoins)
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return errors.Wrapf(ErrorInvalidFrom, "%s", err)
}

return nil
}

func (m *MsgBurnHarborTokensRequest) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m))
}

func (m *MsgBurnHarborTokensRequest) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(m.From)
if err != nil {
panic(err)
}

return []sdk.AccAddress{from}
}
Loading

0 comments on commit e85f946

Please sign in to comment.