Skip to content

Commit

Permalink
update nft builder file
Browse files Browse the repository at this point in the history
  • Loading branch information
harish551 committed Oct 27, 2023
1 parent 7c20f59 commit ab21faf
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions x/onft/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ var (
nftKeyName = fmt.Sprintf("%s%s", Namespace, "name")
nftKeyURIHash = fmt.Sprintf("%s%s", Namespace, "uri_hash")
nftKeyPreviewURI = fmt.Sprintf("%s%s", Namespace, "preview_uri")
nftKeyTransferable = fmt.Sprintf("%s%s", Namespace, "transferable")
nftKeyExtensible = fmt.Sprintf("%s%s", Namespace, "extensible")
nftKeyNSFW = fmt.Sprintf("%s%s", Namespace, "nsfw")
nftKeyRoyaltyShare = fmt.Sprintf("%s%s", Namespace, "royalty_share")
nftKeyDescription = fmt.Sprintf("%s%s", Namespace, "description")
)

type ClassBuilder struct {
Expand Down Expand Up @@ -121,7 +118,7 @@ func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, er

dataMap := make(map[string]interface{})
if err := json.Unmarshal(classDataBz, &dataMap); err != nil {
any, err := codectypes.NewAnyWithValue(&DenomMetadata{
denomMeta, err := codectypes.NewAnyWithValue(&DenomMetadata{
Creator: creator,
Schema: schema,
Description: description,
Expand All @@ -138,7 +135,7 @@ func (cb ClassBuilder) Build(classID, classURI, classData string) (nft.Class, er
Symbol: symbol,
Description: description,
UriHash: uriHash,
Data: any,
Data: denomMeta,
}, nil
}
if v, ok := dataMap[ClassKeyName]; ok {
Expand Down Expand Up @@ -244,20 +241,20 @@ func NewNFTBuilder(cdc codec.Codec) NFTBuilder {
}

// BuildMetadata encode nft into the metadata format defined by ics721
func (nb NFTBuilder) BuildMetadata(token nft.NFT) (string, error) {
func (nb NFTBuilder) BuildMetadata(_nft nft.NFT) (string, error) {
var message proto.Message
if err := nb.cdc.UnpackAny(token.Data, &message); err != nil {
if err := nb.cdc.UnpackAny(_nft.Data, &message); err != nil {
return "", err
}

nftMetadata, ok := message.(*ONFTMetadata)
if !ok {
return "", errors.New("unsupport classMetadata")
return "", errors.New("unsupported classMetadata")
}
kvals := make(map[string]interface{})
if len(nftMetadata.Data) > 0 {
err := json.Unmarshal([]byte(nftMetadata.Data), &kvals)
if err != nil && IsIBCDenom(token.ClassId) {
if err != nil && IsIBCDenom(_nft.ClassId) {
// when nftMetadata is not a legal json, there is no need to parse the data
return base64.RawStdEncoding.EncodeToString([]byte(nftMetadata.Data)), nil
}
Expand All @@ -267,7 +264,9 @@ func (nb NFTBuilder) BuildMetadata(token nft.NFT) (string, error) {
}
}
kvals[nftKeyName] = MediaField{Value: nftMetadata.Name}
kvals[nftKeyURIHash] = MediaField{Value: token.UriHash}
kvals[nftKeyURIHash] = MediaField{Value: _nft.UriHash}
kvals[nftKeyPreviewURI] = MediaField{Value: nftMetadata.PreviewURI}
kvals[nftKeyDescription] = MediaField{Value: nftMetadata.Description}
data, err := json.Marshal(kvals)
if err != nil {
return "", err
Expand All @@ -276,7 +275,7 @@ func (nb NFTBuilder) BuildMetadata(token nft.NFT) (string, error) {
}

// Build create a nft from ics721 packet data
func (nb NFTBuilder) Build(classId, nftID, tokenURI, nftData string) (nft.NFT, error) {
func (nb NFTBuilder) Build(classId, nftID, nftURI, nftData string) (nft.NFT, error) {
nftDataBz, err := base64.RawStdEncoding.DecodeString(nftData)
if err != nil {
return nft.NFT{}, err
Expand All @@ -294,7 +293,7 @@ func (nb NFTBuilder) Build(classId, nftID, tokenURI, nftData string) (nft.NFT, e
return nft.NFT{
ClassId: classId,
Id: nftID,
Uri: tokenURI,
Uri: nftURI,
Data: metadata,
}, nil
}
Expand Down Expand Up @@ -341,7 +340,7 @@ func (nb NFTBuilder) Build(classId, nftID, tokenURI, nftData string) (nft.NFT, e
return nft.NFT{
ClassId: classId,
Id: nftID,
Uri: tokenURI,
Uri: nftURI,
UriHash: uriHash,
Data: metadata,
}, nil
Expand Down

0 comments on commit ab21faf

Please sign in to comment.