NFT with Metadata
NFT with Metadata
14 Oct 2022
Contributed by Flow Blockchain
An NFT with metadata in it. This NFT also uses the metadata contract to establish easy views for displaying the NFT's metadata.
Smart Contract Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
pub let id: UInt64
pub let name: String
pub let thumbnail: String
pub let description: String
pub let power: String
pub let will: String
pub let determination: String
pub fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Type<NewExampleNFT.Traits>()
]
}
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
return MetadataViews.Display(
name: self.name,
description: self.description,
thumbnail: MetadataViews.HTTPFile(
url: self.thumbnail
)
)
case Type<NewExampleNFT.Traits>():
return NewExampleNFT.Traits(
power: self.power,
will: self.will,
determination: self.determination
)
}
return nil
}
init(
id: UInt64,
name: String,
description: String,
thumbnail: String,
power: String,
will: String,
determination: String
) {
self.id = id
self.name = name
self.thumbnail = thumbnail
self.description = description
self.power = power
self.will = will
self.determination= determination
}
}
This is a NFT resource that abides by the NFT standard and the Metadata standard. It imports both interfaces into the resource.
From there, metadata such as id, name, thumbnail, description, power, will, and determination are defined and initialized. The functions to resolveView and getViews are also included to make viewing the metadata easy.
Transaction Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import NonFungibleToken from 0x03
import NewExampleNFT from 0x02
// This transaction uses the NFTMinter resource to mint a new NFT
// It must be run with the account that has the minter resource
// stored in /storage/NFTMinter
transaction{
// local variable for storing the minter reference
let minter: &NewExampleNFT.NFTMinter
prepare(signer: AuthAccount) {
// borrow a reference to the NFTMinter resource in storage
self.minter = signer.borrow<&NewExampleNFT.NFTMinter>(from: NewExampleNFT.MinterStoragePath)
?? panic("Could not borrow a reference to the NFT minter")
}
execute {
// Borrow the recipient's public NFT collection reference
let receiver = getAccount(0x02)
.getCapability(NewExampleNFT.CollectionPublicPath)
.borrow<&{NonFungibleToken.CollectionPublic}>()
?? panic("Could not get receiver reference to the NFT Collection")
// Mint the NFT and deposit it to the recipient's collection
self.minter.mintNFT(
recipient: receiver,
name: "Second NFT",
description: "The Best NFT",
thumbnail: "NFT: Thumbnail",
power: "The best",
will: "The strongest",
determination: "unbeatable"
)
log("Minted an NFT")
}
}
Because the above smart contract has metadata in the NFT that is initialized, when minting the NFT you will need to send in those parameters in your transaction.
Here we borrow the minter from the account that holds the NFT Minter. We then take the address of the recepient and make sure they have the capability to store the NFT. Afterwards we mint the NFT with the parameters specified.
Up Next: Metadata Views
31%