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

feat(onft): purge denom message #145

Merged
merged 4 commits into from
Feb 5, 2024
Merged
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
13 changes: 13 additions & 0 deletions proto/OmniFlix/onft/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ service Msg {

rpc TransferDenom(MsgTransferDenom) returns (MsgTransferDenomResponse);

rpc PurgeDenom(MsgPurgeDenom) returns (MsgPurgeDenomResponse);

rpc MintONFT(MsgMintONFT) returns (MsgMintONFTResponse);

rpc TransferONFT(MsgTransferONFT) returns (MsgTransferONFTResponse);
Expand Down Expand Up @@ -84,6 +86,17 @@ message MsgUpdateDenom {

message MsgUpdateDenomResponse {}

message MsgPurgeDenom {
option (cosmos.msg.v1.signer) = "sender";
option (amino.name) = "OmniFlix/onft/MsgPurgeDenom";
option (gogoproto.equal) = false;

string id = 1;
string sender = 2;
}

message MsgPurgeDenomResponse {}

message MsgTransferDenom {
option (cosmos.msg.v1.signer) = "sender";
option (amino.name) = "OmniFlix/onft/MsgTransferDenom";
Expand Down
33 changes: 33 additions & 0 deletions x/onft/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewTxCmd() *cobra.Command {
GetCmdCreateDenom(),
GetCmdUpdateDenom(),
GetCmdTransferDenom(),
GetCmdPurgeDenom(),
GetCmdMintONFT(),
GetCmdTransferONFT(),
GetCmdBurnONFT(),
Expand Down Expand Up @@ -470,6 +471,38 @@ $ %s tx onft burn [denom-id] [onft-id] --from=<key-name> --chain-id=<chain-id> -
return cmd
}

func GetCmdPurgeDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "purge-denom [recipient] [denom-id]",
Long: strings.TrimSpace(
fmt.Sprintf(`Purge an empty denom .
Example:
$ %s tx onft purge-denom [denom-id] --from=<key-name> --chain-id=<chain-id> --fees=<fee>`,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgPurgeDenom(
args[0],
clientCtx.GetFromAddress().String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)

return cmd
}

func parseSplitShares(splitSharesStr string) ([]*types.WeightedAddress, error) {
splitSharesStr = strings.TrimSpace(splitSharesStr)
splitsStrList := strings.Split(splitSharesStr, ",")
Expand Down
38 changes: 38 additions & 0 deletions x/onft/keeper/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,41 @@ func (k Keeper) GetDenomInfo(ctx sdk.Context, denomID string) (*types.Denom, err
RoyaltyReceivers: denomMetadata.RoyaltyReceivers,
}, nil
}

// PurgeDenom deletes the denom if no nfts in it
func (k Keeper) PurgeDenom(
ctx sdk.Context,
denomID string,
sender sdk.AccAddress,
) error {
denom, err := k.GetDenomInfo(ctx, denomID)
if err != nil {
return err
}

// authorize
if sender.String() != denom.Creator {
return errorsmod.Wrapf(
sdkerrors.ErrUnauthorized,
"%s is not allowed to purge denom %s", sender,
denomID,
)
}
if k.nk.GetTotalSupply(ctx, denomID) != 0 {
return errorsmod.Wrapf(
types.ErrNotAllowed,
"can not purge denom (%s) with nfts available in it",
denomID,
)
}
// delete the denom
k.DeleteDenomFromStore(ctx, denomID)

k.emitPurgeONFTDenomEvent(ctx, denomID)
return nil
}

func (k Keeper) DeleteDenomFromStore(ctx sdk.Context, denomId string) {
store := ctx.KVStore(k.storeKey)
store.Delete(classStoreKey(denomId))
}
9 changes: 9 additions & 0 deletions x/onft/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (k Keeper) emitTransferONFTDenomEvent(ctx sdk.Context, denomId, symbol, sen
)
}

func (k Keeper) emitPurgeONFTDenomEvent(ctx sdk.Context, denomId string) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
onfttypes.EventTypePurgeONFTDenom,
sdk.NewAttribute(onfttypes.AttributeKeyDenomID, denomId),
),
)
}

func (k Keeper) emitMintONFTEvent(ctx sdk.Context, nftId, denomId, uri, owner string) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
12 changes: 12 additions & 0 deletions x/onft/keeper/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package keeper

import (
nftkeeper "github.com/cosmos/cosmos-sdk/x/nft/keeper"
)

func classStoreKey(classID string) []byte {
key := make([]byte, len(nftkeeper.ClassKey)+len(classID))
copy(key, nftkeeper.ClassKey)
copy(key[len(nftkeeper.ClassKey):], classID)
return key
}
16 changes: 16 additions & 0 deletions x/onft/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ func (m msgServer) BurnONFT(goCtx context.Context,

return &types.MsgBurnONFTResponse{}, nil
}

func (m msgServer) PurgeDenom(goCtx context.Context,
msg *types.MsgPurgeDenom,
) (*types.MsgPurgeDenomResponse, error) {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := m.Keeper.PurgeDenom(ctx, msg.Id, sender); err != nil {
return nil, err
}

return &types.MsgPurgeDenomResponse{}, nil
}
1 change: 1 addition & 0 deletions x/onft/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ var (
ErrInvalidONFTMetadata = errorsmod.Register(ModuleName, 25, "invalid nft data")
ErrDenomIdExists = errorsmod.Register(ModuleName, 26, "denom exists")
ErrInvalidRoyaltyReceivers = errorsmod.Register(ModuleName, 27, "invalid royalty receivers")
ErrNotAllowed = errorsmod.Register(ModuleName, 28, "not allowed ")
)
1 change: 1 addition & 0 deletions x/onft/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
EventTypeCreateONFTDenom = "create_onft_denom"
EventTypeUpdateONFTDenom = "update_onft_denom"
EventTypeTransferONFTDenom = "transfer_onft_denom"
EventTypePurgeONFTDenom = "purge_onft_denom"

EventTypeMintONFT = "mint_onft"
EventTypeTransferONFT = "transfer_onft"
Expand Down
31 changes: 31 additions & 0 deletions x/onft/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,37 @@ func (msg MsgTransferDenom) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{from}
}

func NewMsgPurgeDenom(id, sender string) *MsgPurgeDenom {
return &MsgPurgeDenom{
Id: id,
Sender: sender,
}
}

func (msg MsgPurgeDenom) Route() string { return RouterKey }

func (msg MsgPurgeDenom) Type() string { return TypeMsgTransferDenom }

func (msg MsgPurgeDenom) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address; %s", err)
}
return nil
}

func (msg MsgPurgeDenom) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

func (msg MsgPurgeDenom) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}

func NewMsgMintONFT(
denomId, sender, recipient string, metadata Metadata, data string,
transferable, extensible, nsfw bool, royaltyShare sdk.Dec,
Expand Down
Loading
Loading