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

update ics721 nft wrapper with royalty receivers part #140

Merged
merged 2 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
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func NewAppKeeper(
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.IBCKeeper.PortKeeper,
appKeepers.AccountKeeper,
ics721nft.NewKeeper(appCodec, appKeepers.ONFTKeeper, appKeepers.AccountKeeper),
ics721nft.NewKeeper(appCodec, appKeepers.ONFTKeeper, appKeepers.AccountKeeper, appKeepers.BankKeeper),
appKeepers.ScopedNFTTransferKeeper,
)

Expand Down
3 changes: 3 additions & 0 deletions x/ics721nft/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type (
SetAccount(sdk.Context, authtypes.AccountI)
GetModuleAddress(name string) sdk.AccAddress
}
BankKeeper interface {
BlockedAddr(addr sdk.AccAddress) bool
}

ICS721Class struct {
ID string
Expand Down
75 changes: 54 additions & 21 deletions x/ics721nft/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ics721nft

import (
errorsmod "cosmossdk.io/errors"
onftkeeper "github.com/OmniFlix/omniflixhub/v2/x/onft/keeper"
onfttypes "github.com/OmniFlix/omniflixhub/v2/x/onft/types"
nfttransfer "github.com/bianjieai/nft-transfer/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/nft"
nftkeeper "github.com/cosmos/cosmos-sdk/x/nft/keeper"
)
Expand All @@ -17,6 +19,7 @@ type Keeper struct {
nk nftkeeper.Keeper
cdc codec.Codec
ak AccountKeeper
bk BankKeeper
cb onfttypes.ClassBuilder
nb onfttypes.NFTBuilder
}
Expand All @@ -25,11 +28,13 @@ type Keeper struct {
func NewKeeper(cdc codec.Codec,
k onftkeeper.Keeper,
ak AccountKeeper,
bk BankKeeper,
) Keeper {
return Keeper{
nk: k.NFTkeeper(),
cdc: cdc,
ak: ak,
bk: bk,
cb: onfttypes.NewClassBuilder(cdc, ak.GetModuleAddress),
nb: onfttypes.NewNFTBuilder(cdc),
}
Expand All @@ -53,9 +58,13 @@ func (k Keeper) CreateOrUpdateClass(ctx sdk.Context,
}
} else {
denomMetadata := &onfttypes.DenomMetadata{
Creator: k.ak.GetModuleAddress(onfttypes.ModuleName).String(),
PreviewUri: "",
Schema: "",
Creator: k.ak.GetModuleAddress(onfttypes.ModuleName).String(),
PreviewUri: "",
Schema: "",
Description: "",
Data: "",
UriHash: "",
RoyaltyReceivers: nil,
}

metadata, err := codectypes.NewAnyWithValue(denomMetadata)
Expand All @@ -69,6 +78,18 @@ func (k Keeper) CreateOrUpdateClass(ctx sdk.Context,
Data: metadata,
}
}
var denomMeta onfttypes.DenomMetadata
if err := k.cdc.UnpackAny(class.Data, &denomMeta); err != nil {
return err
}
if denomMeta.RoyaltyReceivers != nil && !k.validRoyaltyReceiverAddresses(denomMeta.RoyaltyReceivers) {
denomMeta.RoyaltyReceivers = nil
dMeta, err := codectypes.NewAnyWithValue(&denomMeta)
if err != nil {
return err
}
class.Data = dMeta
}
if k.nk.HasClass(ctx, classID) {
return k.nk.UpdateClass(ctx, class)
}
Expand Down Expand Up @@ -96,23 +117,22 @@ func (k Keeper) Transfer(
ctx sdk.Context,
classID,
tokenID,
tokenData string,
_ string,
receiver sdk.AccAddress,
) error {
if err := k.nk.Transfer(ctx, classID, tokenID, receiver); err != nil {
_nft, _ := k.nk.GetNFT(ctx, classID, tokenID)
nftMetadata, err := onfttypes.UnmarshalNFTMetadata(k.cdc, _nft.Data.GetValue())
if err != nil {
return err
}
if len(tokenData) == 0 {
return nil
if !nftMetadata.Transferable {
k.Logger(ctx).Error("non-transferable nft")
return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "non-transferable nft")
}
_nft, _ := k.nk.GetNFT(ctx, classID, tokenID)
token, err := k.nb.Build(classID, tokenID, _nft.GetUri(), tokenData)
if err != nil {
k.Logger(ctx).Error("unable to build token on transfer from packet data", "error:", err.Error())
if err := k.nk.Transfer(ctx, classID, tokenID, receiver); err != nil {
return err
}

return k.nk.Update(ctx, token)
return nil
}

// GetClass implement the method of ICS721Keeper.GetClass
Expand Down Expand Up @@ -140,14 +160,6 @@ func (k Keeper) GetNFT(ctx sdk.Context, classID, tokenID string) (nfttransfer.NF
if !has {
return nil, false
}
nftMetadata, err := onfttypes.UnmarshalNFTMetadata(k.cdc, _nft.Data.GetValue())
if err != nil {
return nil, false
}
if !nftMetadata.Transferable {
k.Logger(ctx).Error("non-transferable nft")
return nil, false
}
metadata, err := k.nb.BuildMetadata(_nft)
if err != nil {
k.Logger(ctx).Error("encode nft data failed")
Expand Down Expand Up @@ -180,3 +192,24 @@ func (k Keeper) HasClass(ctx sdk.Context, classID string) bool {
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "ics721/NFTKeeper")
}

func (k Keeper) validRoyaltyReceiverAddresses(addresses []*onfttypes.WeightedAddress) bool {
weightSum := sdk.NewDec(0)
for _, addr := range addresses {
address, err := sdk.AccAddressFromBech32(addr.Address)
if err != nil {
return false
}
if k.bk.BlockedAddr(address) {
return false
}
if !addr.Weight.IsPositive() {
return false
}
if addr.Weight.GT(sdk.NewDec(1)) {
return false
}
weightSum = weightSum.Add(addr.Weight)
}
return weightSum.Equal(sdk.NewDec(1))
}
69 changes: 41 additions & 28 deletions x/onft/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ const (
)

var (
ClassKeyName = fmt.Sprintf("%s%s", Namespace, "name")
ClassKeySymbol = fmt.Sprintf("%s%s", Namespace, "symbol")
ClassKeyDescription = fmt.Sprintf("%s%s", Namespace, "description")
ClassKeyURIHash = fmt.Sprintf("%s%s", Namespace, "uri_hash")
ClassKeyCreator = fmt.Sprintf("%s%s", Namespace, "creator")
ClassKeySchema = fmt.Sprintf("%s%s", Namespace, "schema")
ClassKeyPreviewURI = fmt.Sprintf("%s%s", Namespace, "preview_uri")
nftKeyName = fmt.Sprintf("%s%s", Namespace, "name")
nftKeyURIHash = fmt.Sprintf("%s%s", Namespace, "uri_hash")
nftKeyPreviewURI = fmt.Sprintf("%s%s", Namespace, "preview_uri")
nftKeyDescription = fmt.Sprintf("%s%s", Namespace, "description")
nftKeyCreatedAt = fmt.Sprintf("%s%s", Namespace, "created_at")
nftKeyExtensible = fmt.Sprintf("%s%s", Namespace, "extensible")
nftKeyNSFW = fmt.Sprintf("%s%s", Namespace, "nsfw")
nftKeyRoyaltyShare = fmt.Sprintf("%s%s", Namespace, "royalty_share")
ClassKeyName = fmt.Sprintf("%s%s", Namespace, "name")
ClassKeySymbol = fmt.Sprintf("%s%s", Namespace, "symbol")
ClassKeyDescription = fmt.Sprintf("%s%s", Namespace, "description")
ClassKeyURIHash = fmt.Sprintf("%s%s", Namespace, "uri_hash")
ClassKeyCreator = fmt.Sprintf("%s%s", Namespace, "creator")
ClassKeySchema = fmt.Sprintf("%s%s", Namespace, "schema")
ClassKeyPreviewURI = fmt.Sprintf("%s%s", Namespace, "preview_uri")
ClassKeyRoyaltyReceivers = fmt.Sprintf("%s%s", Namespace, "royalty_receivers")
nftKeyName = fmt.Sprintf("%s%s", Namespace, "name")
nftKeyURIHash = fmt.Sprintf("%s%s", Namespace, "uri_hash")
nftKeyPreviewURI = fmt.Sprintf("%s%s", Namespace, "preview_uri")
nftKeyDescription = fmt.Sprintf("%s%s", Namespace, "description")
nftKeyCreatedAt = fmt.Sprintf("%s%s", Namespace, "created_at")
nftKeyExtensible = fmt.Sprintf("%s%s", Namespace, "extensible")
nftKeyNSFW = fmt.Sprintf("%s%s", Namespace, "nsfw")
nftKeyRoyaltyShare = fmt.Sprintf("%s%s", Namespace, "royalty_share")
)

type ClassBuilder struct {
Expand Down Expand Up @@ -97,6 +98,7 @@ func (cb ClassBuilder) BuildMetadata(class nft.Class) (string, error) {
kvals[ClassKeyCreator] = MediaField{Value: hexCreator}
kvals[ClassKeySchema] = MediaField{Value: metadata.Schema}
kvals[ClassKeyPreviewURI] = MediaField{Value: metadata.PreviewUri}
kvals[ClassKeyRoyaltyReceivers] = MediaField{Value: metadata.RoyaltyReceivers}
data, err := json.Marshal(kvals)
if err != nil {
return "", err
Expand All @@ -112,13 +114,14 @@ func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, er
}

var (
name = ""
symbol = ""
description = ""
uriHash = ""
schema = ""
previewURI = ""
creator = cb.getModuleAddress(ModuleName).String()
name = ""
symbol = ""
description = ""
uriHash = ""
schema = ""
previewURI = ""
royaltyReceivers []*WeightedAddress
creator = cb.getModuleAddress(ModuleName).String()
)

dataMap := make(map[string]interface{})
Expand Down Expand Up @@ -210,6 +213,15 @@ func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, er
}
}

if v, ok := dataMap[ClassKeyRoyaltyReceivers]; ok {
if vMap, ok := v.(map[string]interface{}); ok {
if vAddrs, ok := vMap[KeyMediaFieldValue].([]*WeightedAddress); ok {
royaltyReceivers = vAddrs
delete(dataMap, ClassKeyRoyaltyReceivers)
}
}
}

data := ""
if len(dataMap) > 0 {
dataBz, err := json.Marshal(dataMap)
Expand All @@ -220,12 +232,13 @@ func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, er
}

denomMeta, err := codectypes.NewAnyWithValue(&DenomMetadata{
Creator: creator,
Schema: schema,
Description: description,
PreviewUri: previewURI,
Data: data,
UriHash: uriHash,
Creator: creator,
Schema: schema,
Description: description,
PreviewUri: previewURI,
Data: data,
UriHash: uriHash,
RoyaltyReceivers: royaltyReceivers,
})
if err != nil {
return nft.Class{}, err
Expand Down
Loading